Skip to content

Commit

Permalink
Backport PR jupyterlab#14664: Fix semantic commands enabled status
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval authored and meeseeksmachine committed Aug 3, 2023
1 parent d3bf456 commit 7ece176
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 28 deletions.
2 changes: 1 addition & 1 deletion galata/test/documentation/debugger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test.describe('Debugger', () => {
await createNotebook(page);

const runButton = await page.waitForSelector(
'.jp-Toolbar-item >> [data-command="runmenu:run"]'
'.jp-Toolbar-item >> [data-command="notebook:run-cell-and-select-next"]'
);

// Inject mouse pointer
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/application-extension/schema/top-bar.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
14 changes: 12 additions & 2 deletions packages/apputils-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,11 @@ const utilityCommands: JupyterFrontEndPlugin<void> = {
commands.addCommand(CommandIDs.runAllEnabled, {
label: trans.__('Run All Enabled Commands Passed as Args'),
execute: async args => {
const commands: string[] = args.commands as string[];
const commands: string[] = (args.commands as string[]) ?? [];
const commandArgs: any = args.args;
const argList = Array.isArray(args);
const errorIfNotEnabled: boolean = args.errorIfNotEnabled as boolean;
const errorIfNotEnabled: boolean =
(args.errorIfNotEnabled as boolean) ?? false;
for (let i = 0; i < commands.length; i++) {
const cmd = commands[i];
const arg = argList ? commandArgs[i] : commandArgs;
Expand All @@ -627,6 +628,15 @@ const utilityCommands: JupyterFrontEndPlugin<void> = {
}
}
}
},
isEnabled: args => {
const commands: string[] = (args.commands as string[]) ?? [];
const commandArgs: any = args.args;
const argList = Array.isArray(args);

return commands.some((cmd, idx) =>
app.commands.isEnabled(cmd, argList ? commandArgs[idx] : commandArgs)
);
}
});

Expand Down
4 changes: 3 additions & 1 deletion packages/apputils/src/toolbar/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function createDefaultFactory(
command: tId,
args: tArgs,
label: tLabel,
caption: tCaption,
icon: tIcon
} = toolbarItem;
const id = tId ?? '';
Expand All @@ -156,7 +157,8 @@ export function createDefaultFactory(
id,
args,
icon,
label
label,
caption: tCaption as string
});
}
case 'spacer':
Expand Down
5 changes: 5 additions & 0 deletions packages/cell-toolbar-extension/schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions packages/csvviewer-extension/schema/csv.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions packages/csvviewer-extension/schema/tsv.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions packages/filebrowser-extension/schema/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions packages/fileeditor-extension/schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
14 changes: 8 additions & 6 deletions packages/fileeditor-extension/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ export namespace Commands {

// Add a code runner to the run menu.
if (consoleTracker) {
addCodeRunnersToRunMenu(menu, consoleTracker);
addCodeRunnersToRunMenu(menu, consoleTracker, isEnabled);
}
}

Expand All @@ -1303,24 +1303,26 @@ export namespace Commands {
*/
export function addCodeRunnersToRunMenu(
menu: IMainMenu,
consoleTracker: IConsoleTracker
consoleTracker: IConsoleTracker,
isEnabled: () => boolean
): void {
const isEnabled = (current: IDocumentWidget<FileEditor>) =>
const isEnabled_ = (current: IDocumentWidget<FileEditor>) =>
isEnabled() &&
current.context &&
!!consoleTracker.find(
widget => widget.sessionContext.session?.path === current.context.path
);
menu.runMenu.codeRunners.restart.add({
id: CommandIDs.restartConsole,
isEnabled
isEnabled: isEnabled_
});
menu.runMenu.codeRunners.run.add({
id: CommandIDs.runCode,
isEnabled
isEnabled: isEnabled_
});
menu.runMenu.codeRunners.runAll.add({
id: CommandIDs.runAllCode,
isEnabled
isEnabled: isEnabled_
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/htmlviewer-extension/schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
21 changes: 17 additions & 4 deletions packages/notebook-extension/schema/panel.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@
{ "name": "cut", "command": "notebook:cut-cell", "rank": 21 },
{ "name": "copy", "command": "notebook:copy-cell", "rank": 22 },
{ "name": "paste", "command": "notebook:paste-cell-below", "rank": 23 },
{ "name": "run", "command": "runmenu:run", "rank": 30 },
{ "name": "interrupt", "command": "kernelmenu:interrupt", "rank": 31 },
{ "name": "restart", "command": "kernelmenu:restart", "rank": 32 },
{
"name": "run",
"command": "notebook:run-cell-and-select-next",
"rank": 30
},
{
"name": "interrupt",
"command": "notebook:interrupt-kernel",
"rank": 31
},
{ "name": "restart", "command": "notebook:restart-kernel", "rank": 32 },
{
"name": "restart-and-run",
"command": "runmenu:restart-and-run-all",
"command": "notebook:restart-run-all",
"rank": 33
},
{ "name": "cellType", "rank": 40 },
Expand Down Expand Up @@ -72,6 +80,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
32 changes: 19 additions & 13 deletions packages/notebook-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ import {
moveDownIcon,
moveUpIcon,
notebookIcon,
pasteIcon
pasteIcon,
refreshIcon,
runIcon,
stopIcon
} from '@jupyterlab/ui-components';
import { ArrayExt } from '@lumino/algorithm';
import { CommandRegistry } from '@lumino/commands';
Expand Down Expand Up @@ -2183,7 +2186,8 @@ function addCommands(
);
}
},
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled()),
icon: args => (args.toolbar ? runIcon : undefined)
});
commands.addCommand(CommandIDs.run, {
label: args => {
Expand Down Expand Up @@ -2326,7 +2330,8 @@ function addCommands(
return sessionDialogs.restart(current.sessionContext);
}
},
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled()),
icon: args => (args.toolbar ? refreshIcon : undefined)
});
commands.addCommand(CommandIDs.shutdown, {
label: trans.__('Shut Down Kernel'),
Expand Down Expand Up @@ -2421,8 +2426,8 @@ function addCommands(
await commands.execute(CommandIDs.runAll);
}
},
isEnabled,
icon: fastForwardIcon
isEnabled: args => (args.toolbar ? true : isEnabled()),
icon: args => (args.toolbar ? fastForwardIcon : undefined)
});
commands.addCommand(CommandIDs.clearAllOutputs, {
label: trans.__('Clear Outputs of All Cells'),
Expand Down Expand Up @@ -2464,7 +2469,8 @@ function addCommands(
return kernel.interrupt();
}
},
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled()),
icon: args => (args.toolbar ? stopIcon : undefined)
});
commands.addCommand(CommandIDs.toCode, {
label: trans.__('Change to Code Cell Type'),
Expand Down Expand Up @@ -2524,7 +2530,7 @@ function addCommands(
}
},
icon: args => (args.toolbar ? cutIcon : undefined),
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled())
});
commands.addCommand(CommandIDs.copy, {
label: args => {
Expand All @@ -2551,7 +2557,7 @@ function addCommands(
}
},
icon: args => (args.toolbar ? copyIcon : undefined),
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled())
});
commands.addCommand(CommandIDs.pasteBelow, {
label: args => {
Expand All @@ -2578,7 +2584,7 @@ function addCommands(
}
},
icon: args => (args.toolbar ? pasteIcon : undefined),
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled())
});
commands.addCommand(CommandIDs.pasteAbove, {
label: args => {
Expand Down Expand Up @@ -2631,7 +2637,7 @@ function addCommands(
}
},
icon: args => (args.toolbar ? duplicateIcon : undefined),
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled())
});
commands.addCommand(CommandIDs.pasteAndReplace, {
label: args => {
Expand Down Expand Up @@ -2676,7 +2682,7 @@ function addCommands(
return NotebookActions.deleteCells(current.content);
}
},
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled())
});
commands.addCommand(CommandIDs.split, {
label: trans.__('Split Cell'),
Expand Down Expand Up @@ -2733,7 +2739,7 @@ function addCommands(
}
},
icon: args => (args.toolbar ? addAboveIcon : undefined),
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled())
});
commands.addCommand(CommandIDs.insertBelow, {
label: trans.__('Insert Cell Below'),
Expand All @@ -2746,7 +2752,7 @@ function addCommands(
}
},
icon: args => (args.toolbar ? addBelowIcon : undefined),
isEnabled
isEnabled: args => (args.toolbar ? true : isEnabled())
});
commands.addCommand(CommandIDs.selectAbove, {
label: trans.__('Select Cell Above'),
Expand Down
5 changes: 5 additions & 0 deletions packages/settingregistry/src/plugin-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
Expand Down
6 changes: 5 additions & 1 deletion packages/ui-components/src/components/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,10 @@ export namespace CommandToolbarButtonComponent {
* Overrides command label
*/
label?: string;
/**
* Overrides command caption
*/
caption?: string;
}
}

Expand Down Expand Up @@ -1089,7 +1093,7 @@ namespace Private {
dataset: { 'data-command': options.id },
icon,
iconClass,
tooltip,
tooltip: options.caption ?? tooltip,
onClick,
enabled,
label: options.label ?? label
Expand Down

0 comments on commit 7ece176

Please sign in to comment.