Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to configure Ctrl + W behavior #606

Merged
merged 1 commit into from Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/app.ts
Expand Up @@ -21,6 +21,7 @@ import { IServerFactory, JupyterServerFactory } from './server';
import { connectAndGetServerInfo, IJupyterServerInfo } from './connect';
import { UpdateDialog } from './updatedialog/updatedialog';
import {
CtrlWBehavior,
DEFAULT_WIN_HEIGHT,
DEFAULT_WIN_WIDTH,
LogLevel,
Expand Down Expand Up @@ -377,7 +378,8 @@ export class JupyterApplication implements IApplication, IDisposable {
overrideDefaultServerArgs: userSettings.getValue(
SettingType.overrideDefaultServerArgs
),
serverEnvVars: userSettings.getValue(SettingType.serverEnvVars)
serverEnvVars: userSettings.getValue(SettingType.serverEnvVars),
ctrlWBehavior: userSettings.getValue(SettingType.ctrlWBehavior)
},
this._registry
);
Expand Down Expand Up @@ -791,6 +793,13 @@ export class JupyterApplication implements IApplication, IDisposable {
}
);

this._evm.registerEventHandler(
EventTypeMain.SetCtrlWBehavior,
(_event, behavior: CtrlWBehavior) => {
userSettings.setValue(SettingType.ctrlWBehavior, behavior);
}
);

this._evm.registerSyncEventHandler(
EventTypeMain.GetServerInfo,
(event): Promise<IServerInfo> => {
Expand Down
10 changes: 10 additions & 0 deletions src/main/config/settings.ts
Expand Up @@ -33,6 +33,12 @@ export enum LogLevel {
Debug = 'debug'
}

export enum CtrlWBehavior {
Close = 'close',
Warn = 'warn',
DoNotClose = 'do-not-close'
}

export type KeyValueMap = { [key: string]: string };

export enum SettingType {
Expand All @@ -52,6 +58,8 @@ export enum SettingType {

startupMode = 'startupMode',

ctrlWBehavior = 'ctrlWBehavior',

logLevel = 'logLevel'
}

Expand Down Expand Up @@ -137,6 +145,8 @@ export class UserSettings {

startupMode: new Setting<StartupMode>(StartupMode.WelcomePage),

ctrlWBehavior: new Setting<CtrlWBehavior>(CtrlWBehavior.Close),

logLevel: new Setting<string>(LogLevel.Warn)
};

Expand Down
3 changes: 2 additions & 1 deletion src/main/eventtypes.ts
Expand Up @@ -54,7 +54,8 @@ export enum EventTypeMain {
ShowInvalidPythonPathMessage = 'show-invalid-python-path-message',
SetLogLevel = 'set-log-level',
SetServerLaunchArgs = 'set-server-launch-args',
SetServerEnvVars = 'set-server-env-vars'
SetServerEnvVars = 'set-server-env-vars',
SetCtrlWBehavior = 'set-ctrl-w-behavior'
}

// events sent to Renderer process
Expand Down
34 changes: 34 additions & 0 deletions src/main/labview/labview.ts
Expand Up @@ -4,6 +4,7 @@
import {
BrowserView,
clipboard,
dialog,
Menu,
MenuItemConstructorOptions
} from 'electron';
Expand All @@ -21,8 +22,10 @@ import {
} from '../utils';
import { SessionWindow } from '../sessionwindow/sessionwindow';
import {
CtrlWBehavior,
FrontEndMode,
SettingType,
userSettings,
WorkspaceSettings
} from '../config/settings';
import { IDisposable } from '../tokens';
Expand Down Expand Up @@ -112,6 +115,37 @@ export class LabView implements IDisposable {
this._labUIReady = true;
});
}

const ctrlWBehavior = userSettings.getValue(SettingType.ctrlWBehavior);

if (ctrlWBehavior !== CtrlWBehavior.Close) {
this._view.webContents.on('before-input-event', (event, input) => {
if (
input.code === 'KeyW' &&
((input.meta && process.platform === 'darwin') || input.control)
) {
let skipClose = false;

if (ctrlWBehavior === CtrlWBehavior.Warn) {
const choice = dialog.showMessageBoxSync({
type: 'warning',
message: 'Do you want to close the session?',
buttons: ['Close session', 'Cancel'],
defaultId: 1,
cancelId: 1
});

skipClose = choice === 1;
} else {
skipClose = true;
}

if (skipClose) {
event.preventDefault();
}
}
});
}
}

public get view(): BrowserView {
Expand Down
3 changes: 3 additions & 0 deletions src/main/settingsdialog/preload.ts
Expand Up @@ -102,6 +102,9 @@ contextBridge.exposeInMainWorld('electronAPI', {
},
setServerEnvVars: (serverEnvVars: any) => {
ipcRenderer.send(EventTypeMain.SetServerEnvVars, serverEnvVars);
},
setCtrlWBehavior: (behavior: string) => {
ipcRenderer.send(EventTypeMain.SetCtrlWBehavior, behavior);
}
});

Expand Down
22 changes: 20 additions & 2 deletions src/main/settingsdialog/settingsdialog.ts
Expand Up @@ -8,6 +8,7 @@ import * as fs from 'fs';
const semver = require('semver');
import { ThemedWindow } from '../dialog/themedwindow';
import {
CtrlWBehavior,
FrontEndMode,
KeyValueMap,
LogLevel,
Expand Down Expand Up @@ -40,7 +41,8 @@ export class SettingsDialog {
logLevel,
serverArgs,
overrideDefaultServerArgs,
serverEnvVars
serverEnvVars,
ctrlWBehavior
} = options;
const installUpdatesAutomaticallyEnabled = process.platform === 'darwin';
const installUpdatesAutomatically =
Expand Down Expand Up @@ -85,6 +87,8 @@ export class SettingsDialog {
}
}

const ctrlWLabel = process.platform === 'darwin' ? 'Cmd + W' : 'Ctrl + W';

const template = `
<style>
#container {
Expand Down Expand Up @@ -491,6 +495,15 @@ export class SettingsDialog {
</jp-tab-panel>

<jp-tab-panel id="tab-panel-advanced">
<div class="row setting-section">
<jp-radio-group orientation="horizontal">
<label slot="label">${ctrlWLabel} behavior</label>
<jp-radio name="ctrl-w-behavior" value="close" <%= ctrlWBehavior === 'close' ? 'checked' : '' %>>Close session</jp-radio>
<jp-radio name="ctrl-w-behavior" value="warn" <%= ctrlWBehavior === 'warn' ? 'checked' : '' %>>Warn before close</jp-radio>
<jp-radio name="ctrl-w-behavior" value="do-not-close" <%= ctrlWBehavior === 'do-not-close' ? 'checked' : '' %>>Do not close</jp-radio>
</jp-radio-group>
</div>

<div class="row setting-section">
<div class="row">
<label for="log-level">Log level</label>
Expand Down Expand Up @@ -587,6 +600,9 @@ export class SettingsDialog {
window.electronAPI.setServerLaunchArgs(additionalServerArgs.value, overrideDefaultServerArgs.checked);
window.electronAPI.setServerEnvVars(parseServerEnvVars().envVars);

const ctrlWBehavior = document.querySelector('jp-radio[name="ctrl-w-behavior"].checked').value;
window.electronAPI.setCtrlWBehavior(ctrlWBehavior);

if (defaultPythonEnvChanged) {
if (bundledEnvRadio.checked) {
window.electronAPI.setDefaultPythonPath('');
Expand Down Expand Up @@ -633,7 +649,8 @@ export class SettingsDialog {
logLevel,
serverArgs,
overrideDefaultServerArgs,
serverEnvVars: strServerEnvVars
serverEnvVars: strServerEnvVars,
ctrlWBehavior
});
}

Expand Down Expand Up @@ -672,5 +689,6 @@ export namespace SettingsDialog {
serverArgs: string;
overrideDefaultServerArgs: boolean;
serverEnvVars: KeyValueMap;
ctrlWBehavior: CtrlWBehavior;
}
}