Path: blob/main/src/vs/workbench/contrib/notebook/browser/controller/layoutActions.ts
5248 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Codicon } from '../../../../../base/common/codicons.js';6import { DisposableStore } from '../../../../../base/common/lifecycle.js';7import { URI, UriComponents } from '../../../../../base/common/uri.js';8import { localize, localize2 } from '../../../../../nls.js';9import { Categories } from '../../../../../platform/action/common/actionCommonCategories.js';10import { Action2, MenuId, MenuRegistry, registerAction2 } from '../../../../../platform/actions/common/actions.js';11import { ICommandService } from '../../../../../platform/commands/common/commands.js';12import { ConfigurationTarget, IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';13import { ContextKeyExpr } from '../../../../../platform/contextkey/common/contextkey.js';14import { ServicesAccessor } from '../../../../../platform/instantiation/common/instantiation.js';15import { IQuickInputService, IQuickPickItem } from '../../../../../platform/quickinput/common/quickInput.js';16import { NOTEBOOK_ACTIONS_CATEGORY } from './coreActions.js';17import { getNotebookEditorFromEditorPane } from '../notebookBrowser.js';18import { INotebookEditorService } from '../services/notebookEditorService.js';19import { NotebookSetting } from '../../common/notebookCommon.js';20import { NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_IS_ACTIVE_EDITOR } from '../../common/notebookContextKeys.js';21import { INotebookService } from '../../common/notebookService.js';22import { IEditorService } from '../../../../services/editor/common/editorService.js';23import { IPreferencesService } from '../../../../services/preferences/common/preferences.js';2425registerAction2(class NotebookConfigureLayoutAction extends Action2 {26constructor() {27super({28id: 'workbench.notebook.layout.select',29title: localize2('workbench.notebook.layout.select.label', "Select between Notebook Layouts"),30f1: true,31precondition: ContextKeyExpr.equals(`config.${NotebookSetting.openGettingStarted}`, true),32category: NOTEBOOK_ACTIONS_CATEGORY,33menu: [34{35id: MenuId.EditorTitle,36group: 'notebookLayout',37when: ContextKeyExpr.and(38NOTEBOOK_IS_ACTIVE_EDITOR,39ContextKeyExpr.notEquals('config.notebook.globalToolbar', true),40ContextKeyExpr.equals(`config.${NotebookSetting.openGettingStarted}`, true)41),42order: 043},44{45id: MenuId.NotebookToolbar,46group: 'notebookLayout',47when: ContextKeyExpr.and(48ContextKeyExpr.equals('config.notebook.globalToolbar', true),49ContextKeyExpr.equals(`config.${NotebookSetting.openGettingStarted}`, true)50),51order: 052}53]54});55}56run(accessor: ServicesAccessor): void {57accessor.get(ICommandService).executeCommand('workbench.action.openWalkthrough', { category: 'notebooks', step: 'notebookProfile' }, true);58}59});6061registerAction2(class NotebookConfigureLayoutAction extends Action2 {62constructor() {63super({64id: 'workbench.notebook.layout.configure',65title: localize2('workbench.notebook.layout.configure.label', "Customize Notebook Layout"),66f1: true,67category: NOTEBOOK_ACTIONS_CATEGORY,68menu: [69{70id: MenuId.NotebookToolbar,71group: 'notebookLayout',72when: ContextKeyExpr.equals('config.notebook.globalToolbar', true),73order: 174}75]76});77}78run(accessor: ServicesAccessor): void {79accessor.get(IPreferencesService).openSettings({ jsonEditor: false, query: '@tag:notebookLayout' });80}81});8283registerAction2(class NotebookConfigureLayoutFromEditorTitle extends Action2 {84constructor() {85super({86id: 'workbench.notebook.layout.configure.editorTitle',87title: localize2('workbench.notebook.layout.configure.label', "Customize Notebook Layout"),88f1: false,89category: NOTEBOOK_ACTIONS_CATEGORY,90menu: [91{92id: MenuId.NotebookEditorLayoutConfigure,93group: 'notebookLayout',94when: NOTEBOOK_IS_ACTIVE_EDITOR,95order: 196}97]98});99}100run(accessor: ServicesAccessor): void {101accessor.get(IPreferencesService).openSettings({ jsonEditor: false, query: '@tag:notebookLayout' });102}103});104105MenuRegistry.appendMenuItem(MenuId.EditorTitle, {106submenu: MenuId.NotebookEditorLayoutConfigure,107title: localize2('customizeNotebook', "Customize Notebook..."),108icon: Codicon.gear,109group: 'navigation',110order: -1,111when: NOTEBOOK_IS_ACTIVE_EDITOR112});113114registerAction2(class ToggleLineNumberFromEditorTitle extends Action2 {115constructor() {116super({117id: 'notebook.toggleLineNumbersFromEditorTitle',118title: localize2('notebook.toggleLineNumbers', 'Toggle Notebook Line Numbers'),119shortTitle: localize2('notebook.toggleLineNumbers.short', 'Line Numbers'),120precondition: NOTEBOOK_EDITOR_FOCUSED,121menu: [122{123id: MenuId.NotebookEditorLayoutConfigure,124group: 'notebookLayoutDetails',125order: 1,126when: NOTEBOOK_IS_ACTIVE_EDITOR127}],128category: NOTEBOOK_ACTIONS_CATEGORY,129f1: true,130toggled: {131condition: ContextKeyExpr.notEquals('config.notebook.lineNumbers', 'off'),132title: localize('notebook.showLineNumbers', "Line Numbers"),133}134});135}136137async run(accessor: ServicesAccessor): Promise<void> {138return accessor.get(ICommandService).executeCommand('notebook.toggleLineNumbers');139}140});141142registerAction2(class ToggleCellToolbarPositionFromEditorTitle extends Action2 {143constructor() {144super({145id: 'notebook.toggleCellToolbarPositionFromEditorTitle',146title: localize2('notebook.toggleCellToolbarPosition', 'Toggle Cell Toolbar Position'),147menu: [{148id: MenuId.NotebookEditorLayoutConfigure,149group: 'notebookLayoutDetails',150order: 3151}],152category: NOTEBOOK_ACTIONS_CATEGORY,153f1: false154});155}156157async run(accessor: ServicesAccessor, ...args: unknown[]): Promise<void> {158return accessor.get(ICommandService).executeCommand('notebook.toggleCellToolbarPosition', ...args);159}160});161162registerAction2(class ToggleBreadcrumbFromEditorTitle extends Action2 {163constructor() {164super({165id: 'breadcrumbs.toggleFromEditorTitle',166title: localize2('notebook.toggleBreadcrumb', 'Toggle Breadcrumbs'),167shortTitle: localize2('notebook.toggleBreadcrumb.short', 'Breadcrumbs'),168toggled: {169condition: ContextKeyExpr.equals('config.breadcrumbs.enabled', true),170title: localize('cmd.toggle2', "Breadcrumbs")171},172menu: [{173id: MenuId.NotebookEditorLayoutConfigure,174group: 'notebookLayoutDetails',175order: 2176}],177category: NOTEBOOK_ACTIONS_CATEGORY,178f1: false179});180}181182async run(accessor: ServicesAccessor): Promise<void> {183return accessor.get(ICommandService).executeCommand('breadcrumbs.toggle');184}185});186187registerAction2(class SaveMimeTypeDisplayOrder extends Action2 {188constructor() {189super({190id: 'notebook.saveMimeTypeOrder',191title: localize2('notebook.saveMimeTypeOrder', "Save Mimetype Display Order"),192f1: true,193category: NOTEBOOK_ACTIONS_CATEGORY,194precondition: NOTEBOOK_IS_ACTIVE_EDITOR,195});196}197198run(accessor: ServicesAccessor) {199const service = accessor.get(INotebookService);200const disposables = new DisposableStore();201const qp = disposables.add(accessor.get(IQuickInputService).createQuickPick<IQuickPickItem & { target: ConfigurationTarget }>());202qp.placeholder = localize('notebook.placeholder', 'Settings file to save in');203qp.items = [204{ target: ConfigurationTarget.USER, label: localize('saveTarget.machine', 'User Settings') },205{ target: ConfigurationTarget.WORKSPACE, label: localize('saveTarget.workspace', 'Workspace Settings') },206];207208disposables.add(qp.onDidAccept(() => {209const target = qp.selectedItems[0]?.target;210if (target !== undefined) {211service.saveMimeDisplayOrder(target);212}213qp.dispose();214}));215216disposables.add(qp.onDidHide(() => disposables.dispose()));217218qp.show();219}220});221222registerAction2(class NotebookWebviewResetAction extends Action2 {223constructor() {224super({225id: 'workbench.notebook.layout.webview.reset',226title: localize2('workbench.notebook.layout.webview.reset.label', "Reset Notebook Webview"),227f1: false,228category: NOTEBOOK_ACTIONS_CATEGORY229});230}231run(accessor: ServicesAccessor, args?: UriComponents): void {232const editorService = accessor.get(IEditorService);233234if (args) {235const uri = URI.revive(args);236const notebookEditorService = accessor.get(INotebookEditorService);237const widgets = notebookEditorService.listNotebookEditors().filter(widget => widget.hasModel() && widget.textModel.uri.toString() === uri.toString());238for (const widget of widgets) {239if (widget.hasModel()) {240widget.getInnerWebview()?.reload();241}242}243} else {244const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane);245if (!editor) {246return;247}248249editor.getInnerWebview()?.reload();250}251}252});253254registerAction2(class ToggleNotebookStickyScroll extends Action2 {255constructor() {256super({257id: 'notebook.action.toggleNotebookStickyScroll',258title: {259...localize2('toggleStickyScroll', "Toggle Notebook Sticky Scroll"),260mnemonicTitle: localize({ key: 'mitoggleNotebookStickyScroll', comment: ['&& denotes a mnemonic'] }, "&&Sticky Scroll"),261},262shortTitle: localize2('toggleStickyScroll.short', "Sticky Scroll"),263category: Categories.View,264toggled: {265condition: ContextKeyExpr.equals('config.notebook.stickyScroll.enabled', true),266title: localize('notebookStickyScroll', "Sticky Scroll"),267mnemonicTitle: localize({ key: 'mitoggleNotebookStickyScroll', comment: ['&& denotes a mnemonic'] }, "&&Sticky Scroll"),268},269menu: [270{ id: MenuId.CommandPalette },271{ id: MenuId.NotebookStickyScrollContext, group: 'notebookView', order: 2 },272{ id: MenuId.NotebookToolbarContext, group: 'notebookView', order: 2 }273]274});275}276277override async run(accessor: ServicesAccessor): Promise<void> {278const configurationService = accessor.get(IConfigurationService);279const newValue = !configurationService.getValue('notebook.stickyScroll.enabled');280return configurationService.updateValue('notebook.stickyScroll.enabled', newValue);281}282});283284285