Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Jan 7, 2024
1 parent f811ae7 commit e51b922
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 28 deletions.
2 changes: 2 additions & 0 deletions crates/components/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ pub fn Input<'a>(cx: Scope<'a, InputProps<'a>>) -> Element {
corner_radius: "10",
margin: "{margin}",
cursor_reference: cursor_attr,
focus_id: focus_manager.attribute(cx),
role: "textInput",
main_align: "center",
paragraph {
margin: "8 12",
Expand Down
42 changes: 39 additions & 3 deletions crates/renderer/src/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ use freya_core::{
},
types::FocusSender,
};
use winit::{event::WindowEvent, event_loop::EventLoopProxy, window::Window};
use winit::{
dpi::{LogicalPosition, LogicalSize},
event::WindowEvent,
event_loop::EventLoopProxy,
window::Window,
};

/// Manages the accessibility integration with Accesskit.
pub struct NativeAccessibility {
Expand Down Expand Up @@ -42,17 +47,42 @@ impl NativeAccessibility {
}

/// Focus a new accessibility node
pub fn set_accessibility_focus(&mut self, id: AccessibilityId) {
pub fn set_accessibility_focus(&self, id: AccessibilityId, window: &Window) {
let tree = self
.accessibility_state
.lock()
.unwrap()
.set_focus_with_update(id);
if let Some(tree) = tree {
// Update the IME Cursor area
self.update_ime_position(tree.focus, window);

// Update the adapter
self.accessibility_adapter.update_if_active(|| tree);
}
}

fn update_ime_position(&self, accessibility_id: AccessibilityId, window: &Window) {
let accessibility_state = self.accessibility_state.lock().unwrap();
let node = accessibility_state
.nodes()
.find_map(|(id, n)| {
if *id == accessibility_id {
Some(n)
} else {
None
}
})
.unwrap();
let node_bounds = node.bounds();
if let Some(node_bounds) = node_bounds {
window.set_ime_cursor_area(
LogicalPosition::new(node_bounds.min_x(), node_bounds.min_y()),
LogicalSize::new(node_bounds.width(), node_bounds.height()),
)
}
}

/// Process an Accessibility event
pub fn process_accessibility_event(&mut self, window: &Window, event: &WindowEvent) {
self.accessibility_adapter.process_event(window, event)
Expand All @@ -75,20 +105,26 @@ impl NativeAccessibility {

/// Focus the next accessibility node
pub fn focus_next_node(
&mut self,
&self,
direction: AccessibilityFocusDirection,
focus_sender: &FocusSender,
window: &Window,
) {
let tree = self
.accessibility_state
.lock()
.unwrap()
.set_focus_on_next_node(direction);

// Update the App
focus_sender
.send(tree.focus)
.expect("Failed to focus the Node.");

// Update the IME Cursor area
self.update_ime_position(tree.focus, window);

// Update the Adapter
self.accessibility_adapter.update_if_active(|| tree);
}
}
19 changes: 13 additions & 6 deletions crates/renderer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,22 +336,29 @@ impl<State: 'static + Clone> App<State> {
);
}

pub fn window_env(&mut self) -> &mut WindowEnv<State> {
pub fn window_env_mut(&mut self) -> &mut WindowEnv<State> {
&mut self.window_env
}

pub fn accessibility(&mut self) -> &mut NativeAccessibility {
&mut self.accessibility
pub fn window_env(&self) -> &WindowEnv<State> {
&self.window_env
}

pub fn accessibility(&self) -> &NativeAccessibility {
&self.accessibility
}

pub fn process_accessibility_event(&mut self, event: &WindowEvent) {
self.accessibility
.process_accessibility_event(&self.window_env.window, event)
}

pub fn focus_next_node(&mut self, direction: AccessibilityFocusDirection) {
self.accessibility
.focus_next_node(direction, &self.focus_sender)
pub fn focus_next_node(&self, direction: AccessibilityFocusDirection) {
self.accessibility.focus_next_node(
direction,
&self.focus_sender,
self.window_env().window(),
)
}

pub fn tick(&self) {
Expand Down
33 changes: 16 additions & 17 deletions crates/renderer/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn run_event_loop<State: Clone>(
let mut cursor_pos = CursorPoint::default();
let mut modifiers_state = ModifiersState::empty();

let window_env = app.window_env();
let window_env = app.window_env_mut();

window_env.run_on_setup();

Expand All @@ -41,10 +41,11 @@ pub fn run_event_loop<State: Clone>(
_ = proxy.send_event(EventMessage::PollVDOM);
}
Event::UserEvent(EventMessage::FocusAccessibilityNode(id)) => {
app.accessibility().set_accessibility_focus(id);
app.accessibility()
.set_accessibility_focus(id, app.window_env().window());
}
Event::UserEvent(EventMessage::RequestRerender) => {
app.window_env().window_mut().request_redraw();
app.window_env_mut().window_mut().request_redraw();
}
Event::UserEvent(EventMessage::RequestRedraw) => app.render(&hovered_node),
Event::UserEvent(EventMessage::RequestRelayout) => {
Expand All @@ -58,11 +59,12 @@ pub fn run_event_loop<State: Clone>(
..
})) => {
if Action::Focus == request.action {
app.accessibility().set_accessibility_focus(request.target);
app.accessibility()
.set_accessibility_focus(request.target, app.window_env().window());
}
}
Event::UserEvent(EventMessage::SetCursorIcon(icon)) => {
app.window_env().window.set_cursor_icon(icon)
app.window_env_mut().window.set_cursor_icon(icon)
}
Event::UserEvent(ev) => {
if let EventMessage::UpdateTemplate(template) = ev {
Expand All @@ -78,17 +80,14 @@ pub fn run_event_loop<State: Clone>(
Event::WindowEvent { event, .. } => {
app.process_accessibility_event(&event);
match event {
WindowEvent::Ime(e) => match e {
Ime::Commit(text) => {
app.send_event(FreyaEvent::Keyboard {
name: "keydown".to_string(),
key: Key::Character(text),
code: Code::Unidentified,
modifiers: get_modifiers(modifiers_state),
});
}
_ => {}
},
WindowEvent::Ime(Ime::Commit(text)) => {
app.send_event(FreyaEvent::Keyboard {
name: "keydown".to_string(),
key: Key::Character(text),
code: Code::Unidentified,
modifiers: get_modifiers(modifiers_state),
});
}
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
app.process_layout();
Expand Down Expand Up @@ -205,7 +204,7 @@ pub fn run_event_loop<State: Clone>(
}
}
Event::LoopExiting => {
app.window_env().run_on_exit();
app.window_env_mut().run_on_exit();
}
_ => (),
}
Expand Down
8 changes: 6 additions & 2 deletions crates/renderer/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl<T: Clone> WindowEnv<T> {
.unwrap();

let mut window = window.expect("Could not create window with OpenGL context");
window.set_ime_allowed(true);
let raw_window_handle = window.raw_window_handle();

let context_attributes = ContextAttributesBuilder::new()
Expand Down Expand Up @@ -210,6 +211,11 @@ impl<T: Clone> WindowEnv<T> {
&mut self.window
}

/// Get a reference to the Window.
pub fn window(&self) -> &Window {
&self.window
}

/// Measure the layout
pub fn process_layout(
&mut self,
Expand Down Expand Up @@ -310,8 +316,6 @@ impl<T: Clone> WindowEnv<T> {
if let Some(on_setup) = on_setup {
(on_setup)(self.window_mut())
}

self.window.set_ime_allowed(true);
}

/// Run the `on_exit` callback that was passed to the launch function
Expand Down

0 comments on commit e51b922

Please sign in to comment.