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.

Keys: whether a menu or sheet is open is known without searching the page

Yashwanth Kamireddi1adde71c5753
diff --git a/src/lib/screen.ts b/src/lib/screen.ts
index 10beafc..01f0c25 100644
--- a/src/lib/screen.ts
+++ b/src/lib/screen.ts
@@ -21,11 +21,6 @@ export function useActiveRef() {
return ref;
}
-/**
- * Whether a menu, a picker's list or a sheet is open in front, which answers its own keys. A
- * screen's own list of rows (a list box marked `data-rows`, as Notes and Contacts draw theirs)
- * is not one, and nothing in a screen kept out of sight counts.
- */
/** Whether a modal sheet is up (a confirmation, Keyboard Shortcuts, an import): the keys that
* go anywhere — Ctrl+K, Ctrl+, — wait until it is answered, rather than taking the focus out
* of it or changing the screen under it. */
@@ -36,9 +31,57 @@ export function modalOpen(): boolean {
return false;
}
+const OVERLAY = '[role="menu"], [role="listbox"], [role="dialog"], [role="alertdialog"], .confirm';
+
+/**
+ * Every element that may be an overlay, kept as the page changes rather than looked for each
+ * time. Looked for on every key, a search of the whole page cost 4–5 ms an arrow press with
+ * an address book or a folder of 3,000 rows on screen — most of each step. Here the page is
+ * searched once; after that only what is added, or given a role or class, is looked at, and
+ * what has gone is dropped as it is met.
+ */
+let candidates: { found: Set<Element>; watch: MutationObserver } | null = null;
+/** What was added, or given a role or class, that may be an overlay, put with the rest. */
+function takeChanges(found: Set<Element>, records: MutationRecord[]) {
+ for (const r of records) {
+ if (r.type === "attributes") {
+ if ((r.target as Element).matches(OVERLAY)) found.add(r.target as Element);
+ continue;
+ }
+ r.addedNodes.forEach((node) => {
+ if (node.nodeType !== Node.ELEMENT_NODE) return;
+ const el = node as Element;
+ if (el.matches(OVERLAY)) found.add(el);
+ if (el.firstElementChild) el.querySelectorAll(OVERLAY).forEach((x) => found.add(x));
+ });
+ }
+}
+function overlayCandidates(): Set<Element> {
+ if (!candidates) {
+ const found = new Set<Element>(document.querySelectorAll(OVERLAY));
+ const watch = new MutationObserver((records) => takeChanges(found, records));
+ watch.observe(document.documentElement, { childList: true, subtree: true, attributes: true, attributeFilter: ["role", "class"] });
+ candidates = { found, watch };
+ }
+ // What changed since the observer last ran, taken now: an overlay drawn a moment ago, in the
+ // same task, counts as surely as one drawn long before.
+ takeChanges(candidates.found, candidates.watch.takeRecords());
+ return candidates.found;
+}
+
+/**
+ * Whether a menu, a picker's list or a sheet is open in front, which answers its own keys. A
+ * screen's own list of rows (a list box marked `data-rows`, as Notes and Contacts draw theirs)
+ * is not one, and nothing in a screen kept out of sight counts.
+ */
export function overlayOpen(): boolean {
- const open = document.querySelectorAll('[role="menu"], [role="listbox"], [role="dialog"], [role="alertdialog"], .confirm');
- for (const el of open) {
+ const found = overlayCandidates();
+ for (const el of found) {
+ // Gone from the page, or no longer one (its role or class changed): let go of it.
+ if (!el.isConnected || !el.matches(OVERLAY)) {
+ found.delete(el);
+ continue;
+ }
if (el.matches("[data-rows]") || el.closest("[inert], [hidden]")) continue;
return true;
}