Skip to content

Commit

Permalink
close JupyterLab tab by default with Ctrl / Cmd + W
Browse files Browse the repository at this point in the history
  • Loading branch information
mbektas committed Apr 24, 2023
1 parent 58149bf commit ef17965
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/main/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export enum LogLevel {
}

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

Expand Down Expand Up @@ -138,7 +139,7 @@ export class UserSettings {

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

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

logLevel: new Setting<string>(LogLevel.Warn)
};
Expand Down
18 changes: 15 additions & 3 deletions src/main/labview/labview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,27 @@ export class LabView implements IDisposable {

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

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

if (ctrlWBehavior === CtrlWBehavior.Warn) {
if (ctrlWBehavior === CtrlWBehavior.CloseTab) {
event.preventDefault();
await this._view.webContents.executeJavaScript(`
{
const lab = window.jupyterapp || window.jupyterlab;
if (lab) {
lab.commands.execute('application:close');
}
}
0; // response
`);
skipClose = true;
} else if (ctrlWBehavior === CtrlWBehavior.Warn) {
const choice = dialog.showMessageBoxSync({
type: 'warning',
message: 'Do you want to close the session?',
Expand Down
3 changes: 2 additions & 1 deletion src/main/settingsdialog/settingsdialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,9 @@ export class SettingsDialog {
<div class="row setting-section">
<jp-radio-group orientation="horizontal">
<label slot="label">${ctrlWLabel} behavior</label>
<jp-radio name="ctrl-w-behavior" value="close-tab" <%= ctrlWBehavior === 'close-tab' ? 'checked' : '' %>>Close tab</jp-radio>
<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="warn" <%= ctrlWBehavior === 'warn' ? 'checked' : '' %>>Warn and close session</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>
Expand Down

0 comments on commit ef17965

Please sign in to comment.