forked from vincelwt/chatgpt-mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (160 loc) · 4.39 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require("update-electron-app")();
const { menubar } = require("menubar");
const Nucleus = require("nucleus-analytics");
const path = require("path");
const {
app,
nativeImage,
Tray,
Menu,
globalShortcut,
shell,
} = require("electron");
const contextMenu = require("electron-context-menu");
const image = nativeImage.createFromPath(
path.join(__dirname, `images/newiconTemplate.png`)
);
app.on("ready", () => {
Nucleus.init("638d9ccf4a5ed2dae43ce122");
const tray = new Tray(image);
const mb = menubar({
browserWindow: {
icon: image,
transparent: path.join(__dirname, `images/iconApp.png`),
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 = [
// add links to github repo and vince's twitter
{
label: "Quit",
accelerator: "Command+Q",
click: () => {
app.quit();
},
},
{
label: "Reload",
accelerator: "Command+R",
click: () => {
window.reload();
},
},
{
label: "Open in browser",
click: () => {
shell.openExternal("https://chat.openai.com/chat");
},
},
{
type: "separator",
},
{
label: "View on GitHub",
click: () => {
shell.openExternal("https://github.com/vincelwt/chatgpt-mac");
},
},
{
label: "Author on Twitter",
click: () => {
shell.openExternal("https://twitter.com/vincelwt");
},
},
];
tray.on("right-click", () => {
mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate));
});
tray.on("click", (e) => {
//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();
}
});