Public: anyone can read this; the crew can change it.
The 3D view's grid keeps the old theme's colour after a theme switch #17000
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/7404-the-3d-view-s-grid-keeps-the-old-theme-s → main 0 commits fork 3 threads · 3 unresolved
5 files
changed +91
−2
src/web/client.ts +4 −1
| @@ -1438,7 +1438,10 @@ const shellCore = /* js */ ` | ||
| 1438 | 1438 | var root = doc.documentElement; |
| 1439 | 1439 | root.classList.add('theme-anim'); |
| 1440 | 1440 | if (value === 'system') root.removeAttribute('data-theme'); else root.setAttribute('data-theme', value); |
| 1441 | window.setTimeout(function () { root.classList.remove('theme-anim'); }, 260); | |
| 1441 | window.setTimeout(function () { root.classList.remove('theme-anim'); | |
| 1442 | // Anything drawn from the page's colours once (the 3D grid) re-reads them now the fade is over. | |
| 1443 | doc.dispatchEvent(new CustomEvent('forge-theme')); | |
| 1444 | }, 260); | |
| 1442 | 1445 | // The menu's select and the settings gallery agree on what is worn. |
| 1443 | 1446 | doc.querySelectorAll('.theme-form select[name=theme]').forEach(function (o) { o.value = value; }); |
| 1444 | 1447 | doc.querySelectorAll('.theme-form button[name=theme]').forEach(function (o) { o.setAttribute('aria-pressed', o.value === value ? 'true' : 'false'); }); |
src/web/render/grid-ink.ts added +27
| @@ -0,0 +1,27 @@ | ||
| 1 | /** | |
| 2 | * The 3D grid's ink. three's GridHelper bakes its two colours into the geometry's vertex-colour | |
| 3 | * attribute when it is built; `material.color` only multiplies them. So a theme switch writes the | |
| 4 | * new ink into that attribute, and flags it for upload. | |
| 5 | */ | |
| 6 | ||
| 7 | /** The parts of a GridHelper this touches. */ | |
| 8 | export interface InkedGrid { | |
| 9 | readonly geometry: { | |
| 10 | readonly attributes: { readonly color?: { readonly array: { [index: number]: number; readonly length: number }; needsUpdate: boolean } }; | |
| 11 | }; | |
| 12 | } | |
| 13 | ||
| 14 | /** | |
| 15 | * Write `ink` (linear r, g, b in 0..1, as three's Color holds them) into every vertex of the grid. | |
| 16 | */ | |
| 17 | export function recolourGrid(grid: InkedGrid, ink: { readonly r: number; readonly g: number; readonly b: number }): void { | |
| 18 | const color = grid.geometry.attributes.color; | |
| 19 | if (color === undefined) return; | |
| 20 | const a = color.array; | |
| 21 | for (let i = 0; i + 2 < a.length; i += 3) { | |
| 22 | a[i] = ink.r; | |
| 23 | a[i + 1] = ink.g; | |
| 24 | a[i + 2] = ink.b; | |
| 25 | } | |
| 26 | color.needsUpdate = true; | |
| 27 | } |
src/web/render/model3d-client.ts +21 −1
| @@ -39,6 +39,7 @@ import { | ||
| 39 | 39 | } from "three"; |
| 40 | 40 | import { OrbitControls } from "three/addons/controls/OrbitControls.js"; |
| 41 | 41 | import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js"; |
| 42 | import { recolourGrid } from "./grid-ink.ts"; | |
| 42 | 43 | import { REFUSED_URI, withinModelDir } from "./gltf-uri.ts"; |
| 43 | 44 | import { parse3mf } from "./threemf.ts"; |
| 44 | 45 | import { daeFromText } from "./collada.ts"; |
| @@ -65,6 +66,7 @@ interface Element { | ||
| 65 | 66 | className: string; |
| 66 | 67 | readonly style: { left: string; top: string }; |
| 67 | 68 | addEventListener(type: string, listener: (event: PointerLike) => void, capture?: boolean): void; |
| 69 | removeEventListener(type: string, listener: (event: PointerLike) => void): void; | |
| 68 | 70 | append(...nodes: Element[]): void; |
| 69 | 71 | focus(): void; |
| 70 | 72 | replaceChildren(...nodes: Element[]): void; |
| @@ -85,9 +87,10 @@ interface Browser { | ||
| 85 | 87 | readonly devicePixelRatio: number; |
| 86 | 88 | readonly location: { readonly href: string }; |
| 87 | 89 | getComputedStyle(element: Element): { color: string; getPropertyValue(name: string): string }; |
| 90 | matchMedia?(query: string): { addEventListener(type: string, listener: () => void): void; removeEventListener(type: string, listener: () => void): void }; | |
| 88 | 91 | ResizeObserver: new (callback: () => void) => { observe(element: Element): void; disconnect(): void }; |
| 89 | 92 | } |
| 90 | const { document, devicePixelRatio, location, getComputedStyle, ResizeObserver } = globalThis as unknown as Browser; | |
| 93 | const { document, devicePixelRatio, location, getComputedStyle, matchMedia, ResizeObserver } = globalThis as unknown as Browser; | |
| 91 | 94 | |
| 92 | 95 | interface Loaded { |
| 93 | 96 | readonly object: Object3D; |
| @@ -214,6 +217,8 @@ function mount(figure: Element): void { | ||
| 214 | 217 | row.hidden = false; |
| 215 | 218 | }; |
| 216 | 219 | |
| 220 | // Set once the grid exists; undoes the theme listeners when the figure goes. | |
| 221 | let unlisten = (): void => {}; | |
| 217 | 222 | load(src, format).then( |
| 218 | 223 | ({ object, triangles, box, rows, overCap }) => { |
| 219 | 224 | if (!figure.isConnected) return renderer.dispose(); |
| @@ -247,6 +252,20 @@ function mount(figure: Element): void { | ||
| 247 | 252 | const step = niceStep(span * 2); |
| 248 | 253 | const extent = Math.ceil((span * 2) / step) * step; |
| 249 | 254 | const grid = new GridHelper(extent, Math.round(extent / step), ink, ink); |
| 255 | // A theme switch recolours the page at once; the grid follows with one frame. The shell | |
| 256 | // announces its own choice as "forge-theme"; "system" follows prefers-color-scheme. | |
| 257 | const retheme = (): void => { | |
| 258 | if (!figure.isConnected) return; | |
| 259 | recolourGrid(grid, new Color(getComputedStyle(figure).color)); | |
| 260 | draw(); | |
| 261 | }; | |
| 262 | document.addEventListener("forge-theme", retheme); | |
| 263 | const scheme = matchMedia?.("(prefers-color-scheme: dark)"); | |
| 264 | scheme?.addEventListener("change", retheme); | |
| 265 | unlisten = (): void => { | |
| 266 | document.removeEventListener("forge-theme", retheme); | |
| 267 | scheme?.removeEventListener("change", retheme); | |
| 268 | }; | |
| 250 | 269 | grid.material.transparent = true; |
| 251 | 270 | grid.material.opacity = 0.2; |
| 252 | 271 | // Just under the part, so its bottom face never flickers with the grid. |
| @@ -394,6 +413,7 @@ function mount(figure: Element): void { | ||
| 394 | 413 | live.push({ |
| 395 | 414 | figure, |
| 396 | 415 | dispose() { |
| 416 | unlisten(); | |
| 397 | 417 | observer.disconnect(); |
| 398 | 418 | controls.dispose(); |
| 399 | 419 | renderer.dispose(); |
src/web/render/vendor/three.d.ts +8
| @@ -39,7 +39,11 @@ declare module "three" { | ||
| 39 | 39 | } |
| 40 | 40 | export class Color { |
| 41 | 41 | constructor(color: string | number); |
| 42 | r: number; | |
| 43 | g: number; | |
| 44 | b: number; | |
| 42 | 45 | getHex(): number; |
| 46 | set(color: string | number): this; | |
| 43 | 47 | } |
| 44 | 48 | export class Box3 { |
| 45 | 49 | min: Vector3; |
| @@ -76,6 +80,8 @@ declare module "three" { | ||
| 76 | 80 | export const DoubleSide: number; |
| 77 | 81 | export class BufferAttribute { |
| 78 | 82 | count: number; |
| 83 | array: ArrayLike<number> & { [index: number]: number }; | |
| 84 | needsUpdate: boolean; | |
| 79 | 85 | } |
| 80 | 86 | export class Float32BufferAttribute extends BufferAttribute { |
| 81 | 87 | constructor(array: Float32Array, itemSize: number); |
| @@ -84,6 +90,7 @@ declare module "three" { | ||
| 84 | 90 | index: BufferAttribute | null; |
| 85 | 91 | setAttribute(name: string, attribute: BufferAttribute): this; |
| 86 | 92 | getAttribute(name: string): BufferAttribute | undefined; |
| 93 | attributes: { color?: BufferAttribute }; | |
| 87 | 94 | computeVertexNormals(): void; |
| 88 | 95 | setFromPoints(points: Vector3[]): this; |
| 89 | 96 | } |
| @@ -106,6 +113,7 @@ declare module "three" { | ||
| 106 | 113 | } |
| 107 | 114 | export class GridHelper extends Object3D { |
| 108 | 115 | constructor(size: number, divisions: number, center: Color, grid: Color); |
| 116 | geometry: BufferGeometry; | |
| 109 | 117 | material: Material; |
| 110 | 118 | } |
| 111 | 119 | export class AxesHelper extends Object3D { |
test/render-css-client.test.ts +31
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | |
| 8 | 8 | import { describe, expect, test } from "bun:test"; |
| 9 | 9 | import { readFileSync } from "node:fs"; |
| 10 | import { recolourGrid } from "../src/web/render/grid-ink.ts"; | |
| 10 | 11 | import { gcodeCss } from "../src/web/render/gcode-css.ts"; |
| 11 | 12 | import { model3dCss } from "../src/web/render/model3d-css.ts"; |
| 12 | 13 | import { model3dAsset } from "../src/web/render/model3d.ts"; |
| @@ -67,4 +68,34 @@ describe("model3d-client", () => { | ||
| 67 | 68 | expect(code).not.toMatch(/import\s*\(?\s*["']https?:/); |
| 68 | 69 | expect(code).not.toMatch(/from\s*["']https?:/); |
| 69 | 70 | }); |
| 71 | ||
| 72 | test("recolouring a real GridHelper leaves every vertex the new ink, not old x new (#7404)", async () => { | |
| 73 | // The vendored build, which the bundle is made from. | |
| 74 | const { Color, GridHelper } = (await import(new URL("../src/web/render/vendor/three/build/three.module.js", import.meta.url).href)) as unknown as { | |
| 75 | Color: typeof import("three").Color; | |
| 76 | GridHelper: typeof import("three").GridHelper; | |
| 77 | }; | |
| 78 | const old = new Color("#202020"); | |
| 79 | const next = new Color("#e8e8e8"); | |
| 80 | const grid = new GridHelper(10, 10, old, old); | |
| 81 | const colour = grid.geometry.attributes.color!; | |
| 82 | expect(colour.array[0]).toBeCloseTo(old.r, 6); | |
| 83 | const before = (colour as unknown as { version: number }).version; | |
| 84 | recolourGrid(grid, next); | |
| 85 | for (let i = 0; i < colour.array.length; i += 3) { | |
| 86 | expect(colour.array[i]).toBeCloseTo(next.r, 6); | |
| 87 | expect(colour.array[i + 1]).toBeCloseTo(next.g, 6); | |
| 88 | expect(colour.array[i + 2]).toBeCloseTo(next.b, 6); | |
| 89 | } | |
| 90 | // needsUpdate is write-only in three: it bumps `version`, which is what the renderer compares. | |
| 91 | expect((colour as unknown as { version: number }).version).toBeGreaterThan(before); | |
| 92 | }); | |
| 93 | ||
| 94 | test("the shell announces a theme switch, and the viewer listens and lets go (#7404)", async () => { | |
| 95 | const code = (await model3dAsset())!.code; | |
| 96 | expect(code).toContain("forge-theme"); | |
| 97 | expect(code).toContain("removeEventListener"); | |
| 98 | const shell = readFileSync(new URL("../src/web/client.ts", import.meta.url), "utf8"); | |
| 99 | expect(shell).toContain("new CustomEvent('forge-theme')"); | |
| 100 | }); | |
| 70 | 101 | }); |
new above
offline
Bug: GridHelper's colours live in its vertex-colour attribute, and
material.coloronly multiplies them. Setting it here gives old x new, not the new ink. Rewrite the colour attribute (or rebuild the grid) with the new ink. Principle 4 (one text colour) is not honoured on a switch.