Skip to content

Commit 72b7dcc

Browse files
authored
feat: support to toggle side bar visibility by command (#203)
* feat: support to toggle side bar visibility by command * fix: improve quick show sidebar command * fix: add quick toggle sidebar action into command palette
1 parent 442b1a9 commit 72b7dcc

File tree

10 files changed

+144
-28
lines changed

10 files changed

+144
-28
lines changed

src/controller/menuBar.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
MENU_VIEW_ACTIVITYBAR,
88
MENU_VIEW_MENUBAR,
99
MENU_VIEW_STATUSBAR,
10-
MENU_VIEW_SIDEBAR,
1110
} from 'mo/model/workbench/menuBar';
1211
import { Controller } from 'mo/react/controller';
1312
import {
@@ -18,6 +17,9 @@ import {
1817
MenuBarService,
1918
LayoutService,
2019
} from 'mo/services';
20+
import { ID_SIDE_BAR } from 'mo/common/id';
21+
import { IMonacoService, MonacoService } from 'mo/monaco/monacoService';
22+
import { CommandQuickSideBarViewAction } from 'mo/monaco/quickToggleSideBarAction';
2123

2224
export interface IMenuBarController {
2325
onSelect?: (key: string, item?: IActivityBarItem) => void;
@@ -35,12 +37,14 @@ export class MenuBarController
3537
private readonly editorService: IEditorService;
3638
private readonly menuBarService: IMenuBarService;
3739
private readonly layoutService: ILayoutService;
40+
private readonly monacoService: IMonacoService;
3841

3942
constructor() {
4043
super();
4144
this.editorService = container.resolve(EditorService);
4245
this.menuBarService = container.resolve(MenuBarService);
4346
this.layoutService = container.resolve(LayoutService);
47+
this.monacoService = container.resolve(MonacoService);
4448
}
4549

4650
public readonly onClick = (event: React.MouseEvent, item: IMenuBarItem) => {
@@ -61,7 +65,7 @@ export class MenuBarController
6165
case MENU_VIEW_STATUSBAR:
6266
this.updateStatusBar();
6367
break;
64-
case MENU_VIEW_SIDEBAR:
68+
case ID_SIDE_BAR:
6569
this.updateSideBar();
6670
break;
6771
}
@@ -106,12 +110,8 @@ export class MenuBarController
106110
};
107111

108112
public updateSideBar = () => {
109-
this.layoutService.setSideBarHidden();
110-
const {
111-
sideBar: { hidden },
112-
} = this.layoutService.getState();
113-
this.menuBarService.update(MENU_VIEW_SIDEBAR, {
114-
icon: hidden ? '' : 'check',
115-
});
113+
this.monacoService.commandService.executeCommand(
114+
CommandQuickSideBarViewAction.ID
115+
);
116116
};
117117
}

src/extensions/activityBar/index.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IExtension } from 'mo/model/extension';
22
import { IExtensionService } from 'mo/services';
33
import molecule from 'mo';
44
import { builtInActivityBar } from 'mo/model';
5+
import { CommandQuickSideBarViewAction } from 'mo/monaco/quickToggleSideBarAction';
56

67
export const ExtendsActivityBar: IExtension = {
78
activate(extensionCtx: IExtensionService) {
@@ -10,16 +11,22 @@ export const ExtendsActivityBar: IExtension = {
1011
molecule.activityBar.addContextMenu(contextMenu);
1112

1213
molecule.activityBar.onChange((pre, cur) => {
13-
if (pre === cur) {
14-
molecule.activityBar.setActive(undefined);
15-
molecule.layout.setSideBarHidden();
16-
} else {
14+
if (cur !== pre) {
1715
molecule.activityBar.setActive(cur);
1816
molecule.sidebar.setActive(cur);
17+
1918
const { sideBar } = molecule.layout.getState();
2019
if (sideBar.hidden) {
21-
molecule.layout.setSideBarHidden();
20+
extensionCtx.executeCommand(
21+
CommandQuickSideBarViewAction.ID,
22+
cur
23+
);
2224
}
25+
} else {
26+
extensionCtx.executeCommand(
27+
CommandQuickSideBarViewAction.ID,
28+
cur
29+
);
2330
}
2431
});
2532
},

src/extensions/keybinding/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { SelectLocaleAction } from 'mo/i18n/selectLocaleAction';
44
import { QuickAccessSettings } from 'mo/monaco/quickAccessSettingsAction';
55
import { CommandQuickAccessViewAction } from 'mo/monaco/quickAccessViewAction';
66
import { SelectColorThemeAction } from 'mo/monaco/selectColorThemeAction';
7+
import { CommandQuickSideBarViewAction } from 'mo/monaco/quickToggleSideBarAction';
78

89
export const ExtendsKeybinding: IExtension = {
910
activate(extensionCtx: IExtensionService) {
1011
extensionCtx.registerAction(CommandQuickAccessViewAction);
1112
extensionCtx.registerAction(SelectColorThemeAction);
1213
extensionCtx.registerAction(QuickAccessSettings);
1314
extensionCtx.registerAction(SelectLocaleAction);
15+
extensionCtx.registerAction(CommandQuickSideBarViewAction);
1416
},
1517
};

src/i18n/source/en.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default {
2020
'menu.appearance': 'Appearance',
2121
'menu.showMenuBar': 'Show Menu Bar',
2222
'menu.showSideBar': 'Show Side bar',
23+
'menu.showSideBar.label': 'Toggle Side Bar Visibility',
2324
'menu.showStatusBar': 'Show Status Bar',
2425
'menu.showActivityBar': 'Show Activity Bar',
2526
'menu.run': 'Run',

src/i18n/source/zh-CN.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"menu.appearance": "外观",
2323
"menu.showMenuBar": "显示菜单栏",
2424
"menu.showSideBar": "显示边栏",
25+
"menu.showSideBar.label": "切换侧边栏",
2526
"menu.showStatusBar": "显示状态栏",
2627
"menu.showActivityBar": "显示活动栏",
2728
"menu.hideActivityBar": "隐藏活动栏",

src/model/workbench/menuBar.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import { localize } from 'mo/i18n/localize';
3+
import { ID_SIDE_BAR } from 'mo/common/id';
34

45
/**
56
* The activity bar event definition
@@ -26,7 +27,6 @@ export interface IMenuBar {
2627
export const MENU_FILE_UNDO = 'undo';
2728
export const MENU_FILE_REDO = 'redo';
2829
export const MENU_VIEW_MENUBAR = 'workbench.action.showMenuBar';
29-
export const MENU_VIEW_SIDEBAR = 'workbench.action.showSideBar';
3030
export const MENU_VIEW_ACTIVITYBAR = 'workbench.action.showActivityBar';
3131
export const MENU_VIEW_STATUSBAR = 'workbench.action.showStatusBar';
3232

@@ -97,7 +97,7 @@ export function builtInMenuBarData() {
9797
},
9898
{
9999
icon: 'check',
100-
id: MENU_VIEW_SIDEBAR,
100+
id: ID_SIDE_BAR,
101101
name: localize('menu.showSideBar', 'Show Side Bar'),
102102
},
103103
{

src/monaco/common.ts

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/
66
import { KeybindingsRegistry } from 'monaco-editor/esm/vs/platform/keybinding/common/keybindingsRegistry';
77
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
88
import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/commands';
9+
import { localize } from 'monaco-editor/esm/vs/nls';
910
import {
1011
MenuRegistry,
1112
MenuId,
@@ -19,6 +20,27 @@ export enum KeybindingWeight {
1920
ExternalExtension = 400,
2021
}
2122

23+
export const CATEGORIES = {
24+
View: { value: localize('view', 'View'), original: 'View' },
25+
Help: { value: localize('help', 'Help'), original: 'Help' },
26+
Preferences: {
27+
value: localize('preferences', 'Preferences'),
28+
original: 'Preferences',
29+
},
30+
Developer: {
31+
value: localize(
32+
{
33+
key: 'developer',
34+
comment: [
35+
'A developer on Code itself or someone diagnosing issues in Code',
36+
],
37+
},
38+
'Developer'
39+
),
40+
original: 'Developer',
41+
},
42+
};
43+
2244
export abstract class Action2 {
2345
constructor(
2446
readonly desc: Readonly<{
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
2+
import { KeyChord } from 'monaco-editor/esm/vs/base/common/keyCodes';
3+
import { localize } from 'mo/i18n/localize';
4+
import { KeyMod, KeyCode } from 'mo/monaco';
5+
import { Action2, CATEGORIES, KeybindingWeight } from './common';
6+
import { container } from 'tsyringe';
7+
import {
8+
ActivityBarService,
9+
IActivityBarService,
10+
ILayoutService,
11+
IMenuBarService,
12+
ISidebarService,
13+
LayoutService,
14+
MenuBarService,
15+
SidebarService,
16+
} from 'mo/services';
17+
import { ID_SIDE_BAR } from 'mo/common/id';
18+
19+
export class CommandQuickSideBarViewAction extends Action2 {
20+
static readonly ID = ID_SIDE_BAR;
21+
static readonly LABEL = localize(
22+
'menu.showSideBar.label',
23+
'Toggle Side Bar Visibility'
24+
);
25+
private readonly layoutService: ILayoutService;
26+
private readonly activityBarService: IActivityBarService;
27+
private readonly menuBarService: IMenuBarService;
28+
private readonly sideBarService: ISidebarService;
29+
private _preActivityBar: string | undefined;
30+
31+
constructor() {
32+
super({
33+
id: CommandQuickSideBarViewAction.ID,
34+
label: CommandQuickSideBarViewAction.LABEL,
35+
title: CommandQuickSideBarViewAction.LABEL,
36+
category: CATEGORIES.View,
37+
alias: 'Toggle Side Bar',
38+
precondition: undefined,
39+
f1: true,
40+
keybinding: {
41+
when: undefined,
42+
weight: KeybindingWeight.WorkbenchContrib,
43+
// eslint-disable-next-line new-cap
44+
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_B),
45+
},
46+
});
47+
this.layoutService = container.resolve(LayoutService);
48+
this.activityBarService = container.resolve(ActivityBarService);
49+
this.menuBarService = container.resolve(MenuBarService);
50+
this.sideBarService = container.resolve(SidebarService);
51+
}
52+
run(accessor: ServicesAccessor, ...args) {
53+
const sidebarId = args[0];
54+
const { sideBar } = this.layoutService.getState();
55+
const { selected } = this.activityBarService.getState();
56+
if (sideBar.hidden) {
57+
this.activityBarService.setActive(
58+
sidebarId || this._preActivityBar
59+
);
60+
this.sideBarService.setActive(sidebarId || this._preActivityBar);
61+
this.menuBarService.update(CommandQuickSideBarViewAction.ID, {
62+
icon: 'check',
63+
});
64+
this.layoutService.setSideBarHidden();
65+
} else {
66+
this.activityBarService.setActive();
67+
this.sideBarService.setActive();
68+
this.menuBarService.update(CommandQuickSideBarViewAction.ID, {
69+
icon: '',
70+
});
71+
this.layoutService.setSideBarHidden();
72+
this._preActivityBar = selected;
73+
}
74+
}
75+
}

src/services/extensionService.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
IColorThemeService,
1010
} from './theme/colorThemeService';
1111
import { Action2, registerAction2 } from 'mo/monaco/common';
12+
import { IMonacoService, MonacoService } from 'mo/monaco/monacoService';
1213

1314
export interface IExtensionService {
1415
/**
@@ -32,16 +33,19 @@ export interface IExtensionService {
3233
* ```
3334
*/
3435
registerAction(actionClass: { new (): Action2 }): void;
36+
executeCommand(id: string, ...args: any): void;
3537
}
3638

3739
@singleton()
3840
export class ExtensionService implements IExtensionService {
3941
public extensions: IExtension[] = [];
4042
private readonly colorThemeService: IColorThemeService;
43+
private readonly monacoService: IMonacoService;
4144

4245
constructor(@inject('Extensions') extensions: IExtension[] = []) {
4346
this.load(extensions);
4447
this.colorThemeService = container.resolve(ColorThemeService);
48+
this.monacoService = container.resolve(MonacoService);
4549
}
4650

4751
public load(extensions: IExtension[] = []) {
@@ -85,6 +89,10 @@ export class ExtensionService implements IExtensionService {
8589
registerAction2(actionClass);
8690
}
8791

92+
public executeCommand(id, ...args) {
93+
this.monacoService.commandService.executeCommand(id, ...args);
94+
}
95+
8896
unload(extension: IExtension) {
8997
console.log('unload extension:', extension.name);
9098
}

stories/extensions/test/index.tsx

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import molecule from 'mo';
33
import {
44
MENU_VIEW_ACTIVITYBAR,
55
MENU_VIEW_MENUBAR,
6-
MENU_VIEW_SIDEBAR,
76
MENU_VIEW_STATUSBAR,
87
} from 'mo/model/workbench/menuBar';
98
import { IExtension } from 'mo/model';
109

1110
import TestPane from './testPane';
1211
import { Entry } from './entry';
12+
import { Position } from 'mo/model/workbench/layout';
1313

1414
export const ExtendTestPane: IExtension = {
1515
activate() {
@@ -34,7 +34,6 @@ export const ExtendTestPane: IExtension = {
3434
molecule.editor.setEntry(<Entry />);
3535

3636
molecule.settings.onChangeConfiguration(async (value) => {
37-
console.log('onChangeConfiguration:', value);
3837
molecule.settings.update(value);
3938
const config = await molecule.settings.getConfiguration();
4039
const workbench: any = config.workbench;
@@ -60,16 +59,17 @@ export const ExtendTestPane: IExtension = {
6059
icon: hidden ? '' : 'check',
6160
});
6261
}
63-
if (workbench?.sidebar) {
64-
const hidden = workbench?.sidebar.hidden;
65-
molecule.layout.setState({
66-
...layoutViewState,
67-
sideBar: { ...layoutViewState.sideBar, hidden },
68-
});
69-
70-
molecule.menuBar.update(MENU_VIEW_SIDEBAR, {
71-
icon: hidden ? '' : 'check',
72-
});
62+
if (workbench.sidebar) {
63+
switch (workbench.sidebar) {
64+
case 'left':
65+
molecule.layout.setSideBarPosition(Position.LEFT);
66+
break;
67+
case 'right':
68+
molecule.layout.setSideBarPosition(Position.RIGHT);
69+
break;
70+
default:
71+
break;
72+
}
7373
}
7474
if (workbench?.statusBar) {
7575
const hidden = workbench?.statusBar.hidden;

0 commit comments

Comments
 (0)