perf(desktop): stop the translucency slider thrashing the main process
Dragging the intensity slider was janky, and the four frost levels looked nearly identical. Same cause. At step=1 a drag emits ~100 updates, and every one of them did four expensive things: a synchronous localStorage.setItem, an IPC wake, a synchronous fs.writeFileSync in main, and setVibrancy + setBackgroundColor on every open window. The vibrancy call is the one that also broke the frost picker — it animates over 150ms, so re-issuing it per tick restarted the animation before macOS could ever settle the material. The levels weren't indistinguishable, they were never finishing. The fix follows from a property the mode already has: under glass the intensity is a pure renderer concern. windowOpacityFor returns 1 for the whole range, so main has nothing to do on an intensity change at all. - main diffs the incoming state against the current one and passes a `changed` set to applyWindowTranslucency. An intensity-only change under glass now touches zero native properties. Crossing zero still moves the backing, since that flips glass on and off. - main's disk write is coalesced onto a 250ms trailing timer, flushed on before-quit. Only a cold launch reads that file. - the renderer paints every tick (the field has to track the hand) but coalesces the localStorage write and the IPC send onto a 120ms trailing timer, flushed on pagehide. Covered as contracts rather than timings: a table asserting exactly what each kind of update changes natively (nothing, across the whole intensity range under glass), and store tests that a six-tick drag produces one write and one IPC call while every intermediate value still paints. Both directions mutation-checked — restoring per-tick writes fails, and debouncing the paint fails too.
This commit is contained in:
committed by
brooklyn!
parent
b86db1410b
commit
f3bfa9ae53
@@ -924,6 +924,13 @@ function windowOpacity() {
|
||||
// every other state needs the opaque themed backing (anti-flash, and it is
|
||||
// what makes clear mode fade to the desktop instead of to black).
|
||||
//
|
||||
// `changed` says which native properties actually need touching. Dragging the
|
||||
// intensity slider emits ~100 updates, and in glass mode NONE of them change
|
||||
// anything native — the effect is painted by the renderer and windowOpacityFor
|
||||
// returns 1 throughout. Re-issuing setVibrancy on every tick restarts its
|
||||
// 150ms animation before macOS can settle the material, which reads as jank
|
||||
// and flattens the frost levels into each other.
|
||||
//
|
||||
// CAUTION (measured, macOS 26 / Electron 40): a runtime
|
||||
// setBackgroundColor('#00000000') is silently LOST on a window whose
|
||||
// compositor hasn't been up for a few seconds — including calls from
|
||||
@@ -931,7 +938,7 @@ function windowOpacity() {
|
||||
// rely on this path: windows are BORN with the right backing
|
||||
// (windowBackingOptions at each creation site). This path only has to cover
|
||||
// live toggles from Settings, where the window is long settled.
|
||||
function applyWindowTranslucency(win) {
|
||||
function applyWindowTranslucency(win, changed = { backing: true, material: true, opacity: true }) {
|
||||
if (!win || win.isDestroyed()) {
|
||||
return
|
||||
}
|
||||
@@ -940,18 +947,19 @@ function applyWindowTranslucency(win) {
|
||||
// Backing swap + material are scoped to registered chat windows (see
|
||||
// translucencyBackedWindows above).
|
||||
if (translucencyBackedWindows.has(win)) {
|
||||
if (typeof win.setBackgroundColor === 'function') {
|
||||
if (changed.backing && typeof win.setBackgroundColor === 'function') {
|
||||
win.setBackgroundColor(glassActive(translucencyState) ? '#00000000' : getWindowBackgroundColor())
|
||||
}
|
||||
|
||||
// Glass frost level = the vibrancy material (macOS has no blur-radius
|
||||
// knob). Animate the hop so slider-adjacent switches feel continuous.
|
||||
if (IS_MAC && typeof win.setVibrancy === 'function') {
|
||||
// knob). Animate the hop so a deliberate frost switch feels continuous —
|
||||
// which only works if we don't re-issue it on unrelated updates.
|
||||
if (changed.material && IS_MAC && typeof win.setVibrancy === 'function') {
|
||||
win.setVibrancy(vibrancyForTranslucency(translucencyState), { animationDuration: 150 })
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof win.setOpacity === 'function') {
|
||||
if (changed.opacity && typeof win.setOpacity === 'function') {
|
||||
win.setOpacity(windowOpacity())
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -13805,25 +13813,69 @@ ipcMain.on('hermes:native-theme', (_event, mode) => {
|
||||
}
|
||||
})
|
||||
|
||||
// See-through window translucency. Persist + re-apply opacity to every open
|
||||
// window at runtime (no recreation, so caching/sessions are untouched).
|
||||
// See-through window translucency. Persist + re-apply to every open window at
|
||||
// runtime (no recreation, so caching/sessions are untouched).
|
||||
//
|
||||
// The intensity slider is a HOT path: ~100 updates per drag. Two things make
|
||||
// that cheap. Native work is diffed, so an intensity-only change under glass
|
||||
// touches nothing (it's painted by the renderer). And the disk write is
|
||||
// coalesced onto a trailing timer, because writePersistedTranslucency is a
|
||||
// synchronous writeFileSync and doing one per tick blocks the main process
|
||||
// mid-drag. Only a cold launch reads that file, so it just has to be correct
|
||||
// once the hand comes off the slider.
|
||||
let translucencyWriteTimer = null
|
||||
|
||||
function scheduleTranslucencyWrite() {
|
||||
if (translucencyWriteTimer) {
|
||||
clearTimeout(translucencyWriteTimer)
|
||||
}
|
||||
|
||||
translucencyWriteTimer = setTimeout(() => {
|
||||
translucencyWriteTimer = null
|
||||
writePersistedTranslucency(translucencyState)
|
||||
}, 250)
|
||||
}
|
||||
|
||||
// Flush a pending write before the process can exit, so a quit landing inside
|
||||
// the debounce window doesn't lose the setting.
|
||||
app.on('before-quit', () => {
|
||||
if (translucencyWriteTimer) {
|
||||
clearTimeout(translucencyWriteTimer)
|
||||
translucencyWriteTimer = null
|
||||
writePersistedTranslucency(translucencyState)
|
||||
}
|
||||
})
|
||||
|
||||
ipcMain.on('hermes:translucency', (_event, payload) => {
|
||||
const next = normalizeTranslucency(payload, IS_MAC)
|
||||
const previous = translucencyState
|
||||
|
||||
if (
|
||||
next.intensity === translucencyState.intensity &&
|
||||
next.mode === translucencyState.mode &&
|
||||
next.material === translucencyState.material &&
|
||||
next.scope === translucencyState.scope
|
||||
next.intensity === previous.intensity &&
|
||||
next.mode === previous.mode &&
|
||||
next.material === previous.material &&
|
||||
next.scope === previous.scope
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
translucencyState = next
|
||||
writePersistedTranslucency(next)
|
||||
|
||||
for (const win of BrowserWindow.getAllWindows()) {
|
||||
applyWindowTranslucency(win)
|
||||
// Which native properties actually moved. `scope` is renderer-only (which
|
||||
// surfaces thin), so it never appears here.
|
||||
const changed = {
|
||||
// The backing follows whether glass is ON, not the intensity behind it.
|
||||
backing: glassActive(previous) !== glassActive(next),
|
||||
material: vibrancyForTranslucency(previous) !== vibrancyForTranslucency(next),
|
||||
opacity: windowOpacityFor(previous) !== windowOpacityFor(next)
|
||||
}
|
||||
|
||||
scheduleTranslucencyWrite()
|
||||
|
||||
if (changed.backing || changed.material || changed.opacity) {
|
||||
for (const win of BrowserWindow.getAllWindows()) {
|
||||
applyWindowTranslucency(win, changed)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -260,3 +260,48 @@ describe('windowBackingOptions', () => {
|
||||
expect(windowBackingOptions(clear(0), '#f7f7f7')).toEqual({ backgroundColor: '#f7f7f7' })
|
||||
})
|
||||
})
|
||||
|
||||
// The jank fix, as a contract: dragging the intensity slider under glass must
|
||||
// not touch ANY native property. Each tick used to re-issue setVibrancy, whose
|
||||
// 150ms animation then restarted before macOS could settle the material —
|
||||
// which is both the stutter and the reason the frost levels read alike.
|
||||
describe('what an update actually changes natively', () => {
|
||||
const nativeDiff = (previous: TranslucencyState, next: TranslucencyState) => ({
|
||||
backing: glassActive(previous) !== glassActive(next),
|
||||
material: vibrancyFor(previous) !== vibrancyFor(next),
|
||||
opacity: windowOpacityFor(previous) !== windowOpacityFor(next)
|
||||
})
|
||||
|
||||
it('is nothing at all while dragging intensity under glass', () => {
|
||||
for (let intensity = 41; intensity <= 100; intensity += 1) {
|
||||
expect(nativeDiff(glass(intensity - 1), glass(intensity)), `at ${intensity}`).toEqual({
|
||||
backing: false,
|
||||
material: false,
|
||||
opacity: false
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('is only the opacity while dragging intensity under clear', () => {
|
||||
expect(nativeDiff(clear(40), clear(41))).toEqual({ backing: false, material: false, opacity: true })
|
||||
})
|
||||
|
||||
it('is the material alone when the frost level changes', () => {
|
||||
expect(nativeDiff(glass(60, 'under-window'), glass(60, 'header'))).toEqual({
|
||||
backing: false,
|
||||
material: true,
|
||||
opacity: false
|
||||
})
|
||||
})
|
||||
|
||||
// Crossing zero flips glass on/off, which is exactly when the backing has to
|
||||
// move — the one intensity change that is NOT free.
|
||||
it('is the backing and material when glass crosses zero', () => {
|
||||
expect(nativeDiff(glass(0), glass(1))).toEqual({ backing: true, material: true, opacity: false })
|
||||
expect(nativeDiff(glass(1), glass(0))).toEqual({ backing: true, material: true, opacity: false })
|
||||
})
|
||||
|
||||
it('is everything when switching between the two modes', () => {
|
||||
expect(nativeDiff(clear(60), glass(60))).toEqual({ backing: true, material: true, opacity: true })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -84,7 +84,11 @@ describe('window translucency lever', () => {
|
||||
expect($translucency.get().mode).toBe(GLASS_SUPPORTED ? 'glass' : 'clear')
|
||||
})
|
||||
|
||||
it('persists intensity and mode together under one stable key', () => {
|
||||
// Persistence is debounced: the slider emits ~100 updates per drag and
|
||||
// localStorage.setItem is synchronous, so writing per tick janks the drag.
|
||||
// Nothing reads the stored value until a cold launch.
|
||||
it('persists intensity and mode together under one stable key, once the drag settles', () => {
|
||||
vi.useFakeTimers()
|
||||
const writes: PersistenceEvent[] = []
|
||||
|
||||
const stop = onPersistenceEvent(event => {
|
||||
@@ -94,34 +98,77 @@ describe('window translucency lever', () => {
|
||||
})
|
||||
|
||||
try {
|
||||
setTranslucency(23)
|
||||
// A drag: every intermediate value paints, none of them hit storage.
|
||||
for (let intensity = 18; intensity <= 23; intensity += 1) {
|
||||
setTranslucency(intensity)
|
||||
}
|
||||
|
||||
expect(writes).toHaveLength(0)
|
||||
|
||||
vi.advanceTimersByTime(200)
|
||||
|
||||
// One write, carrying the value the hand landed on.
|
||||
expect(writes).toHaveLength(1)
|
||||
expect(writes.at(-1)).toEqual({
|
||||
key: KEY,
|
||||
op: 'write',
|
||||
value: JSON.stringify({
|
||||
intensity: 23,
|
||||
mode: 'clear',
|
||||
material: DEFAULT_GLASS_MATERIAL,
|
||||
scope: DEFAULT_GLASS_SCOPE
|
||||
})
|
||||
})
|
||||
} finally {
|
||||
stop()
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
expect(writes.at(-1)).toEqual({
|
||||
key: KEY,
|
||||
op: 'write',
|
||||
value: JSON.stringify({
|
||||
intensity: 23,
|
||||
it('mirrors the whole state to the desktop bridge, coalesced across a drag', () => {
|
||||
vi.useFakeTimers()
|
||||
const calls: unknown[] = []
|
||||
window.hermesDesktop = { setTranslucency: (payload: unknown) => calls.push(payload) } as never
|
||||
|
||||
try {
|
||||
for (let intensity = 36; intensity <= 40; intensity += 1) {
|
||||
setTranslucency(intensity)
|
||||
}
|
||||
|
||||
// Each tick would otherwise wake the main process.
|
||||
expect(calls).toHaveLength(0)
|
||||
|
||||
vi.advanceTimersByTime(200)
|
||||
|
||||
expect(calls).toHaveLength(1)
|
||||
expect(calls.at(-1)).toEqual({
|
||||
intensity: 40,
|
||||
mode: 'clear',
|
||||
material: DEFAULT_GLASS_MATERIAL,
|
||||
scope: DEFAULT_GLASS_SCOPE
|
||||
})
|
||||
})
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('mirrors the whole state to the desktop bridge', () => {
|
||||
const calls: unknown[] = []
|
||||
window.hermesDesktop = { setTranslucency: (payload: unknown) => calls.push(payload) } as never
|
||||
// The page paint is the one thing that must NOT wait for the debounce —
|
||||
// the field has to track the hand.
|
||||
it('paints every tick even though it persists once', () => {
|
||||
vi.useFakeTimers()
|
||||
|
||||
setTranslucency(40)
|
||||
expect(calls.at(-1)).toEqual({
|
||||
intensity: 40,
|
||||
mode: 'clear',
|
||||
material: DEFAULT_GLASS_MATERIAL,
|
||||
scope: DEFAULT_GLASS_SCOPE
|
||||
})
|
||||
try {
|
||||
setTranslucencyMode('glass')
|
||||
setTranslucency(30)
|
||||
expect(keep()).toBe('70%')
|
||||
|
||||
setTranslucency(80)
|
||||
expect(keep()).toBe('20%')
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
setTranslucencyMode('clear')
|
||||
setTranslucency(TRANSLUCENCY_MIN)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -251,10 +251,40 @@ const applyGlassSurfaces = ({ intensity, mode, scope }: TranslucencyState): void
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
$translucency.subscribe(state => {
|
||||
// The intensity slider fires ~100 updates per drag, and two of the three
|
||||
// things a sync does are expensive per-tick: localStorage.setItem is
|
||||
// synchronous, and the IPC send wakes the main process. Only the page paint
|
||||
// has to keep up with the hand — nothing reads the persisted value until a
|
||||
// cold launch, and main's own work is diffed anyway.
|
||||
//
|
||||
// So: paint every tick, persist and notify on a trailing edge.
|
||||
let flushTimer: null | number = null
|
||||
|
||||
const flush = () => {
|
||||
flushTimer = null
|
||||
|
||||
const state = $translucency.get()
|
||||
|
||||
writeJson(KEY, state)
|
||||
applyGlassSurfaces(state)
|
||||
window.hermesDesktop?.setTranslucency?.(state)
|
||||
}
|
||||
|
||||
$translucency.subscribe(state => {
|
||||
applyGlassSurfaces(state)
|
||||
|
||||
if (flushTimer !== null) {
|
||||
window.clearTimeout(flushTimer)
|
||||
}
|
||||
|
||||
flushTimer = window.setTimeout(flush, 120)
|
||||
})
|
||||
|
||||
// A window closing mid-drag must not lose the setting.
|
||||
window.addEventListener('pagehide', () => {
|
||||
if (flushTimer !== null) {
|
||||
window.clearTimeout(flushTimer)
|
||||
flush()
|
||||
}
|
||||
})
|
||||
|
||||
$translucencyPeek.subscribe(count => {
|
||||
|
||||
Reference in New Issue
Block a user