forked from smol-ai/GodMode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
378 lines (339 loc) · 10.7 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// require("update-electron-app")(); // uncomment this line to enable auto-updates.
// we commented out because of https://github.com/smol-ai/menubar/issues/17
// Logging library
const log = require('electron-log');
// Menubar library to create electron applications that reside in the system's menubar
const { menubar } = require('menubar');
// Path module for handling and transforming file paths
const path = require('path');
// Importing necessary modules from electron
const {
app,
nativeImage,
Tray,
Menu,
globalShortcut,
shell,
screen,
} = require('electron');
// Getting the application's version from package.json
const { version } = require('./package.json');
// Providers for different services
const providers = {
OpenAi: require('./providers/openai'),
Bard: require('./providers/bard'),
Bing: require('./providers/bing'),
Claude: require('./providers/claude'),
Claude2: require('./providers/claude2'),
OobaBooga: require('./providers/oobabooga'),
Smol: require('./providers/smol'),
};
// Getting all the providers in an array
const allProviders = Object.values(providers);
// Electron-store used for persistent data storage
const Store = require('electron-store');
const store = new Store();
console.log('if process.env.NODE_ENV is development, reset the store', process.env.NODE_ENV )
if (process.env.NODE_ENV === 'development') {
store.clear() // reset to defaults when in dev
}
log.info('store reset', store); // Logging the store
// Initialize fullscreen toggle
store.set('isFullscreen', false);
// Context menu for electron apps
const contextMenu = require('electron-context-menu');
// Creating an icon image
const image = nativeImage.createFromPath(
path.join(__dirname, `images/iconTemplate.png`)
);
// Once the app is ready, the following code will execute
app.on('ready', () => {
const tray = new Tray(image);
let { width } = screen.getPrimaryDisplay().workAreaSize;
const mb = menubar({
browserWindow: {
icon: image,
transparent: path.join(__dirname, `images/iconApp.png`),
autoHideMenuBar: false,
webPreferences: {
webviewTag: true,
nodeIntegration: true,
contextIsolation: false,
enableWebView: true, // from chatgpt
// nativeWindowOpen: true,
},
width: store.get('isFullscreen', false) ? width : 1200,
height: 750,
},
tray,
showOnAllWorkspaces: true,
preloadWindow: true,
showDockIcon: false,
icon: image,
});
// On menubar ready, the following code will execute
mb.on('ready', () => {
const { window } = mb;
if (process.platform !== 'darwin') {
window.setSkipTaskbar(true);
} else {
app.dock.hide();
}
// The createContextMenuTemplate function creates the context menu template
// It contains the header, providers' toggles, links, and footer of the menu
const createContextMenuTemplate = () => {
const menuHeader = [
{
label: 'Quit',
accelerator: 'CommandorControl+Q',
click: () => {
app.quit();
},
},
{
label: 'Reload',
accelerator: 'CommandorControl+R',
click: () => {
window.reload();
},
},
{
label: 'Quick Open (use this!)',
accelerator: 'CommandorControl+Shift+G',
click: () => {
window.reload();
},
}, {
label: 'Toggle Fullscreen',
accelerator: 'CommandorControl+Shift+F',
type: 'checkbox',
checked: store.get('isFullscreen', false),
click: () => {
const fullscreen = !store.get('isFullscreen', false);
store.set('isFullscreen', fullscreen);
if (fullscreen) {
window.setBounds({x: 0, width: width, height: window.getSize()[1]});
} else {
window.setBounds({x: width - 1200, width: 1200, height: window.getSize()[1]});
}
},
}
];
const separator = { type: 'separator' };
const providersToggles = allProviders.map(provider => {
return {
label: provider.fullName,
type: 'checkbox',
checked: store.get(`${provider.webviewId}Enabled`, provider.isEnabled()),
click: () => {
store.set(
`${provider.webviewId}Enabled`,
!provider.isEnabled()
);
setTimeout(() => {
window.reload();
}, 100)
},
};
});
const superPromptEnterKey = {
label: 'Super Prompt "Enter" Key',
type: 'checkbox',
checked: store.get('SuperPromptEnterKey', false),
click: () => {
store.set(
'SuperPromptEnterKey',
!store.get('SuperPromptEnterKey', false)
);
window.reload();
},
};
const providerLinks = allProviders.map(provider => {
return {
label: `Visit ${provider.name} website`,
click: () => {
shell.openExternal(provider.url);
},
};
});
const menuFooter = [
// Removing the preferences window for now because all settings are now
// in the menubar context menu dropdown. (Seemed like a better UX)
// {
// label: 'Preferences',
// click: () => {
// const preferencesWindow = new BrowserWindow({
// parent: null,
// modal: false,
// alwaysOnTop: true,
// show: false,
// autoHideMenuBar: true,
// width: 500,
// height: 300,
// webPreferences: {
// nodeIntegration: true,
// contextIsolation: false,
// },
// });
// preferencesWindow.loadFile('preferences.html');
// preferencesWindow.once('ready-to-show', () => {
// mb.hideWindow();
// preferencesWindow.show();
// });
// // When the preferences window is closed, show the main window again
// preferencesWindow.on('close', () => {
// mb.showWindow();
// mb.window.reload(); // reload the main window to apply the new settings
// });
// },
// },
{
label: 'View on GitHub',
click: () => {
shell.openExternal('https://github.com/smol-ai/menubar');
},
},
{
type: 'separator',
},
{
label: 'Version ' + version,
}
]
// Return the complete context menu template
return [
...menuHeader,
separator,
...providersToggles,
separator,
superPromptEnterKey,
separator,
...providerLinks,
separator,
...menuFooter,
]
};
// Create the context menu when right-clicking the tray icon
tray.on('right-click', () => {
const contextMenuTemplate = createContextMenuTemplate();
mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate));
});
// Create the context menu when clicking the tray icon with control or meta key
tray.on('click', e => {
//check if ctrl or meta key is pressed while clicking
if (e.ctrlKey || e.metaKey) {
const contextMenuTemplate = createContextMenuTemplate();
mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate));
}
});
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();
}
});
// Fullscreen menu shortcut
globalShortcut.register('CommandOrControl+Shift+F', () => {
const fullscreen = !store.get('isFullscreen', false);
store.set('isFullscreen', fullscreen);
const { window } = mb;
if (fullscreen) {
window.setBounds({x: 0, width: width, height: window.getSize()[1]});
} else {
window.setBounds({x: width - 1200, width: 1200, height: window.getSize()[1]});
}
});
Menu.setApplicationMenu(menu, { autoHideMenuBar: false });
// open devtools if in dev mode
if (process.env.NODE_ENV === 'development') {
window.webContents.openDevTools();
}
console.log('Menubar app is ready.');
});
app.on('web-contents-created', (e, contents) => {
if (contents.getType() == 'webview') {
// contents.on("will-navigate", (event, url, frameName, disposition, options, additionalFeatures) => {
// console.log({frameName})
// if (frameName === 'my_popup') {
// // Open `url` in a new window...
// event.preventDefault()
// Object.assign(options, {
// parent: win,
// width: 500,
// height: 400
// })
// event.newGuest = new BrowserWindow(options)
// }
// })
// open link with external browser in webview
contents.setWindowOpenHandler('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 === 'x') contents.cut();
// if (key === "v") contents.paste(); // we will handle this manually
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();
});
}
// 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 === 'x') contents.cut();
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();
}
});