-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
153 lines (134 loc) · 4.2 KB
/
main.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
const { app, BrowserWindow, screen, webFrame, Menu, Tray } = require('electron');
const path = require('path');
const Store = require('electron-store');
const store = new Store();
const prompt = require('electron-prompt');
/* ============= DEV ============= */
/* Hot reload on code change ? * /
if (process.env.NODE_ENV !== 'production') {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
});
}
/* ============= /DEV ============= */
let tray = null;
let mainWindow = null;
let appIsQuitting = false;
const defaultUrl = 'https://chat.openai.com/chat/';
function createMainWindow () {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
let appConfigLoadUrl = store.get('app_loadUrl') ?? defaultUrl;
const appConfigWindowBounds = store.get('app_windowBounds');
const appConfigAlwaysOnTop = store.get('app_alwaysOnTop') ?? false;
mainWindow = new BrowserWindow({
minWidth: 420,
minHeight: 500,
width: appConfigWindowBounds?.width ?? Math.round(width * 0.25),
height: appConfigWindowBounds?.height ?? height,
x: appConfigWindowBounds?.x ?? Math.round(width * 0.75),
y: appConfigWindowBounds?.y ?? 0,
frame: false,
alwaysOnTop: appConfigAlwaysOnTop,
resizable: true,
movable: true, // Window dragging
webPreferences: {
nodeIntegration: true, // Enable Node.js integration in the renderer process
contextIsolation: false, // Enable Node.js integration in the renderer process
}
})
mainWindow.loadURL(appConfigLoadUrl);
mainWindow.on('closed', () => {
mainWindow = null;
})
mainWindow.on('minimize', (event) => {
event.preventDefault();
mainWindow.hide();
})
app.on('before-quit', () => {
appIsQuitting = true;
});
app.on('window-all-closed', () => {
if (!appIsQuitting) {
app.quit();
}
});
app.on('quit', () => {
if (!appIsQuitting) {
app.relaunch();
app.exit(0);
}
});
mainWindow.on('close', (event) => {
store.set('app_windowBounds', mainWindow.getBounds());
if (!appIsQuitting) {
event.preventDefault();
mainWindow.hide();
}
return false;
});
/* TRAY */
tray = new Tray(path.join(__dirname, 'assets/chatGPT_logo.png'));
tray.on('click', () => {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
})
const zoomMenu = Menu.buildFromTemplate([
{ label: 'Zoom In', accelerator: 'CmdOrCtrl+Plus', click: function() { webFrame.setZoomLevel(webFrame.getZoomLevel() + 1); } },
{ label: 'Zoom Out', accelerator: 'CmdOrCtrl+-', click: function() { webFrame.setZoomLevel(webFrame.getZoomLevel() - 1); } },
{ label: 'Reset Zoom', accelerator: 'CmdOrCtrl+0', click: function() { webFrame.setZoomLevel(0); } }
]);
const contextMenu = Menu.buildFromTemplate([
//{ label: 'Zoom', submenu: zoomMenu },
{
label: 'Always on top',
type: 'checkbox',
checked: appConfigAlwaysOnTop,
click: function() {
let aotValue = !mainWindow.isAlwaysOnTop();
mainWindow.setAlwaysOnTop(aotValue);
store.set('app_alwaysOnTop', aotValue);
}
},
{
label: 'Change URL',
click: () => {
prompt({
title: 'Enter a new URL',
label: 'URL: This would be opened in the app',
value: appConfigLoadUrl,
width: 480,
height: 180,
inputAttrs: {
type: 'url',
},
})
.then( (result) => {
let newUrl = (result && result.trim() !== '')?result:defaultUrl;
appConfigLoadUrl = newUrl;
store.set('app_loadUrl', newUrl);
mainWindow.loadURL(newUrl);
})
.catch((err) => {
console.error(err);
});
},
},
{ label: 'Restart', click: () => { restartApp() } },
{ label: 'Quit', click: () => { appIsQuitting = true; app.quit() } },
])
tray.setContextMenu(contextMenu);
}
app.on('ready', createMainWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createMainWindow();
}
})
function restartApp() {
app.relaunch();
app.quit();
}