-
-
Notifications
You must be signed in to change notification settings - Fork 853
Expand file tree
/
Copy pathwindow.ts
More file actions
316 lines (267 loc) · 8.22 KB
/
Copy pathwindow.ts
File metadata and controls
316 lines (267 loc) · 8.22 KB
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
import { app, BrowserWindow, BrowserWindowConstructorOptions } from 'electron'
import path from 'path'
import { forceQuit } from '.'
import * as config from './config'
import * as constants from './constants'
import * as dock from './dock'
import * as helpers from './helpers'
import * as ipc from './ipc'
import { __DEV__ } from './libs/electron-is-dev'
import { WindowState, windowStateKeeper } from './libs/electron-window-state'
import * as menu from './menu'
import * as screen from './screen'
import * as tray from './tray'
let mainWindow: BrowserWindow | undefined
let mainWindowState: WindowState
let menubarWindowState: WindowState
export function getMainWindow() {
return mainWindow
}
export function init() {
const display = screen.getDisplayFromCursor()
mainWindowState = windowStateKeeper({
defaultWidth: display.workAreaSize.width,
defaultHeight:
display.workAreaSize.height - (process.platform === 'linux' ? 28 : 0),
file: `main-window-display-${display.id}.json`,
fullScreen: false,
})
menubarWindowState = windowStateKeeper({
defaultWidth: 350,
defaultHeight: 630,
file: `menubar-window-display-${display.id}.json`,
fullScreen: false,
})
mainWindow = createWindow()
update()
}
let mainWindowReadyToShowCount = 0
let screenId: number
export function createWindow() {
const win = new BrowserWindow(getBrowserWindowOptions())
win.loadURL(constants.START_URL)
win.once('ready-to-show', () => {
mainWindowReadyToShowCount++
if (
mainWindowReadyToShowCount === 1 &&
(__DEV__ || app.getLoginItemSettings().wasOpenedAsHidden)
)
return
helpers.showWindow(win)
})
win.on('show', () => {
menu.updateMenu()
screenId = screen.getDisplayFromWindow(win).id
updateBrowserWindowOptions()
})
win.on('hide', () => {
menu.updateMenu()
})
win.on('close', (e) => {
menu.updateMenu()
if (process.platform === 'darwin' && !forceQuit) {
e.preventDefault()
win.hide()
return
} else {
// For other platforms, allow the window to be closed
mainWindow = undefined; // Set the reference to mainWindow to undefined
win.destroy() // Quit the application
}
})
win.on('closed', () => {
win.destroy()
mainWindow = undefined
})
win.on('resize', () => {
if (config.store.get('isMenuBarMode')) {
tray.alignWindowWithTray(win)
} else if (config.store.get('lockOnCenter')) {
center(win)
}
})
win.on('move', () => {
screenId = screen.getDisplayFromWindow(win).id
})
win.on('blur', () => {
menu.updateMenu()
function shouldHide() {
const isSameDisplay = screen.getDisplayFromCursor().id === screenId
return (
isSameDisplay &&
config.store.get('isMenuBarMode') &&
config.store.get('isMenuBarModeChangedAt') &&
Date.now() - config.store.get('isMenuBarModeChangedAt')! > 1000 &&
!win.isDestroyed()
)
}
if (!shouldHide()) return
setTimeout(() => {
if (!shouldHide()) return
win.hide()
}, 200)
})
win.on('maximize', () => {
ipc.emit('is-maximized-change', true)
})
win.on('unmaximize', () => {
ipc.emit('is-maximized-change', false)
})
win.on('enter-full-screen', () => {
ipc.emit('fullscreen-change', true)
const _dock = dock.getDock()
if (_dock) _dock.show()
})
win.on('leave-full-screen', () => {
ipc.emit('fullscreen-change', false)
if (!(mainWindow && mainWindow.isFocused())) return
update()
})
return win
}
export function getBrowserWindowOptions() {
const options: BrowserWindowConstructorOptions = {
titleBarStyle:
process.platform === 'darwin' && !config.store.get('isMenuBarMode')
? 'hidden'
: 'default',
minWidth: 320,
minHeight: 450,
backgroundColor: '#1F2229',
darkTheme: true,
icon:
process.platform === 'darwin' || process.platform === 'win32'
? undefined
: path.join(__dirname, '../assets/icons/icon.png'),
resizable: true,
show: !!(getMainWindow() && getMainWindow()!.isVisible()),
title: 'DevHub',
webPreferences: {
affinity: 'main-window',
backgroundThrottling: false,
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js'),
},
...(config.store.get('isMenuBarMode')
? {
x: menubarWindowState.x,
y: menubarWindowState.y,
width: menubarWindowState.width,
height: menubarWindowState.height,
alwaysOnTop: true,
center: false,
frame: false,
fullscreenable: false,
maxWidth: screen.getDisplayFromCursor().size.width * 0.9,
maxHeight: screen.getDisplayFromCursor().size.height,
movable: false,
skipTaskbar: true,
}
: {
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
alwaysOnTop: false,
center: true,
frame: constants.FRAME_IS_DIFFERENT_BETWEEN_MODES,
fullscreenable: true,
maxWidth: undefined,
maxHeight: undefined,
movable: !config.store.get('lockOnCenter'),
skipTaskbar: false,
}),
}
return options
}
export function update() {
menu.updateMenu()
updateBrowserWindowOptions()
}
export function updateOrRecreateWindow() {
if (constants.FRAME_IS_DIFFERENT_BETWEEN_MODES || !mainWindow) {
const oldWindow = mainWindow
mainWindow = createWindow()
if (oldWindow) oldWindow.close()
}
update()
helpers.showWindow(mainWindow)
}
let isFirstTime = true
function updateBrowserWindowOptions() {
if (!mainWindow) return
const options = getBrowserWindowOptions()
if (config.store.get('isMenuBarMode')) {
mainWindowState.unmanage()
menubarWindowState.manage(mainWindow)
} else {
menubarWindowState.unmanage()
mainWindowState.manage(mainWindow)
}
if (mainWindow.setWindowButtonVisibility)
mainWindow.setWindowButtonVisibility(options.titleBarStyle === 'hidden')
const maximize =
!config.store.get('isMenuBarMode') &&
(mainWindow.isMaximized() || (isFirstTime && mainWindowState.isMaximized))
isFirstTime = false
mainWindow.setAlwaysOnTop(options.alwaysOnTop === true)
mainWindow.setMinimumSize(
Math.floor(options.minWidth || 0),
Math.floor(options.minHeight || 0),
)
mainWindow.setMaximumSize(
Math.ceil(options.maxWidth || screen.getDisplayFromCursor().size.width),
Math.ceil(options.maxHeight || screen.getDisplayFromCursor().size.height),
)
mainWindow.setMovable(options.movable !== false)
if (!config.store.get('isMenuBarMode')) {
mainWindow.setPosition(
maximize ? screen.getDisplayFromCursor().workArea.x || 0 : options.x || 0,
maximize ? screen.getDisplayFromCursor().workArea.y || 0 : options.y || 0,
false,
)
}
// Note: setSkipTaskbar was causing the app to freeze on linux
if (process.platform === 'darwin' || process.platform === 'win32') {
mainWindow.setSkipTaskbar(options.skipTaskbar === true)
}
if (maximize) {
// Node: workAreaSize.heigth is wrong on linux, causing the app content to jump on window open
if (process.platform !== 'linux') {
mainWindow.setSize(
screen.getDisplayFromCursor().workAreaSize.width,
screen.getDisplayFromCursor().workAreaSize.height,
false,
)
}
} else {
if (mainWindow.isMaximized() || config.store.get('isMenuBarMode'))
mainWindow.unmaximize()
mainWindow.setSize(options.width || 500, options.height || 500, false)
}
const _dock = dock.getDock()
if (_dock) {
if (options.skipTaskbar === true) {
_dock.hide()
} else {
_dock.show()
}
}
if (config.store.get('isMenuBarMode')) {
tray.alignWindowWithTray(mainWindow)
} else {
if (config.store.get('lockOnCenter')) {
center(mainWindow)
}
}
}
export function center(win: BrowserWindow) {
const windowBounds = win.getBounds()
const workArea = screen.getDisplayFromCursor().workArea
win.setPosition(
Math.round(workArea.x + workArea.width / 2 - windowBounds.width / 2),
Math.round(workArea.y + workArea.height / 2 - windowBounds.height / 2),
)
}