Skip to content

Commit

Permalink
Merge pull request #7385 from vidartf/main-no-pallette
Browse files Browse the repository at this point in the history
Allow main app plugin to work without palette
  • Loading branch information
afshin committed Jan 14, 2020
2 parents 1c1ef71 + b99db39 commit bb8276c
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 154 deletions.
37 changes: 20 additions & 17 deletions packages/application-extension/src/index.tsx
Expand Up @@ -90,14 +90,14 @@ namespace CommandIDs {
*/
const main: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/application-extension:main',
requires: [ICommandPalette, IRouter, IWindowResolver],
optional: [IConnectionLost],
requires: [IRouter, IWindowResolver],
optional: [ICommandPalette, IConnectionLost],
activate: (
app: JupyterFrontEnd,
palette: ICommandPalette,
router: IRouter,
resolver: IWindowResolver,
connectionLost: IConnectionLost | undefined
palette: ICommandPalette | null,
connectionLost: IConnectionLost | null
) => {
if (!(app instanceof JupyterLab)) {
throw new Error(`${main.id} must be activated in JupyterLab.`);
Expand Down Expand Up @@ -486,7 +486,7 @@ const sidebar: JupyterFrontEndPlugin<void> = {
/**
* Add the main application commands.
*/
function addCommands(app: JupyterLab, palette: ICommandPalette): void {
function addCommands(app: JupyterLab, palette: ICommandPalette | null): void {
const { commands, contextMenu, shell } = app;
const category = 'Main Area';

Expand Down Expand Up @@ -572,31 +572,27 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
shell.activateNextTab();
}
});
palette.addItem({ command: CommandIDs.activateNextTab, category });

commands.addCommand(CommandIDs.activatePreviousTab, {
label: 'Activate Previous Tab',
execute: () => {
shell.activatePreviousTab();
}
});
palette.addItem({ command: CommandIDs.activatePreviousTab, category });

commands.addCommand(CommandIDs.activateNextTabBar, {
label: 'Activate Next Tab Bar',
execute: () => {
shell.activateNextTabBar();
}
});
palette.addItem({ command: CommandIDs.activateNextTabBar, category });

commands.addCommand(CommandIDs.activatePreviousTabBar, {
label: 'Activate Previous Tab Bar',
execute: () => {
shell.activatePreviousTabBar();
}
});
palette.addItem({ command: CommandIDs.activatePreviousTabBar, category });

// A CSS selector targeting tabs in the main area. This is a very
// specific selector since we really only want tabs that are
Expand All @@ -614,7 +610,6 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
}
}
});
palette.addItem({ command: CommandIDs.close, category });
contextMenu.addItem({
command: CommandIDs.close,
selector: tabSelector,
Expand All @@ -627,7 +622,6 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
shell.closeAll();
}
});
palette.addItem({ command: CommandIDs.closeAll, category });

commands.addCommand(CommandIDs.closeOtherTabs, {
label: () => `Close All Other Tabs`,
Expand All @@ -648,7 +642,6 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
closeWidgets(otherWidgets);
}
});
palette.addItem({ command: CommandIDs.closeOtherTabs, category });
contextMenu.addItem({
command: CommandIDs.closeOtherTabs,
selector: tabSelector,
Expand All @@ -667,7 +660,6 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
closeWidgets(widgetsRightOf(widget));
}
});
palette.addItem({ command: CommandIDs.closeRightTabs, category });
contextMenu.addItem({
command: CommandIDs.closeRightTabs,
selector: tabSelector,
Expand All @@ -689,7 +681,6 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
isToggled: () => !shell.leftCollapsed,
isVisible: () => !shell.isEmpty('left')
});
palette.addItem({ command: CommandIDs.toggleLeftArea, category });

app.commands.addCommand(CommandIDs.toggleRightArea, {
label: () => 'Show Right Sidebar',
Expand All @@ -706,7 +697,6 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
isToggled: () => !shell.rightCollapsed,
isVisible: () => !shell.isEmpty('right')
});
palette.addItem({ command: CommandIDs.toggleRightArea, category });

app.commands.addCommand(CommandIDs.togglePresentationMode, {
label: () => 'Presentation Mode',
Expand All @@ -716,7 +706,6 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
isToggled: () => shell.presentationMode,
isVisible: () => true
});
palette.addItem({ command: CommandIDs.togglePresentationMode, category });

app.commands.addCommand(CommandIDs.setMode, {
isVisible: args => {
Expand Down Expand Up @@ -744,7 +733,21 @@ function addCommands(app: JupyterLab, palette: ICommandPalette): void {
return app.commands.execute(CommandIDs.setMode, args);
}
});
palette.addItem({ command: CommandIDs.toggleMode, category });

if (palette) {
palette.addItem({ command: CommandIDs.activateNextTab, category });
palette.addItem({ command: CommandIDs.activatePreviousTab, category });
palette.addItem({ command: CommandIDs.activateNextTabBar, category });
palette.addItem({ command: CommandIDs.activatePreviousTabBar, category });
palette.addItem({ command: CommandIDs.close, category });
palette.addItem({ command: CommandIDs.closeAll, category });
palette.addItem({ command: CommandIDs.closeOtherTabs, category });
palette.addItem({ command: CommandIDs.closeRightTabs, category });
palette.addItem({ command: CommandIDs.toggleLeftArea, category });
palette.addItem({ command: CommandIDs.toggleRightArea, category });
palette.addItem({ command: CommandIDs.togglePresentationMode, category });
palette.addItem({ command: CommandIDs.toggleMode, category });
}
}

/**
Expand Down
210 changes: 110 additions & 100 deletions packages/console-extension/src/index.ts
Expand Up @@ -97,16 +97,20 @@ const tracker: JupyterFrontEndPlugin<IConsoleTracker> = {
id: '@jupyterlab/console-extension:tracker',
provides: IConsoleTracker,
requires: [
IMainMenu,
ICommandPalette,
ConsolePanel.IContentFactory,
IEditorServices,
ILayoutRestorer,
IFileBrowserFactory,
IRenderMimeRegistry,
ISettingRegistry
],
optional: [ILauncher, ILabStatus, ISessionContextDialogs],
optional: [
IMainMenu,
ICommandPalette,
ILauncher,
ILabStatus,
ISessionContextDialogs
],
activate: activateConsole,
autoStart: true
};
Expand Down Expand Up @@ -136,14 +140,14 @@ export default plugins;
*/
async function activateConsole(
app: JupyterFrontEnd,
mainMenu: IMainMenu,
palette: ICommandPalette,
contentFactory: ConsolePanel.IContentFactory,
editorServices: IEditorServices,
restorer: ILayoutRestorer,
browserFactory: IFileBrowserFactory,
rendermime: IRenderMimeRegistry,
settingRegistry: ISettingRegistry,
mainMenu: IMainMenu | null,
palette: ICommandPalette | null,
launcher: ILauncher | null,
status: ILabStatus | null,
sessionDialogs: ISessionContextDialogs | null
Expand Down Expand Up @@ -511,90 +515,94 @@ async function activateConsole(
isEnabled
});

// Add command palette items
[
CommandIDs.create,
CommandIDs.linebreak,
CommandIDs.clear,
CommandIDs.runUnforced,
CommandIDs.runForced,
CommandIDs.restart,
CommandIDs.interrupt,
CommandIDs.changeKernel,
CommandIDs.closeAndShutdown
].forEach(command => {
palette.addItem({ command, category, args: { isPalette: true } });
});

// Add a console creator to the File menu
mainMenu.fileMenu.newMenu.addGroup([{ command: CommandIDs.create }], 0);

// Add a close and shutdown command to the file menu.
mainMenu.fileMenu.closeAndCleaners.add({
tracker,
action: 'Shutdown',
name: 'Console',
closeAndCleanup: (current: ConsolePanel) => {
return showDialog({
title: 'Shut down the console?',
body: `Are you sure you want to close "${current.title.label}"?`,
buttons: [Dialog.cancelButton(), Dialog.warnButton()]
}).then(result => {
if (result.button.accept) {
return current.console.sessionContext.shutdown().then(() => {
current.dispose();
});
} else {
return void 0;
}
});
}
} as IFileMenu.ICloseAndCleaner<ConsolePanel>);
if (palette) {
// Add command palette items
[
CommandIDs.create,
CommandIDs.linebreak,
CommandIDs.clear,
CommandIDs.runUnforced,
CommandIDs.runForced,
CommandIDs.restart,
CommandIDs.interrupt,
CommandIDs.changeKernel,
CommandIDs.closeAndShutdown
].forEach(command => {
palette.addItem({ command, category, args: { isPalette: true } });
});
}

// Add a kernel user to the Kernel menu
mainMenu.kernelMenu.kernelUsers.add({
tracker,
interruptKernel: current => {
let kernel = current.console.sessionContext.session?.kernel;
if (kernel) {
return kernel.interrupt();
}
return Promise.resolve(void 0);
},
noun: 'Console',
restartKernel: current =>
sessionDialogs!.restart(current.console.sessionContext),
restartKernelAndClear: current => {
return sessionDialogs!
.restart(current.console.sessionContext)
.then(restarted => {
if (restarted) {
current.console.clear();
if (mainMenu) {
// Add a console creator to the File menu
mainMenu.fileMenu.newMenu.addGroup([{ command: CommandIDs.create }], 0);

// Add a close and shutdown command to the file menu.
mainMenu.fileMenu.closeAndCleaners.add({
tracker,
action: 'Shutdown',
name: 'Console',
closeAndCleanup: (current: ConsolePanel) => {
return showDialog({
title: 'Shut down the console?',
body: `Are you sure you want to close "${current.title.label}"?`,
buttons: [Dialog.cancelButton(), Dialog.warnButton()]
}).then(result => {
if (result.button.accept) {
return current.console.sessionContext.shutdown().then(() => {
current.dispose();
});
} else {
return void 0;
}
return restarted;
});
},
changeKernel: current =>
sessionDialogs!.selectKernel(current.console.sessionContext),
shutdownKernel: current => current.console.sessionContext.shutdown()
} as IKernelMenu.IKernelUser<ConsolePanel>);

// Add a code runner to the Run menu.
mainMenu.runMenu.codeRunners.add({
tracker,
noun: 'Cell',
pluralNoun: 'Cells',
run: current => current.console.execute(true)
} as IRunMenu.ICodeRunner<ConsolePanel>);

// Add a clearer to the edit menu
mainMenu.editMenu.clearers.add({
tracker,
noun: 'Console Cells',
clearCurrent: (current: ConsolePanel) => {
return current.console.clear();
}
} as IEditMenu.IClearer<ConsolePanel>);
}
} as IFileMenu.ICloseAndCleaner<ConsolePanel>);

// Add a kernel user to the Kernel menu
mainMenu.kernelMenu.kernelUsers.add({
tracker,
interruptKernel: current => {
let kernel = current.console.sessionContext.session?.kernel;
if (kernel) {
return kernel.interrupt();
}
return Promise.resolve(void 0);
},
noun: 'Console',
restartKernel: current =>
sessionDialogs!.restart(current.console.sessionContext),
restartKernelAndClear: current => {
return sessionDialogs!
.restart(current.console.sessionContext)
.then(restarted => {
if (restarted) {
current.console.clear();
}
return restarted;
});
},
changeKernel: current =>
sessionDialogs!.selectKernel(current.console.sessionContext),
shutdownKernel: current => current.console.sessionContext.shutdown()
} as IKernelMenu.IKernelUser<ConsolePanel>);

// Add a code runner to the Run menu.
mainMenu.runMenu.codeRunners.add({
tracker,
noun: 'Cell',
pluralNoun: 'Cells',
run: current => current.console.execute(true)
} as IRunMenu.ICodeRunner<ConsolePanel>);

// Add a clearer to the edit menu
mainMenu.editMenu.clearers.add({
tracker,
noun: 'Console Cells',
clearCurrent: (current: ConsolePanel) => {
return current.console.clear();
}
} as IEditMenu.IClearer<ConsolePanel>);
}

// For backwards compatibility and clarity, we explicitly label the run
// keystroke with the actual effected change, rather than the generic
Expand Down Expand Up @@ -634,21 +642,23 @@ async function activateConsole(
})
);

mainMenu.settingsMenu.addGroup(
[
{
type: 'submenu' as Menu.ItemType,
submenu: executeMenu
}
],
10
);
if (mainMenu) {
mainMenu.settingsMenu.addGroup(
[
{
type: 'submenu' as Menu.ItemType,
submenu: executeMenu
}
],
10
);

// Add kernel information to the application help menu.
mainMenu.helpMenu.kernelUsers.add({
tracker,
getKernel: current => current.sessionContext.session?.kernel
} as IHelpMenu.IKernelUser<ConsolePanel>);
// Add kernel information to the application help menu.
mainMenu.helpMenu.kernelUsers.add({
tracker,
getKernel: current => current.sessionContext.session?.kernel
} as IHelpMenu.IKernelUser<ConsolePanel>);
}

app.contextMenu.addItem({
command: CommandIDs.clear,
Expand Down

0 comments on commit bb8276c

Please sign in to comment.