Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 13ef865

Browse files
committed
Add option to create context-menu only menubar apps
1 parent 390e2c2 commit 13ef865

File tree

2 files changed

+114
-68
lines changed

2 files changed

+114
-68
lines changed

dist/server/api/menuBar.js

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,46 @@ router.post("/hide", (req, res) => {
2525
});
2626
router.post("/create", (req, res) => {
2727
res.sendStatus(200);
28-
const { width, height, url, label, alwaysOnTop, vibrancy, backgroundColor, transparency, icon, showDockIcon, contextMenu } = req.body;
29-
state_1.default.activeMenuBar = (0, menubar_1.menubar)({
30-
icon: icon || state_1.default.icon.replace("icon.png", "IconTemplate.png"),
31-
index: url,
32-
showDockIcon,
33-
showOnAllWorkspaces: false,
34-
browserWindow: {
35-
width,
36-
height,
37-
alwaysOnTop,
38-
vibrancy,
39-
backgroundColor,
40-
transparent: transparency,
41-
webPreferences: {
42-
nodeIntegration: true,
43-
sandbox: false,
44-
contextIsolation: false
28+
const { width, height, url, label, alwaysOnTop, vibrancy, backgroundColor, transparency, icon, showDockIcon, onlyShowContextWindow, contextMenu } = req.body;
29+
if (onlyShowContextWindow === true) {
30+
const tray = new electron_1.Tray(icon || state_1.default.icon.replace("icon.png", "IconTemplate.png"));
31+
tray.setContextMenu(buildMenu(contextMenu));
32+
state_1.default.activeMenuBar = (0, menubar_1.menubar)({
33+
tray,
34+
index: false,
35+
showDockIcon,
36+
showOnAllWorkspaces: false,
37+
browserWindow: {
38+
show: false,
39+
width: 0,
40+
height: 0,
4541
}
46-
}
47-
});
48-
state_1.default.activeMenuBar.on("after-create-window", () => {
49-
require("@electron/remote/main").enable(state_1.default.activeMenuBar.window.webContents);
50-
});
42+
});
43+
}
44+
else {
45+
state_1.default.activeMenuBar = (0, menubar_1.menubar)({
46+
icon: icon || state_1.default.icon.replace("icon.png", "IconTemplate.png"),
47+
index: url,
48+
showDockIcon,
49+
showOnAllWorkspaces: false,
50+
browserWindow: {
51+
width,
52+
height,
53+
alwaysOnTop,
54+
vibrancy,
55+
backgroundColor,
56+
transparent: transparency,
57+
webPreferences: {
58+
nodeIntegration: true,
59+
sandbox: false,
60+
contextIsolation: false
61+
}
62+
}
63+
});
64+
state_1.default.activeMenuBar.on("after-create-window", () => {
65+
require("@electron/remote/main").enable(state_1.default.activeMenuBar.window.webContents);
66+
});
67+
}
5168
state_1.default.activeMenuBar.on("ready", () => {
5269
state_1.default.activeMenuBar.tray.setTitle(label);
5370
state_1.default.activeMenuBar.on("hide", () => {
@@ -60,17 +77,22 @@ router.post("/create", (req, res) => {
6077
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
6178
});
6279
});
63-
state_1.default.activeMenuBar.tray.on("right-click", () => {
64-
(0, utils_1.notifyLaravel)("events", {
65-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarContextMenuOpened"
80+
if (onlyShowContextWindow !== true) {
81+
state_1.default.activeMenuBar.tray.on("right-click", () => {
82+
(0, utils_1.notifyLaravel)("events", {
83+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarContextMenuOpened"
84+
});
85+
state_1.default.activeMenuBar.tray.popUpContextMenu(buildMenu(contextMenu));
6686
});
67-
let menu = electron_1.Menu.buildFromTemplate([{ role: "quit" }]);
68-
if (contextMenu) {
69-
const menuEntries = contextMenu.map(helper_1.mapMenu);
70-
menu = electron_1.Menu.buildFromTemplate(menuEntries);
71-
}
72-
state_1.default.activeMenuBar.tray.popUpContextMenu(menu);
73-
});
87+
}
7488
});
7589
});
90+
function buildMenu(contextMenu) {
91+
let menu = electron_1.Menu.buildFromTemplate([{ role: "quit" }]);
92+
if (contextMenu) {
93+
const menuEntries = contextMenu.map(helper_1.mapMenu);
94+
menu = electron_1.Menu.buildFromTemplate(menuEntries);
95+
}
96+
return menu;
97+
}
7698
exports.default = router;

src/server/api/menuBar.ts

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from "express";
2-
import { Menu, nativeImage } from "electron";
2+
import { Menu, Tray } from "electron";
33
import { mapMenu } from "./helper";
44
import state from "../state";
55
import { menubar } from "menubar";
@@ -42,33 +42,50 @@ router.post("/create", (req, res) => {
4242
transparency,
4343
icon,
4444
showDockIcon,
45+
onlyShowContextWindow,
4546
contextMenu
4647
} = req.body;
4748

48-
state.activeMenuBar = menubar({
49-
icon: icon || state.icon.replace("icon.png", "IconTemplate.png"),
50-
index: url,
51-
showDockIcon,
52-
showOnAllWorkspaces: false,
53-
browserWindow: {
54-
width,
55-
height,
56-
alwaysOnTop,
57-
vibrancy,
58-
backgroundColor,
59-
transparent: transparency,
60-
webPreferences: {
61-
nodeIntegration: true,
62-
sandbox: false,
63-
contextIsolation: false
49+
if (onlyShowContextWindow === true) {
50+
const tray = new Tray(icon || state.icon.replace("icon.png", "IconTemplate.png"));
51+
tray.setContextMenu(buildMenu(contextMenu));
52+
53+
state.activeMenuBar = menubar({
54+
tray,
55+
index: false,
56+
showDockIcon,
57+
showOnAllWorkspaces: false,
58+
browserWindow: {
59+
show: false,
60+
width: 0,
61+
height: 0,
6462
}
65-
}
66-
});
67-
63+
});
6864

69-
state.activeMenuBar.on("after-create-window", () => {
70-
require("@electron/remote/main").enable(state.activeMenuBar.window.webContents);
71-
});
65+
} else {
66+
state.activeMenuBar = menubar({
67+
icon: icon || state.icon.replace("icon.png", "IconTemplate.png"),
68+
index: url,
69+
showDockIcon,
70+
showOnAllWorkspaces: false,
71+
browserWindow: {
72+
width,
73+
height,
74+
alwaysOnTop,
75+
vibrancy,
76+
backgroundColor,
77+
transparent: transparency,
78+
webPreferences: {
79+
nodeIntegration: true,
80+
sandbox: false,
81+
contextIsolation: false
82+
}
83+
}
84+
});
85+
state.activeMenuBar.on("after-create-window", () => {
86+
require("@electron/remote/main").enable(state.activeMenuBar.window.webContents);
87+
});
88+
}
7289

7390
state.activeMenuBar.on("ready", () => {
7491
state.activeMenuBar.tray.setTitle(label);
@@ -78,27 +95,34 @@ router.post("/create", (req, res) => {
7895
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarHidden"
7996
});
8097
});
98+
8199
state.activeMenuBar.on("show", () => {
82100
notifyLaravel("events", {
83101
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarShown"
84102
});
85103
});
86104

87-
state.activeMenuBar.tray.on("right-click", () => {
88-
notifyLaravel("events", {
89-
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarContextMenuOpened"
90-
})
91-
92-
let menu = Menu.buildFromTemplate([{ role: "quit" }]);
105+
if (onlyShowContextWindow !== true) {
106+
state.activeMenuBar.tray.on("right-click", () => {
107+
notifyLaravel("events", {
108+
event: "\\Native\\Laravel\\Events\\MenuBar\\MenuBarContextMenuOpened"
109+
});
93110

94-
if (contextMenu) {
95-
const menuEntries = contextMenu.map(mapMenu);
96-
menu = Menu.buildFromTemplate(menuEntries);
97-
}
98-
99-
state.activeMenuBar.tray.popUpContextMenu(menu);
100-
});
111+
state.activeMenuBar.tray.popUpContextMenu(buildMenu(contextMenu));
112+
});
113+
}
101114
});
102115
});
103116

117+
function buildMenu(contextMenu) {
118+
let menu = Menu.buildFromTemplate([{ role: "quit" }]);
119+
120+
if (contextMenu) {
121+
const menuEntries = contextMenu.map(mapMenu);
122+
menu = Menu.buildFromTemplate(menuEntries);
123+
}
124+
125+
return menu;
126+
}
127+
104128
export default router;

0 commit comments

Comments
 (0)