forked from iamkarlson/notion-snap
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmenuBarHandling.js
45 lines (38 loc) · 1.24 KB
/
menuBarHandling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const { globalShortcut, Menu, MenuItem } = require("electron");
let isMenuOnAltBackslash = false;
function toggleMenuBar(win) {
const isMenuBarVisible = win.autoHideMenuBar;
win.setAutoHideMenuBar(!isMenuBarVisible);
win.setMenuBarVisibility(isMenuBarVisible);
}
function registerMenuHandling(win) {
// Menu bar toggling shortcut register
const menuShortcut = globalShortcut.register("Alt+\\", () => {
toggleMenuBar(win);
});
if (!menuShortcut) {
console.error("Failed to register global shortcut!");
}
// Preventing menu bar toggling on Alt
win.webContents.on("before-input-event", (event, input) => {
if (isMenuOnAltBackslash && input.alt) {
event.preventDefault();
}
});
// Adding menu item to the "Window" menu
const menu = Menu.getApplicationMenu();
const windowMenu = menu.items?.find((el) => el.role === "windowmenu");
if (windowMenu) {
windowMenu.submenu.insert(0, new MenuItem({
label: "Open menu bar on ALT+\\",
type: 'checkbox',
checked: isMenuOnAltBackslash,
click: () => {
isMenuOnAltBackslash = !isMenuOnAltBackslash;
win.setAutoHideMenuBar(true);
win.setMenuBarVisibility(false);
}
}));
}
}
module.exports = { registerMenuHandling };