Skip to content

Commit 6ee19ca

Browse files
authored
MenuBar improvements (#131)
* Code style * Fix variable name * Add tooltip support * Add resizable option * Add icon change support * Preload window for UX * Add standard window preload script This makes the menubar window work just like other windows * Support custom event on click
1 parent 1aadd5f commit 6ee19ca

File tree

1 file changed

+132
-97
lines changed
  • resources/js/electron-plugin/src/server/api

1 file changed

+132
-97
lines changed

resources/js/electron-plugin/src/server/api/menuBar.ts

Lines changed: 132 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -9,138 +9,173 @@ import { join } from "path";
99
const router = express.Router();
1010

1111
router.post("/label", (req, res) => {
12-
res.sendStatus(200);
12+
res.sendStatus(200);
1313

14-
const { label } = req.body;
14+
const { label } = req.body;
1515

16-
state.activeMenuBar.tray.setTitle(label);
16+
state.activeMenuBar.tray.setTitle(label);
17+
});
18+
19+
router.post("/tooltip", (req, res) => {
20+
res.sendStatus(200);
21+
22+
const { tooltip } = req.body;
23+
24+
state.activeMenuBar.tray.setToolTip(tooltip);
25+
});
26+
27+
router.post("/icon", (req, res) => {
28+
res.sendStatus(200);
29+
30+
const { icon } = req.body;
31+
32+
state.activeMenuBar.tray.setImage(icon);
1733
});
1834

1935
router.post("/context-menu", (req, res) => {
20-
res.sendStatus(200);
21-
const { contextMenu } = req.body;
22-
23-
state.activeMenuBar.tray.setContextMenu(buildMenu(contextMenu));
36+
res.sendStatus(200);
37+
38+
const { contextMenu } = req.body;
39+
40+
state.activeMenuBar.tray.setContextMenu(buildMenu(contextMenu));
2441
});
2542

2643
router.post("/show", (req, res) => {
27-
res.sendStatus(200);
44+
res.sendStatus(200);
2845

29-
state.activeMenuBar.showWindow();
46+
state.activeMenuBar.showWindow();
3047
});
3148

3249
router.post("/hide", (req, res) => {
33-
res.sendStatus(200);
50+
res.sendStatus(200);
3451

35-
state.activeMenuBar.hideWindow();
52+
state.activeMenuBar.hideWindow();
3653
});
3754

3855
router.post("/create", (req, res) => {
39-
res.sendStatus(200);
40-
41-
const {
42-
width,
43-
height,
44-
url,
45-
label,
46-
alwaysOnTop,
47-
vibrancy,
48-
backgroundColor,
49-
transparency,
50-
icon,
51-
showDockIcon,
52-
onlyShowContextWindow,
53-
windowPosition,
54-
contextMenu
55-
} = req.body;
56-
57-
if (onlyShowContextWindow === true) {
58-
const tray = new Tray(icon || state.icon.replace("icon.png", "IconTemplate.png"));
59-
tray.setContextMenu(buildMenu(contextMenu));
60-
61-
state.activeMenuBar = menubar({
62-
tray,
63-
index: false,
64-
showDockIcon,
65-
showOnAllWorkspaces: false,
66-
browserWindow: {
67-
show: false,
68-
width: 0,
69-
height: 0,
70-
}
71-
});
56+
res.sendStatus(200);
7257

73-
} else {
74-
state.activeMenuBar = menubar({
75-
icon: icon || state.icon.replace("icon.png", "IconTemplate.png"),
76-
index: url,
77-
showDockIcon,
78-
showOnAllWorkspaces: false,
79-
windowPosition: windowPosition ?? "trayCenter",
80-
browserWindow: {
58+
const {
8159
width,
8260
height,
61+
url,
62+
label,
8363
alwaysOnTop,
8464
vibrancy,
8565
backgroundColor,
86-
transparent: transparency,
87-
webPreferences: {
88-
nodeIntegration: true,
89-
sandbox: false,
90-
contextIsolation: false
66+
transparency,
67+
icon,
68+
showDockIcon,
69+
onlyShowContextMenu,
70+
windowPosition,
71+
contextMenu,
72+
tooltip,
73+
resizable,
74+
event,
75+
} = req.body;
76+
77+
if (onlyShowContextMenu) {
78+
const tray = new Tray(icon || state.icon.replace("icon.png", "IconTemplate.png"));
79+
80+
tray.setContextMenu(buildMenu(contextMenu));
81+
82+
if (event) {
83+
tray.on('click', (e) => {
84+
notifyLaravel('events', {
85+
event,
86+
payload: e,
87+
});
88+
});
9189
}
92-
}
93-
});
94-
state.activeMenuBar.on("after-create-window", () => {
95-
require("@electron/remote/main").enable(state.activeMenuBar.window.webContents);
96-
});
97-
}
9890

99-
state.activeMenuBar.on("ready", () => {
100-
state.activeMenuBar.tray.setTitle(label);
91+
state.activeMenuBar = menubar({
92+
tray,
93+
tooltip,
94+
index: false,
95+
showDockIcon,
96+
showOnAllWorkspaces: false,
97+
browserWindow: {
98+
show: false,
99+
width: 0,
100+
height: 0,
101+
}
102+
});
103+
} else {
104+
state.activeMenuBar = menubar({
105+
icon: icon || state.icon.replace("icon.png", "IconTemplate.png"),
106+
preloadWindow: true,
107+
tooltip,
108+
index: url,
109+
showDockIcon,
110+
showOnAllWorkspaces: false,
111+
windowPosition: windowPosition ?? "trayCenter",
112+
browserWindow: {
113+
width,
114+
height,
115+
resizable,
116+
alwaysOnTop,
117+
vibrancy,
118+
backgroundColor,
119+
transparent: transparency,
120+
webPreferences: {
121+
preload: join(__dirname, '../../electron-plugin/dist/preload/index.js'),
122+
nodeIntegration: true,
123+
sandbox: false,
124+
contextIsolation: false,
125+
}
126+
}
127+
});
101128

102-
state.activeMenuBar.on("hide", () => {
103-
notifyLaravel("events", {
104-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
105-
});
106-
});
129+
state.activeMenuBar.on("after-create-window", () => {
130+
require("@electron/remote/main").enable(state.activeMenuBar.window.webContents);
131+
});
132+
}
107133

108-
state.activeMenuBar.on("show", () => {
109-
notifyLaravel("events", {
110-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
111-
});
112-
});
134+
state.activeMenuBar.on("ready", () => {
135+
state.activeMenuBar.tray.setTitle(label);
113136

114-
state.activeMenuBar.tray.on("drop-files", (event, files) => {
115-
notifyLaravel("events", {
116-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDroppedFiles",
117-
payload: [
118-
files
119-
]
120-
});
121-
});
137+
state.activeMenuBar.on("hide", () => {
138+
notifyLaravel("events", {
139+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
140+
});
141+
});
122142

123-
if (onlyShowContextWindow !== true) {
124-
state.activeMenuBar.tray.on("right-click", () => {
125-
notifyLaravel("events", {
126-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarContextMenuOpened"
143+
state.activeMenuBar.on("show", () => {
144+
notifyLaravel("events", {
145+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
146+
});
127147
});
128148

129-
state.activeMenuBar.tray.popUpContextMenu(buildMenu(contextMenu));
130-
});
131-
}
132-
});
149+
state.activeMenuBar.tray.on("drop-files", (event, files) => {
150+
notifyLaravel("events", {
151+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDroppedFiles",
152+
payload: [
153+
files
154+
]
155+
});
156+
});
157+
158+
if (! onlyShowContextMenu) {
159+
state.activeMenuBar.tray.on("right-click", () => {
160+
notifyLaravel("events", {
161+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarContextMenuOpened"
162+
});
163+
164+
state.activeMenuBar.tray.popUpContextMenu(buildMenu(contextMenu));
165+
});
166+
}
167+
});
133168
});
134169

135170
function buildMenu(contextMenu) {
136-
let menu = Menu.buildFromTemplate([{ role: "quit" }]);
171+
let menu = Menu.buildFromTemplate([{ role: "quit" }]);
137172

138-
if (contextMenu) {
139-
const menuEntries = contextMenu.map(mapMenu);
140-
menu = Menu.buildFromTemplate(menuEntries);
141-
}
173+
if (contextMenu) {
174+
const menuEntries = contextMenu.map(mapMenu);
175+
menu = Menu.buildFromTemplate(menuEntries);
176+
}
142177

143-
return menu;
178+
return menu;
144179
}
145180

146181
export default router;

0 commit comments

Comments
 (0)