Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions js/controlStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,10 @@ export const sliderStyles = {
"& .MuiSlider-rail": { height: 2, borderRadius: 0 },
"& .MuiSlider-track": { height: 2, borderRadius: 0 },
"& .MuiSlider-valueLabel": { borderRadius: 0 },
// A finger needs a taller strip and a larger thumb than a mouse; desktop layout keeps the compact rows.
"@media (pointer: coarse)": {
py: "14px",
"& .MuiSlider-thumb": { width: 18, height: 18 },
},
},
};
26 changes: 16 additions & 10 deletions js/show2d/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ function Show2D() {
touchAction: "none",
zIndex: 5,
"&:hover": { opacity: 1 },
// A finger needs about 44 px: the grip stays 16 px drawn, its hit area grows on touch screens.
"@media (pointer: coarse)": { opacity: 0.9, "&::before": { content: '""', position: "absolute", inset: -14 } },
}), [themeColors.accent]);
const [showStats] = useModelState<boolean>("show_stats");
const [statsMean] = useModelState<number[]>("stats_mean");
Expand Down Expand Up @@ -7018,7 +7020,9 @@ function Show2D() {

// Resize Handlers
// -------------------------------------------------------------------------
const handleCanvasResizeStart = (e: React.MouseEvent) => {
// Pointer events, so a finger or a pen drags the grip as a mouse does (mouse events never arrive for a touch drag).
const handleCanvasResizeStart = (e: React.PointerEvent) => {
if (e.pointerType === "mouse" && e.button !== 0) return;
e.stopPropagation();
e.preventDefault();
canvasResizeCleanupRef.current?.();
Expand All @@ -7033,7 +7037,7 @@ function Show2D() {
let rafId = 0;
let latestSize = start.size;

const handleMouseMove = (event: MouseEvent) => {
const handleMouseMove = (event: PointerEvent) => {
const delta = Math.max(event.clientX - start.x, event.clientY - start.y);
latestSize = Math.max(200, start.size + delta);
if (!rafId) {
Expand All @@ -7050,15 +7054,17 @@ function Show2D() {
applyResizeViewAnchor(latestSize);
setCanvasSize(latestSize);
setPanelWidthPxTrait(Math.round(latestSize));
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseup", finishResize);
document.removeEventListener("pointermove", handleMouseMove);
document.removeEventListener("pointerup", finishResize);
document.removeEventListener("pointercancel", finishResize);
window.removeEventListener("blur", finishResize);
canvasResizeCleanupRef.current = null;
window.setTimeout(() => { canvasResizeViewAnchorRef.current = null; }, 0);
};

document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", finishResize);
document.addEventListener("pointermove", handleMouseMove);
document.addEventListener("pointerup", finishResize);
document.addEventListener("pointercancel", finishResize);
window.addEventListener("blur", finishResize);
canvasResizeCleanupRef.current = finishResize;
};
Expand Down Expand Up @@ -8629,7 +8635,7 @@ function Show2D() {
)}
{showResizeControls && (
<Box
onMouseDown={handleCanvasResizeStart}
onPointerDown={handleCanvasResizeStart}
title="Resize panels"
sx={resizeGripSx}
/>
Expand Down Expand Up @@ -8798,7 +8804,7 @@ function Show2D() {
)}
{showResizeControls && (
<Box
onMouseDown={(e: React.MouseEvent) => {
onPointerDown={(e: React.PointerEvent) => {
e.stopPropagation();
handleCanvasResizeStart(e);
}}
Expand Down Expand Up @@ -8904,7 +8910,7 @@ function Show2D() {
{renderPanelAnnotations(0)}
{renderPanelFrameControls(0)}
{showResizeControls && (
<Box onMouseDown={handleCanvasResizeStart} title="Resize image" sx={resizeGripSx} />
<Box onPointerDown={handleCanvasResizeStart} title="Resize image" sx={resizeGripSx} />
)}
</Box>
)}
Expand Down Expand Up @@ -9458,7 +9464,7 @@ function Show2D() {
</Box>
)}
{showResizeControls && (
<Box onMouseDown={handleCanvasResizeStart} title="Resize image" sx={resizeGripSx} />
<Box onPointerDown={handleCanvasResizeStart} title="Resize image" sx={resizeGripSx} />
)}
</Box>
{/* FFT Stats Bar */}
Expand Down
112 changes: 89 additions & 23 deletions js/show3d/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,8 @@ function Show3D() {
touchAction: "none",
zIndex: 5,
"&:hover": { opacity: 1 },
// A finger needs about 44 px: the grip stays 16 px drawn, its hit area grows on touch screens.
"@media (pointer: coarse)": { opacity: 0.9, "&::before": { content: '""', position: "absolute", inset: -14 } },
}), [themeColors.accent]);
const [statsMean] = useModelState<number>("stats_mean");
const [statsMin] = useModelState<number>("stats_min");
Expand Down Expand Up @@ -4153,9 +4155,11 @@ function Show3D() {
setLogScale(nextLogScale);
}, [logScale, setLogScale, setVmaxPerPanel, setVminPerPanel]);

// Frame labels for the per-panel titles, written straight to the page while playing (set beside frameLabelForIndex).
const frameLabelForIndexRef = React.useRef<((idx: number) => string) | undefined>(undefined);
const updatePlaybackLiveControls = React.useCallback((idx: number) => {
const settings = playRef.current;
writeLiveFrameControls(rootRef.current, playbackSliderRef.current, playbackLiveCountRef.current, idx, Math.max(1, settings.nSlices || nSlices || 1), settings.loop);
writeLiveFrameControls(rootRef.current, playbackSliderRef.current, playbackLiveCountRef.current, idx, Math.max(1, settings.nSlices || nSlices || 1), settings.loop, frameLabelForIndexRef.current);
}, [nSlices]);

const viewportTransformActive = React.useCallback(() => {
Expand Down Expand Up @@ -4973,7 +4977,11 @@ function Show3D() {
const settings = playRef.current;
const effectiveFps = clampPlaybackFps(settings.fps);
const intervalMs = playbackIntervalMs(effectiveFps);
const uiUpdateIntervalMs = effectiveFps >= 60 ? 250 : 100;
// React commits (stats, histogram, star state) once a second while playing: each commit renders the whole
// viewer, and at 10 per second they were most of the main-thread time a frame cost (18.5 of 18.5 ms vs 3.5 ms
// under a 4x CPU throttle, three 512 px panels). The slider, counters and frame labels are written straight
// to the page every frame by updatePlaybackLiveControls.
const uiUpdateIntervalMs = 1000;

// First tick paints immediately; otherwise every playback start drops
// one frame before the cadence timer is even allowed to run.
Expand Down Expand Up @@ -6956,7 +6964,7 @@ function Show3D() {
// Profile height resize
React.useEffect(() => {
if (!isResizingProfile) return;
const handleMouseMove = (e: MouseEvent) => {
const handleMouseMove = (e: PointerEvent) => {
if (!profileResizeStart) return;
const delta = e.clientY - profileResizeStart.y;
setProfileHeight(Math.max(40, Math.min(300, profileResizeStart.height + delta)));
Expand All @@ -6965,11 +6973,13 @@ function Show3D() {
setIsResizingProfile(false);
setProfileResizeStart(null);
};
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", handleMouseUp);
document.addEventListener("pointermove", handleMouseMove);
document.addEventListener("pointerup", handleMouseUp);
document.addEventListener("pointercancel", handleMouseUp);
return () => {
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseup", handleMouseUp);
document.removeEventListener("pointermove", handleMouseMove);
document.removeEventListener("pointerup", handleMouseUp);
document.removeEventListener("pointercancel", handleMouseUp);
};
}, [isResizingProfile, profileResizeStart]);

Expand Down Expand Up @@ -8586,8 +8596,40 @@ function Show3D() {
beginPan(e);
};

// One finger drives the active tool (profile line, ROI, lens) through the same handlers as the mouse: a touch drag
// sends no mouse events, so these tools did nothing on a phone. Two fingers still pinch and pan the view.
const toolTouchRef = React.useRef(false);
const touchAsMouse = (touch: React.Touch, e: React.TouchEvent) => ({
clientX: touch.clientX,
clientY: touch.clientY,
button: 0,
buttons: 1,
shiftKey: false,
altKey: false,
ctrlKey: false,
metaKey: false,
detail: 1,
currentTarget: e.currentTarget,
target: e.target,
nativeEvent: e.nativeEvent,
preventDefault: () => e.preventDefault(),
stopPropagation: () => e.stopPropagation(),
}) as unknown as React.MouseEvent;
const handleCanvasTouchStart = (e: React.TouchEvent) => {
if (profileActive || effectiveRoiActive) return;
const toolActive = profileActive || effectiveRoiActive || showLens;
if (toolActive && e.touches.length === 1) {
e.preventDefault();
toolTouchRef.current = true;
handleCanvasMouseMove(touchAsMouse(e.touches[0], e));
handleCanvasMouseDown(touchAsMouse(e.touches[0], e));
return;
}
if (toolTouchRef.current && e.touches.length >= 2) {
// a second finger turns the tool gesture into a pinch: end the tool drag where the first finger is
handleCanvasMouseUp(touchAsMouse(e.touches[0], e));
toolTouchRef.current = false;
}
if ((profileActive || effectiveRoiActive) && e.touches.length < 2) return;
if (e.touches.length === 1) {
const touch = e.touches[0];
const panelIdx = panelIdxFromClient(touch.clientX, touch.clientY);
Expand Down Expand Up @@ -8619,6 +8661,11 @@ function Show3D() {
};

const handleCanvasTouchMove = (e: React.TouchEvent) => {
if (toolTouchRef.current && e.touches.length === 1) {
e.preventDefault();
handleCanvasMouseMove(touchAsMouse(e.touches[0], e));
return;
}
const start = touchTransformRef.current;
if (!start) return;
const canvas = canvasRef.current;
Expand Down Expand Up @@ -8658,6 +8705,13 @@ function Show3D() {
};

const handleCanvasTouchEnd = (e: React.TouchEvent) => {
if (toolTouchRef.current) {
if (e.touches.length > 0) return;
const last = e.changedTouches[0];
if (last) handleCanvasMouseUp(touchAsMouse(last, e));
toolTouchRef.current = false;
return;
}
if (e.touches.length > 0 || !touchTransformRef.current) return;
commitLivePanelTransforms();
touchTransformRef.current = null;
Expand Down Expand Up @@ -9717,17 +9771,24 @@ function Show3D() {
};

// Resize handlers
const handleMainResizeStart = (e: React.MouseEvent) => {
// Pointer events with capture, so a mouse, a finger (Galaxy Fold, touch laptops) and a pen all drag the grip:
// mouse events alone never arrive for a touch drag.
const handleMainResizeStart = (e: React.PointerEvent<HTMLElement>) => {
if (e.pointerType === "mouse" && e.button !== 0) return;
e.stopPropagation();
e.preventDefault();
const grip = e.currentTarget;
const pointerId = e.pointerId;
try { grip.setPointerCapture(pointerId); } catch { /* capture is best effort */ }
const rect = canvasContainerRef.current?.getBoundingClientRect();
const startSize = rect && rect.width > 0 ? rect.width : mainCanvasSize;
const startX = e.clientX;
const startY = e.clientY;
const visiblePanels = Math.max(1, visiblePanelCount || 1);
let rafId = 0;
let latestSize = startSize;
const handleMouseMove = (e: MouseEvent) => {
const handleMouseMove = (e: PointerEvent) => {
if (e.pointerId !== pointerId) return;
const delta = Math.max(e.clientX - startX, e.clientY - startY);
const nextSize = startSize + delta;
// Absolute minimum: 200 px per panel column. Lets reader shrink BELOW
Expand All @@ -9743,16 +9804,20 @@ function Show3D() {
});
}
};
const handleMouseUp = () => {
const handleMouseUp = (e: PointerEvent) => {
if (e.pointerId !== pointerId) return;
cancelAnimationFrame(rafId);
setMainCanvasSize(latestSize);
const colsLocal = panelColsForCount(visiblePanels);
setCanvasSizeTrait(Math.round(latestSize / colsLocal));
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseup", handleMouseUp);
document.removeEventListener("pointermove", handleMouseMove);
document.removeEventListener("pointerup", handleMouseUp);
document.removeEventListener("pointercancel", handleMouseUp);
try { grip.releasePointerCapture(pointerId); } catch { /* already released */ }
};
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", handleMouseUp);
document.addEventListener("pointermove", handleMouseMove);
document.addEventListener("pointerup", handleMouseUp);
document.addEventListener("pointercancel", handleMouseUp);
};

const clampSlice = (idx: number) => Math.max(0, Math.min(nSlices - 1, Math.round(idx)));
Expand All @@ -9763,6 +9828,7 @@ function Show3D() {
if (!text || text === String(idx) || text === String(idx + 1)) return "";
return text;
}, [labels]);
frameLabelForIndexRef.current = frameLabelForIndex;
const formatFrameValueLabel = React.useCallback((idx: number) => {
const rounded = clampSlice(idx);
const label = frameLabelForIndex(rounded);
Expand Down Expand Up @@ -11446,7 +11512,7 @@ function Show3D() {
overflowWrap: "anywhere",
}}
>
{panelTitleContent(panel)}{frameLabel ? ` · ${frameLabel}` : ""}{" "}
{panelTitleContent(panel)}<span key={`label-${shownIdx}`} data-show3d-panel-frame-label="true">{frameLabel ? ` · ${frameLabel}` : ""}</span>{" "}
<span key={`${shown}/${total}`} data-show3d-panel-frame-count="true" data-real-frame-count={total}>
{`${shown}/${total}`}
</span>
Expand Down Expand Up @@ -11722,7 +11788,7 @@ function Show3D() {
{panelChromeVisible && effectiveRoiActive && roiItems.length > 0 && showRoiResizeHint && (
<Box sx={{ position: "absolute", left: 6, top: 6, px: 0.6, py: 0.25, bgcolor: "rgba(0,0,0,0.45)", pointerEvents: "none" }}>
<Typography sx={{ fontSize: 9, color: "rgba(255,255,255,0.8)", lineHeight: 1.1 }}>
Hover ROI edge to resize
Drag an ROI edge to resize
</Typography>
</Box>
)}
Expand All @@ -11740,7 +11806,7 @@ function Show3D() {
return (
<Box
key={`resize-${panel}`}
onMouseDown={handleMainResizeStart}
onPointerDown={handleMainResizeStart}
title="Resize panels"
sx={{
position: "absolute",
Expand Down Expand Up @@ -11910,8 +11976,8 @@ function Show3D() {
/>
{showResizeControls && (
<div
onMouseDown={(e) => { e.preventDefault(); setIsResizingProfile(true); setProfileResizeStart({ y: e.clientY, height: profileHeight }); }}
style={{ width: "100%", height: 4, cursor: "ns-resize", borderLeft: `1px solid ${themeColors.border}`, borderRight: `1px solid ${themeColors.border}`, borderBottom: `1px solid ${themeColors.border}`, background: `linear-gradient(to bottom, ${themeColors.border}, transparent)` }}
onPointerDown={(e) => { e.preventDefault(); setIsResizingProfile(true); setProfileResizeStart({ y: e.clientY, height: profileHeight }); }}
style={{ width: "100%", height: 4, cursor: "ns-resize", touchAction: "none", borderLeft: `1px solid ${themeColors.border}`, borderRight: `1px solid ${themeColors.border}`, borderBottom: `1px solid ${themeColors.border}`, background: `linear-gradient(to bottom, ${themeColors.border}, transparent)` }}
/>
)}
</Box>
Expand Down Expand Up @@ -12132,7 +12198,7 @@ function Show3D() {
<Box sx={{ ...controlRow, ...mobileControlRowSx, width: "fit-content", maxWidth: "100%", flexWrap: "wrap", border: `1px solid ${themeColors.border}`, bgcolor: themeColors.controlBg, boxSizing: "border-box" }}>
<Box sx={{ display: "flex", alignItems: "center", gap: isMobileViewport ? "4px" : `${SPACING.SM}px`, flexShrink: 0 }}>
<Typography sx={{ ...typography.label, color: themeColors.textMuted, fontSize: isMobileViewport ? 10 : typography.label.fontSize, flexShrink: 0 }}>fps</Typography>
<Slider value={playbackFps} min={1} max={MAX_PLAYBACK_FPS} step={1} onChange={(_, v) => setPlaybackFps(v as number)} size="small" sx={{ ...sliderStyles.small, width: isMobileViewport ? 40 : 44, mx: isMobileViewport ? "3px" : 0, flexShrink: 0 }} aria-label="Playback frames per second" valueLabelDisplay="auto" />
<Slider value={playbackFps} min={1} max={MAX_PLAYBACK_FPS} step={1} onChange={(_, v) => setPlaybackFps(v as number)} size="small" sx={{ ...sliderStyles.small, width: isMobileViewport ? 72 : 44, mx: isMobileViewport ? "6px" : 0, flexShrink: 0 }} aria-label="Playback frames per second" valueLabelDisplay="auto" />
<Typography sx={{ ...typography.label, color: themeColors.textMuted, fontSize: isMobileViewport ? 10 : typography.label.fontSize, minWidth: isMobileViewport ? 16 : 20, flexShrink: 0 }}>{Math.round(playbackFps)}</Typography>
</Box>
<Box
Expand Down Expand Up @@ -12391,7 +12457,7 @@ function Show3D() {
<canvas ref={previewCanvasRef} width={previewCanvasDims.w} height={previewCanvasDims.h} style={{ position: "absolute", top: 0, left: 0, width: "100%", height: "100%", imageRendering: "pixelated" }} role="img" aria-label={`ROI preview crop${previewCropDims ? ` (${previewCropDims.w} by ${previewCropDims.h} pixels)` : ""}`} />
<canvas ref={previewOverlayRef} width={Math.round(previewCanvasDims.w * dpr)} height={Math.round(previewCanvasDims.h * dpr)} style={{ position: "absolute", top: 0, left: 0, width: "100%", height: "100%", pointerEvents: "none" }} aria-hidden="true" />
{showResizeControls && (
<Box onMouseDown={handleMainResizeStart} title="Resize image" sx={{ position: "absolute", bottom: 0, right: 0, ...resizeGripSx }} />
<Box onPointerDown={handleMainResizeStart} title="Resize image" sx={{ position: "absolute", bottom: 0, right: 0, ...resizeGripSx }} />
)}
</Box>
{/* All-ROI Stats - one row per ROI, same style as main stats bar */}
Expand Down Expand Up @@ -12559,7 +12625,7 @@ function Show3D() {
return (
<Box
key={`fft-resize-${panel}`}
onMouseDown={handleMainResizeStart}
onPointerDown={handleMainResizeStart}
title="Resize FFT panels"
sx={{
position: "absolute",
Expand Down
11 changes: 11 additions & 0 deletions js/show3d/playback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ describe("live frame controls", () => {
writeLiveFrameControls(b.root, b.slider, null, 7, 20, false);
expect(b.root.querySelector("[data-show3d-playback-count]")!.textContent).toBe("8/20");
});

it("write each panel's frame label while playing, without a React render", () => {
document.body.innerHTML = "";
const a = widget(true);
a.root.insertAdjacentHTML("beforeend", `<span data-show3d-panel-frame-label="true"></span><span data-show3d-panel-frame-label="true"></span>`);
const labels = (idx: number) => (idx === 0 ? "" : `t = ${idx * 0.5} s`);
writeLiveFrameControls(a.root, a.slider, null, 4, 20, false, labels);
expect([...a.root.querySelectorAll("[data-show3d-panel-frame-label]")].map((el) => el.textContent)).toEqual([" · t = 2 s", " · t = 2 s"]);
writeLiveFrameControls(a.root, a.slider, null, 0, 20, false, labels);
expect(a.root.querySelector("[data-show3d-panel-frame-label]")!.textContent).toBe("");
});
});

describe("CPU playback colormap", () => {
Expand Down
Loading
Loading