dsh-notify-tray 0.1.12 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +3 -3
- package/README.md +3 -3
- package/README.zh-CN.md +3 -3
- package/assets/mascot-peeking.prompt.txt +6 -6
- package/assets/notify-icon.png +0 -0
- package/assets/tray-darwin.js +283 -283
- package/cordis.patch.yml +6 -6
- package/lib/ToastActivator.cs +33 -0
- package/lib/catalog.js +448 -448
- package/lib/client.js +18 -90
- package/lib/format.js +284 -284
- package/lib/host/context.js +11 -3
- package/lib/host/notify.js +26 -21
- package/lib/host/routes.js +9 -42
- package/lib/host/spend.js +190 -190
- package/lib/host/tray.js +120 -120
- package/lib/icon.js +159 -159
- package/lib/index.js +2 -7
- package/lib/ledger.js +285 -285
- package/lib/models-dev.json +2684 -2684
- package/lib/notices.js +3 -4
- package/lib/pricing.js +302 -302
- package/lib/quota.js +290 -290
- package/lib/system.js +11 -22
- package/lib/tracker.js +250 -250
- package/lib/tray/index.js +142 -142
- package/lib/tray/linux.js +393 -393
- package/lib/voice.js +76 -76
- package/lib/whale.js +396 -396
- package/lib/windows-toast.js +92 -0
- package/locale/en.json +7 -7
- package/locale/zh.json +7 -7
- package/package.json +2 -1
package/lib/host/tray.js
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The status-bar whale: keeps the per-platform tray helper running while the
|
|
3
|
-
* `tray` setting asks for it, feeds it views, and turns its menu clicks into
|
|
4
|
-
* actions. Windows has no status bar to live in (and the desktop app keeps a
|
|
5
|
-
* tray icon of its own): there the plugin is notifications only.
|
|
6
|
-
*/
|
|
7
|
-
import { openUrl, showApp } from '../system.js'
|
|
8
|
-
import { startTray } from '../tray/index.js'
|
|
9
|
-
import { iconSpec } from '../whale.js'
|
|
10
|
-
|
|
11
|
-
/** Platforms with a status bar the whale can live in. */
|
|
12
|
-
export const TRAY_PLATFORMS = new Set(['darwin', 'linux', 'freebsd'])
|
|
13
|
-
|
|
14
|
-
export function setupTray(ctx, host) {
|
|
15
|
-
const { cfg, logger, tracker, ledger } = host
|
|
16
|
-
let tray
|
|
17
|
-
let refreshTimer
|
|
18
|
-
host.refresh = () => {
|
|
19
|
-
if (refreshTimer) return
|
|
20
|
-
// Coalesce bursts (streaming usage samples) into one tray update.
|
|
21
|
-
refreshTimer = setTimeout(() => {
|
|
22
|
-
refreshTimer = undefined
|
|
23
|
-
try { tray?.update(host.view()) } catch (error) { logger.warn('更新状态栏失败', error) }
|
|
24
|
-
}, 300)
|
|
25
|
-
}
|
|
26
|
-
tracker.onChange(host.refresh)
|
|
27
|
-
ledger.onChange(host.refresh)
|
|
28
|
-
// "已运行 n 分钟" in the menu ticks even when nothing else changes.
|
|
29
|
-
ctx.effect(() => {
|
|
30
|
-
const timer = setInterval(() => { if (tracker.summary().running > 0) host.refresh() }, 60_000)
|
|
31
|
-
return () => clearInterval(timer)
|
|
32
|
-
}, 'notify-tray: minute tick')
|
|
33
|
-
|
|
34
|
-
host.open = () => showApp({ webOrigin: host.webOrigin }).catch(() => false)
|
|
35
|
-
host.onAction = action => {
|
|
36
|
-
if (action === 'show-app') return void host.open()
|
|
37
|
-
if (action === 'usage-page' && host.account.usageUrl) return void openUrl(host.account.usageUrl)
|
|
38
|
-
if (action === 'refresh-quota') return void host.pollQuota(true)
|
|
39
|
-
if (action.startsWith('url:')) return void openUrl(action.slice(4))
|
|
40
|
-
if (action === 'hide-tray') {
|
|
41
|
-
// Hidden for this run only: the settings page offers to bring it back.
|
|
42
|
-
trayHidden = true
|
|
43
|
-
host.syncTray()
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
if (action.startsWith('open:')) {
|
|
47
|
-
host.focusQueue.push(action.slice(5))
|
|
48
|
-
host.open()
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// The icon follows the `tray` setting live, and "隐藏状态栏图标" in its menu
|
|
53
|
-
// hides it until the settings page (or a restart) brings it back.
|
|
54
|
-
let trayHidden = false
|
|
55
|
-
let trayStarting
|
|
56
|
-
let trayDisposed = false
|
|
57
|
-
/** A start that failed (no D-Bus, no tray host…) is not retried until the setting or the page asks again. */
|
|
58
|
-
let trayFailed = false
|
|
59
|
-
const trayWanted = () => Boolean(cfg.tray) && !trayHidden && !trayDisposed && TRAY_PLATFORMS.has(process.platform)
|
|
60
|
-
const stopTray = () => {
|
|
61
|
-
const t = tray
|
|
62
|
-
tray = undefined
|
|
63
|
-
return t?.stop()
|
|
64
|
-
}
|
|
65
|
-
host.syncTray = () => {
|
|
66
|
-
if (trayStarting) return trayStarting
|
|
67
|
-
if (!trayWanted()) return stopTray()
|
|
68
|
-
if (tray) return undefined
|
|
69
|
-
trayStarting = startTray({ stateDir: host.dir, icon: iconSpec(), onAction: host.onAction, logger })
|
|
70
|
-
.then(t => {
|
|
71
|
-
if (!trayWanted()) return void t.stop()
|
|
72
|
-
tray = t
|
|
73
|
-
tray.update(host.view())
|
|
74
|
-
})
|
|
75
|
-
.catch(error => {
|
|
76
|
-
trayFailed = true
|
|
77
|
-
logger.warn('状态栏图标启动失败,只保留通知功能', error)
|
|
78
|
-
})
|
|
79
|
-
.finally(() => {
|
|
80
|
-
trayStarting = undefined
|
|
81
|
-
// The setting may have flipped while the helper was starting.
|
|
82
|
-
if (!trayFailed && Boolean(tray) !== trayWanted()) host.syncTray()
|
|
83
|
-
})
|
|
84
|
-
return trayStarting
|
|
85
|
-
}
|
|
86
|
-
/** The settings page's "show the whale again": undoes the tray menu's hide. */
|
|
87
|
-
host.showTrayAgain = () => {
|
|
88
|
-
trayHidden = false
|
|
89
|
-
trayFailed = false
|
|
90
|
-
return host.syncTray()
|
|
91
|
-
}
|
|
92
|
-
host.trayStatus = () => ({
|
|
93
|
-
supported: TRAY_PLATFORMS.has(process.platform),
|
|
94
|
-
enabled: Boolean(cfg.tray),
|
|
95
|
-
hidden: trayHidden,
|
|
96
|
-
shown: Boolean(tray),
|
|
97
|
-
failed: trayFailed,
|
|
98
|
-
})
|
|
99
|
-
ctx.effect(() => {
|
|
100
|
-
trayDisposed = false
|
|
101
|
-
host.syncTray()
|
|
102
|
-
// Settings fields are read live; a cheap check picks up a flipped toggle without a restart.
|
|
103
|
-
let enabled = Boolean(cfg.tray)
|
|
104
|
-
const timer = setInterval(() => {
|
|
105
|
-
if (Boolean(cfg.tray) !== enabled) {
|
|
106
|
-
enabled = Boolean(cfg.tray)
|
|
107
|
-
trayFailed = false
|
|
108
|
-
// Switching the setting back on also undoes a hide from the tray menu.
|
|
109
|
-
if (enabled) trayHidden = false
|
|
110
|
-
}
|
|
111
|
-
if (!trayFailed && !trayStarting && Boolean(tray) !== trayWanted()) host.syncTray()
|
|
112
|
-
}, 2000)
|
|
113
|
-
return () => {
|
|
114
|
-
trayDisposed = true
|
|
115
|
-
clearInterval(timer)
|
|
116
|
-
clearTimeout(refreshTimer)
|
|
117
|
-
stopTray()
|
|
118
|
-
}
|
|
119
|
-
}, 'notify-tray: tray')
|
|
120
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The status-bar whale: keeps the per-platform tray helper running while the
|
|
3
|
+
* `tray` setting asks for it, feeds it views, and turns its menu clicks into
|
|
4
|
+
* actions. Windows has no status bar to live in (and the desktop app keeps a
|
|
5
|
+
* tray icon of its own): there the plugin is notifications only.
|
|
6
|
+
*/
|
|
7
|
+
import { openUrl, showApp } from '../system.js'
|
|
8
|
+
import { startTray } from '../tray/index.js'
|
|
9
|
+
import { iconSpec } from '../whale.js'
|
|
10
|
+
|
|
11
|
+
/** Platforms with a status bar the whale can live in. */
|
|
12
|
+
export const TRAY_PLATFORMS = new Set(['darwin', 'linux', 'freebsd'])
|
|
13
|
+
|
|
14
|
+
export function setupTray(ctx, host) {
|
|
15
|
+
const { cfg, logger, tracker, ledger } = host
|
|
16
|
+
let tray
|
|
17
|
+
let refreshTimer
|
|
18
|
+
host.refresh = () => {
|
|
19
|
+
if (refreshTimer) return
|
|
20
|
+
// Coalesce bursts (streaming usage samples) into one tray update.
|
|
21
|
+
refreshTimer = setTimeout(() => {
|
|
22
|
+
refreshTimer = undefined
|
|
23
|
+
try { tray?.update(host.view()) } catch (error) { logger.warn('更新状态栏失败', error) }
|
|
24
|
+
}, 300)
|
|
25
|
+
}
|
|
26
|
+
tracker.onChange(host.refresh)
|
|
27
|
+
ledger.onChange(host.refresh)
|
|
28
|
+
// "已运行 n 分钟" in the menu ticks even when nothing else changes.
|
|
29
|
+
ctx.effect(() => {
|
|
30
|
+
const timer = setInterval(() => { if (tracker.summary().running > 0) host.refresh() }, 60_000)
|
|
31
|
+
return () => clearInterval(timer)
|
|
32
|
+
}, 'notify-tray: minute tick')
|
|
33
|
+
|
|
34
|
+
host.open = () => showApp({ webOrigin: host.webOrigin }).catch(() => false)
|
|
35
|
+
host.onAction = action => {
|
|
36
|
+
if (action === 'show-app') return void host.open()
|
|
37
|
+
if (action === 'usage-page' && host.account.usageUrl) return void openUrl(host.account.usageUrl)
|
|
38
|
+
if (action === 'refresh-quota') return void host.pollQuota(true)
|
|
39
|
+
if (action.startsWith('url:')) return void openUrl(action.slice(4))
|
|
40
|
+
if (action === 'hide-tray') {
|
|
41
|
+
// Hidden for this run only: the settings page offers to bring it back.
|
|
42
|
+
trayHidden = true
|
|
43
|
+
host.syncTray()
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
if (action.startsWith('open:')) {
|
|
47
|
+
host.focusQueue.push(action.slice(5))
|
|
48
|
+
host.open()
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The icon follows the `tray` setting live, and "隐藏状态栏图标" in its menu
|
|
53
|
+
// hides it until the settings page (or a restart) brings it back.
|
|
54
|
+
let trayHidden = false
|
|
55
|
+
let trayStarting
|
|
56
|
+
let trayDisposed = false
|
|
57
|
+
/** A start that failed (no D-Bus, no tray host…) is not retried until the setting or the page asks again. */
|
|
58
|
+
let trayFailed = false
|
|
59
|
+
const trayWanted = () => Boolean(cfg.tray) && !trayHidden && !trayDisposed && TRAY_PLATFORMS.has(process.platform)
|
|
60
|
+
const stopTray = () => {
|
|
61
|
+
const t = tray
|
|
62
|
+
tray = undefined
|
|
63
|
+
return t?.stop()
|
|
64
|
+
}
|
|
65
|
+
host.syncTray = () => {
|
|
66
|
+
if (trayStarting) return trayStarting
|
|
67
|
+
if (!trayWanted()) return stopTray()
|
|
68
|
+
if (tray) return undefined
|
|
69
|
+
trayStarting = startTray({ stateDir: host.dir, icon: iconSpec(), onAction: host.onAction, logger })
|
|
70
|
+
.then(t => {
|
|
71
|
+
if (!trayWanted()) return void t.stop()
|
|
72
|
+
tray = t
|
|
73
|
+
tray.update(host.view())
|
|
74
|
+
})
|
|
75
|
+
.catch(error => {
|
|
76
|
+
trayFailed = true
|
|
77
|
+
logger.warn('状态栏图标启动失败,只保留通知功能', error)
|
|
78
|
+
})
|
|
79
|
+
.finally(() => {
|
|
80
|
+
trayStarting = undefined
|
|
81
|
+
// The setting may have flipped while the helper was starting.
|
|
82
|
+
if (!trayFailed && Boolean(tray) !== trayWanted()) host.syncTray()
|
|
83
|
+
})
|
|
84
|
+
return trayStarting
|
|
85
|
+
}
|
|
86
|
+
/** The settings page's "show the whale again": undoes the tray menu's hide. */
|
|
87
|
+
host.showTrayAgain = () => {
|
|
88
|
+
trayHidden = false
|
|
89
|
+
trayFailed = false
|
|
90
|
+
return host.syncTray()
|
|
91
|
+
}
|
|
92
|
+
host.trayStatus = () => ({
|
|
93
|
+
supported: TRAY_PLATFORMS.has(process.platform),
|
|
94
|
+
enabled: Boolean(cfg.tray),
|
|
95
|
+
hidden: trayHidden,
|
|
96
|
+
shown: Boolean(tray),
|
|
97
|
+
failed: trayFailed,
|
|
98
|
+
})
|
|
99
|
+
ctx.effect(() => {
|
|
100
|
+
trayDisposed = false
|
|
101
|
+
host.syncTray()
|
|
102
|
+
// Settings fields are read live; a cheap check picks up a flipped toggle without a restart.
|
|
103
|
+
let enabled = Boolean(cfg.tray)
|
|
104
|
+
const timer = setInterval(() => {
|
|
105
|
+
if (Boolean(cfg.tray) !== enabled) {
|
|
106
|
+
enabled = Boolean(cfg.tray)
|
|
107
|
+
trayFailed = false
|
|
108
|
+
// Switching the setting back on also undoes a hide from the tray menu.
|
|
109
|
+
if (enabled) trayHidden = false
|
|
110
|
+
}
|
|
111
|
+
if (!trayFailed && !trayStarting && Boolean(tray) !== trayWanted()) host.syncTray()
|
|
112
|
+
}, 2000)
|
|
113
|
+
return () => {
|
|
114
|
+
trayDisposed = true
|
|
115
|
+
clearInterval(timer)
|
|
116
|
+
clearTimeout(refreshTimer)
|
|
117
|
+
stopTray()
|
|
118
|
+
}
|
|
119
|
+
}, 'notify-tray: tray')
|
|
120
|
+
}
|
package/lib/icon.js
CHANGED
|
@@ -1,159 +1,159 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Software rasterizer for the Linux tray icon: the whale from lib/whale.js
|
|
3
|
-
* filled with the state colour, emitted as StatusNotifierItem IconPixmap data
|
|
4
|
-
* (ARGB32, network byte order), plus RGBA / PNG for the notification icon.
|
|
5
|
-
* No image libraries needed.
|
|
6
|
-
*
|
|
7
|
-
* Polygons are the flattened bezier outlines, filled by scanline with the
|
|
8
|
-
* nonzero winding rule at 4×4 supersampling, so the eye and cap band holes
|
|
9
|
-
* come out and edges stay smooth. It runs on the host's main thread, so it
|
|
10
|
-
* has to be quick: a 48 px frame takes about a millisecond.
|
|
11
|
-
*/
|
|
12
|
-
import { deflateSync } from 'node:zlib'
|
|
13
|
-
import { BOUNDS, STATE_RGB, flatten, whalePaths } from './whale.js'
|
|
14
|
-
|
|
15
|
-
export { STATE_RGB }
|
|
16
|
-
|
|
17
|
-
const SS = 4
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @param {number} size - square edge in pixels.
|
|
21
|
-
* @param {string} state - idle | running | attention | error | done.
|
|
22
|
-
* @param {number} [frame] - animation frame for states that animate.
|
|
23
|
-
* @returns {Buffer} ARGB32 pixels, row-major.
|
|
24
|
-
*/
|
|
25
|
-
export function renderIcon(size, state, frame = 0) {
|
|
26
|
-
const [r, g, b] = STATE_RGB[state] ?? STATE_RGB.idle
|
|
27
|
-
const polys = flatten(whalePaths(STATE_RGB[state] ? state : 'idle', frame), 8)
|
|
28
|
-
// Fit the whale's bounds into the square, centred; work in subsample units.
|
|
29
|
-
const scale = Math.min(size / BOUNDS.w, size / BOUNDS.h)
|
|
30
|
-
const ox = (size - BOUNDS.w * scale) / 2
|
|
31
|
-
const oy = (size - BOUNDS.h * scale) / 2
|
|
32
|
-
const edges = [] // x0, y0, x1, y1, direction — horizontal edges dropped
|
|
33
|
-
for (const poly of polys) {
|
|
34
|
-
for (let i = 0, n = poly.length; i < n; i += 1) {
|
|
35
|
-
const [ax, ay] = poly[i]
|
|
36
|
-
const [bx, by] = poly[(i + 1) % n]
|
|
37
|
-
const y0 = (oy + (ay - BOUNDS.y) * scale) * SS
|
|
38
|
-
const y1 = (oy + (by - BOUNDS.y) * scale) * SS
|
|
39
|
-
if (y0 === y1) continue
|
|
40
|
-
edges.push((ox + (ax - BOUNDS.x) * scale) * SS, y0, (ox + (bx - BOUNDS.x) * scale) * SS, y1, y1 > y0 ? 1 : -1)
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const cover = new Uint16Array(size * size)
|
|
45
|
-
const rows = size * SS
|
|
46
|
-
const xs = []
|
|
47
|
-
const dirs = []
|
|
48
|
-
const order = []
|
|
49
|
-
for (let sy = 0; sy < rows; sy += 1) {
|
|
50
|
-
const y = sy + 0.5
|
|
51
|
-
xs.length = 0
|
|
52
|
-
dirs.length = 0
|
|
53
|
-
for (let e = 0; e < edges.length; e += 5) {
|
|
54
|
-
const y0 = edges[e + 1]
|
|
55
|
-
const y1 = edges[e + 3]
|
|
56
|
-
// Half-open rule: an edge crosses the scanline when exactly one endpoint is above it.
|
|
57
|
-
if ((y0 <= y) === (y1 <= y)) continue
|
|
58
|
-
xs.push(edges[e] + ((y - y0) * (edges[e + 2] - edges[e])) / (y1 - y0))
|
|
59
|
-
dirs.push(edges[e + 4])
|
|
60
|
-
}
|
|
61
|
-
if (xs.length < 2) continue
|
|
62
|
-
order.length = xs.length
|
|
63
|
-
for (let i = 0; i < xs.length; i += 1) order[i] = i
|
|
64
|
-
order.sort((p, q) => xs[p] - xs[q])
|
|
65
|
-
const row = (sy / SS) | 0
|
|
66
|
-
let wind = 0
|
|
67
|
-
for (let i = 0; i < order.length - 1; i += 1) {
|
|
68
|
-
wind += dirs[order[i]]
|
|
69
|
-
if (wind === 0) continue
|
|
70
|
-
// Subsample columns whose centre (sx + 0.5) lies inside [xa, xb).
|
|
71
|
-
const from = Math.max(0, Math.ceil(xs[order[i]] - 0.5))
|
|
72
|
-
const to = Math.min(rows, Math.ceil(xs[order[i + 1]] - 0.5))
|
|
73
|
-
for (let sx = from; sx < to; sx += 1) cover[row * size + ((sx / SS) | 0)] += 1
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const out = Buffer.alloc(size * size * 4)
|
|
78
|
-
for (let p = 0; p < size * size; p += 1) {
|
|
79
|
-
out[p * 4] = Math.round((cover[p] / (SS * SS)) * 255)
|
|
80
|
-
out[p * 4 + 1] = r
|
|
81
|
-
out[p * 4 + 2] = g
|
|
82
|
-
out[p * 4 + 3] = b
|
|
83
|
-
}
|
|
84
|
-
return out
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Quick ASCII dump for tests and eyeballing: '#' where the whale is opaque. */
|
|
88
|
-
export function asciiIcon(size, state, frame = 0) {
|
|
89
|
-
const px = renderIcon(size, state, frame)
|
|
90
|
-
const rows = []
|
|
91
|
-
for (let y = 0; y < size; y += 1) {
|
|
92
|
-
let row = ''
|
|
93
|
-
for (let x = 0; x < size; x += 1) {
|
|
94
|
-
const a = px[(y * size + x) * 4]
|
|
95
|
-
row += a > 160 ? '#' : a > 60 ? '+' : ' '
|
|
96
|
-
}
|
|
97
|
-
rows.push(row)
|
|
98
|
-
}
|
|
99
|
-
return rows.join('\n')
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** RGBA pixels (straight alpha, row-major), the layout freedesktop's `image-data` hint wants. */
|
|
103
|
-
export function rgbaIcon(size, state, frame = 0) {
|
|
104
|
-
const argb = renderIcon(size, state, frame)
|
|
105
|
-
const out = Buffer.alloc(argb.length)
|
|
106
|
-
for (let p = 0; p < argb.length; p += 4) {
|
|
107
|
-
out[p] = argb[p + 1]
|
|
108
|
-
out[p + 1] = argb[p + 2]
|
|
109
|
-
out[p + 2] = argb[p + 3]
|
|
110
|
-
out[p + 3] = argb[p]
|
|
111
|
-
}
|
|
112
|
-
return out
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const CRC_TABLE = (() => {
|
|
116
|
-
const table = new Uint32Array(256)
|
|
117
|
-
for (let n = 0; n < 256; n += 1) {
|
|
118
|
-
let c = n
|
|
119
|
-
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
|
|
120
|
-
table[n] = c >>> 0
|
|
121
|
-
}
|
|
122
|
-
return table
|
|
123
|
-
})()
|
|
124
|
-
|
|
125
|
-
function crc32(buffer) {
|
|
126
|
-
let c = 0xffffffff
|
|
127
|
-
for (const byte of buffer) c = CRC_TABLE[(c ^ byte) & 0xff] ^ (c >>> 8)
|
|
128
|
-
return (c ^ 0xffffffff) >>> 0
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function chunk(type, data) {
|
|
132
|
-
const head = Buffer.alloc(8)
|
|
133
|
-
head.writeUInt32BE(data.length, 0)
|
|
134
|
-
head.write(type, 4, 'ascii')
|
|
135
|
-
const crc = Buffer.alloc(4)
|
|
136
|
-
crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), data])), 0)
|
|
137
|
-
return Buffer.concat([head, data, crc])
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** The whale as a PNG file's bytes (for notification servers that only take an icon path). */
|
|
141
|
-
export function pngIcon(size, state, frame = 0) {
|
|
142
|
-
const rgba = rgbaIcon(size, state, frame)
|
|
143
|
-
const raw = Buffer.alloc((size * 4 + 1) * size)
|
|
144
|
-
for (let y = 0; y < size; y += 1) {
|
|
145
|
-
raw[y * (size * 4 + 1)] = 0 // filter: none
|
|
146
|
-
rgba.copy(raw, y * (size * 4 + 1) + 1, y * size * 4, (y + 1) * size * 4)
|
|
147
|
-
}
|
|
148
|
-
const ihdr = Buffer.alloc(13)
|
|
149
|
-
ihdr.writeUInt32BE(size, 0)
|
|
150
|
-
ihdr.writeUInt32BE(size, 4)
|
|
151
|
-
ihdr[8] = 8 // bit depth
|
|
152
|
-
ihdr[9] = 6 // RGBA
|
|
153
|
-
return Buffer.concat([
|
|
154
|
-
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
155
|
-
chunk('IHDR', ihdr),
|
|
156
|
-
chunk('IDAT', deflateSync(raw)),
|
|
157
|
-
chunk('IEND', Buffer.alloc(0)),
|
|
158
|
-
])
|
|
159
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Software rasterizer for the Linux tray icon: the whale from lib/whale.js
|
|
3
|
+
* filled with the state colour, emitted as StatusNotifierItem IconPixmap data
|
|
4
|
+
* (ARGB32, network byte order), plus RGBA / PNG for the notification icon.
|
|
5
|
+
* No image libraries needed.
|
|
6
|
+
*
|
|
7
|
+
* Polygons are the flattened bezier outlines, filled by scanline with the
|
|
8
|
+
* nonzero winding rule at 4×4 supersampling, so the eye and cap band holes
|
|
9
|
+
* come out and edges stay smooth. It runs on the host's main thread, so it
|
|
10
|
+
* has to be quick: a 48 px frame takes about a millisecond.
|
|
11
|
+
*/
|
|
12
|
+
import { deflateSync } from 'node:zlib'
|
|
13
|
+
import { BOUNDS, STATE_RGB, flatten, whalePaths } from './whale.js'
|
|
14
|
+
|
|
15
|
+
export { STATE_RGB }
|
|
16
|
+
|
|
17
|
+
const SS = 4
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {number} size - square edge in pixels.
|
|
21
|
+
* @param {string} state - idle | running | attention | error | done.
|
|
22
|
+
* @param {number} [frame] - animation frame for states that animate.
|
|
23
|
+
* @returns {Buffer} ARGB32 pixels, row-major.
|
|
24
|
+
*/
|
|
25
|
+
export function renderIcon(size, state, frame = 0) {
|
|
26
|
+
const [r, g, b] = STATE_RGB[state] ?? STATE_RGB.idle
|
|
27
|
+
const polys = flatten(whalePaths(STATE_RGB[state] ? state : 'idle', frame), 8)
|
|
28
|
+
// Fit the whale's bounds into the square, centred; work in subsample units.
|
|
29
|
+
const scale = Math.min(size / BOUNDS.w, size / BOUNDS.h)
|
|
30
|
+
const ox = (size - BOUNDS.w * scale) / 2
|
|
31
|
+
const oy = (size - BOUNDS.h * scale) / 2
|
|
32
|
+
const edges = [] // x0, y0, x1, y1, direction — horizontal edges dropped
|
|
33
|
+
for (const poly of polys) {
|
|
34
|
+
for (let i = 0, n = poly.length; i < n; i += 1) {
|
|
35
|
+
const [ax, ay] = poly[i]
|
|
36
|
+
const [bx, by] = poly[(i + 1) % n]
|
|
37
|
+
const y0 = (oy + (ay - BOUNDS.y) * scale) * SS
|
|
38
|
+
const y1 = (oy + (by - BOUNDS.y) * scale) * SS
|
|
39
|
+
if (y0 === y1) continue
|
|
40
|
+
edges.push((ox + (ax - BOUNDS.x) * scale) * SS, y0, (ox + (bx - BOUNDS.x) * scale) * SS, y1, y1 > y0 ? 1 : -1)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const cover = new Uint16Array(size * size)
|
|
45
|
+
const rows = size * SS
|
|
46
|
+
const xs = []
|
|
47
|
+
const dirs = []
|
|
48
|
+
const order = []
|
|
49
|
+
for (let sy = 0; sy < rows; sy += 1) {
|
|
50
|
+
const y = sy + 0.5
|
|
51
|
+
xs.length = 0
|
|
52
|
+
dirs.length = 0
|
|
53
|
+
for (let e = 0; e < edges.length; e += 5) {
|
|
54
|
+
const y0 = edges[e + 1]
|
|
55
|
+
const y1 = edges[e + 3]
|
|
56
|
+
// Half-open rule: an edge crosses the scanline when exactly one endpoint is above it.
|
|
57
|
+
if ((y0 <= y) === (y1 <= y)) continue
|
|
58
|
+
xs.push(edges[e] + ((y - y0) * (edges[e + 2] - edges[e])) / (y1 - y0))
|
|
59
|
+
dirs.push(edges[e + 4])
|
|
60
|
+
}
|
|
61
|
+
if (xs.length < 2) continue
|
|
62
|
+
order.length = xs.length
|
|
63
|
+
for (let i = 0; i < xs.length; i += 1) order[i] = i
|
|
64
|
+
order.sort((p, q) => xs[p] - xs[q])
|
|
65
|
+
const row = (sy / SS) | 0
|
|
66
|
+
let wind = 0
|
|
67
|
+
for (let i = 0; i < order.length - 1; i += 1) {
|
|
68
|
+
wind += dirs[order[i]]
|
|
69
|
+
if (wind === 0) continue
|
|
70
|
+
// Subsample columns whose centre (sx + 0.5) lies inside [xa, xb).
|
|
71
|
+
const from = Math.max(0, Math.ceil(xs[order[i]] - 0.5))
|
|
72
|
+
const to = Math.min(rows, Math.ceil(xs[order[i + 1]] - 0.5))
|
|
73
|
+
for (let sx = from; sx < to; sx += 1) cover[row * size + ((sx / SS) | 0)] += 1
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const out = Buffer.alloc(size * size * 4)
|
|
78
|
+
for (let p = 0; p < size * size; p += 1) {
|
|
79
|
+
out[p * 4] = Math.round((cover[p] / (SS * SS)) * 255)
|
|
80
|
+
out[p * 4 + 1] = r
|
|
81
|
+
out[p * 4 + 2] = g
|
|
82
|
+
out[p * 4 + 3] = b
|
|
83
|
+
}
|
|
84
|
+
return out
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Quick ASCII dump for tests and eyeballing: '#' where the whale is opaque. */
|
|
88
|
+
export function asciiIcon(size, state, frame = 0) {
|
|
89
|
+
const px = renderIcon(size, state, frame)
|
|
90
|
+
const rows = []
|
|
91
|
+
for (let y = 0; y < size; y += 1) {
|
|
92
|
+
let row = ''
|
|
93
|
+
for (let x = 0; x < size; x += 1) {
|
|
94
|
+
const a = px[(y * size + x) * 4]
|
|
95
|
+
row += a > 160 ? '#' : a > 60 ? '+' : ' '
|
|
96
|
+
}
|
|
97
|
+
rows.push(row)
|
|
98
|
+
}
|
|
99
|
+
return rows.join('\n')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** RGBA pixels (straight alpha, row-major), the layout freedesktop's `image-data` hint wants. */
|
|
103
|
+
export function rgbaIcon(size, state, frame = 0) {
|
|
104
|
+
const argb = renderIcon(size, state, frame)
|
|
105
|
+
const out = Buffer.alloc(argb.length)
|
|
106
|
+
for (let p = 0; p < argb.length; p += 4) {
|
|
107
|
+
out[p] = argb[p + 1]
|
|
108
|
+
out[p + 1] = argb[p + 2]
|
|
109
|
+
out[p + 2] = argb[p + 3]
|
|
110
|
+
out[p + 3] = argb[p]
|
|
111
|
+
}
|
|
112
|
+
return out
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const CRC_TABLE = (() => {
|
|
116
|
+
const table = new Uint32Array(256)
|
|
117
|
+
for (let n = 0; n < 256; n += 1) {
|
|
118
|
+
let c = n
|
|
119
|
+
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
|
|
120
|
+
table[n] = c >>> 0
|
|
121
|
+
}
|
|
122
|
+
return table
|
|
123
|
+
})()
|
|
124
|
+
|
|
125
|
+
function crc32(buffer) {
|
|
126
|
+
let c = 0xffffffff
|
|
127
|
+
for (const byte of buffer) c = CRC_TABLE[(c ^ byte) & 0xff] ^ (c >>> 8)
|
|
128
|
+
return (c ^ 0xffffffff) >>> 0
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function chunk(type, data) {
|
|
132
|
+
const head = Buffer.alloc(8)
|
|
133
|
+
head.writeUInt32BE(data.length, 0)
|
|
134
|
+
head.write(type, 4, 'ascii')
|
|
135
|
+
const crc = Buffer.alloc(4)
|
|
136
|
+
crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), data])), 0)
|
|
137
|
+
return Buffer.concat([head, data, crc])
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The whale as a PNG file's bytes (for notification servers that only take an icon path). */
|
|
141
|
+
export function pngIcon(size, state, frame = 0) {
|
|
142
|
+
const rgba = rgbaIcon(size, state, frame)
|
|
143
|
+
const raw = Buffer.alloc((size * 4 + 1) * size)
|
|
144
|
+
for (let y = 0; y < size; y += 1) {
|
|
145
|
+
raw[y * (size * 4 + 1)] = 0 // filter: none
|
|
146
|
+
rgba.copy(raw, y * (size * 4 + 1) + 1, y * size * 4, (y + 1) * size * 4)
|
|
147
|
+
}
|
|
148
|
+
const ihdr = Buffer.alloc(13)
|
|
149
|
+
ihdr.writeUInt32BE(size, 0)
|
|
150
|
+
ihdr.writeUInt32BE(size, 4)
|
|
151
|
+
ihdr[8] = 8 // bit depth
|
|
152
|
+
ihdr[9] = 6 // RGBA
|
|
153
|
+
return Buffer.concat([
|
|
154
|
+
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
155
|
+
chunk('IHDR', ihdr),
|
|
156
|
+
chunk('IDAT', deflateSync(raw)),
|
|
157
|
+
chunk('IEND', Buffer.alloc(0)),
|
|
158
|
+
])
|
|
159
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -45,13 +45,8 @@ export const Config = z.object({
|
|
|
45
45
|
notifyApproval: z.boolean().default(true).description('需要批准工具调用时通知').volatile(),
|
|
46
46
|
notifyChoice: z.boolean().default(true).description('需要你做选择时通知').volatile(),
|
|
47
47
|
notifyInput: z.boolean().default(true).description('需要你输入文字时通知').volatile(),
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
z.const('client').description('只由 DSH 窗口弹'),
|
|
51
|
-
z.const('system').description('只由系统弹(适合 dsh web / 命令行)'),
|
|
52
|
-
z.const('both').description('两边都弹'),
|
|
53
|
-
z.const('off').description('不弹通知(macOS / Linux 上只看状态栏)'),
|
|
54
|
-
]).default('auto').description('通知通道').volatile(),
|
|
48
|
+
// Read the old master mute until the user explicitly enables an event type.
|
|
49
|
+
channel: z.string().default('system').hidden().volatile(),
|
|
55
50
|
suppressWhenFocused: z.boolean().default(true).description('正在看这个会话时不弹').volatile(),
|
|
56
51
|
sound: z.boolean().default(true).description('系统通知带提示音').volatile(),
|
|
57
52
|
tray: z.boolean().default(true).description('显示状态栏图标(macOS / Linux,立即生效;Windows 没有状态栏,忽略此项)').volatile(),
|