Skip to content

Commit 7550753

Browse files
committed
Fix WASM startup in Trunk
1 parent efd53ae commit 7550753

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

vizmat-app/index.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@
55
const pickerInput = document.getElementById("picker-keyboard-input");
66
if (!loader) return;
77

8+
const viewportState = {
9+
width: window.innerWidth,
10+
height: window.innerHeight,
11+
};
12+
let pickerOpen = false;
13+
14+
const applyViewportSizing = () => {
15+
const width = window.innerWidth;
16+
const height = window.innerHeight;
17+
const shouldUpdate =
18+
!pickerOpen || height >= viewportState.height || width !== viewportState.width;
19+
20+
if (shouldUpdate) {
21+
viewportState.width = width;
22+
viewportState.height = height;
23+
24+
const root = document.documentElement;
25+
const desktopCanvasHeight = Math.min(height * 0.8, 900);
26+
const mobileCanvasHeight = Math.min(height * 0.82, 900);
27+
28+
root.style.setProperty("--app-viewport-height", `${height}px`);
29+
root.style.setProperty(
30+
"--app-canvas-height-desktop",
31+
`${desktopCanvasHeight}px`,
32+
);
33+
root.style.setProperty(
34+
"--app-canvas-height-mobile",
35+
`${mobileCanvasHeight}px`,
36+
);
37+
}
38+
};
39+
40+
window.addEventListener("resize", () => {
41+
window.requestAnimationFrame(applyViewportSizing);
42+
});
43+
applyViewportSizing();
44+
845
const emitPickerQuery = (action) => {
946
if (!pickerInput) return;
1047
const query = pickerInput.value || "";
@@ -20,18 +57,22 @@
2057

2158
if (pickerInput) {
2259
window.addEventListener("vizmat-structure-picker-open", () => {
60+
pickerOpen = true;
2361
pickerInput.value = "";
2462
try {
2563
pickerInput.focus({ preventScroll: true });
2664
} catch (error) {
2765
pickerInput.focus();
66+
window.scrollTo(0, 0);
2867
}
2968
emitPickerQuery("change");
3069
});
3170

3271
window.addEventListener("vizmat-structure-picker-close", () => {
72+
pickerOpen = false;
3373
pickerInput.blur();
3474
pickerInput.value = "";
75+
applyViewportSizing();
3576
});
3677

3778
pickerInput.addEventListener("input", () => emitPickerQuery("change"));
@@ -322,6 +363,44 @@
322363
window.setTimeout(() => loader.remove(), 260);
323364
};
324365

366+
const setStatus = (message) => {
367+
if (status) {
368+
status.textContent = message;
369+
}
370+
};
371+
372+
const startApp = () => {
373+
const bindings = window.wasmBindings;
374+
if (!bindings || typeof bindings.start !== "function") {
375+
setStatus("Waiting for WebAssembly bindings...");
376+
return false;
377+
}
378+
379+
try {
380+
bindings.start();
381+
hideLoader();
382+
return true;
383+
} catch (error) {
384+
console.error("Failed to start WASM app:", error);
385+
setStatus("Failed to start WebAssembly app.");
386+
return false;
387+
}
388+
};
389+
390+
// Trunk injects the WASM module and then fires this event.
391+
window.addEventListener(
392+
"TrunkApplicationStarted",
393+
() => {
394+
startApp();
395+
},
396+
{ once: true },
397+
);
398+
399+
// If Trunk fired before this script loaded, start immediately.
400+
if (window.wasmBindings?.start) {
401+
startApp();
402+
}
403+
325404
// Trunk injects a startup script and emits this event after WASM init.
326405
// If the event fired before this listener was added, hide immediately.
327406
if (window.wasmBindings) {

0 commit comments

Comments
 (0)