Skip to content

Commit ecca7dd

Browse files
authored
fix: menubar tray state (#156)
1 parent f5d9dc6 commit ecca7dd

File tree

4 files changed

+143
-110
lines changed

4 files changed

+143
-110
lines changed

resources/js/electron-plugin/dist/server/api/menuBar.js

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ const router = express.Router();
1010
router.post("/label", (req, res) => {
1111
res.sendStatus(200);
1212
const { label } = req.body;
13-
state.activeMenuBar.tray.setTitle(label);
13+
state.tray.setTitle(label);
1414
});
1515
router.post("/tooltip", (req, res) => {
1616
res.sendStatus(200);
1717
const { tooltip } = req.body;
18-
state.activeMenuBar.tray.setToolTip(tooltip);
18+
state.tray.setToolTip(tooltip);
1919
});
2020
router.post("/icon", (req, res) => {
2121
res.sendStatus(200);
2222
const { icon } = req.body;
23-
state.activeMenuBar.tray.setImage(icon);
23+
state.tray.setImage(icon);
2424
});
2525
router.post("/context-menu", (req, res) => {
2626
res.sendStatus(200);
2727
const { contextMenu } = req.body;
28-
state.activeMenuBar.tray.setContextMenu(buildMenu(contextMenu));
28+
state.tray.setContextMenu(buildMenu(contextMenu));
2929
});
3030
router.post("/show", (req, res) => {
3131
res.sendStatus(200);
@@ -47,6 +47,9 @@ router.post("/create", (req, res) => {
4747
const tray = new Tray(icon || state.icon.replace("icon.png", "IconTemplate.png"));
4848
tray.setContextMenu(buildMenu(contextMenu));
4949
tray.setToolTip(tooltip);
50+
tray.setTitle(label);
51+
eventsForTray(tray, onlyShowContextMenu, contextMenu, shouldSendCreatedEvent);
52+
state.tray = tray;
5053
if (!showDockIcon) {
5154
app.dock.hide();
5255
}
@@ -80,66 +83,70 @@ router.post("/create", (req, res) => {
8083
state.activeMenuBar.on("after-create-window", () => {
8184
enable(state.activeMenuBar.window.webContents);
8285
});
83-
}
84-
state.activeMenuBar.on("ready", () => {
85-
state.activeMenuBar.tray.setTitle(label);
86-
if (shouldSendCreatedEvent) {
87-
notifyLaravel("events", {
88-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarCreated"
86+
state.activeMenuBar.on("ready", () => {
87+
eventsForTray(state.activeMenuBar.tray, onlyShowContextMenu, contextMenu, shouldSendCreatedEvent);
88+
state.tray = state.activeMenuBar.tray;
89+
state.tray.setTitle(label);
90+
state.activeMenuBar.on("hide", () => {
91+
notifyLaravel("events", {
92+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
93+
});
8994
});
90-
}
91-
state.activeMenuBar.on("hide", () => {
92-
notifyLaravel("events", {
93-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
95+
state.activeMenuBar.on("show", () => {
96+
notifyLaravel("events", {
97+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
98+
});
9499
});
95100
});
96-
state.activeMenuBar.on("show", () => {
97-
notifyLaravel("events", {
98-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
99-
});
101+
}
102+
});
103+
function eventsForTray(tray, onlyShowContextMenu, contextMenu, shouldSendCreatedEvent) {
104+
if (shouldSendCreatedEvent) {
105+
notifyLaravel("events", {
106+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarCreated"
100107
});
101-
state.activeMenuBar.tray.on("drop-files", (event, files) => {
102-
notifyLaravel("events", {
103-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDroppedFiles",
104-
payload: [
105-
files
106-
]
107-
});
108+
}
109+
tray.on("drop-files", (event, files) => {
110+
notifyLaravel("events", {
111+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDroppedFiles",
112+
payload: [
113+
files
114+
]
108115
});
109-
state.activeMenuBar.tray.on('click', (combo, bounds, position) => {
110-
notifyLaravel('events', {
111-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarClicked",
112-
payload: {
113-
combo,
114-
bounds,
115-
position,
116-
},
117-
});
116+
});
117+
tray.on('click', (combo, bounds, position) => {
118+
notifyLaravel('events', {
119+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarClicked",
120+
payload: {
121+
combo,
122+
bounds,
123+
position,
124+
},
118125
});
119-
state.activeMenuBar.tray.on("right-click", (combo, bounds) => {
120-
notifyLaravel("events", {
121-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarRightClicked",
122-
payload: {
123-
combo,
124-
bounds,
125-
}
126-
});
127-
if (!onlyShowContextMenu) {
128-
state.activeMenuBar.hideWindow();
129-
state.activeMenuBar.tray.popUpContextMenu(buildMenu(contextMenu));
126+
});
127+
tray.on("right-click", (combo, bounds) => {
128+
notifyLaravel("events", {
129+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarRightClicked",
130+
payload: {
131+
combo,
132+
bounds,
130133
}
131134
});
132-
state.activeMenuBar.tray.on('double-click', (combo, bounds) => {
133-
notifyLaravel('events', {
134-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDoubleClicked",
135-
payload: {
136-
combo,
137-
bounds,
138-
},
139-
});
135+
if (!onlyShowContextMenu) {
136+
state.activeMenuBar.hideWindow();
137+
tray.popUpContextMenu(buildMenu(contextMenu));
138+
}
139+
});
140+
tray.on('double-click', (combo, bounds) => {
141+
notifyLaravel('events', {
142+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDoubleClicked",
143+
payload: {
144+
combo,
145+
bounds,
146+
},
140147
});
141148
});
142-
});
149+
}
143150
function buildMenu(contextMenu) {
144151
let menu = Menu.buildFromTemplate([{ role: "quit" }]);
145152
if (contextMenu) {

resources/js/electron-plugin/dist/server/state.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function generateRandomString(length) {
2525
export default {
2626
electronApiPort: null,
2727
activeMenuBar: null,
28+
tray: null,
2829
php: null,
2930
phpPort: null,
3031
phpIni: null,

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

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ router.post("/label", (req, res) => {
1414

1515
const { label } = req.body;
1616

17-
state.activeMenuBar.tray.setTitle(label);
17+
state.tray.setTitle(label);
1818
});
1919

2020
router.post("/tooltip", (req, res) => {
2121
res.sendStatus(200);
2222

2323
const { tooltip } = req.body;
2424

25-
state.activeMenuBar.tray.setToolTip(tooltip);
25+
state.tray.setToolTip(tooltip);
2626
});
2727

2828
router.post("/icon", (req, res) => {
2929
res.sendStatus(200);
3030

3131
const { icon } = req.body;
3232

33-
state.activeMenuBar.tray.setImage(icon);
33+
state.tray.setImage(icon);
3434
});
3535

3636
router.post("/context-menu", (req, res) => {
3737
res.sendStatus(200);
3838

3939
const { contextMenu } = req.body;
4040

41-
state.activeMenuBar.tray.setContextMenu(buildMenu(contextMenu));
41+
state.tray.setContextMenu(buildMenu(contextMenu));
4242
});
4343

4444
router.post("/show", (req, res) => {
@@ -84,14 +84,24 @@ router.post("/create", (req, res) => {
8484

8585

8686
if (onlyShowContextMenu) {
87+
// Create a tray icon
8788
const tray = new Tray(icon || state.icon.replace("icon.png", "IconTemplate.png"));
8889

90+
// Set the context menu
8991
tray.setContextMenu(buildMenu(contextMenu));
9092
tray.setToolTip(tooltip);
93+
tray.setTitle(label);
94+
95+
// Set the event listeners + send created event
96+
eventsForTray(tray, onlyShowContextMenu, contextMenu, shouldSendCreatedEvent);
97+
98+
// Set the tray to the state
99+
state.tray = tray;
91100

92101
if (!showDockIcon) {
93102
app.dock.hide();
94103
}
104+
95105
} else {
96106
state.activeMenuBar = menubar({
97107
icon: icon || state.icon.replace("icon.png", "IconTemplate.png"),
@@ -122,76 +132,89 @@ router.post("/create", (req, res) => {
122132
state.activeMenuBar.on("after-create-window", () => {
123133
enable(state.activeMenuBar.window.webContents);
124134
});
125-
}
126135

127-
state.activeMenuBar.on("ready", () => {
136+
state.activeMenuBar.on("ready", () => {
137+
// Set the event listeners
138+
eventsForTray(state.activeMenuBar.tray, onlyShowContextMenu, contextMenu, shouldSendCreatedEvent);
128139

129-
state.activeMenuBar.tray.setTitle(label);
140+
// Set the tray to the state
141+
state.tray = state.activeMenuBar.tray;
130142

131-
if (shouldSendCreatedEvent) {
132-
notifyLaravel("events", {
133-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarCreated"
134-
});
135-
}
143+
// Set the title
144+
state.tray.setTitle(label);
136145

137-
state.activeMenuBar.on("hide", () => {
138-
notifyLaravel("events", {
139-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
146+
state.activeMenuBar.on("hide", () => {
147+
notifyLaravel("events", {
148+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
149+
});
140150
});
141-
});
142151

143-
state.activeMenuBar.on("show", () => {
144-
notifyLaravel("events", {
145-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
152+
state.activeMenuBar.on("show", () => {
153+
notifyLaravel("events", {
154+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
155+
});
146156
});
157+
147158
});
159+
}
148160

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-
});
161+
});
162+
163+
164+
165+
function eventsForTray(tray, onlyShowContextMenu, contextMenu, shouldSendCreatedEvent) {
166+
167+
if (shouldSendCreatedEvent) {
168+
notifyLaravel("events", {
169+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarCreated"
156170
});
171+
}
157172

158-
state.activeMenuBar.tray.on('click', (combo, bounds, position) => {
159-
notifyLaravel('events', {
160-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarClicked",
161-
payload: {
162-
combo,
163-
bounds,
164-
position,
165-
},
166-
});
173+
tray.on("drop-files", (event, files) => {
174+
notifyLaravel("events", {
175+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDroppedFiles",
176+
payload: [
177+
files
178+
]
167179
});
180+
});
168181

169-
state.activeMenuBar.tray.on("right-click", (combo, bounds) => {
170-
notifyLaravel("events", {
171-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarRightClicked",
172-
payload: {
173-
combo,
174-
bounds,
175-
}
176-
});
182+
tray.on('click', (combo, bounds, position) => {
183+
notifyLaravel('events', {
184+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarClicked",
185+
payload: {
186+
combo,
187+
bounds,
188+
position,
189+
},
190+
});
191+
});
177192

178-
if (! onlyShowContextMenu) {
179-
state.activeMenuBar.hideWindow();
180-
state.activeMenuBar.tray.popUpContextMenu(buildMenu(contextMenu));
193+
tray.on("right-click", (combo, bounds) => {
194+
notifyLaravel("events", {
195+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarRightClicked",
196+
payload: {
197+
combo,
198+
bounds,
181199
}
182200
});
183201

184-
state.activeMenuBar.tray.on('double-click', (combo, bounds) => {
185-
notifyLaravel('events', {
186-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDoubleClicked",
187-
payload: {
188-
combo,
189-
bounds,
190-
},
191-
});
202+
if (!onlyShowContextMenu) {
203+
state.activeMenuBar.hideWindow();
204+
tray.popUpContextMenu(buildMenu(contextMenu));
205+
}
206+
});
207+
208+
tray.on('double-click', (combo, bounds) => {
209+
notifyLaravel('events', {
210+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarDoubleClicked",
211+
payload: {
212+
combo,
213+
bounds,
214+
},
192215
});
193216
});
194-
});
217+
}
195218

196219
function buildMenu(contextMenu) {
197220
let menu = Menu.buildFromTemplate([{ role: "quit" }]);

resources/js/electron-plugin/src/server/state.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BrowserWindow, UtilityProcess } from "electron";
1+
import {BrowserWindow, Tray, UtilityProcess} from "electron";
22
import Store from "electron-store";
33
import { notifyLaravel } from "./utils.js";
44

@@ -23,6 +23,7 @@ settingsStore.onDidAnyChange((newValue, oldValue) => {
2323
interface State {
2424
electronApiPort: number | null;
2525
activeMenuBar: any;
26+
tray: Tray | null;
2627
php: string | null;
2728
phpPort: number | null;
2829
phpIni: any;
@@ -52,6 +53,7 @@ function generateRandomString(length: number) {
5253
export default {
5354
electronApiPort: null,
5455
activeMenuBar: null,
56+
tray: null,
5557
php: null,
5658
phpPort: null,
5759
phpIni: null,

0 commit comments

Comments
 (0)