-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
246 lines (221 loc) · 6.04 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
// @ts-check
const process = require('node:process')
const { clipboard, shell, app, BrowserWindow, Menu, MenuItem } = require('electron')
const createState = require('electron-window-state')
const yargs = require('yargs')
let main = null
yargs
.scriptName('broz')
.usage('$0 [url]')
.showHelpOnFail(false)
.alias('h', 'help')
.alias('v', 'version')
.command(
'* [url]',
'launch broz',
args => args
.positional('url', {
type: 'string',
default: 'https://github.com/antfu/broz#readme',
desc: 'launch broz with url, the http:// protocol can be omitted',
})
.option('top', {
type: 'boolean',
default: false,
desc: 'set window always on top',
})
.option('height', {
type: 'number',
default: undefined,
desc: 'set initial window height',
})
.option('width', {
type: 'number',
default: undefined,
desc: 'set initial window width',
}),
async (args) => {
app.setName('Broz')
app.on('window-all-closed', () => app.quit())
try {
await app.whenReady()
main = createMainWindow(args)
await main.loadURL(
args.url.includes('://')
? args.url
: `http://${args.url}`,
)
}
catch (e) {
console.error(e)
process.exit(1)
}
},
)
.help()
.parse()
const windowSizes = [
{ width: 1920, height: 1080 },
{ width: 1280, height: 720 },
{ width: 1024, height: 768 },
{ width: 960, height: 540 },
{ width: 640, height: 360 },
{ width: 1000, height: 1000 },
{ width: 500, height: 500 },
]
function createMainWindow(args) {
const state = createState({
defaultWidth: 960,
defaultHeight: 540,
})
const main = new BrowserWindow({
x: state.x,
y: state.y,
width: args.width ?? state.width,
height: args.height ?? state.height,
show: true,
titleBarStyle: 'hidden',
trafficLightPosition: { x: -100, y: -100 },
frame: false,
})
state.manage(main)
const debouncedSaveWindowState = debounce(
event => state.saveState(event.sender),
500,
)
main.on('resize', debouncedSaveWindowState)
main.on('move', debouncedSaveWindowState)
const menu = Menu.getApplicationMenu()
menu.insert(1, new MenuItem({
label: 'Broz',
submenu: [
{
label: 'Copy URL',
click: () => {
const win = BrowserWindow.getFocusedWindow() || main
clipboard.writeText(win.webContents.getURL())
},
},
{
label: 'Open in System Browser',
click: () => {
const win = BrowserWindow.getFocusedWindow() || main
shell.openExternal(win.webContents.getURL())
},
},
{
type: 'separator',
},
{
label: 'Resize',
submenu: windowSizes.map(({ width, height }) => ({
label: `${width} x ${height} (${getRatio(width, height)})`,
click: () => {
const win = BrowserWindow.getFocusedWindow() || main
win.setSize(width, height, true)
state.saveState(win)
},
})),
},
{
label: 'Flip Size',
click: () => {
const win = BrowserWindow.getFocusedWindow() || main
const [width, height] = win.getSize()
main.setSize(height, width)
state.saveState(win)
},
},
{
label: 'Center Window',
click: () => {
const win = BrowserWindow.getFocusedWindow() || main
main.center()
state.saveState(win)
},
},
],
}))
Menu.setApplicationMenu(menu)
configureWindow(main, args)
return main
}
/**
* @param {BrowserWindow} win
*/
function configureWindow(win, args) {
// injecting a dragable area
win.webContents.on('dom-ready', () => {
win.webContents.executeJavaScript(`;(() => {
const el = document.createElement('div')
el.id = 'injected-broz-drag'
const style = document.createElement('style')
style.innerHTML="#injected-broz-drag{position:fixed;left:10px;top:10px;width:40px;height:40px;border-radius:50%;cursor:grab;-webkit-app-region:drag;z-index:99999999;}#injected-broz-drag:hover{background:#8885;}"
document.body.appendChild(el)
document.body.appendChild(style)
const rootStyle = document.createElement('style')
rootStyle.innerHTML="::-webkit-scrollbar {display: none;}"
document.head.appendChild(rootStyle)
})()`)
})
win.webContents.setWindowOpenHandler(() => {
const [x, y] = win.getPosition()
const [width, height] = win.getSize()
return {
action: 'allow',
overrideBrowserWindowOptions: {
x: x + 50,
y: y + 50,
width,
height,
},
}
})
win.webContents.on('before-input-event', (event, input) => {
if (input.control || input.meta) {
if (input.key === ']') {
win.webContents.goForward()
event.preventDefault()
}
else if (input.key === '[') {
win.webContents.goBack()
event.preventDefault()
}
else if (input.key === '-') {
win.webContents.emit('zoom-changed', event, 'out')
event.preventDefault()
}
else if (input.key === '=') {
win.webContents.emit('zoom-changed', event, 'in')
event.preventDefault()
}
}
})
win.webContents.on('did-create-window', (win) => {
configureWindow(win, args)
})
win.webContents.on('zoom-changed', (event, zoomDirection) => {
const currentZoom = win.webContents.getZoomFactor()
if (zoomDirection === 'in')
win.webContents.zoomFactor = currentZoom + 0.15
if (zoomDirection === 'out')
win.webContents.zoomFactor = currentZoom - 0.15
})
if (args.top)
win.setAlwaysOnTop(true, 'floating')
return win
}
function debounce(fn, delay) {
let timeoutID = null
return function (...args) {
clearTimeout(timeoutID)
timeoutID = setTimeout(() => {
fn(...args)
}, delay)
}
}
function getRatio(width, height) {
const gcd = (a, b) => b ? gcd(b, a % b) : a
const r = gcd(width, height)
return `${width / r}:${height / r}`
}