Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(glass): judge panel text against the panel, and decide once per colour
iceclatterWT30a34472db78
diff --git a/src/renderer/terminal/glass-cell-backgrounds.test.ts b/src/renderer/terminal/glass-cell-backgrounds.test.tsindex 95b230ba7..75ad1e5e1 100644--- a/src/renderer/terminal/glass-cell-backgrounds.test.ts+++ b/src/renderer/terminal/glass-cell-backgrounds.test.ts@@ -91,6 +91,23 @@ describe('glassPanelFill: a lift or sink of the glass, never a second slab of tifor (const t of [0.2, 0.675, 0.95]) expect(glassPanelFill(grey(224), DARK, t, [BLACK])).toBeNull()})+ // Polarity is judged against the PANEL: #303030 is lighter than nodeterm-dark's #1e1e1e, yet it is+ // dark text on this bar. Judged by the theme, the bar went translucent at worst contrast 1.00.+ it('dark-grey text on a light bar (dark theme) keeps the bar opaque', () => {+ for (const t of [0.2, 0.675, 0.725, 0.95]) {+ for (const bar of [grey(0xd0), grey(0xe4)]) {+ for (const text of [grey(0x30), grey(0x3a)]) expect(glassPanelFill(bar, DARK, t, [text]), `t=${t}`).toBeNull()+ }+ }+ })++ it.each(['nodeterm-light', 'catppuccin-latte'])('light-grey text on a dark bar (%s) keeps the bar opaque', (id) => {+ const theme = resolveTerminalTheme(id).theme+ const light = { bg: parseHex(theme.background!)!, fg: parseHex(theme.foreground!)! }+ const readable = glassTintAlpha(theme.foreground!, theme.background!)+ for (const t of [0.2, readable, 0.95]) expect(glassPanelFill(grey(0x30), light, t, [grey(0xe4)]), `t=${t}`).toBeNull()+ })+it('decoration under 3:1 on the opaque panel (a dim prompt chevron) does not force it opaque', () => {expect(glassPanelFill(grey(58), DARK, 0.675, [grey(78)])).not.toBeNull()})@@ -161,20 +178,20 @@ type FakeCell = { bg: number; fg?: number; ch?: string }/** A structural fake of the addon's RectangleRenderer, doing what the real `_updateRectangle`* does with the attribute array (colour, then `$a = 1`). One class per test: the wrap patches the* prototype, like it does the real shared one. */-function fakeAddon(cells: Record<number, FakeCell>) {+function fakeAddon(cells: Record<number, FakeCell>, rows: Record<number, Record<number, FakeCell>> = {}) {class RectangleRenderer {_themeService = { colors: { background: { rgba: 0x1e1e1e00 }, foreground: { rgba: 0xe6e6e6ff }, ansi: [] } }_terminal = {buffer: {active: {viewportY: 0,- getNullCell: () => ({ bg: 0, fg: 0, getChars: () => '' }),- getLine: () => ({- getCell: (x: number, cell: { bg: number; fg: number; getChars: () => string }) => {- const c = cells[x] ?? { bg: 0 }+ getNullCell: () => ({ bg: 0, fg: 0, getCode: () => 0 }),+ getLine: (row: number) => ({+ getCell: (x: number, cell: { bg: number; fg: number; getCode: () => number }) => {+ const c = (rows[row] ?? cells)[x] ?? { bg: 0 }cell.bg = c.bgcell.fg = c.fg ?? 0- cell.getChars = () => c.ch ?? ''+ cell.getCode = () => c.ch?.codePointAt(0) ?? 0return cell}})@@ -189,9 +206,9 @@ function fakeAddon(cells: Record<number, FakeCell>) {}const rr = new RectangleRenderer()const addon = { _renderer: { _rectangleRenderer: { value: rr } } } as unknown as WebglAddon- const draw = (fg: number, bg: number, x = 0, endX = x + 1): number[] => {+ const draw = (fg: number, bg: number, x = 0, endX = x + 1, y = 0): number[] => {const v = { attributes: new Float32Array(8) }- rr._updateRectangle(v, 0, fg, bg, x, endX, 0)+ rr._updateRectangle(v, 0, fg, bg, x, endX, y)return Array.from(v.attributes)}return { addon, rr, draw }@@ -222,6 +239,32 @@ describe('installGlassCellBackgrounds', () => {expect(draw(0, bar, 0, 2)[7]).toBe(1)})+ it('decides once per panel colour: a multi-row light box does not stripe, and opaque sticks', () => {+ const bar = CM_RGB | 0xe0e0e0+ const blank = { 0: { bg: bar }, 1: { bg: bar } }+ const texted = { 0: { bg: bar }, 1: { bg: bar, fg: CM_RGB | 0x000000, ch: 'x' } }+ const { addon, rr, draw } = fakeAddon({}, { 0: blank, 1: texted, 2: blank })+ installGlassCellBackgrounds(addon)+ setGlassCellAlpha(rr._terminal, 0.675)+ expect(draw(0, bar, 0, 2, 0)[7]).toBeLessThan(1) // blank row, before the box's text was seen+ expect(draw(0, bar, 0, 2, 1)[7]).toBe(1) // dark text: the colour turns opaque…+ expect(draw(0, bar, 0, 2, 2)[7]).toBe(1) // …for every other row of the box+ expect(draw(0, bar, 0, 2, 0)[7]).toBe(1) // and the first row on the next rebuild+ setGlassCellAlpha(rr._terminal, 0.7) // a slider move starts over+ expect(draw(0, bar, 0, 2, 0)[7]).toBeLessThan(1)+ })++ it('an exception after the original leaves the stock rectangle instead of breaking the frame', () => {+ const { addon, rr, draw } = fakeAddon({ 0: { bg: panel } })+ installGlassCellBackgrounds(addon)+ setGlassCellAlpha(rr._terminal, 0.675)+ rr._terminal.buffer.active.getLine = () => {+ throw new Error('renamed internals')+ }+ expect(() => draw(0, panel)).not.toThrow()+ expect(draw(0, panel)[7]).toBe(1)+ })+it('non-glass terminals are byte-identical to the stock renderer', () => {const stock = fakeAddon({ 0: { bg: panel } })const wrapped = fakeAddon({ 0: { bg: panel } })diff --git a/src/renderer/terminal/glass-cell-backgrounds.ts b/src/renderer/terminal/glass-cell-backgrounds.tsindex 5ce88d95c..e3ec7a270 100644--- a/src/renderer/terminal/glass-cell-backgrounds.ts+++ b/src/renderer/terminal/glass-cell-backgrounds.ts@@ -102,12 +102,8 @@ const TEXT_MIN_CONTRAST = 3* 4.5:1. Left of the tick (no guarantee there, as on the plain glass) it slides toward pure white in* proportion to how far the glass already is from 4.5:1, so it is continuous at the tick.*- * Every text colour on the run is then checked (glyphs under `TEXT_MIN_CONTRAST` on the opaque- * panel are decoration, e.g. a dim prompt chevron):- * - text of INVERTED polarity (dark text on a dark theme — the panel is what makes it readable,- * e.g. a light bar) must keep min(4.5, its contrast on the opaque panel) over ANY backdrop;- * - other text, while the guarantee is on, must fare no worse than on the plain glass around it.- * A panel that fails returns null: opaque, exactly the app's own panel.+ * Every text colour on the run is then checked (`panelTextOk`); a panel that fails returns null:+ * opaque, exactly the app's own panel.** Returns the fill (0..255 colour, alpha) or null for opaque. `alpha` 0 = draw nothing.*/@@ -117,6 +113,14 @@ export function glassPanelFill(t: number,texts: readonly Rgb[]): { rgb: Rgb; alpha: number } | null {+ const fill = panelFill(c, theme, t)+ return fill && texts.every((f) => panelTextOk(f, c, fill, theme, t)) ? fill : null+}++type PanelFill = { rgb: Rgb; alpha: number }++/** The panel's lift/sink before any text is considered; null only under Reduce Transparency. */+function panelFill(c: Rgb, theme: { bg: Rgb; fg: Rgb }, t: number): PanelFill | null {if (t >= 1) return null // Reduce Transparency: the app's panels, opaqueconst [lc, ac, bc] = oklab(c)const [lb, ab, bb] = oklab(theme.bg)@@ -124,39 +128,44 @@ export function glassPanelFill(if (dist < PANEL_DEAD_ZONE) return { rgb: c, alpha: 0 }const alpha = Math.min(PANEL_ALPHA_MAX, Math.max(PANEL_ALPHA_MIN, PANEL_K * dist))const glassWorst = worstContrast(theme.fg, theme.bg, t)- const guaranteed = glassWorst >= 4.5const fgLight = relativeLuminance(theme.fg) > relativeLuminance(theme.bg)- let rgb: Rgb- if (Math.hypot(ac, bc) > PANEL_CHROMA_MIN) {- rgb = c- } else {- const lift = lc > lb- const extreme: Rgb = lift ? [255, 255, 255] : [0, 0, 0]- if (lift !== fgLight) {- rgb = extreme- } else {- const ceiling = composite(theme.bg, extreme, t)- const toward = guaranteed ? 0 : Math.min(1, (4.5 - glassWorst) / 3.5)- rgb = composite(extreme, ceiling, toward)- }- }+ if (Math.hypot(ac, bc) > PANEL_CHROMA_MIN) return { rgb: c, alpha }+ const lift = lc > lb+ const extreme: Rgb = lift ? [255, 255, 255] : [0, 0, 0]+ if (lift !== fgLight) return { rgb: extreme, alpha }+ const ceiling = composite(theme.bg, extreme, t)+ const toward = glassWorst >= 4.5 ? 0 : Math.min(1, (4.5 - glassWorst) / 3.5)+ return { rgb: composite(extreme, ceiling, toward), alpha }+}++/**+ * Does text colour `f` stay readable on panel `c` drawn as `fill`? Glyphs under+ * `TEXT_MIN_CONTRAST` on the opaque panel are decoration (e.g. a dim prompt chevron) and pass.+ * - text of INVERTED polarity — on the other side of the PANEL from the theme foreground (dark+ * text on a light bar in a dark theme): the panel is what makes it readable, so it must keep+ * min(4.5, its contrast on the opaque panel) over ANY backdrop;+ * - other text, while the guarantee is on, must fare no worse than on the plain glass around it.+ * Polarity is judged against the panel, not the theme background: #303030 text is "lighter than+ * #1e1e1e" yet dark on a #e4e4e4 bar, and judging it by the theme let that bar go translucent+ * (worst contrast 1.00).+ */+function panelTextOk(f: Rgb, c: Rgb, fill: PanelFill, theme: { bg: Rgb; fg: Rgb }, t: number): boolean {+ if (fill.alpha === 0) return true // drawn as nothing: the plain glass+ const own = contrastRatio(f, c)+ if (own < TEXT_MIN_CONTRAST) return true+ const guaranteed = worstContrast(theme.fg, theme.bg, t) >= 4.5+ const fgLight = relativeLuminance(theme.fg) > relativeLuminance(theme.bg)+ const lf = relativeLuminance(f)+ const inverted = lf > relativeLuminance(c) !== fgLight+ if (!inverted && !guaranteed) return true// Over backdrop X the node shows G(X) = B·t + X·(1−t); the panel paints rgb·alpha over it. Both// are affine in X, so the composite's luminance range is spanned by X = black and X = white.- const lo = composite(rgb, composite(theme.bg, [0, 0, 0], t), alpha)- const hi = composite(rgb, composite(theme.bg, [255, 255, 255], t), alpha)+ const lo = composite(fill.rgb, composite(theme.bg, [0, 0, 0], t), fill.alpha)+ const hi = composite(fill.rgb, composite(theme.bg, [255, 255, 255], t), fill.alpha)const [llo, lhi] = [relativeLuminance(lo), relativeLuminance(hi)].sort((x, y) => x - y)- const lbg = relativeLuminance(theme.bg)- for (const f of texts) {- const own = contrastRatio(f, c)- if (own < TEXT_MIN_CONTRAST) continue- const lf = relativeLuminance(f)- const inverted = lf > lbg !== fgLight- if (!inverted && !guaranteed) continue- const worst = lf > llo && lf < lhi ? 1 : Math.min(contrastRatio(f, lo), contrastRatio(f, hi))- const need = inverted ? Math.min(4.5, own) : Math.min(own, worstContrast(f, theme.bg, t))- if (worst < need - 1e-9) return null- }- return { rgb, alpha }+ const worst = lf > llo && lf < lhi ? 1 : Math.min(contrastRatio(f, lo), contrastRatio(f, hi))+ const need = inverted ? Math.min(4.5, own) : Math.min(own, worstContrast(f, theme.bg, t))+ return worst >= need - 1e-9}/** The node's glass alpha per terminal; absent = not glass = stock rendering. */@@ -166,12 +175,32 @@ const cellAlpha = new WeakMap<Terminal, number>()* model rebuild (`term.clearTextureAtlas()`), since backgrounds are only recomputed for changed* cells. */export function setGlassCellAlpha(term: Terminal, alpha: number | null): boolean {+ panelVerdicts.delete(term)if (alpha === null) return cellAlpha.delete(term)if (cellAlpha.get(term) === alpha) return falsecellAlpha.set(term, alpha)return true}+/**+ * One verdict per PANEL COLOUR per terminal, not per run: addon-webgl rebuilds every row's+ * rectangles on any cell change (each cursor blink included), and deciding each row alone striped a+ * multi-row light box (rows with dark text opaque, the blank rows of the same box lifted). The fill+ * is computed once; each text colour is checked once; a failure makes the colour opaque for good —+ * the rows drawn before it catch up on the next rebuild. Dropped whenever the slider alpha or the+ * theme's bg/fg changes.+ * ponytail: an OSC 4 palette-only change keeps the old verdicts until the next alpha/theme change;+ * key on the ANSI table too if an app is ever seen recolouring its panels that way.+ */+interface PanelVerdicts {+ key: string+ panels: Map<number, { fill: PanelFill | null; ok: Set<number> }>+}+const panelVerdicts = new WeakMap<Terminal, PanelVerdicts>()++/** The colour part of a packed fg/bg word (mode + palette index or RGB), flags stripped. */+const COLOR_BITS = CM_MASK | 0xffffff+type Vertices = { attributes: Float32Array }type UpdateRectangle = (v: Vertices,@@ -191,7 +220,7 @@ interface RectangleRendererLike {interface CellLike {bg?: numberfg?: number- getChars?(): string+ getCode?(): number}/** Kept on the prototype, not in module state, so a hot-reloaded copy of this module re-wraps the@@ -223,28 +252,27 @@ export function installGlassCellBackgrounds(addon: WebglAddon): boolean {if (typeof orig !== 'function') return falseproto[ORIGINAL] = origlet scratch: CellLike | undefined- proto._updateRectangle = function (- this: RectangleRendererLike,- v: Vertices,+ // The part after the original, for glass terminals only. `a` is read by the caller AFTER the+ // original, which may have grown the array.+ const glassRectangle = (+ rr: RectangleRendererLike,+ a: Float32Array,offset: number,fg: number,bg: number,startX: number,endX: number,- y: number- ): void {- orig.call(this, v, offset, fg, bg, startX, endX, y)- const t = cellAlpha.get(this._terminal)- if (t === undefined) return- const colors = this._themeService?.colors+ y: number,+ t: number+ ): void => {+ const colors = rr._themeService?.colorsconst back = colors?.background?.rgbaconst fore = colors?.foreground?.rgbaif (back === undefined || fore === undefined) return- const a = v.attributes // re-read: the original may have grown the arraylet line: { getCell(x: number, cell: never): unknown } | undefinedlet cellBg = NaN // unknown = treated as a renderer override (stock opaque)if ((bg & CM_MASK) !== 0 && !(fg & FG_INVERSE)) {- const buf = this._terminal.buffer.active+ const buf = rr._terminal.buffer.activescratch ??= buf.getNullCell() as unknown as CellLikeline = buf.getLine(buf.viewportY + y)line?.getCell(startX, scratch as never)@@ -256,28 +284,52 @@ export function installGlassCellBackgrounds(addon: WebglAddon): boolean {if (kind < 1) writeFill(a, offset, rgbOf(back), kind)return}+ const key = `${t}|${back}|${fore}`+ let verdicts = panelVerdicts.get(rr._terminal)+ if (verdicts?.key !== key) panelVerdicts.set(rr._terminal, (verdicts = { key, panels: new Map() }))+ const run: Rgb = [a[offset + 4] * 255, a[offset + 5] * 255, a[offset + 6] * 255]+ const theme = { bg: rgbOf(back), fg: rgbOf(fore) }+ let panel = verdicts.panels.get(bg & COLOR_BITS)+ if (!panel) verdicts.panels.set(bg & COLOR_BITS, (panel = { fill: panelFill(run, theme, t), ok: new Set() }))// The run's text colours: a run spans cells of one bg but any fg, and the fg the renderer- // passes is only the first cell's.- const texts: Rgb[] = []- let lastFg = -1- for (let x = startX; x < endX && line && scratch; x++) {+ // passes is only the first cell's. Each colour is checked once per panel colour.+ for (let x = startX; x < endX && panel.fill && line && scratch; x++) {line.getCell(x, scratch as never)const f = scratch.fg- const ch = scratch.getChars?.() ?? ''- if (typeof f !== 'number' || f === lastFg || ch === '' || ch === ' ') continue- lastFg = f+ const code = scratch.getCode?.() ?? 0+ if (typeof f !== 'number' || code === 0 || code === 32 || panel.ok.has(f & COLOR_BITS)) continueconst mode = f & CM_MASK- texts.push(+ const text =mode === CM_RGB? rgbOf((f & 0xffffff) << 8): (mode === CM_P16 || mode === CM_P256) && colors?.ansi?.[f & 0xff]? rgbOf(colors.ansi[f & 0xff].rgba): rgbOf(fore)- )+ if (panelTextOk(text, run, panel.fill, theme, t)) panel.ok.add(f & COLOR_BITS)+ else panel.fill = null // opaque sticks for this colour+ }+ if (panel.fill) writeFill(a, offset, panel.fill.rgb, panel.fill.alpha)+ }+ proto._updateRectangle = function (+ this: RectangleRendererLike,+ v: Vertices,+ offset: number,+ fg: number,+ bg: number,+ startX: number,+ endX: number,+ y: number+ ): void {+ orig.call(this, v, offset, fg, bg, startX, endX, y)+ const t = cellAlpha.get(this._terminal)+ if (t === undefined) return+ // Fail open per call too: an exception here would otherwise break every frame. The original+ // already wrote the stock rectangle, so returning leaves exactly that.+ try {+ glassRectangle(this, v.attributes, offset, fg, bg, startX, endX, y, t)+ } catch {+ /* stock rectangle */}- const run: Rgb = [a[offset + 4] * 255, a[offset + 5] * 255, a[offset + 6] * 255]- const fill = glassPanelFill(run, { bg: rgbOf(back), fg: rgbOf(fore) }, t, texts)- if (fill) writeFill(a, offset, fill.rgb, fill.alpha)}return true} catch {