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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/7404-the-3d-view-s-grid-keeps-the-old-theme-s → main 0 commits fork 3 threads · 3 unresolved

  • Merged. torque/7404-the-3d-view-s-grid-keeps-the-old-theme-s was merged into main as 005552a.

Review

Sign in to review this change.
5 files changed +91 −2
src/web/client.ts +4 −1
@@ -1438,7 +1438,10 @@ const shellCore = /* js */ `
14381438 var root = doc.documentElement;
14391439 root.classList.add('theme-anim');
14401440 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);
14421445 // The menu's select and the settings gallery agree on what is worn.
14431446 doc.querySelectorAll('.theme-form select[name=theme]').forEach(function (o) { o.value = value; });
14441447 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 {
3939 } from "three";
4040 import { OrbitControls } from "three/addons/controls/OrbitControls.js";
4141 import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
42 import { recolourGrid } from "./grid-ink.ts";
4243 import { REFUSED_URI, withinModelDir } from "./gltf-uri.ts";
4344 import { parse3mf } from "./threemf.ts";
4445 import { daeFromText } from "./collada.ts";
@@ -65,6 +66,7 @@ interface Element {
6566 className: string;
6667 readonly style: { left: string; top: string };
6768 addEventListener(type: string, listener: (event: PointerLike) => void, capture?: boolean): void;
69 removeEventListener(type: string, listener: (event: PointerLike) => void): void;
6870 append(...nodes: Element[]): void;
6971 focus(): void;
7072 replaceChildren(...nodes: Element[]): void;
@@ -85,9 +87,10 @@ interface Browser {
8587 readonly devicePixelRatio: number;
8688 readonly location: { readonly href: string };
8789 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 };
8891 ResizeObserver: new (callback: () => void) => { observe(element: Element): void; disconnect(): void };
8992 }
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;
9194
9295 interface Loaded {
9396 readonly object: Object3D;
@@ -214,6 +217,8 @@ function mount(figure: Element): void {
214217 row.hidden = false;
215218 };
216219
220 // Set once the grid exists; undoes the theme listeners when the figure goes.
221 let unlisten = (): void => {};
217222 load(src, format).then(
218223 ({ object, triangles, box, rows, overCap }) => {
219224 if (!figure.isConnected) return renderer.dispose();
@@ -247,6 +252,20 @@ function mount(figure: Element): void {
247252 const step = niceStep(span * 2);
248253 const extent = Math.ceil((span * 2) / step) * step;
249254 const grid = new GridHelper(extent, Math.round(extent / step), ink, ink);
outdated

Bug: GridHelper's colours live in its vertex-colour attribute, and material.color only 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.

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;
outdated

These listeners are never removed, so every mounted viewer leaves two behind. Remove them when the figure leaves the page.

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 };
250269 grid.material.transparent = true;
251270 grid.material.opacity = 0.2;
252271 // Just under the part, so its bottom face never flickers with the grid.
@@ -394,6 +413,7 @@ function mount(figure: Element): void {
394413 live.push({
395414 figure,
396415 dispose() {
416 unlisten();
397417 observer.disconnect();
398418 controls.dispose();
399419 renderer.dispose();
src/web/render/vendor/three.d.ts +8
@@ -39,7 +39,11 @@ declare module "three" {
3939 }
4040 export class Color {
4141 constructor(color: string | number);
42 r: number;
43 g: number;
44 b: number;
4245 getHex(): number;
46 set(color: string | number): this;
4347 }
4448 export class Box3 {
4549 min: Vector3;
@@ -76,6 +80,8 @@ declare module "three" {
7680 export const DoubleSide: number;
7781 export class BufferAttribute {
7882 count: number;
83 array: ArrayLike<number> & { [index: number]: number };
84 needsUpdate: boolean;
7985 }
8086 export class Float32BufferAttribute extends BufferAttribute {
8187 constructor(array: Float32Array, itemSize: number);
@@ -84,6 +90,7 @@ declare module "three" {
8490 index: BufferAttribute | null;
8591 setAttribute(name: string, attribute: BufferAttribute): this;
8692 getAttribute(name: string): BufferAttribute | undefined;
93 attributes: { color?: BufferAttribute };
8794 computeVertexNormals(): void;
8895 setFromPoints(points: Vector3[]): this;
8996 }
@@ -106,6 +113,7 @@ declare module "three" {
106113 }
107114 export class GridHelper extends Object3D {
108115 constructor(size: number, divisions: number, center: Color, grid: Color);
116 geometry: BufferGeometry;
109117 material: Material;
110118 }
111119 export class AxesHelper extends Object3D {
test/render-css-client.test.ts +31
@@ -7,6 +7,7 @@
77
88 import { describe, expect, test } from "bun:test";
99 import { readFileSync } from "node:fs";
10 import { recolourGrid } from "../src/web/render/grid-ink.ts";
1011 import { gcodeCss } from "../src/web/render/gcode-css.ts";
1112 import { model3dCss } from "../src/web/render/model3d-css.ts";
1213 import { model3dAsset } from "../src/web/render/model3d.ts";
@@ -67,4 +68,34 @@ describe("model3d-client", () => {
6768 expect(code).not.toMatch(/import\s*\(?\s*["']https?:/);
6869 expect(code).not.toMatch(/from\s*["']https?:/);
6970 });
71
outdated

A string grep does not show the grid changes colour. It would pass with the multiplier bug. Test the recolour's effect on the grid's colours.

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 });
70101 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs