From a752f91a40499ce6f70a1d8c023aa2a8712b33c9 Mon Sep 17 00:00:00 2001 From: adammharris Date: Tue, 17 Nov 2026 04:23:03 +0000 Subject: [PATCH] perf: a menu's step and a press draw only the controls they change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `MenuButton` read the whole layer to know whether its menu was open, and `Modal` and `PhotoViewer` read it for the page's size: every step of the pointer over an open menu, and every toast, drew each of them again. Each focusable control read which control has the keys, so a press anywhere drew every control on the page. All four are memos now, which tell only the controls whose answer changed. Found while timing Diaryx's entry menus on a library of 2,000 pages, where a search draws a "…" for every hit. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P3EBi1gz7evG9fSSg22tcL --- src/components/menus.rs | 7 ++++++- src/components/modal.rs | 4 +++- src/components/viewer.rs | 4 +++- src/unit.rs | 11 +++++++++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/components/menus.rs b/src/components/menus.rs index 7cf2690..ffd408a 100644 --- a/src/components/menus.rs +++ b/src/components/menus.rs @@ -127,7 +127,12 @@ pub fn MenuButton( false }); opener.owner.clone().set(focus.id); - let expanded = unit.layer.read().menu_owner() == Some(focus.id); + // Whether its menu is open, as a memo: the layer changes with every + // step of the pointer over an open menu, and a button whose menu did + // not open or close is not drawn again for it. + let id = focus.id; + let expanded = use_memo(move || unit.layer.read().menu_owner() == Some(id)); + let expanded = expanded(); let entries2 = entries.clone(); rsx! { button { diff --git a/src/components/modal.rs b/src/components/modal.rs index aba5db7..014311d 100644 --- a/src/components/modal.rs +++ b/src/components/modal.rs @@ -127,7 +127,9 @@ pub fn Modal( drag.set(None); } - let viewport = unit.layer.read().viewport; + // The page's size alone, as a memo: the layer changes with every step + // of the pointer over a menu, and this is not drawn again for that. + let viewport = use_memo(move || unit.layer.read().viewport)(); let sheet = match placement { Placement::Center => false, Placement::Sheet => sheet_suits(viewport, *unit.density.peek()), diff --git a/src/components/viewer.rs b/src/components/viewer.rs index acb530f..f9a8672 100644 --- a/src/components/viewer.rs +++ b/src/components/viewer.rs @@ -283,7 +283,9 @@ pub fn PhotoViewer( // The stage: the window inside its safe area, if the host said how // large the window is. let (safe, _) = unit.edges(); - let viewport = unit.layer.read().viewport; + // The page's size alone, as a memo: the layer changes with every step + // of the pointer over a menu, and this is not drawn again for that. + let viewport = use_memo(move || unit.layer.read().viewport)(); let stage = ( viewport.0 - safe.left - safe.right, viewport.1 - safe.top - safe.bottom, diff --git a/src/unit.rs b/src/unit.rs index 3785c3a..df212a6 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -380,6 +380,10 @@ pub struct Focus { kind: Kind, unit: Unit, keys: CopyValue>, + /// Whether this control has the keys. A memo, so that a control is + /// drawn again when it gains or loses them, and not every control on + /// the page each time they move: a press anywhere moves them. + has: Memo, } /// A control that can have the keys: `keys` is called with each key while it @@ -387,11 +391,14 @@ pub struct Focus { /// handler sees the control's latest props. pub fn use_focus(kind: Kind, keys: impl Fn(&Key, Modifiers) -> bool + 'static) -> Focus { let unit = use_unit(); + let id = use_hook(next_id); + let has = use_memo(move || *unit.focused.read() == Some(id)); let f = use_hook(|| Focus { - id: next_id(), + id, kind, unit, keys: CopyValue::new(None), + has, }); f.keys.clone().set(Some(Rc::new(keys))); // A control taken out of the page gives up the keys: its handler is @@ -404,7 +411,7 @@ impl Focus { /// Whether this control has the keys — read, so the control redraws its /// ring when that changes. pub fn is_focused(&self) -> bool { - self.unit.is_focused(self.id) + *self.has.read() } fn focused(&self, dom: bool) -> Focused {