-
Notifications
You must be signed in to change notification settings - Fork 1
/
WindowService.ts
94 lines (81 loc) · 2.07 KB
/
WindowService.ts
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
// WindowsService.ts
import {
BrowserWindow,
Screen,
screen,
IpcMain,
ipcMain,
Dialog,
dialog,
} from 'electron'
/**
* Testable service for creating BrowserWindows in our app,
* with mockable dependencies.
*/
export class WindowService {
_WindowClass: typeof BrowserWindow
_screenInstance: Screen
_ipcMainInstance: IpcMain
_dialogInstance: Dialog
constructor(
WindowClass: typeof BrowserWindow,
screenInstance: Screen,
ipcMainInstance: IpcMain,
dialogInstance: Dialog,
) {
this._WindowClass = WindowClass
this._screenInstance = screenInstance
this._ipcMainInstance = ipcMainInstance
this._dialogInstance = dialogInstance
}
createWindow(): BrowserWindow {
const screenRect = this._screenInstance.getPrimaryDisplay().workArea
// 1/2 screen width, 1/2 screen height, centered on primary display
const windowRect = {
x: Math.round(screenRect.x + screenRect.width / 4),
y: Math.round(screenRect.y + screenRect.height / 4),
width: Math.round(screenRect.width / 2),
height: Math.round(screenRect.height / 2),
}
const win = new this._WindowClass({
...windowRect,
title: 'Main Window',
show: false,
webPreferences: {
nodeIntegration: true,
},
})
win.loadFile('index.html')
win.once('ready-to-show', () => {
win.show()
})
win.on('close', async (e) => {
e.preventDefault()
const { response } = await this._dialogInstance.showMessageBox(win, {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?',
})
if (response === 0) {
win.destroy()
}
})
// See: https://github.com/electron/electron/issues/38560
win.webContents.ipc.handle('get-state', async () => {
return {
rock: '🤘',
roll: '🍩',
}
})
return win
}
}
/** Default WindowService instance */
export const windowService = new WindowService(
BrowserWindow,
screen,
ipcMain,
dialog,
)
export default windowService