Skip to content

Commit

Permalink
feat: Queued focus (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 authored Jun 2, 2024
1 parent a2a7078 commit 459b54b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/common/src/event_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum EventMessage {
ActionRequestEvent(ActionRequestEvent),
/// Focus the given accessibility NodeID
FocusAccessibilityNode(NodeId),
/// Queue a focus the given accessibility NodeID
QueueFocusAccessibilityNode(NodeId),
/// Focus the next accessibility Node
FocusNextAccessibilityNode,
/// Focus the previous accessibility Node
Expand Down
9 changes: 9 additions & 0 deletions crates/hooks/src/use_focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ impl UseFocus {
}
}

/// Queue a focus to this node
pub fn queue_focus(&mut self) {
if !*self.is_focused.peek() {
self.platform
.send(EventMessage::QueueFocusAccessibilityNode(self.id))
.ok();
}
}

/// Get the node focus ID
pub fn id(&self) -> AccessibilityId {
self.id
Expand Down
10 changes: 10 additions & 0 deletions crates/renderer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct App<State: 'static + Clone> {
pub(crate) plugins: PluginsManager,
pub(crate) measure_layout_on_next_render: bool,
pub(crate) default_fonts: Vec<String>,
pub(crate) queued_focus_node: Option<AccessibilityId>,
}

impl<State: 'static + Clone> App<State> {
Expand Down Expand Up @@ -111,6 +112,7 @@ impl<State: 'static + Clone> App<State> {
plugins,
measure_layout_on_next_render: false,
default_fonts,
queued_focus_node: None,
}
}

Expand Down Expand Up @@ -229,6 +231,10 @@ impl<State: 'static + Clone> App<State> {
rdom,
&mut self.accessibility.accessibility_manager().lock().unwrap(),
);

if let Some(node_id) = self.queued_focus_node.take() {
self.focus_node(node_id)
}
}

/// Send an event
Expand Down Expand Up @@ -287,6 +293,10 @@ impl<State: 'static + Clone> App<State> {
.focus_node(node_id, &self.platform_sender, &self.window_env.window)
}

pub fn queue_focus_node(&mut self, node_id: AccessibilityId) {
self.queued_focus_node = Some(node_id);
}

pub fn focus_next_node(&self, direction: AccessibilityFocusDirection) {
self.accessibility.focus_next_node(
direction,
Expand Down
3 changes: 3 additions & 0 deletions crates/renderer/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub fn run_event_loop<State: Clone>(
Event::UserEvent(EventMessage::FocusAccessibilityNode(node_id)) => {
app.focus_node(node_id);
}
Event::UserEvent(EventMessage::QueueFocusAccessibilityNode(node_id)) => {
app.queue_focus_node(node_id);
}
Event::UserEvent(EventMessage::RequestRerender) => {
app.window_env.window.request_redraw();
}
Expand Down
5 changes: 4 additions & 1 deletion examples/floating_editors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn Editor() -> Element {
let font_weight = if *is_bold.read() { "bold" } else { "normal" };

use_hook(|| {
focus_manager.focus();
focus_manager.queue_focus();
});

let onclick = move |_: MouseEvent| {
Expand All @@ -195,9 +195,12 @@ fn Editor() -> Element {
}
};

let focus_id = focus_manager.attribute();

rsx!(
rect {
onclick,
focus_id,
width: "100%",
height: "100%",
rect {
Expand Down

0 comments on commit 459b54b

Please sign in to comment.