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 {