From 1aa277be4b5c2bbaf1623443a3c8a937715cf11a Mon Sep 17 00:00:00 2001 From: salman Date: Thu, 3 Sep 2026 13:38:11 +0200 Subject: [PATCH] Add touch-action menu to ROOT plot window A hamburger button on the left of the plot title bar opens a dropdown of FFT, clean/original view and reset actions, wired to the same handlers as the musrview keyboard shortcuts so plot commands stay reachable on touch devices where f/c/u/q are unavailable. Touch-friendly hit targets in CSS. --- ZGUI.css | 69 ++++++++++++++++++++++++++++ muSRFitRoot.js | 86 ++++++++++++++++++++++++++++++++++- tests/root-plot-clean.test.js | 39 ++++++++++++++++ 3 files changed, 193 insertions(+), 1 deletion(-) diff --git a/ZGUI.css b/ZGUI.css index 2463871..a5a5e8b 100644 --- a/ZGUI.css +++ b/ZGUI.css @@ -1250,6 +1250,7 @@ button:not(.tablinks):not(.action-btn):not(.editor-source-tab):not(.status-state display: flex; justify-content: space-between; padding: 9px 12px; + position: relative; touch-action: none; user-select: none; } @@ -1292,6 +1293,74 @@ button:not(.tablinks):not(.action-btn):not(.editor-source-tab):not(.status-state width: 28px; } +/* Hamburger button on the left of the plot title bar; opens the plot-action + menu so plot commands are reachable without a keyboard (notably on touch). */ +.web-plot-menu-button { + align-items: center; + cursor: pointer; + display: inline-flex; + flex: 0 0 auto; + font-size: 16px; + height: 30px; + justify-content: center; + line-height: 1; + margin-right: 8px; + padding: 0 !important; + width: 34px; +} + +/* Dropdown of plot actions, anchored under the hamburger button. The title bar + is flex, so the menu participates in flow and must be absolutely positioned + relative to the first (hamburger) column to draw over the canvas. */ +.web-plot-action-menu { + background: var(--mu-panel); + border: 1px solid var(--mu-border); + border-radius: 8px; + box-shadow: 0 14px 34px rgba(0, 0, 0, 0.3); + display: flex; + flex-direction: column; + gap: 2px; + left: 8px; + min-width: 168px; + padding: 6px; + position: absolute; + top: calc(100% + 6px); + z-index: 20; +} + +.web-plot-action-menu[hidden] { + display: none; +} + +.web-plot-action { + align-items: center; + background: transparent; + border: 0; + border-radius: 6px; + color: var(--mu-text); + cursor: pointer; + display: flex; + font-size: 14px; + min-height: 44px; + padding: 0 12px; + text-align: left; + white-space: nowrap; + width: 100%; +} + +.web-plot-action:hover, +.web-plot-action:focus-visible { + background: var(--mu-green); + color: #fff; + outline: none; +} + +@media (min-width: 760px) { + .web-plot-action { + min-height: 32px; + } +} + .web-plot-window[data-plot-state="minimized"] .web-plot-header { border-bottom: 0; diff --git a/muSRFitRoot.js b/muSRFitRoot.js index baa03a5..f3235fc 100644 --- a/muSRFitRoot.js +++ b/muSRFitRoot.js @@ -615,6 +615,8 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) { let plotWindow = document.createElement("dialog"); let plotHeader = document.createElement("div"); + let plotMenuButton = document.createElement("button"); + let plotActionMenu = document.createElement("div"); let plotDragHandle = document.createElement("div"); let plotControls = document.createElement("div"); let minimizeButton = document.createElement("button"); @@ -629,6 +631,16 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) { plotWindow.setAttribute("aria-modal", "false"); plotWindow.tabIndex = -1; plotHeader.className = "web-plot-header web-root-plot-header"; + plotMenuButton.type = "button"; + plotMenuButton.className = "web-plot-menu-button"; + plotMenuButton.innerHTML = "☰"; + plotMenuButton.title = "Plot actions"; + plotMenuButton.setAttribute("aria-label", "Plot actions"); + plotMenuButton.setAttribute("aria-haspopup", "menu"); + plotMenuButton.setAttribute("aria-expanded", "false"); + plotActionMenu.className = "web-plot-action-menu"; + plotActionMenu.setAttribute("role", "menu"); + plotActionMenu.hidden = true; plotDragHandle.className = "web-plot-drag-handle"; plotControls.className = "web-plot-controls"; minimizeButton.type = "button"; @@ -648,6 +660,8 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) { plotControls.appendChild(minimizeButton); plotControls.appendChild(maximizeButton); plotControls.appendChild(closeButton); + plotHeader.appendChild(plotMenuButton); + plotHeader.appendChild(plotActionMenu); plotHeader.appendChild(plotDragHandle); plotHeader.appendChild(plotControls); plotWindow.appendChild(plotHeader); @@ -757,6 +771,73 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) { renderMusrfitRootPlots(plotContainer, spectrumSpecs, renderVersion, rootViewMode); + // Build one action, e.g. a menu button, for a plot action. Toggle/stateful + // actions report their current result through updateJobStatus. + function rootPlotActionButton(label, title, onClick) { + let button = document.createElement("button"); + button.type = "button"; + button.className = "web-plot-action"; + button.textContent = label; + button.setAttribute("role", "menuitem"); + button.tabIndex = -1; + if (title) { + button.title = title; + } + button.addEventListener("click", function() { + plotActionMenu.hidden = true; + plotMenuButton.setAttribute("aria-expanded", "false"); + onClick(); + }); + return button; + } + + let fftActionButton = null; + if (plotContext.msrPath) { + // FFT is server-side musrview work, so it is only offered when the plot + // knows the .msr behind it. + fftActionButton = rootPlotActionButton("FFT", "Toggle Fourier power spectrum", function() { + toggleMusrfitRootFFT(); + }); + plotActionMenu.appendChild(fftActionButton); + } + plotActionMenu.appendChild(rootPlotActionButton( + "Clean / Original view", + "alternate between the compact clean view and the full ROOT view", + function() { + rootViewMode = rootViewMode === "clean" ? "original" : "clean"; + plotWindow.dataset.plotView = rootViewMode; + rerenderCurrentRootPlots(rootViewMode === "clean" + ? "Clean ROOT plot view" : "Original ROOT plot view"); + } + )); + plotActionMenu.appendChild(rootPlotActionButton( + "Reset view", + "redraw the current plot to undo pan and zoom", + function() { + rerenderCurrentRootPlots("ROOT plot view reset"); + } + )); + + function toggleRootPlotActionMenu(forceOpen) { + let open = forceOpen !== undefined ? Boolean(forceOpen) : plotActionMenu.hidden; + plotActionMenu.hidden = !open; + plotMenuButton.setAttribute("aria-expanded", open ? "true" : "false"); + } + + plotMenuButton.addEventListener("click", function(event) { + event.stopPropagation(); + toggleRootPlotActionMenu(); + }); + + // Close the action menu when interacting anywhere else in the window. + plotWindow.addEventListener("pointerdown", function(event) { + if (!plotActionMenu.hidden + && event.target !== plotMenuButton + && !plotActionMenu.contains(event.target)) { + toggleRootPlotActionMenu(false); + } + }); + // Minimal musrview-like shortcuts for the focused ROOT plot window. plotWindow.addEventListener("keydown", function(event) { if (event.altKey || event.ctrlKey || event.metaKey @@ -895,7 +976,10 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) { // The blank header is intentionally only a drag handle plus window controls. plotHeader.addEventListener("pointerdown", function(event) { - if (plotWindow.dataset.plotState !== "normal" || plotControls.contains(event.target)) { + if (plotWindow.dataset.plotState !== "normal" + || plotControls.contains(event.target) + || plotMenuButton.contains(event.target) + || plotActionMenu.contains(event.target)) { return; } let bounds = plotWindow.getBoundingClientRect(); diff --git a/tests/root-plot-clean.test.js b/tests/root-plot-clean.test.js index 7442bf9..0763155 100644 --- a/tests/root-plot-clean.test.js +++ b/tests/root-plot-clean.test.js @@ -232,11 +232,50 @@ function testCleanViewIsDefaultAndToggleExists() { assert.match(code, /setTooltipAllowed\(viewMode !== "clean"\)/); } +// The plot window must expose its commands on a touch screen, where the musrview +// keyboard shortcuts (f/c/u/q) are unreachable. A hamburger button on the title +// bar opens a dropdown of plot actions that invoke the same handlers. +function testTouchActionMenu() { + const code = fs.readFileSync(path.join(repoRoot, "muSRFitRoot.js"), "utf8"); + + // Hamburger button created and placed first in the title bar. + assert.match(code, /plotMenuButton.innerHTML = "☰"/); + assert.match(code, /plotHeader\.appendChild\(plotMenuButton\)/); + + // The dropdown exists and defaults to hidden; the hamburger toggles it. + assert.match(code, /let plotActionMenu = document\.createElement\("div"\)/); + assert.match(code, /plotActionMenu\.hidden = true/); + assert.match(code, /function toggleRootPlotActionMenu/); + + // Each musrview shortcut is mirrored by a menu action wired to the shared + // handler: FFT (f), clean/original (c), reset (u). + assert.match(code, /rootPlotActionButton\("FFT"/); + assert.match(code, /"Clean \/ Original view"/); + assert.match(code, /"Reset view"/); + + // FFT is only meaningful when the plot knows its source .msr, matching the + // keyboard guard on the "f" shortcut. + assert.match(code, /if \(plotContext\.msrPath\)\s*\{\s*\/\/[^]*?fftActionButton = rootPlotActionButton/); + assert.match(code, /shortcut === "f"/); + + const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8"); + // Touch-friendly hit targets and a label that appears only on wider screens. + assert.match(css, /\.web-plot-menu-button/); + assert.match(css, /\.web-plot-action-menu/); + assert.match(css, /\.web-plot-action/); + assert.match(css, /min-height: 44px/); + + // The action menu must not begin a window drag and must close on outside tap. + assert.match(code, /plotMenuButton\.contains\(event\.target\)/); + assert.match(code, /!plotActionMenu\.contains\(event\.target\)/); +} + function runTests() { testCleanLegendLabels(); testCleanCanvasLayout(); testCrowdedLegendFallsBackAndShrinks(); testCleanViewIsDefaultAndToggleExists(); + testTouchActionMenu(); console.log("ROOT clean-view tests passed."); }