Skip to content
Merged
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
141 changes: 126 additions & 15 deletions ts/Menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,86 @@ export class MenuItem {

// ---- Internal menu-building helpers ----

function closeAllMenus(): void {
export function closeAllMenus(): void {
document.querySelectorAll('.gwt-MenuItem.open, .topMenuItem.open, li.hasSubmenu.open').forEach(el => el.classList.remove('open'));
}

// Get out of the way once an element or tool is picked: close the dropdowns and, in the
// collapsed (touch) layout, the hamburger menu bar and the sidebar -- they cover the canvas
// the element is about to be placed on.
export function closeMenusAndOverlays(): void {
closeAllMenus();
document.querySelectorAll<HTMLInputElement>('input.toptrigger:checked, input.trigger:checked')
.forEach(cb => { cb.checked = false; });
}

function anyMenuOpen(): boolean {
return document.querySelector('.gwt-MenuItem.open, .topMenuItem.open') !== null;
}

const MENU_PANEL_SELECTOR = '.menuDropdown, .elmContextMenu';
// an *open* panel, i.e. the one whose halo counts when deciding whether a tap dismisses
const OPEN_PANEL_SELECTOR = 'td.gwt-MenuItem.open > .menuDropdown, .elmContextMenu';
// How close a tap has to be to an open panel to still count as "aimed at the menu". A finger
// missing a row by a few pixels -- beside the panel, at its arrow, just under the last row or
// just above it on the bar -- is aiming at the menu, not at "dismiss it".
const NEAR_PANEL_PAD = 16;

// Does hovering open a menu here? Only in the full menu bar. The collapsed layout -- used on
// narrow windows as well as on touch screens -- is a list that is opened by tapping; letting a
// mouse hover pop the menus open there was jarring. Keep the breakpoint in sync with the
// media query in style.css.
let collapsedLayout: MediaQueryList | null = null;
function hoverOpensMenus(): boolean {
if (collapsedLayout == null) collapsedLayout = window.matchMedia('(max-width: 600px), (pointer: coarse)');
return !collapsedLayout.matches;
}

function isInPanel(target: EventTarget | null): boolean {
return target instanceof Element && target.closest(MENU_PANEL_SELECTOR) !== null;
}

// Should this interaction dismiss the menus? Only if it neither hit a panel nor landed within
// the halo of one. Deciding by hit target instead of relying on stopPropagation from every
// entry: panels built elsewhere (scope popups, dynamic subcircuits), separators, gaps and
// sub-panel padding have no listener of their own, and nested inline submenus expose plenty
// of those surfaces.
function dismissesMenus(e: Event): boolean {
if (isInPanel(e.target)) return false;
const p = e as MouseEvent;
const t = (e as TouchEvent).changedTouches?.[0] ?? (e as TouchEvent).touches?.[0];
const x = p.clientX ?? t?.clientX;
const y = p.clientY ?? t?.clientY;
if (x == null || y == null) return true;
for (const panel of Array.from(document.querySelectorAll<HTMLElement>(OPEN_PANEL_SELECTOR))) {
const r = panel.getBoundingClientRect();
if (x >= r.left - NEAR_PANEL_PAD && x <= r.right + NEAR_PANEL_PAD &&
y >= r.top - NEAR_PANEL_PAD && y <= r.bottom + NEAR_PANEL_PAD) return false;
}
return true;
}

// Keep a dropdown panel inside the viewport horizontally. A panel opens to the right of
// its anchor by default; with many top-level menus (or a narrow landscape window) the
// right-most ones would hang off the screen, where their items cannot be tapped. Align
// such a panel to the anchor's right edge instead (opens leftwards). Only absolute
// positioning is adjusted: the small-screen layout pins panels with position: fixed.
function fitDropdown(ul: HTMLUListElement): void {
ul.style.left = '';
ul.style.right = '';
ul.style.maxHeight = '';
if (getComputedStyle(ul).position !== 'absolute') return;
// Horizontal: the panel is laid out where the stylesheet puts it (left: 0 below a
// top-level menu, left: 100% below a submenu entry). If that hangs off the right side of
// the window, pin it to the anchor's right edge instead so it opens leftwards.
// left must be reset to auto too: with both left and right set the box is
// over-constrained and right would be ignored.
if (ul.getBoundingClientRect().right > window.innerWidth) {
ul.style.left = 'auto';
ul.style.right = '0';
}
}

// Build "<i class='cirjsicon-X'></i>&nbsp;Label" HTML for icon menu items.
function iconHtml(icon: string, text: string): string {
const ls = Locale.LS(text);
Expand Down Expand Up @@ -217,13 +289,34 @@ export class Menu {
for (const sib of Array.from(this.ul.children))
if (sib !== li) sib.classList.remove('open');
li.classList.add('open');
fitDropdown(sub.ul);
// Do NOT scroll the panel here. Scrolling right after expanding moved the entry
// out from under the finger: the next tap then hit the first child -- a command
// that silently added an element and closed the menu. The header stays where it
// was tapped; the submenu expands below it and the user scrolls if it does not fit.
};
li.addEventListener('mouseover', e => { e.stopPropagation(); openThis(); });
// stopPropagation here (and on the click below) keeps the document-level "tap
// elsewhere closes everything" handler from firing on the very tap that opened
// this submenu -- it should only close on a *later* tap outside the menu.
li.addEventListener('touchstart', e => { e.stopPropagation(); openThis(); }, { passive: true });
li.addEventListener('click', e => e.stopPropagation());
// Hover, real mouse only. A touch tap also fires a synthesized mouseover just before
// the click; opening there expanded the submenu, shifted the entries under the finger
// and the click then landed on a different element (typically the top-level menu
// entry), closing everything again. On touch the click alone opens it.
li.addEventListener('pointerover', e => {
if (e.pointerType !== 'mouse' || !hoverOpensMenus()) return;
e.stopPropagation();
openThis();
});
// stopPropagation on touchstart (and on the click below) keeps the document-level
// "tap elsewhere closes everything" handler from firing on the interaction with this
// entry -- it should only close on a *later* tap outside the menu.
// The submenu is opened on click, not on touchstart: touchstart fires the moment a
// finger lands, so scrolling the (already overflowing) panel would open whichever
// submenu happened to be under the finger. A scroll gesture suppresses the click, so
// opening only happens on a real tap. Opening is idempotent -- no toggle on a second
// tap, which on touch only ever meant "tap, then get closed again on release".
li.addEventListener('touchstart', e => e.stopPropagation(), { passive: true });
li.addEventListener('click', e => {
e.stopPropagation();
openThis();
});
return li;
}

Expand Down Expand Up @@ -358,7 +451,13 @@ export class Menus {
bar.style.cssText = 'outline: 0; position: absolute; inset: 0;';

const table = document.createElement('table');
table.addEventListener('click', () => closeAllMenus());
// Only a click on the bar itself closes: the dropdown panels are children of the
// cells, and an unstoppable click from inside one must not dismiss everything. A tap
// just above a panel also lands here (the bar is directly above the dropdown) -- that
// is a miss aiming at the top row, not a request to close.
table.addEventListener('click', e => {
if (dismissesMenus(e)) closeAllMenus();
});
const tbody = document.createElement('tbody');
this.menuBarRow = document.createElement('tr');
tbody.appendChild(this.menuBarRow);
Expand Down Expand Up @@ -518,11 +617,13 @@ export class Menus {
// for its drag/pinch gestures), which suppresses the browser's synthetic click for
// that tap -- so on touch devices, tapping the canvas to dismiss an open menu would
// otherwise never reach this listener via 'click' at all. touchstart still bubbles
// here fine since preventDefault() doesn't stop propagation; menu items that open
// something on their own touchstart (e.g. addSubmenu) call stopPropagation() so
// that doesn't also trigger this and immediately close what it just opened.
document.addEventListener('click', () => closeAllMenus());
document.addEventListener('touchstart', () => closeAllMenus(), { passive: true });
// here fine since preventDefault() doesn't stop propagation.
// Guard by target instead of relying on stopPropagation from every entry: panels built
// in other places (scope popups, dynamic subcircuits) do not necessarily have the
// per-element listeners, and with deeply nested inline submenus a tap easily lands on
// a sub-panel, a gap or a separator. Any interaction inside a menu must never close it.
document.addEventListener('click', e => { if (dismissesMenus(e)) closeAllMenus(); });
document.addEventListener('touchstart', e => { if (dismissesMenus(e)) closeAllMenus(); }, { passive: true });
}

// this is called twice, once for the Draw menu, once for the right mouse popup menu
Expand Down Expand Up @@ -839,15 +940,25 @@ export class Menus {

td.addEventListener('click', e => {
e.stopPropagation();
// the dropdown is a child of this cell: a click that comes from inside it must
// not toggle the whole menu shut
if (isInPanel(e.target)) return;
const wasOpen = td.classList.contains('open');
closeAllMenus();
if (!wasOpen) td.classList.add('open');
if (!wasOpen) {
td.classList.add('open');
fitDropdown(menu.ul);
}
});

td.addEventListener('mouseover', e => {
// Switch menus on hover, real mouse only: on a touch device this mouseover is
// synthesized for the tap, so it would close the menu it is about to open.
td.addEventListener('pointerover', e => {
if (e.pointerType !== 'mouse' || !hoverOpensMenus()) return;
if (anyMenuOpen()) {
closeAllMenus();
td.classList.add('open');
fitDropdown(menu.ul);
}
});

Expand Down
51 changes: 44 additions & 7 deletions ts/UIManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { CirSim } from "./CirSim";
import { SimulationManager } from "./SimulationManager";
import { ElementFactory } from "./ElementFactory";
import { Menus } from "./Menus";
import { Menus, closeMenusAndOverlays } from "./Menus";
import { MouseManager } from "./MouseManager";
import { ScopeManager } from "./ScopeManager";
import { Scope } from "./Scope";
Expand Down Expand Up @@ -93,6 +93,10 @@ export class Scrollbar {
can.style.height = Scrollbar.SCROLLHEIGHT + 'px';
can.style.display = 'block';
can.style.cursor = 'default';
// A finger that lands on a slider while trying to scroll the sidebar used to drag the
// slider instead: the touch handlers cancelled every gesture. With pan-y the browser
// keeps vertical drags (scrolling the panel) and only horizontal drags reach the slider.
can.style.touchAction = 'pan-y';
this.element = can;
this.g = can.getContext('2d')!;

Expand Down Expand Up @@ -125,15 +129,21 @@ export class Scrollbar {
});
can.addEventListener('wheel', (e) => this.onMouseWheel(e), { passive: false });

// Do not cancel the gesture on touchstart: a vertical drag has to stay available for
// scrolling the sidebar (touch-action: pan-y above). The slider only takes horizontal
// moves, and only once the gesture has clearly gone sideways.
let touchStartX = 0, touchStartY = 0;
can.addEventListener('touchstart', (e) => {
e.preventDefault();
this.dragging = false;
const t = e.touches[0];
touchStartX = t.clientX; touchStartY = t.clientY;
this.dragging = false;
this.doMouseDown(t.clientX - can.getBoundingClientRect().left, false);
}, { passive: false });
}, { passive: true });
can.addEventListener('touchmove', (e) => {
e.preventDefault();
const t = e.touches[0];
const dx = Math.abs(t.clientX - touchStartX), dy = Math.abs(t.clientY - touchStartY);
if (dy > dx) { this.dragging = false; return; } // vertical: let the panel scroll
e.preventDefault();
this.doMouseMove(t.clientX - can.getBoundingClientRect().left);
}, { passive: false });
can.addEventListener('touchend', (e) => {
Expand Down Expand Up @@ -609,6 +619,9 @@ export class UIManager {
UIManager.VERTICALPANELWIDTH = Math.floor(width / 5);
if (UIManager.VERTICALPANELWIDTH > 166) UIManager.VERTICALPANELWIDTH = 166;
if (UIManager.VERTICALPANELWIDTH < 128) UIManager.VERTICALPANELWIDTH = 128;
// the touch layout sizes the collapsed sidebar from this value; its contents (sliders,
// labels) are laid out for exactly this width
document.documentElement.style.setProperty('--vpanel-width', UIManager.VERTICALPANELWIDTH + 'px');

this.verticalPanel = document.createElement('div');
this.verticalPanel.className = 'verticalPanel';
Expand All @@ -632,6 +645,8 @@ export class UIManager {
const topPanelCheckboxLabel = document.createElement('label');
topPanelCheckboxLabel.className = 'toptriggerlabel';
topPanelCheckboxLabel.setAttribute('for', 'toptrigger');
// the collapsed layout shows a labelled button instead of a bare burger glyph
topPanelCheckboxLabel.textContent = Locale.LS("Menu");

// make buttons side by side if there's room
this.buttonPanel = (UIManager.VERTICALPANELWIDTH === 166)
Expand All @@ -643,7 +658,12 @@ export class UIManager {
this.menus.dotsCheckItem.setState(true);
this.menus.voltsCheckItem.setState(true);
this.menus.showValuesCheckItem.setState(true);
// The collapsed touch layout has no room for the 40px icon strip and the menu already
// reaches the same commands, so phones and tablets start without it
// (Options > Toolbar brings it back at any time).
const coarse = typeof window !== 'undefined' && window.matchMedia('(pointer: coarse)').matches;
this.menus.toolbarCheckItem.setState(
!coarse &&
!this.hideMenu && !noEditing && !hideSidebar &&
(this.app as any).startCircuit == null &&
(this.app as any).startCircuitText == null &&
Expand Down Expand Up @@ -1521,6 +1541,9 @@ export class UIManager {
content.style.position = 'fixed';
content.style.left = clientX + 'px';
content.style.top = clientY + 'px';
// the menu bar element is reused: drop a height cap left over from a previous open
content.style.maxHeight = '';
content.style.overflowY = '';
document.body.appendChild(content);
this.contextPanel = content;

Expand All @@ -1534,6 +1557,17 @@ export class UIManager {
content.style.top = clampedY + 'px';
}

// Clamping above places the panel by its top edge only, but opening a submenu inside
// it makes it grow again -- and on a short touch viewport that growth ran past the
// bottom edge, leaving the last entries cut off. Cap the height to the room below the
// (possibly clamped) top and let the panel scroll instead. 6px: leave room for the
// panel's own border/padding, which max-height does not cover.
const room = window.innerHeight - clampedY - 6;
if (h > room) {
content.style.maxHeight = Math.max(60, room) + 'px';
content.style.overflowY = 'auto';
}

// close on outside click
const close = (evt: MouseEvent) => {
if (!content.contains(evt.target as Node)) {
Expand Down Expand Up @@ -1626,9 +1660,12 @@ export class UIManager {
this.mouse.mouseMode = mode;
if (mode !== MouseManager.MODE_SELECT_POSTS)
this.mouse.clearPostSelection();
if (mode === MouseManager.MODE_ADD_ELM)
if (mode === MouseManager.MODE_ADD_ELM) {
this.setCursorStyle("cursorCross");
else
// the item was picked from a menu: clear the menus and the collapsed-layout
// overlays so the canvas is visible for placing it
closeMenusAndOverlays();
} else
this.setCursorStyle("cursorPointer");
}

Expand Down
Loading