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
7 changes: 6 additions & 1 deletion src/components/menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/components/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 3 additions & 1 deletion src/components/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,25 @@ pub struct Focus {
kind: Kind,
unit: Unit,
keys: CopyValue<Option<KeyFn>>,
/// 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<bool>,
}

/// A control that can have the keys: `keys` is called with each key while it
/// does, and says whether it used it. Call it on every render, so the
/// 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
Expand All @@ -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 {
Expand Down
Loading