Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Menus: lines light at once when the pointer leaves a submenu's way

Yashwanth Kamireddi0679eadc92b4
diff --git a/.uiverify/menu-aim.mjs b/.uiverify/menu-aim.mjs
new file mode 100644
index 0000000..07e4b4a
--- /dev/null
+++ b/.uiverify/menu-aim.mjs
@@ -0,0 +1,51 @@
+// A menu with a submenu open answers the pointer as a Mac menu does: moving back up to the
+// lines above highlights each at once and closes the submenu; only a move heading into the
+// submenu keeps it open while it crosses another line.
+import { engine } from "./engine.mjs";
+import { SERVICES, installStub, openScreen } from "./stub.mjs";
+const b = await engine.launch();
+const ctx = await b.newContext({ viewport: { width: 1280, height: 800 } });
+await ctx.addInitScript(installStub, { screen: "library", services: SERVICES, twoFactor: null });
+const page = await ctx.newPage();
+page.on("pageerror", (e) => { console.log(`FAIL page error: ${e.message}`); process.exitCode = 1; });
+await page.goto("http://localhost:4173", { waitUntil: "networkidle" });
+await openScreen(page, "Photos");
+await page.waitForTimeout(400);
+let failures = 0;
+const ok = (p, m) => { console.log(`${p ? "PASS" : "FAIL"} ${m}`); if (!p) failures++; };
+const active = () => page.locator(".row-menu-item.is-active").allInnerTexts();
+const centre = async (loc) => { const r = await loc.boundingBox(); return [r.x + r.width / 2, r.y + r.height / 2]; };
+
+await page.locator(".px-tile").first().click({ button: "right" });
+const add = page.locator(".row-menu-item", { hasText: "Add to Album" });
+const [ax, ay] = await centre(add);
+await page.mouse.move(ax, ay, { steps: 4 });
+await page.waitForTimeout(80);
+ok((await page.locator(".row-menu").count()) === 2, "hovering Add to Album opens its albums beside it");
+
+// Straight back up, to the first line: highlighted at once, the submenu gone.
+const first = page.locator(".row-menu").first().locator(".row-menu-item").first();
+const [fx, fy] = await centre(first);
+await page.mouse.move(fx, fy, { steps: 6 });
+await page.waitForTimeout(40);
+ok((await active()).join() === (await first.innerText()), `moving back up highlights the line under the pointer at once ${JSON.stringify(await active())}`);
+ok((await page.locator(".row-menu").count()) === 1, "and the submenu closes");
+
+// Down again to Add to Album, then diagonally into the submenu across the line below it.
+await page.mouse.move(ax, ay, { steps: 6 });
+await page.waitForTimeout(80);
+const sub = page.locator(".row-menu").nth(1).locator(".row-menu-item").first();
+const [sx, sy] = await centre(sub);
+const below = page.locator(".row-menu").first().locator(".row-menu-item").nth((await page.locator(".row-menu").first().locator(".row-menu-item").allInnerTexts()).findIndex((t) => t.startsWith("Add to Album")) + 1);
+const [, by] = await centre(below);
+// From near Add to Album's right end, down and right toward the submenu, over the line below.
+const ab = await add.boundingBox();
+await page.mouse.move(ab.x + ab.width - 14, ay, { steps: 2 });
+await page.mouse.move(ab.x + ab.width - 4, by, { steps: 2 });
+await page.waitForTimeout(40);
+ok((await page.locator(".row-menu").count()) === 2, "a move heading into the submenu keeps it open as it crosses the line below");
+await page.mouse.move(sx, sy, { steps: 3 });
+await page.waitForTimeout(80);
+ok((await page.locator(".row-menu").count()) === 2 && (await active()).some((t) => t.startsWith("Add to Album")), "and arrives in it, Add to Album still marked");
+await b.close();
+process.exitCode = failures ? 1 : (process.exitCode ?? 0);
diff --git a/src/components/RowMenu.tsx b/src/components/RowMenu.tsx
index abfa9ab..954f7bf 100644
--- a/src/components/RowMenu.tsx
+++ b/src/components/RowMenu.tsx
@@ -87,6 +87,7 @@ function Panel({
onBack,
focusFirst,
onArrive,
+ hostRef,
}: {
entries: MenuEntry[];
anchor: PanelAnchor;
@@ -98,6 +99,8 @@ function Panel({
focusFirst: boolean;
/** The pointer came into this submenu. */
onArrive?: () => void;
+ /** This level's own box, for the level that opened it to aim at. */
+ hostRef?: React.MutableRefObject<HTMLDivElement | null>;
}) {
const box = useRef<HTMLDivElement | null>(null);
const lines = useRef<(HTMLButtonElement | null)[]>([]);
@@ -108,12 +111,38 @@ function Panel({
const [active, setActive] = useState(focusFirst ? 0 : -1);
const [sub, setSub] = useState<{ index: number; byKeyboard: boolean } | null>(null);
/**
- * Menu aim, as macOS has it: with a submenu open, crossing another line on the way to it
- * does not close it at once. The switch waits a moment, and is called off if the pointer
- * arrives in the submenu — moving diagonally from "Add to List" to its lists used to pass
- * over "Edit Contact", close the submenu, and open it again: the flicker.
+ * Menu aim, as macOS has it: with a submenu open, a pointer heading into it — inside the
+ * triangle from where it just was to the submenu's near edge — may cross other lines
+ * without closing it; the switch waits a moment and is called off when it arrives. Any
+ * other move (back up the menu, away from the submenu) switches at once. Waiting on every
+ * move left the menu looking stuck: the lines above did not light as the pointer crossed
+ * them.
*/
const aim = useRef<number | null>(null);
+ const subBox = useRef<HTMLDivElement | null>(null);
+ // Where the pointer last moved, within this level. A move fires a line's mouseenter before
+ // its mousemove, so on entering a line this is still where the pointer came from.
+ const last = useRef<{ x: number; y: number } | null>(null);
+ const heading = (x: number, y: number) => {
+ const from = last.current;
+ const r = subBox.current?.getBoundingClientRect();
+ if (!from || !r) return false;
+ // The submenu's near edge: its left side when it opened to the right, else its right.
+ const edge = r.left >= from.x ? r.left : r.right;
+ // Only a move toward that side is heading there at all.
+ if ((edge - from.x) * (x - from.x) <= 0) return false;
+ // A hand does not move in straight lines: a short submenu — one album — is aimed at
+ // with room above and below it, or a move that dips a few pixels past it closes it.
+ const a = { x: edge, y: r.top - 48 };
+ const c = { x: edge, y: r.bottom + 48 };
+ const side = (p: { x: number; y: number }, q: { x: number; y: number }) => (q.x - p.x) * (y - p.y) - (q.y - p.y) * (x - p.x);
+ const d1 = side(from, a);
+ const d2 = side(a, c);
+ const d3 = side(c, from);
+ const neg = d1 < 0 || d2 < 0 || d3 < 0;
+ const pos = d1 > 0 || d2 > 0 || d3 > 0;
+ return !(neg && pos);
+ };
const stopAim = () => {
if (aim.current !== null) window.clearTimeout(aim.current);
aim.current = null;
@@ -166,12 +195,18 @@ function Panel({
return (
<>
<div
- ref={box}
+ ref={(el) => {
+ box.current = el;
+ if (hostRef) hostRef.current = el;
+ }}
className="row-menu"
role="menu"
tabIndex={-1}
style={pos ? { left: pos.left, top: pos.top } : { left: 0, top: 0, visibility: "hidden" }}
onMouseEnter={onArrive}
+ onMouseMove={(e) => {
+ last.current = { x: e.clientX, y: e.clientY };
+ }}
onKeyDown={(e) => {
// Keys belong to the deepest open level.
if (sub) return;
@@ -208,7 +243,7 @@ function Panel({
aria-haspopup={entry.submenu ? "menu" : undefined}
aria-expanded={entry.submenu ? sub?.index === index : undefined}
className={`row-menu-item${entry.danger ? " is-danger" : ""}${index === active ? " is-active" : ""}${entry.submenu ? " has-submenu" : ""}`}
- onMouseEnter={() => {
+ onMouseEnter={(e) => {
const go = () => {
aim.current = null;
// The submenu going takes its focus with it: the keys come back here.
@@ -218,7 +253,7 @@ function Panel({
setSub(entry.submenu ? { index, byKeyboard: false } : null);
};
stopAim();
- if (sub && sub.index !== index) aim.current = window.setTimeout(go, 280);
+ if (sub && sub.index !== index && heading(e.clientX, e.clientY)) aim.current = window.setTimeout(go, 280);
else go();
}}
onClick={() => choose(index, false)}
@@ -252,6 +287,7 @@ function Panel({
setActive(sub.index);
}}
focusFirst={sub.byKeyboard}
+ hostRef={subBox}
/>
)}
</>