Path: blob/main/src/vs/workbench/browser/parts/titlebar/titlebarActions.ts
3296 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 { ILocalizedString, localize, localize2 } from '../../../../nls.js';6import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';7import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';8import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';9import { LayoutSettings } from '../../../services/layout/browser/layoutService.js';10import { Action2, MenuId, registerAction2 } from '../../../../platform/actions/common/actions.js';11import { ContextKeyExpr, ContextKeyExpression, IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';12import { ACCOUNTS_ACTIVITY_ID, GLOBAL_ACTIVITY_ID } from '../../../common/activity.js';13import { IAction } from '../../../../base/common/actions.js';14import { IsMainWindowFullscreenContext, IsCompactTitleBarContext, TitleBarStyleContext, TitleBarVisibleContext } from '../../../common/contextkeys.js';15import { CustomTitleBarVisibility, TitleBarSetting, TitlebarStyle } from '../../../../platform/window/common/window.js';1617// --- Context Menu Actions --- //1819export class ToggleTitleBarConfigAction extends Action2 {2021constructor(private readonly section: string, title: string, description: string | ILocalizedString | undefined, order: number, when?: ContextKeyExpression) {2223super({24id: `toggle.${section}`,25title,26metadata: description ? { description } : undefined,27toggled: ContextKeyExpr.equals(`config.${section}`, true),28menu: [29{30id: MenuId.TitleBarContext,31when,32order,33group: '2_config'34},35{36id: MenuId.TitleBarTitleContext,37when,38order,39group: '2_config'40}41]42});43}4445run(accessor: ServicesAccessor, ...args: any[]): void {46const configService = accessor.get(IConfigurationService);47const value = configService.getValue(this.section);48configService.updateValue(this.section, !value);49}50}5152registerAction2(class ToggleCommandCenter extends ToggleTitleBarConfigAction {53constructor() {54super(LayoutSettings.COMMAND_CENTER, localize('toggle.commandCenter', 'Command Center'), localize('toggle.commandCenterDescription', "Toggle visibility of the Command Center in title bar"), 1, IsCompactTitleBarContext.toNegated());55}56});5758registerAction2(class ToggleNavigationControl extends ToggleTitleBarConfigAction {59constructor() {60super('workbench.navigationControl.enabled', localize('toggle.navigation', 'Navigation Controls'), localize('toggle.navigationDescription', "Toggle visibility of the Navigation Controls in title bar"), 2, ContextKeyExpr.and(IsCompactTitleBarContext.toNegated(), ContextKeyExpr.has('config.window.commandCenter')));61}62});6364registerAction2(class ToggleLayoutControl extends ToggleTitleBarConfigAction {65constructor() {66super(LayoutSettings.LAYOUT_ACTIONS, localize('toggle.layout', 'Layout Controls'), localize('toggle.layoutDescription', "Toggle visibility of the Layout Controls in title bar"), 4);67}68});6970registerAction2(class ToggleCustomTitleBar extends Action2 {71constructor() {72super({73id: `toggle.${TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY}`,74title: localize('toggle.hideCustomTitleBar', 'Hide Custom Title Bar'),75menu: [76{ id: MenuId.TitleBarContext, order: 0, when: ContextKeyExpr.equals(TitleBarStyleContext.key, TitlebarStyle.NATIVE), group: '3_toggle' },77{ id: MenuId.TitleBarTitleContext, order: 0, when: ContextKeyExpr.equals(TitleBarStyleContext.key, TitlebarStyle.NATIVE), group: '3_toggle' },78]79});80}8182run(accessor: ServicesAccessor, ...args: any[]): void {83const configService = accessor.get(IConfigurationService);84configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.NEVER);85}86});8788registerAction2(class ToggleCustomTitleBarWindowed extends Action2 {89constructor() {90super({91id: `toggle.${TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY}.windowed`,92title: localize('toggle.hideCustomTitleBarInFullScreen', 'Hide Custom Title Bar In Full Screen'),93menu: [94{ id: MenuId.TitleBarContext, order: 1, when: IsMainWindowFullscreenContext, group: '3_toggle' },95{ id: MenuId.TitleBarTitleContext, order: 1, when: IsMainWindowFullscreenContext, group: '3_toggle' },96]97});98}99100run(accessor: ServicesAccessor, ...args: any[]): void {101const configService = accessor.get(IConfigurationService);102configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.WINDOWED);103}104});105106class ToggleCustomTitleBar extends Action2 {107108constructor() {109super({110id: `toggle.toggleCustomTitleBar`,111title: localize('toggle.customTitleBar', 'Custom Title Bar'),112toggled: TitleBarVisibleContext,113menu: [114{115id: MenuId.MenubarAppearanceMenu,116order: 6,117when: ContextKeyExpr.or(118ContextKeyExpr.and(119ContextKeyExpr.equals(TitleBarStyleContext.key, TitlebarStyle.NATIVE),120ContextKeyExpr.and(121ContextKeyExpr.equals('config.workbench.layoutControl.enabled', false),122ContextKeyExpr.equals('config.window.commandCenter', false),123ContextKeyExpr.notEquals('config.workbench.editor.editorActionsLocation', 'titleBar'),124ContextKeyExpr.notEquals('config.workbench.activityBar.location', 'top'),125ContextKeyExpr.notEquals('config.workbench.activityBar.location', 'bottom')126)?.negate()127),128IsMainWindowFullscreenContext129),130group: '2_workbench_layout'131},132],133});134}135136run(accessor: ServicesAccessor, ...args: any[]): void {137const configService = accessor.get(IConfigurationService);138const contextKeyService = accessor.get(IContextKeyService);139const titleBarVisibility = configService.getValue<CustomTitleBarVisibility>(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY);140switch (titleBarVisibility) {141case CustomTitleBarVisibility.NEVER:142configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.AUTO);143break;144case CustomTitleBarVisibility.WINDOWED: {145const isFullScreen = IsMainWindowFullscreenContext.evaluate(contextKeyService.getContext(null));146if (isFullScreen) {147configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.AUTO);148} else {149configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.NEVER);150}151break;152}153case CustomTitleBarVisibility.AUTO:154default:155configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.NEVER);156break;157}158}159}160registerAction2(ToggleCustomTitleBar);161162registerAction2(class ShowCustomTitleBar extends Action2 {163constructor() {164super({165id: `showCustomTitleBar`,166title: localize2('showCustomTitleBar', "Show Custom Title Bar"),167precondition: TitleBarVisibleContext.negate(),168f1: true169});170}171172run(accessor: ServicesAccessor, ...args: any[]): void {173const configService = accessor.get(IConfigurationService);174configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.AUTO);175}176});177178registerAction2(class HideCustomTitleBar extends Action2 {179constructor() {180super({181id: `hideCustomTitleBar`,182title: localize2('hideCustomTitleBar', "Hide Custom Title Bar"),183precondition: TitleBarVisibleContext,184f1: true185});186}187188run(accessor: ServicesAccessor, ...args: any[]): void {189const configService = accessor.get(IConfigurationService);190configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.NEVER);191}192});193194registerAction2(class HideCustomTitleBar extends Action2 {195constructor() {196super({197id: `hideCustomTitleBarInFullScreen`,198title: localize2('hideCustomTitleBarInFullScreen', "Hide Custom Title Bar In Full Screen"),199precondition: ContextKeyExpr.and(TitleBarVisibleContext, IsMainWindowFullscreenContext),200f1: true201});202}203204run(accessor: ServicesAccessor, ...args: any[]): void {205const configService = accessor.get(IConfigurationService);206configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.WINDOWED);207}208});209210registerAction2(class ToggleEditorActions extends Action2 {211static readonly settingsID = `workbench.editor.editorActionsLocation`;212constructor() {213214const titleBarContextCondition = ContextKeyExpr.and(215ContextKeyExpr.equals(`config.workbench.editor.showTabs`, 'none').negate(),216ContextKeyExpr.equals(`config.${ToggleEditorActions.settingsID}`, 'default'),217)?.negate();218219super({220id: `toggle.${ToggleEditorActions.settingsID}`,221title: localize('toggle.editorActions', 'Editor Actions'),222toggled: ContextKeyExpr.equals(`config.${ToggleEditorActions.settingsID}`, 'hidden').negate(),223menu: [224{ id: MenuId.TitleBarContext, order: 3, when: titleBarContextCondition, group: '2_config' },225{ id: MenuId.TitleBarTitleContext, order: 3, when: titleBarContextCondition, group: '2_config' }226]227});228}229230run(accessor: ServicesAccessor, ...args: any[]): void {231const configService = accessor.get(IConfigurationService);232const storageService = accessor.get(IStorageService);233234const location = configService.getValue<string>(ToggleEditorActions.settingsID);235if (location === 'hidden') {236const showTabs = configService.getValue<string>(LayoutSettings.EDITOR_TABS_MODE);237238// If tabs are visible, then set the editor actions to be in the title bar239if (showTabs !== 'none') {240configService.updateValue(ToggleEditorActions.settingsID, 'titleBar');241}242243// If tabs are not visible, then set the editor actions to the last location the were before being hidden244else {245const storedValue = storageService.get(ToggleEditorActions.settingsID, StorageScope.PROFILE);246configService.updateValue(ToggleEditorActions.settingsID, storedValue ?? 'default');247}248249storageService.remove(ToggleEditorActions.settingsID, StorageScope.PROFILE);250}251// Store the current value (titleBar or default) in the storage service for later to restore252else {253configService.updateValue(ToggleEditorActions.settingsID, 'hidden');254storageService.store(ToggleEditorActions.settingsID, location, StorageScope.PROFILE, StorageTarget.USER);255}256}257});258259// --- Toolbar actions --- //260261export const ACCOUNTS_ACTIVITY_TILE_ACTION: IAction = {262id: ACCOUNTS_ACTIVITY_ID,263label: localize('accounts', "Accounts"),264tooltip: localize('accounts', "Accounts"),265class: undefined,266enabled: true,267run: function (): void { }268};269270export const GLOBAL_ACTIVITY_TITLE_ACTION: IAction = {271id: GLOBAL_ACTIVITY_ID,272label: localize('manage', "Manage"),273tooltip: localize('manage', "Manage"),274class: undefined,275enabled: true,276run: function (): void { }277};278279280