-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (144 loc) · 4.75 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// todo https://github.com/electron/electron/issues/7476
// require("update-electron-app")();
const fs = require("fs");
const path = require("path");
const data = fs.readFileSync(__dirname + '/package.json', 'utf8');
let pkg = JSON.parse(data);
const {menubar} = require("menubar");
const Analytics = require('electron-google-analytics4').default;
const analytics = new Analytics('G-5WHLLLW38Y', 'LDGJbEmcR1-1naJDfemQFQ');
analytics.setParams({"engagement_time_msec": 1});
const {
app, nativeImage, Tray, Menu, globalShortcut, shell,
} = require("electron");
const contextMenu = require("electron-context-menu");
const image = nativeImage.createFromPath(path.join(__dirname, `images/menubar/icon.png`));
app.on("ready", () => {
analytics.event('page_ready');
const tray = new Tray(image);
const mb = menubar({
browserWindow: {
icon: image,
transparent: true,
webPreferences: {
webviewTag: true,
// nativeWindowOpen: true,
},
width: 450,
height: 550,
}, tray, showOnAllWorkspaces: true, preloadWindow: true, showDockIcon: false, icon: image,
});
mb.on("ready", () => {
const {window} = mb;
if (process.platform !== "darwin") {
window.setSkipTaskbar(true);
} else {
app.dock.hide();
}
const contextMenuTemplate = [{
label: 'Always On Top', type: 'checkbox', checked: false,
click: (menuItem, browserWindow, event) => {
let newChecked = menuItem.checked;
contextMenuTemplate[0].checked = newChecked;
let mainWindow = mb.window;
mainWindow.setAlwaysOnTop(newChecked);
mb.tray.closeContextMenu();
}
}, {
label: "Quit", accelerator: "Command+Q", click: () => {
app.quit();
},
}, {
label: "Reload", accelerator: "Command+R", click: () => {
window.reload();
},
}, {
label: "Open in Browser", click: () => {
shell.openExternal("https://kimi.moonshot.cn/");
},
}, {
type: "separator",
}, {
label: "Home Page", click: () => {
shell.openExternal(`https://logspot.hocgin.top/${pkg.name}`);
},
}, {
label: "More", click: () => {
shell.openExternal("https://logspot.hocgin.top/");
},
},];
tray.on("right-click", () => {
mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate));
});
tray.on("click", (e) => {
analytics.event('page_view');
//check if ctrl or meta key is pressed while clicking
e.ctrlKey || e.metaKey ? mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate)) : null;
});
const menu = new Menu();
globalShortcut.register("CommandOrControl+Shift+g", () => {
if (window.isVisible()) {
mb.hideWindow();
} else {
mb.showWindow();
if (process.platform === "darwin") {
mb.app.show();
}
mb.app.focus();
}
});
Menu.setApplicationMenu(menu);
// open devtools
// window.webContents.openDevTools();
console.log("Menubar app is ready.");
});
app.on("web-contents-created", (e, contents) => {
if (contents.getType() === "webview") {
// open link with external browser in webview
contents.on("new-window", (e, url) => {
e.preventDefault();
shell.openExternal(url);
});
// set context menu in webview
contextMenu({
window: contents,
});
// we can't set the native app menu with "menubar" so need to manually register these events
// register cmd+c/cmd+v events
contents.on("before-input-event", (event, input) => {
const {control, meta, key} = input;
if (!control && !meta) return;
if (key === "c") contents.copy();
if (key === "v") contents.paste();
if (key === "a") contents.selectAll();
if (key === "z") contents.undo();
if (key === "y") contents.redo();
if (key === "q") app.quit();
if (key === "r") contents.reload();
});
}
});
if (process.platform === "darwin") {
// restore focus to previous app on hiding
mb.on("after-hide", () => {
mb.app.hide();
});
}
// open links in new window
// app.on("web-contents-created", (event, contents) => {
// contents.on("will-navigate", (event, navigationUrl) => {
// event.preventDefault();
// shell.openExternal(navigationUrl);
// });
// });
// prevent background flickering
app.commandLine.appendSwitch("disable-backgrounding-occluded-windows", "true");
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});