-
Notifications
You must be signed in to change notification settings - Fork 0
/
vibrancy.ts
85 lines (72 loc) · 2.25 KB
/
vibrancy.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
// JS implementation of support for our mica gyp.
import { BrowserWindow, BrowserWindowConstructorOptions } from 'electron'
const { submitDwmCommand, redrawWindow } = require(__dirname + '/addon/build/Release/hwvibrancy.node')
function removeFrame(window: BrowserWindow) {
const hwnd = window.getNativeWindowHandle().readInt32LE()
const bounds = window.getBounds()
window.hide()
redrawWindow(hwnd, bounds.x, bounds.y, bounds.width, bounds.height)
window.show()
}
export const enum Background {
Automatic,
None,
Mica,
Acrylic,
TabbedMica,
}
export const enum DwmSetting {
Corner = 5,
BorderColor,
CaptionColor,
TextColor,
Frame,
}
export const enum Theme {
Auto,
Dark,
Light,
}
export const enum Corners {
Default,
Square,
Round,
SlightlyRound,
}
export type VibrantBrowserWindowConstructorOptions = Omit<BrowserWindowConstructorOptions, 'transparent' | 'backgroundColor'> & {
effect?: Background | DwmSetting
theme?: Theme | Corners
}
export class VibrantBrowserWindow extends BrowserWindow {
private _lastTheme: VibrantBrowserWindowConstructorOptions['theme']
private _lastEffect: VibrantBrowserWindowConstructorOptions['effect']
setVisualEffect(params: VibrantBrowserWindowConstructorOptions['effect'], value: VibrantBrowserWindowConstructorOptions['theme']) {
const hwnd = this.getNativeWindowHandle().readInt32LE()
submitDwmCommand(hwnd, params!.valueOf(), value!.valueOf())
this._lastEffect = params
this._lastTheme = value
}
constructor(options: VibrantBrowserWindowConstructorOptions) {
super({
...options,
transparent: false,
frame: false,
backgroundColor: '#000000ff',
})
;(this._lastEffect = options.effect ?? Background.Mica), (this._lastTheme = options.theme ?? Theme.Auto)
let interval: NodeJS.Timer | undefined
let frameHasBeenRemoved = false
const applyEffect = () => {
this.setVisualEffect(this._lastEffect, this._lastTheme)
}
this.on('show', () => {
if (!frameHasBeenRemoved) {
frameHasBeenRemoved = true // To prevent an inf loop.
interval = setInterval(applyEffect, 60)
removeFrame(this)
}
applyEffect()
})
this.on('closed', () => interval !== undefined && clearInterval(interval))
}
}