Skip to content

Commit ce863ce

Browse files
committed
Add alt actions to some toolbar items
1 parent a6af569 commit ce863ce

4 files changed

Lines changed: 118 additions & 36 deletions

File tree

src/vs/workbench/contrib/notebook/browser/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ export const DELETE_CELL_COMMAND_ID = 'workbench.notebook.cell.delete';
1414

1515
export const MOVE_CELL_UP_COMMAND_ID = 'workbench.notebook.cell.moveUp';
1616
export const MOVE_CELL_DOWN_COMMAND_ID = 'workbench.notebook.cell.moveDown';
17+
export const COPY_CELL_UP_COMMAND_ID = 'workbench.notebook.cell.copyUp';
18+
export const COPY_CELL_DOWN_COMMAND_ID = 'workbench.notebook.cell.copyDown';
1719

1820
export const EXECUTE_CELL_COMMAND_ID = 'workbench.notebook.cell.execute';

src/vs/workbench/contrib/notebook/browser/contrib/notebookActions.ts

Lines changed: 83 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/commo
1111
import { InputFocusedContext, InputFocusedContextKey } from 'vs/platform/contextkey/common/contextkeys';
1212
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
1313
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
14-
import { DELETE_CELL_COMMAND_ID, EDIT_CELL_COMMAND_ID, INSERT_CODE_CELL_ABOVE_COMMAND_ID, INSERT_CODE_CELL_BELOW_COMMAND_ID, INSERT_MARKDOWN_CELL_ABOVE_COMMAND_ID, INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID, MOVE_CELL_DOWN_COMMAND_ID, MOVE_CELL_UP_COMMAND_ID, SAVE_CELL_COMMAND_ID } from 'vs/workbench/contrib/notebook/browser/constants';
14+
import { DELETE_CELL_COMMAND_ID, EDIT_CELL_COMMAND_ID, INSERT_CODE_CELL_ABOVE_COMMAND_ID, INSERT_CODE_CELL_BELOW_COMMAND_ID, INSERT_MARKDOWN_CELL_ABOVE_COMMAND_ID, INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID, MOVE_CELL_DOWN_COMMAND_ID, MOVE_CELL_UP_COMMAND_ID, SAVE_CELL_COMMAND_ID, COPY_CELL_UP_COMMAND_ID, COPY_CELL_DOWN_COMMAND_ID } from 'vs/workbench/contrib/notebook/browser/constants';
1515
import { INotebookEditor, KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED, NOTEBOOK_EDITOR_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
1616
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
1717
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel';
@@ -438,14 +438,13 @@ export class InsertCodeCellAboveAction extends MenuItemAction {
438438
super(
439439
{
440440
id: INSERT_CODE_CELL_ABOVE_COMMAND_ID,
441-
title: localize('notebookActions.insertCodeCellAbove', "Insert Code Cell Above")
441+
title: localize('notebookActions.insertCodeCellAbove', "Insert Code Cell Above"),
442+
icon: { id: 'codicon/add' }
442443
},
443444
undefined,
444445
{ shouldForwardArgs: true },
445446
contextKeyService,
446447
commandService);
447-
448-
this.class = 'codicon-add';
449448
}
450449
}
451450

@@ -457,14 +456,17 @@ export class InsertCodeCellBelowAction extends MenuItemAction {
457456
super(
458457
{
459458
id: INSERT_CODE_CELL_BELOW_COMMAND_ID,
460-
title: localize('notebookActions.insertCodeCellBelow', "Insert Code Cell Below")
459+
title: localize('notebookActions.insertCodeCellBelow', "Insert Code Cell Below"),
460+
icon: { id: 'codicon/add' }
461+
},
462+
{
463+
id: INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID,
464+
title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below"),
465+
icon: { id: 'codicon/add' }
461466
},
462-
undefined,
463467
{ shouldForwardArgs: true },
464468
contextKeyService,
465469
commandService);
466-
467-
this.class = 'codicon-add';
468470
}
469471
}
470472

@@ -476,14 +478,13 @@ export class InsertMarkdownCellAboveAction extends MenuItemAction {
476478
super(
477479
{
478480
id: INSERT_MARKDOWN_CELL_ABOVE_COMMAND_ID,
479-
title: localize('notebookActions.insertMarkdownCellAbove', "Insert Markdown Cell Above")
481+
title: localize('notebookActions.insertMarkdownCellAbove', "Insert Markdown Cell Above"),
482+
icon: { id: 'codicon/add' }
480483
},
481484
undefined,
482485
{ shouldForwardArgs: true },
483486
contextKeyService,
484487
commandService);
485-
486-
this.class = 'codicon-add';
487488
}
488489
}
489490

@@ -495,14 +496,13 @@ export class InsertMarkdownCellBelowAction extends MenuItemAction {
495496
super(
496497
{
497498
id: INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID,
498-
title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below")
499+
title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below"),
500+
icon: { id: 'codicon/add' }
499501
},
500502
undefined,
501503
{ shouldForwardArgs: true },
502504
contextKeyService,
503505
commandService);
504-
505-
this.class = 'codicon-add';
506506
}
507507
}
508508

@@ -540,14 +540,13 @@ export class EditCellAction extends MenuItemAction {
540540
super(
541541
{
542542
id: EDIT_CELL_COMMAND_ID,
543-
title: localize('notebookActions.editCell', "Edit Cell")
543+
title: localize('notebookActions.editCell', "Edit Cell"),
544+
icon: { id: 'codicon/pencil' }
544545
},
545546
undefined,
546547
{ shouldForwardArgs: true },
547548
contextKeyService,
548549
commandService);
549-
550-
this.class = 'codicon-pencil';
551550
}
552551
}
553552

@@ -580,14 +579,13 @@ export class SaveCellAction extends MenuItemAction {
580579
super(
581580
{
582581
id: SAVE_CELL_COMMAND_ID,
583-
title: localize('notebookActions.saveCell', "Save Cell")
582+
title: localize('notebookActions.saveCell', "Save Cell"),
583+
icon: { id: 'codicon/save' }
584584
},
585585
undefined,
586586
{ shouldForwardArgs: true },
587587
contextKeyService,
588588
commandService);
589-
590-
this.class = 'codicon-save';
591589
}
592590
}
593591

@@ -620,14 +618,15 @@ export class DeleteCellAction extends MenuItemAction {
620618
super(
621619
{
622620
id: DELETE_CELL_COMMAND_ID,
623-
title: localize('notebookActions.deleteCell', "Delete Cell")
621+
title: localize('notebookActions.deleteCell', "Delete Cell"),
622+
icon: { id: 'codicon/x' }
624623
},
625624
undefined,
626625
{ shouldForwardArgs: true },
627626
contextKeyService,
628627
commandService);
629628

630-
this.class = 'codicon-trash';
629+
this.class = 'codicon-x';
631630
}
632631
}
633632

@@ -637,6 +636,12 @@ async function moveCell(context: INotebookCellActionContext, direction: 'up' | '
637636
context.notebookEditor.moveCellDown(context.cell);
638637
}
639638

639+
async function copyCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise<void> {
640+
const text = context.cell.getText();
641+
const newCellDirection = direction === 'up' ? 'above' : 'below';
642+
return context.notebookEditor.insertNotebookCell(context.cell, context.cell.cellKind, newCellDirection, text);
643+
}
644+
640645
registerAction2(class extends Action2 {
641646
constructor() {
642647
super(
@@ -666,14 +671,17 @@ export class MoveCellUpAction extends MenuItemAction {
666671
super(
667672
{
668673
id: MOVE_CELL_UP_COMMAND_ID,
669-
title: localize('notebookActions.moveCellUp', "Move Cell Up")
674+
title: localize('notebookActions.moveCellUp', "Move Cell Up"),
675+
icon: { id: 'codicon/arrow-up' }
676+
},
677+
{
678+
id: COPY_CELL_UP_COMMAND_ID,
679+
title: localize('notebookActions.copyCellUp', "Copy Cell Up"),
680+
icon: { id: 'codicon/arrow-up' }
670681
},
671-
undefined,
672682
{ shouldForwardArgs: true },
673683
contextKeyService,
674684
commandService);
675-
676-
this.class = 'codicon-arrow-up';
677685
}
678686
}
679687

@@ -706,13 +714,60 @@ export class MoveCellDownAction extends MenuItemAction {
706714
super(
707715
{
708716
id: MOVE_CELL_DOWN_COMMAND_ID,
709-
title: localize('notebookActions.moveCellDown', "Move Cell Down")
717+
title: localize('notebookActions.moveCellDown', "Move Cell Down"),
718+
icon: { id: 'codicon/arrow-down' }
719+
},
720+
{
721+
id: COPY_CELL_DOWN_COMMAND_ID,
722+
title: localize('notebookActions.copyCellDown', "Copy Cell Down"),
723+
icon: { id: 'codicon/arrow-down' }
710724
},
711-
undefined,
712725
{ shouldForwardArgs: true },
713726
contextKeyService,
714727
commandService);
715728

716729
this.class = 'codicon-arrow-down';
717730
}
718731
}
732+
733+
registerAction2(class extends Action2 {
734+
constructor() {
735+
super(
736+
{
737+
id: COPY_CELL_UP_COMMAND_ID,
738+
title: localize('notebookActions.copyCellUp', "Copy Cell Up")
739+
});
740+
}
741+
742+
async run(accessor: ServicesAccessor, context?: INotebookCellActionContext) {
743+
if (!context) {
744+
context = getActiveCellContext(accessor);
745+
if (!context) {
746+
return;
747+
}
748+
}
749+
750+
return copyCell(context, 'up');
751+
}
752+
});
753+
754+
registerAction2(class extends Action2 {
755+
constructor() {
756+
super(
757+
{
758+
id: COPY_CELL_DOWN_COMMAND_ID,
759+
title: localize('notebookActions.copyCellDown', "Copy Cell Down")
760+
});
761+
}
762+
763+
async run(accessor: ServicesAccessor, context?: INotebookCellActionContext) {
764+
if (!context) {
765+
context = getActiveCellContext(accessor);
766+
if (!context) {
767+
return;
768+
}
769+
}
770+
771+
return copyCell(context, 'down');
772+
}
773+
});

src/vs/workbench/contrib/notebook/browser/notebookEditor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,12 @@ registerThemingParticipant((theme, collector) => {
703703
}
704704
const link = theme.getColor(textLinkForeground);
705705
if (link) {
706-
collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor a { color: ${link}; }`);
706+
collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell a { color: ${link}; }`);
707707
}
708708
const activeLink = theme.getColor(textLinkActiveForeground);
709709
if (activeLink) {
710-
collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor a:hover,
711-
.monaco-workbench .part.editor > .content .notebook-editor a:active { color: ${activeLink}; }`);
710+
collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .cell a:hover,
711+
.monaco-workbench .part.editor > .content .notebook-editor .cell a:active { color: ${activeLink}; }`);
712712
}
713713
const shortcut = theme.getColor(textPreformatForeground);
714714
if (shortcut) {

src/vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import { CodeCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/c
2424
import { StatefullMarkdownCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/markdownCell';
2525
import { CellKind, EDITOR_BOTTOM_PADDING, EDITOR_TOP_PADDING } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2626
import { CellViewModel } from '../../viewModel/notebookCellViewModel';
27+
import { ContextAwareMenuEntryActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
28+
import { MenuItemAction } from 'vs/platform/actions/common/actions';
29+
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
30+
import { INotificationService } from 'vs/platform/notification/common/notification';
2731

2832
export class NotebookCellListDelegate implements IListVirtualDelegate<CellViewModel> {
2933
private _lineHeight: number;
@@ -61,6 +65,8 @@ abstract class AbstractCellRenderer {
6165
protected readonly notebookEditor: INotebookEditor,
6266
protected readonly contextMenuService: IContextMenuService,
6367
private readonly configurationService: IConfigurationService,
68+
private readonly keybindingService: IKeybindingService,
69+
private readonly notificationService: INotificationService,
6470
language: string,
6571
) {
6672
const editorOptions = deepClone(this.configurationService.getValue<IEditorOptions>('editor', { overrideIdentifier: language }));
@@ -86,6 +92,21 @@ abstract class AbstractCellRenderer {
8692
};
8793
}
8894

95+
protected createToolbar(container: HTMLElement): ToolBar {
96+
const toolbar = new ToolBar(container, this.contextMenuService, {
97+
actionViewItemProvider: action => {
98+
if (action instanceof MenuItemAction) {
99+
const item = new ContextAwareMenuEntryActionViewItem(action, this.keybindingService, this.notificationService, this.contextMenuService);
100+
return item;
101+
}
102+
103+
return undefined;
104+
}
105+
});
106+
107+
return toolbar;
108+
}
109+
89110
showContextMenu(listIndex: number | undefined, element: CellViewModel, x: number, y: number) {
90111
const actions: IAction[] = [
91112
this.instantiationService.createInstance(InsertCodeCellAboveAction),
@@ -125,9 +146,11 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR
125146
notehookEditor: INotebookEditor,
126147
@IInstantiationService instantiationService: IInstantiationService,
127148
@IConfigurationService configurationService: IConfigurationService,
128-
@IContextMenuService contextMenuService: IContextMenuService
149+
@IContextMenuService contextMenuService: IContextMenuService,
150+
@IKeybindingService keybindingService: IKeybindingService,
151+
@INotificationService notificationService: INotificationService,
129152
) {
130-
super(instantiationService, notehookEditor, contextMenuService, configurationService, 'markdown');
153+
super(instantiationService, notehookEditor, contextMenuService, configurationService, keybindingService, notificationService, 'markdown');
131154
}
132155

133156
get templateId() {
@@ -140,7 +163,7 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR
140163
codeInnerContent.style.display = 'none';
141164

142165
const disposables = new DisposableStore();
143-
const toolbar = new ToolBar(container, this.contextMenuService);
166+
const toolbar = this.createToolbar(container);
144167
toolbar.setActions([
145168
this.instantiationService.createInstance(MoveCellUpAction),
146169
this.instantiationService.createInstance(MoveCellDownAction),
@@ -241,8 +264,10 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
241264
@IContextMenuService contextMenuService: IContextMenuService,
242265
@IConfigurationService configurationService: IConfigurationService,
243266
@IInstantiationService instantiationService: IInstantiationService,
267+
@IKeybindingService keybindingService: IKeybindingService,
268+
@INotificationService notificationService: INotificationService,
244269
) {
245-
super(instantiationService, notebookEditor, contextMenuService, configurationService, 'python');
270+
super(instantiationService, notebookEditor, contextMenuService, configurationService, keybindingService, notificationService, 'python');
246271
}
247272

248273
get templateId() {
@@ -254,7 +279,7 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
254279
const toolbarContainer = document.createElement('div');
255280
container.appendChild(toolbarContainer);
256281
DOM.addClasses(toolbarContainer, 'menu', 'codicon-settings-gear', 'codicon');
257-
const toolbar = new ToolBar(container, this.contextMenuService);
282+
const toolbar = this.createToolbar(container);
258283
toolbar.setActions([
259284
this.instantiationService.createInstance(MoveCellUpAction),
260285
this.instantiationService.createInstance(MoveCellDownAction),

0 commit comments

Comments
 (0)