Skip to content

Commit

Permalink
chore: update accesskit
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 authored and jackpot51 committed Feb 9, 2024
1 parent 68b3e49 commit 484ac7b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 70 deletions.
33 changes: 14 additions & 19 deletions accessibility/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Widget and Window IDs.

use std::borrow;
use std::hash::Hash;
use std::sync::atomic::{self, AtomicU64};
use std::{borrow, num::NonZeroU128};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum A11yId {
Window(NonZeroU128),
Window(u64),
Widget(Id),
}

Expand All @@ -20,8 +20,8 @@ pub enum A11yId {
// }
// }

impl From<NonZeroU128> for A11yId {
fn from(id: NonZeroU128) -> Self {
impl From<u64> for A11yId {
fn from(id: u64) -> Self {
Self::Window(id)
}
}
Expand All @@ -46,8 +46,8 @@ impl IdEq for A11yId {

impl From<accesskit::NodeId> for A11yId {
fn from(value: accesskit::NodeId) -> Self {
let val = u128::from(value.0);
if val > u64::MAX as u128 {
let val = u64::from(value.0);
if val > u32::MAX as u64 {
Self::Window(value.0)
} else {
Self::Widget(Id::from(val as u64))
Expand Down Expand Up @@ -110,13 +110,11 @@ impl From<u64> for Id {
}

// Not meant to be used directly
impl From<Id> for NonZeroU128 {
fn from(val: Id) -> NonZeroU128 {
impl From<Id> for u64 {
fn from(val: Id) -> u64 {
match &val.0 {
Internal::Unique(id) => NonZeroU128::try_from(*id as u128).unwrap(),
Internal::Custom(id, _) => {
NonZeroU128::try_from(*id as u128).unwrap()
}
Internal::Unique(id) => *id,
Internal::Custom(id, _) => *id,
// this is a set id, which is not a valid id and will not ever be converted to a NonZeroU128
// so we panic
Internal::Set(_) => {
Expand All @@ -136,14 +134,11 @@ impl ToString for Id {
}
}

// XXX WIndow IDs are made unique by adding u64::MAX to them
// XXX WIndow IDs are made unique by adding u32::MAX to them
/// get window node id that won't conflict with other node ids for the duration of the program
pub fn window_node_id() -> NonZeroU128 {
std::num::NonZeroU128::try_from(
u64::MAX as u128
+ NEXT_WINDOW_ID.fetch_add(1, atomic::Ordering::Relaxed) as u128,
)
.unwrap()
pub fn window_node_id() -> u64 {
u32::MAX as u64
+ NEXT_WINDOW_ID.fetch_add(1, atomic::Ordering::Relaxed) as u64
}

// TODO refactor to make panic impossible?
Expand Down
2 changes: 1 addition & 1 deletion examples/sctk_todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
publish = false

[dependencies]
iced = { path = "../..", default-features=false, features = ["async-std", "wayland", "debug"] }
iced = { path = "../..", default-features=false, features = ["async-std", "wayland", "debug", "a11y"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
iced_core.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion examples/todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false
[dependencies]
iced.workspace = true
iced_core.workspace = true
iced.features = ["async-std", "debug", "winit"]
iced.features = ["async-std", "debug", "winit", "a11y"]

once_cell.workspace = true
serde = { version = "1.0", features = ["derive"] }
Expand Down
7 changes: 3 additions & 4 deletions sctk/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,9 @@ where
);
let focus = focus
.filter(|f_id| window_tree.contains(f_id))
.map(|id| id.into());
adapter.adapter.update(TreeUpdate {
.map(|id| id.into())
.unwrap_or_else(|| tree.root);
adapter.adapter.update_if_active(|| TreeUpdate {
nodes: window_tree.into(),
tree: Some(tree),
focus,
Expand Down Expand Up @@ -1459,8 +1460,6 @@ where
Action::Increment => todo!(),
Action::HideTooltip => todo!(),
Action::ShowTooltip => todo!(),
Action::InvalidateTree => todo!(),
Action::LoadInlineTextBoxes => todo!(),
Action::ReplaceSelectedText => todo!(),
Action::ScrollBackward => todo!(),
Action::ScrollDown => todo!(),
Expand Down
9 changes: 3 additions & 6 deletions sctk/src/event_loop/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ use crate::sctk_event::ActionRequestEvent;
use iced_accessibility::{accesskit, accesskit_unix};
use sctk::reexports::client::protocol::wl_surface::WlSurface;
use sctk::reexports::client::Proxy;
use std::{
num::NonZeroU128,
sync::{Arc, Mutex},
};
use std::sync::{Arc, Mutex};

pub enum A11yWrapper {
Enabled,
Event(ActionRequestEvent),
}

pub struct IcedSctkAdapter {
pub(crate) id: NonZeroU128,
pub(crate) id: u64,
pub(crate) adapter: accesskit_unix::Adapter,
}

Expand All @@ -22,7 +19,7 @@ pub struct IcedSctkActionHandler {
pub(crate) event_list: Arc<Mutex<Vec<A11yWrapper>>>,
}
impl accesskit::ActionHandler for IcedSctkActionHandler {
fn do_action(&self, request: accesskit::ActionRequest) {
fn do_action(&mut self, request: accesskit::ActionRequest) {
let mut event_list = self.event_list.lock().unwrap();
event_list.push(A11yWrapper::Event(
crate::sctk_event::ActionRequestEvent {
Expand Down
13 changes: 5 additions & 8 deletions sctk/src/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,6 @@ where
let event_list = self.a11y_events.clone();
adapter::IcedSctkAdapter {
adapter: Adapter::new(
app_id.unwrap_or_else(|| String::from("None")),
"Iced".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
move || {
event_list
.lock()
Expand All @@ -272,18 +269,18 @@ where
node.set_name(name);
}
let node = node.build(&mut NodeClassSet::lock_global());
let root = NodeId(node_id);
TreeUpdate {
nodes: vec![(NodeId(node_id), node)],
tree: Some(Tree::new(NodeId(node_id))),
focus: None,
nodes: vec![(root, node)],
tree: Some(Tree::new(root)),
focus: root,
}
},
Box::new(adapter::IcedSctkActionHandler {
wl_surface: surface.clone(),
event_list: self.a11y_events.clone(),
}),
)
.unwrap(),
),
id: node_id,
}
}
Expand Down
10 changes: 4 additions & 6 deletions widget/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,7 @@ where
cursor: mouse::Cursor,
) -> iced_accessibility::A11yTree {
use iced_accessibility::{
accesskit::{
Action, CheckedState, NodeBuilder, NodeId, Rect, Role,
},
accesskit::{Action, Checked, NodeBuilder, NodeId, Rect, Role},
A11yNode, A11yTree,
};

Expand Down Expand Up @@ -435,10 +433,10 @@ where
}
None => {}
}
node.set_checked_state(if self.is_checked {
CheckedState::True
node.set_checked(if self.is_checked {
Checked::True
} else {
CheckedState::False
Checked::False
});
if is_hovered {
node.set_hovered();
Expand Down
10 changes: 4 additions & 6 deletions widget/src/toggler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,7 @@ where
cursor: mouse::Cursor,
) -> iced_accessibility::A11yTree {
use iced_accessibility::{
accesskit::{
Action, CheckedState, NodeBuilder, NodeId, Rect, Role,
},
accesskit::{Action, Checked, NodeBuilder, NodeId, Rect, Role},
A11yNode, A11yTree,
};

Expand Down Expand Up @@ -462,10 +460,10 @@ where
}
None => {}
}
node.set_checked_state(if self.is_toggled {
CheckedState::True
node.set_checked(if self.is_toggled {
Checked::True
} else {
CheckedState::False
Checked::False
});
if is_hovered {
node.set_hovered();
Expand Down
23 changes: 7 additions & 16 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,11 @@ async fn run_instance<A, E, C>(
let mut node = NodeBuilder::new(Role::Window);
node.set_name(title.clone());
let node = node.build(&mut iced_accessibility::accesskit::NodeClassSet::lock_global());
let root = NodeId(node_id);
TreeUpdate {
nodes: vec![(NodeId(node_id), node)],
tree: Some(Tree::new(NodeId(node_id))),
focus: None,
nodes: vec![(root, node)],
tree: Some(Tree::new(root)),
focus: root,
}
},
proxy.clone(),
Expand Down Expand Up @@ -421,17 +422,6 @@ async fn run_instance<A, E, C>(
)),
));
}
event::Event::PlatformSpecific(event::PlatformSpecific::MacOS(
event::MacOS::ReceivedUrl(url),
)) => {
use crate::core::event;

events.push(Event::PlatformSpecific(
event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(
url,
)),
));
}
event::Event::UserEvent(message) => {
match message {
UserEventWrapper::Message(m) => messages.push(m),
Expand Down Expand Up @@ -617,8 +607,9 @@ async fn run_instance<A, E, C>(
// TODO maybe optimize this?
let focus = focus
.filter(|f_id| window_tree.contains(f_id))
.map(|id| id.into());
adapter.update(TreeUpdate {
.map(|id| id.into())
.unwrap_or_else(|| tree.root);
adapter.update_if_active(|| TreeUpdate {
nodes: window_tree.into(),
tree: Some(tree),
focus,
Expand Down
7 changes: 4 additions & 3 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,11 @@ async fn run_instance<A, E, C>(
let mut node = NodeBuilder::new(Role::Window);
node.set_name(title.clone());
let node = node.build(&mut iced_accessibility::accesskit::NodeClassSet::lock_global());
let root = NodeId(node_id);
TreeUpdate {
nodes: vec![(NodeId(node_id), node)],
tree: Some(Tree::new(NodeId(node_id))),
focus: None,
nodes: vec![(root, node)],
tree: Some(Tree::new(root)),
focus: root,
}
},
proxy.clone(),
Expand Down

0 comments on commit 484ac7b

Please sign in to comment.