-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move menu creation to a dedicated file
also add tests prior to add new entries for zoom in / zoom out Signed-off-by: Florent Benoit <[email protected]>
- Loading branch information
Showing
5 changed files
with
194 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { Menu } from 'electron'; | ||
import { aboutMenuItem } from 'electron-util/main'; | ||
import { beforeEach, expect, test, vi } from 'vitest'; | ||
|
||
import { ApplicationMenuBuilder } from './application-menu-builder.js'; | ||
|
||
vi.mock('electron', async () => { | ||
class MyCustomWindow { | ||
static readonly singleton = new MyCustomWindow(); | ||
|
||
loadURL(): void {} | ||
setBounds(): void {} | ||
|
||
on(): void {} | ||
|
||
show(): void {} | ||
focus(): void {} | ||
isMinimized(): boolean { | ||
return false; | ||
} | ||
isDestroyed(): boolean { | ||
return false; | ||
} | ||
|
||
static getAllWindows(): unknown[] { | ||
return [MyCustomWindow.singleton]; | ||
} | ||
} | ||
|
||
return { | ||
BrowserWindow: MyCustomWindow, | ||
Menu: { | ||
buildFromTemplate: vi.fn(), | ||
getApplicationMenu: vi.fn(), | ||
setApplicationMenu: vi.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
vi.mock('electron-util/main', async () => { | ||
return { | ||
aboutMenuItem: vi.fn(), | ||
}; | ||
}); | ||
|
||
let applicationMenuBuilder: ApplicationMenuBuilder; | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
applicationMenuBuilder = new ApplicationMenuBuilder(); | ||
}); | ||
|
||
test('check about menu is added', () => { | ||
vi.mocked(Menu.buildFromTemplate).mockImplementation(a => { | ||
return a as unknown as Menu; | ||
}); | ||
|
||
// set a menu | ||
vi.mocked(Menu.getApplicationMenu).mockReturnValue({ | ||
items: [ | ||
{ | ||
role: 'help', | ||
submenu: { | ||
items: [], | ||
}, | ||
}, | ||
], | ||
} as unknown as Menu); | ||
|
||
// mock aboutMenuItem | ||
vi.mocked(aboutMenuItem).mockReturnValue({ | ||
label: 'About', | ||
}); | ||
|
||
const menu = applicationMenuBuilder.build(); | ||
expect(menu).toBeDefined(); | ||
|
||
const items = menu?.items[0]?.submenu as unknown as any[]; | ||
expect(items).toBeDefined(); | ||
|
||
// expect to have the about menu | ||
expect(items[1]?.label).toBe('About'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import { Menu } from 'electron'; | ||
import { aboutMenuItem } from 'electron-util/main'; | ||
|
||
export class ApplicationMenuBuilder { | ||
build(): Electron.Menu | undefined { | ||
const menu = Menu.getApplicationMenu(); // get default menu | ||
if (!menu) { | ||
return undefined; | ||
} | ||
// Add help/about menu entry | ||
menu.items.forEach(i => { | ||
// add the About entry only in the help menu | ||
if (i.role === 'help' && i.submenu) { | ||
const aboutMenuSubItem = aboutMenuItem({}); | ||
aboutMenuSubItem.label = 'About'; | ||
|
||
// create new submenu | ||
// also add a separator before the About entry | ||
const newSubMenu = Menu.buildFromTemplate([...i.submenu.items, { type: 'separator' }, aboutMenuSubItem]); | ||
i.submenu = newSubMenu; | ||
} | ||
}); | ||
return menu; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters