Skip to content

Commit 15e219b

Browse files
authored
fix: customized MenuBar data will be lost when modifying the MenuBar's mode (#650)
* fix: custom menu data is lost when modifying the MenuBar's mode * fix: get the "Appearance" menu id from constants
1 parent 859fd73 commit 15e219b

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

src/controller/__tests__/menuBar.test.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ describe('The menuBar controller', () => {
169169
test('Should support to change the layout mode', () => {
170170
const mockEvent = {} as any;
171171
const mockItem = { id: constants.MENUBAR_MODE_HORIZONTAL };
172-
const mockExecute = jest.fn();
173-
const originalSetMenus = menuBarService.setMenus;
174-
const originalUpdateMenuBarMode = menuBarController.updateMenuBarMode;
175172

176173
// change default mode
177174
const defaultMode = layoutService.getMenuBarMode();
@@ -184,21 +181,20 @@ describe('The menuBar controller', () => {
184181
expect(layoutService.getMenuBarMode()).toBe(anotherMode);
185182

186183
// update to horizontal mode
187-
menuBarService.setMenus = mockExecute;
188184
layoutService.setMenuBarMode(MenuBarMode.vertical);
189185
menuBarController.onClick(mockEvent, mockItem);
190-
expect(mockExecute).toBeCalled();
191-
mockExecute.mockClear();
186+
expect(
187+
menuBarService.getMenuById(constants.MENUBAR_MODE_VERTICAL)
188+
).toBeTruthy();
192189

193190
// update to vertical mode
194191
mockItem.id = constants.MENUBAR_MODE_VERTICAL;
195192
layoutService.setMenuBarMode(MenuBarMode.horizontal);
196193
menuBarController.onClick(mockEvent, mockItem);
197-
expect(mockExecute).toBeCalled();
198-
mockExecute.mockClear();
194+
expect(
195+
menuBarService.getMenuById(constants.MENUBAR_MODE_HORIZONTAL)
196+
).toBeTruthy();
199197

200-
menuBarService.setMenus = originalSetMenus;
201-
menuBarController.updateMenuBarMode = originalUpdateMenuBarMode;
202198
layoutService.reset();
203199
menuBarService.reset();
204200
});

src/controller/menuBar.ts

+36-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class MenuBarController
110110
}
111111
});
112112

113-
this.subscribe(MenuBarEvent.onChangeMode, this.updateMenuBarData);
113+
this.subscribe(MenuBarEvent.onChangeMode, this.updateMenuBarDataByMode);
114114
}
115115

116116
public updateFocusinEle = (ele: HTMLElement | null) => {
@@ -215,10 +215,42 @@ export class MenuBarController
215215
this.layoutService.setMenuBarMode(mode);
216216
};
217217

218-
public updateMenuBarData = (mode: keyof typeof MenuBarMode) => {
218+
private updateMenuBarDataByMode = (mode: keyof typeof MenuBarMode) => {
219219
const { builtInMenuBarData } = this.builtinService.getModules();
220-
const menuBarData = this.getMenuBarDataByMode(mode, builtInMenuBarData);
221-
this.menuBarService.setMenus(menuBarData);
220+
const {
221+
MENUBAR_MODE_HORIZONTAL,
222+
MENUBAR_MODE_VERTICAL,
223+
MENU_APPEARANCE_ID,
224+
} = this.builtinService.getConstants();
225+
let removeKey = MENUBAR_MODE_HORIZONTAL;
226+
let appendKey = MENUBAR_MODE_VERTICAL;
227+
228+
if (mode === MenuBarMode.vertical) {
229+
removeKey = MENUBAR_MODE_VERTICAL;
230+
appendKey = MENUBAR_MODE_HORIZONTAL;
231+
}
232+
233+
const menuItem = this.getMenuBarItem(builtInMenuBarData, appendKey!);
234+
this.menuBarService.remove(removeKey!);
235+
this.menuBarService.append(menuItem!, MENU_APPEARANCE_ID!);
236+
};
237+
238+
private getMenuBarItem = (
239+
data: IMenuBarItem[],
240+
id: string
241+
): IMenuBarItem | null => {
242+
let item: IMenuBarItem;
243+
for (item of data) {
244+
if (item.id === id) {
245+
return { ...item };
246+
} else if (Array.isArray(item.data) && item.data.length > 0) {
247+
const itemData = this.getMenuBarItem(item.data, id);
248+
if (itemData) {
249+
return itemData;
250+
}
251+
}
252+
}
253+
return null;
222254
};
223255

224256
public updateStatusBar = () => {

src/model/workbench/menuBar.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface IMenuBarItem {
2020
icon?: string | JSX.Element;
2121
data?: ISubMenuProps[];
2222
render?: (data: IMenuItemProps) => React.ReactNode | JSX.Element;
23+
disabled?: boolean;
2324
}
2425

2526
export interface IMenuBar {

src/services/builtinService/const.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const constants = {
6464
PANEL_TOOLBOX_RESIZE: 'panel.toolbox.maximize',
6565
PANEL_TOOLBOX_RESTORE_SIZE: 'panel.toolbox.restoreSize',
6666
PANEL_OUTPUT: 'panel.output.title',
67+
MENU_APPEARANCE_ID: 'Appearance',
6768
MENU_FILE_OPEN: 'openFile',
6869
MENU_QUICK_COMMAND: 'editor.action.quickCommand',
6970
MENU_VIEW_MENUBAR: 'workbench.action.showMenuBar',
@@ -584,7 +585,7 @@ export const modules = {
584585
name: localize('menu.openView', 'Open View'),
585586
},
586587
{
587-
id: 'Appearance',
588+
id: constants.MENU_APPEARANCE_ID,
588589
name: localize('menu.appearance', 'Appearance'),
589590
data: [
590591
{

0 commit comments

Comments
 (0)