-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
WindowBuilder.ts
139 lines (116 loc) · 3.92 KB
/
WindowBuilder.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
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
import _ from 'lodash'
import path from 'path'
import { BrowserWindow } from "electron"
import electron from 'electron'
import { createProtocol } from "vue-cli-plugin-electron-builder/lib"
import platformInfo from '../common/platform_info'
import { IGroupedUserSettings } from '../common/appdb/models/user_setting'
import rawLog from 'electron-log'
import querystring from 'query-string'
// eslint-disable-next-line
const remoteMain = require('@electron/remote/main')
const log = rawLog.scope('WindowBuilder')
const windows: BeekeeperWindow[] = []
export interface OpenOptions {
url?: string
runningWayland: boolean
}
function getIcon() {
return path.resolve(path.join(__dirname, '..', `public/icons/png/512x512.png`))
}
class BeekeeperWindow {
private win: BrowserWindow | null
private reloaded = false
constructor(settings: IGroupedUserSettings, openOptions: OpenOptions) {
const theme = settings.theme
const dark = electron.nativeTheme.shouldUseDarkColors || theme.value.toString().includes('dark')
let showFrame = settings.menuStyle && settings.menuStyle.value == 'native' ? true : false
let titleBarStyle: 'default' | 'hidden' = platformInfo.isWindows && settings.menuStyle.value == 'native' ? 'default' : 'hidden'
if (openOptions.runningWayland) {
showFrame = false
titleBarStyle = 'hidden'
}
log.info('constructing the window')
this.win = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
backgroundColor: dark ? "#252525" : '#ffffff',
titleBarStyle,
frame: showFrame,
webPreferences: {
nodeIntegration: Boolean(process.env.ELECTRON_NODE_INTEGRATION),
contextIsolation: false,
spellcheck: false
},
icon: getIcon()
})
const runningInWebpack = !!process.env.WEBPACK_DEV_SERVER_URL
let appUrl = process.env.WEBPACK_DEV_SERVER_URL || 'app://./index.html'
const query = openOptions ? querystring.stringify(openOptions) : null
appUrl = query ? `${appUrl}?${query}` : appUrl
remoteMain.enable(this.win.webContents)
this.win.webContents.zoomLevel = Number(settings.zoomLevel?.value) || 0
if (!runningInWebpack) {
createProtocol('app')
}
this.win.loadURL(appUrl)
if ((platformInfo.env.development && !platformInfo.env.test) || platformInfo.debugEnabled) {
this.win.webContents.openDevTools()
}
this.initializeCallbacks()
this.win.webContents.on('will-navigate', (e, url) => {
if (url === appUrl) return // this is good
log.info("navigate to", url)
e.preventDefault()
const u = new URL(url)
u.searchParams.append('ref', 'bks-app')
electron.shell.openExternal(u.toString());
})
this.win.webContents.setWindowOpenHandler(({ url }) => {
if (url === appUrl){
return {
action: 'allow'
}
} else {
return { action: 'deny' }
}
})
this.win.webContents.on('ipc-message', (e, channel, ...args) => {
if(channel === 'setWindowTitle') {
this.win.setTitle(args[0])
e.preventDefault()
}
})
}
get webContents() {
return this.win ? this.win.webContents : null
}
send(channel: string, ...args: any[]) {
this.win?.webContents.send(channel, ...args)
}
initializeCallbacks() {
if (process.env.WEBPACK_DEV_SERVER_URL && platformInfo.isWindows) {
// this.win?.webContents.on('did-finish-load', this.finishLoadListener.bind(this))
}
this.win?.on('closed', () => {
this.win = null
})
}
finishLoadListener() {
if(!this.reloaded) {
this.win?.webContents.reload()
}
this.reloaded = true
}
get active() {
return !!this.win
}
}
export function getActiveWindows(): BeekeeperWindow[] {
return _.filter(windows, 'active')
}
export function buildWindow(settings: IGroupedUserSettings, options: OpenOptions): void {
windows.push(new BeekeeperWindow(settings, options))
}