feat(client): redesign native UI, themes, settings and avatars
Separate conversation drafts and voice view state, add native settings and restricted CSS themes, implement validated avatar cropping, and refresh dependencies with documented desktop compatibility backports.
This commit is contained in:
parent
44d284a568
commit
8e5ab56611
276 changed files with 53546 additions and 3399 deletions
14
patches/accesskit_atspi_common/src/action.rs
vendored
Normal file
14
patches/accesskit_atspi_common/src/action.rs
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2022 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zbus::wire::Type;
|
||||
|
||||
#[derive(Deserialize, Serialize, Type)]
|
||||
pub struct Action {
|
||||
pub localized_name: String,
|
||||
pub description: String,
|
||||
pub key_binding: String,
|
||||
}
|
||||
822
patches/accesskit_atspi_common/src/adapter.rs
vendored
Normal file
822
patches/accesskit_atspi_common/src/adapter.rs
vendored
Normal file
|
|
@ -0,0 +1,822 @@
|
|||
// Copyright 2022 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
// Derived from Chromium's accessibility abstraction.
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE.chromium file.
|
||||
|
||||
use crate::{
|
||||
AdapterCallback, CacheEvent, Event, ObjectEvent, WindowEvent,
|
||||
context::{ActionHandlerNoMut, ActionHandlerWrapper, AppContext, Context},
|
||||
filters::filter,
|
||||
node::{NodeIdOrRoot, NodeWrapper, PlatformNode, PlatformRoot},
|
||||
util::WindowBounds,
|
||||
};
|
||||
use accesskit::{ActionHandler, Role, TreeUpdate};
|
||||
use accesskit_consumer::{FilterResult, Node, NodeId, Tree, TreeChangeHandler, TreeState};
|
||||
use atspi_common::{InterfaceSet, Politeness, State};
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
sync::{
|
||||
Arc, RwLock,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
},
|
||||
};
|
||||
|
||||
struct AdapterChangeHandler<'a> {
|
||||
adapter: &'a Adapter,
|
||||
added_nodes: HashSet<NodeId>,
|
||||
removed_nodes: HashSet<NodeId>,
|
||||
checked_text_change: HashSet<NodeId>,
|
||||
selection_changed: HashSet<NodeId>,
|
||||
}
|
||||
|
||||
impl<'a> AdapterChangeHandler<'a> {
|
||||
fn new(adapter: &'a Adapter) -> Self {
|
||||
Self {
|
||||
adapter,
|
||||
added_nodes: HashSet::new(),
|
||||
removed_nodes: HashSet::new(),
|
||||
checked_text_change: HashSet::new(),
|
||||
selection_changed: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_node(&mut self, node: &Node) {
|
||||
let id = node.id();
|
||||
if self.added_nodes.contains(&id) {
|
||||
return;
|
||||
}
|
||||
self.added_nodes.insert(id);
|
||||
|
||||
let role = node.role();
|
||||
let is_root = node.is_root();
|
||||
let wrapper = NodeWrapper(node);
|
||||
let interfaces = wrapper.interfaces();
|
||||
self.adapter.register_interfaces(node.id(), interfaces);
|
||||
self.adapter.emit_cache_added(node.id());
|
||||
if is_root && role == Role::Window {
|
||||
let adapter_index = self
|
||||
.adapter
|
||||
.context
|
||||
.read_app_context()
|
||||
.adapter_index(self.adapter.id)
|
||||
.unwrap();
|
||||
self.adapter.window_created(adapter_index, node.id());
|
||||
}
|
||||
|
||||
let live = wrapper.live();
|
||||
if live != Politeness::None {
|
||||
if let Some(name) = wrapper.name() {
|
||||
self.adapter
|
||||
.emit_object_event(node.id(), ObjectEvent::Announcement(name, live));
|
||||
}
|
||||
}
|
||||
if let Some(true) = node.is_selected() {
|
||||
self.enqueue_selection_changed_if_needed(node);
|
||||
}
|
||||
}
|
||||
|
||||
fn add_subtree(&mut self, node: &Node) {
|
||||
self.add_node(node);
|
||||
for child in node.filtered_children(&filter) {
|
||||
self.add_subtree(&child);
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_node(&mut self, node: &Node) {
|
||||
let id = node.id();
|
||||
if self.removed_nodes.contains(&id) {
|
||||
return;
|
||||
}
|
||||
self.removed_nodes.insert(id);
|
||||
|
||||
let role = node.role();
|
||||
let is_root = node.is_root();
|
||||
let wrapper = NodeWrapper(node);
|
||||
if is_root && role == Role::Window {
|
||||
self.adapter.window_destroyed(node.id());
|
||||
}
|
||||
self.adapter
|
||||
.emit_object_event(node.id(), ObjectEvent::StateChanged(State::Defunct, true));
|
||||
self.adapter.emit_cache_removed(node.id());
|
||||
self.adapter
|
||||
.unregister_interfaces(node.id(), wrapper.interfaces());
|
||||
if let Some(true) = node.is_selected() {
|
||||
self.enqueue_selection_changed_if_needed(node);
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_subtree(&mut self, node: &Node) {
|
||||
for child in node.filtered_children(&filter) {
|
||||
self.remove_subtree(&child);
|
||||
}
|
||||
self.remove_node(node);
|
||||
}
|
||||
|
||||
fn emit_text_change_if_needed_parent(&mut self, old_node: &Node, new_node: &Node) {
|
||||
if !new_node.supports_text_ranges() || !old_node.supports_text_ranges() {
|
||||
return;
|
||||
}
|
||||
let id = new_node.id();
|
||||
if self.checked_text_change.contains(&id) {
|
||||
return;
|
||||
}
|
||||
self.checked_text_change.insert(id);
|
||||
let old_text = old_node.document_range().text();
|
||||
let new_text = new_node.document_range().text();
|
||||
|
||||
let mut old_chars = old_text.chars();
|
||||
let mut new_chars = new_text.chars();
|
||||
let mut prefix_usv_count = 0;
|
||||
let mut prefix_byte_count = 0;
|
||||
loop {
|
||||
match (old_chars.next(), new_chars.next()) {
|
||||
(Some(old_char), Some(new_char)) if old_char == new_char => {
|
||||
prefix_usv_count += 1;
|
||||
prefix_byte_count += new_char.len_utf8();
|
||||
}
|
||||
(None, None) => return,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
|
||||
let suffix_byte_count = old_text[prefix_byte_count..]
|
||||
.chars()
|
||||
.rev()
|
||||
.zip(new_text[prefix_byte_count..].chars().rev())
|
||||
.take_while(|(old_char, new_char)| old_char == new_char)
|
||||
.fold(0, |count, (c, _)| count + c.len_utf8());
|
||||
|
||||
let old_content = &old_text[prefix_byte_count..old_text.len() - suffix_byte_count];
|
||||
if let Ok(length) = old_content.chars().count().try_into() {
|
||||
if length > 0 {
|
||||
self.adapter.emit_object_event(
|
||||
id,
|
||||
ObjectEvent::TextRemoved {
|
||||
start_index: prefix_usv_count,
|
||||
length,
|
||||
content: old_content.to_string(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let new_content = &new_text[prefix_byte_count..new_text.len() - suffix_byte_count];
|
||||
if let Ok(length) = new_content.chars().count().try_into() {
|
||||
if length > 0 {
|
||||
self.adapter.emit_object_event(
|
||||
id,
|
||||
ObjectEvent::TextInserted {
|
||||
start_index: prefix_usv_count,
|
||||
length,
|
||||
content: new_content.to_string(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_text_change_if_needed(&mut self, old_node: &Node, new_node: &Node) {
|
||||
if let Role::TextRun | Role::GenericContainer = new_node.role() {
|
||||
if let (Some(old_parent), Some(new_parent)) = (
|
||||
old_node.filtered_parent(&filter),
|
||||
new_node.filtered_parent(&filter),
|
||||
) {
|
||||
self.emit_text_change_if_needed_parent(&old_parent, &new_parent);
|
||||
}
|
||||
} else {
|
||||
self.emit_text_change_if_needed_parent(old_node, new_node);
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_text_selection_change(&self, old_node: Option<&Node>, new_node: &Node) {
|
||||
if !new_node.supports_text_ranges() {
|
||||
return;
|
||||
}
|
||||
let Some(old_node) = old_node else {
|
||||
if let Some(selection) = new_node.text_selection() {
|
||||
if !selection.is_degenerate() {
|
||||
self.adapter
|
||||
.emit_object_event(new_node.id(), ObjectEvent::TextSelectionChanged);
|
||||
}
|
||||
}
|
||||
if let Some(selection_focus) = new_node.text_selection_focus() {
|
||||
if let Ok(offset) = selection_focus.to_global_usv_index().try_into() {
|
||||
self.adapter
|
||||
.emit_object_event(new_node.id(), ObjectEvent::CaretMoved(offset));
|
||||
}
|
||||
}
|
||||
return;
|
||||
};
|
||||
if !old_node.is_focused() || new_node.raw_text_selection() == old_node.raw_text_selection()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(selection) = new_node.text_selection() {
|
||||
if !selection.is_degenerate()
|
||||
|| old_node
|
||||
.text_selection()
|
||||
.map(|selection| !selection.is_degenerate())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
self.adapter
|
||||
.emit_object_event(new_node.id(), ObjectEvent::TextSelectionChanged);
|
||||
}
|
||||
}
|
||||
|
||||
let old_caret_position = old_node
|
||||
.raw_text_selection()
|
||||
.map(|selection| selection.focus);
|
||||
let new_caret_position = new_node
|
||||
.raw_text_selection()
|
||||
.map(|selection| selection.focus);
|
||||
if old_caret_position != new_caret_position {
|
||||
if let Some(selection_focus) = new_node.text_selection_focus() {
|
||||
if let Ok(offset) = selection_focus.to_global_usv_index().try_into() {
|
||||
self.adapter
|
||||
.emit_object_event(new_node.id(), ObjectEvent::CaretMoved(offset));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn enqueue_selection_changed_if_needed_parent(&mut self, node: Node) {
|
||||
if !node.is_container_with_selectable_children() {
|
||||
return;
|
||||
}
|
||||
let id = node.id();
|
||||
if self.selection_changed.contains(&id) {
|
||||
return;
|
||||
}
|
||||
self.selection_changed.insert(id);
|
||||
}
|
||||
|
||||
fn enqueue_selection_changed_if_needed(&mut self, node: &Node) {
|
||||
if !node.is_item_like() {
|
||||
return;
|
||||
}
|
||||
if let Some(node) = node.selection_container(&filter) {
|
||||
self.enqueue_selection_changed_if_needed_parent(node);
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_selection_changed(&mut self) {
|
||||
for id in self.selection_changed.iter() {
|
||||
if self.removed_nodes.contains(id) {
|
||||
continue;
|
||||
}
|
||||
self.adapter
|
||||
.emit_object_event(*id, ObjectEvent::SelectionChanged);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeChangeHandler for AdapterChangeHandler<'_> {
|
||||
fn node_added(&mut self, node: &Node) {
|
||||
if filter(node) == FilterResult::Include {
|
||||
self.add_node(node);
|
||||
}
|
||||
}
|
||||
|
||||
fn node_updated(&mut self, old_node: &Node, new_node: &Node) {
|
||||
self.emit_text_change_if_needed(old_node, new_node);
|
||||
let filter_old = filter(old_node);
|
||||
let filter_new = filter(new_node);
|
||||
if filter_new != filter_old {
|
||||
if filter_new == FilterResult::Include {
|
||||
if filter_old == FilterResult::ExcludeSubtree {
|
||||
self.add_subtree(new_node);
|
||||
} else {
|
||||
self.add_node(new_node);
|
||||
}
|
||||
} else if filter_old == FilterResult::Include {
|
||||
if filter_new == FilterResult::ExcludeSubtree {
|
||||
self.remove_subtree(old_node);
|
||||
} else {
|
||||
self.remove_node(old_node);
|
||||
}
|
||||
}
|
||||
} else if filter_new == FilterResult::Include {
|
||||
let old_wrapper = NodeWrapper(old_node);
|
||||
let new_wrapper = NodeWrapper(new_node);
|
||||
let old_interfaces = old_wrapper.interfaces();
|
||||
let new_interfaces = new_wrapper.interfaces();
|
||||
let kept_interfaces = old_interfaces & new_interfaces;
|
||||
self.adapter
|
||||
.unregister_interfaces(new_wrapper.id(), old_interfaces ^ kept_interfaces);
|
||||
self.adapter
|
||||
.register_interfaces(new_node.id(), new_interfaces ^ kept_interfaces);
|
||||
let bounds = *self.adapter.context.read_root_window_bounds();
|
||||
new_wrapper.notify_changes(&bounds, self.adapter, &old_wrapper);
|
||||
self.emit_text_selection_change(Some(old_node), new_node);
|
||||
if new_node.is_selected() != old_node.is_selected() {
|
||||
self.enqueue_selection_changed_if_needed(new_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn focus_moved(&mut self, old_node: Option<&Node>, new_node: Option<&Node>) {
|
||||
if let (None, Some(new_node)) = (old_node, new_node) {
|
||||
if let Some(root_window) = root_window(new_node.tree_state) {
|
||||
self.adapter.window_activated(&NodeWrapper(&root_window));
|
||||
}
|
||||
} else if let (Some(old_node), None) = (old_node, new_node) {
|
||||
if let Some(root_window) = root_window(old_node.tree_state) {
|
||||
self.adapter.window_deactivated(&NodeWrapper(&root_window));
|
||||
}
|
||||
}
|
||||
if let Some(node) = new_node {
|
||||
self.adapter
|
||||
.emit_object_event(node.id(), ObjectEvent::StateChanged(State::Focused, true));
|
||||
self.emit_text_selection_change(None, node);
|
||||
}
|
||||
if let Some(node) = old_node {
|
||||
self.adapter
|
||||
.emit_object_event(node.id(), ObjectEvent::StateChanged(State::Focused, false));
|
||||
}
|
||||
}
|
||||
|
||||
fn node_removed(&mut self, node: &Node) {
|
||||
if filter(node) == FilterResult::Include {
|
||||
self.remove_node(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static NEXT_ADAPTER_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
/// If you use this function, you must ensure that only one adapter at a time
|
||||
/// has a given ID.
|
||||
pub fn next_adapter_id() -> usize {
|
||||
NEXT_ADAPTER_ID.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub struct Adapter {
|
||||
id: usize,
|
||||
callback: Box<dyn AdapterCallback + Send + Sync>,
|
||||
context: Arc<Context>,
|
||||
}
|
||||
|
||||
impl Debug for Adapter {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Adapter")
|
||||
.field("id", &self.id)
|
||||
.field("callback", &"AdapterCallback")
|
||||
.field("context", &self.context)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Adapter {
|
||||
pub fn new(
|
||||
app_context: &Arc<RwLock<AppContext>>,
|
||||
callback: impl 'static + AdapterCallback + Send + Sync,
|
||||
initial_state: TreeUpdate,
|
||||
is_window_focused: bool,
|
||||
root_window_bounds: WindowBounds,
|
||||
action_handler: impl 'static + ActionHandler + Send,
|
||||
) -> Self {
|
||||
let id = next_adapter_id();
|
||||
Self::with_id(
|
||||
id,
|
||||
app_context,
|
||||
callback,
|
||||
initial_state,
|
||||
is_window_focused,
|
||||
root_window_bounds,
|
||||
action_handler,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn with_id(
|
||||
id: usize,
|
||||
app_context: &Arc<RwLock<AppContext>>,
|
||||
callback: impl 'static + AdapterCallback + Send + Sync,
|
||||
initial_state: TreeUpdate,
|
||||
is_window_focused: bool,
|
||||
root_window_bounds: WindowBounds,
|
||||
action_handler: impl 'static + ActionHandler + Send,
|
||||
) -> Self {
|
||||
Self::with_wrapped_action_handler(
|
||||
id,
|
||||
app_context,
|
||||
callback,
|
||||
initial_state,
|
||||
is_window_focused,
|
||||
root_window_bounds,
|
||||
Arc::new(ActionHandlerWrapper::new(action_handler)),
|
||||
)
|
||||
}
|
||||
|
||||
/// This is an implementation detail of `accesskit_unix`, required for
|
||||
/// robust state transitions with minimal overhead.
|
||||
pub fn with_wrapped_action_handler(
|
||||
id: usize,
|
||||
app_context: &Arc<RwLock<AppContext>>,
|
||||
callback: impl 'static + AdapterCallback + Send + Sync,
|
||||
initial_state: TreeUpdate,
|
||||
is_window_focused: bool,
|
||||
root_window_bounds: WindowBounds,
|
||||
action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
|
||||
) -> Self {
|
||||
let tree = Tree::new(initial_state, is_window_focused);
|
||||
let focus_id = tree.state().focus().map(|node| node.id());
|
||||
let context = Context::new(app_context, tree, action_handler, root_window_bounds);
|
||||
context.write_app_context().push_adapter(id, &context);
|
||||
let adapter = Self {
|
||||
id,
|
||||
callback: Box::new(callback),
|
||||
context,
|
||||
};
|
||||
adapter.register_tree();
|
||||
if let Some(id) = focus_id {
|
||||
adapter.emit_object_event(id, ObjectEvent::StateChanged(State::Focused, true));
|
||||
}
|
||||
adapter
|
||||
}
|
||||
|
||||
fn register_tree(&self) {
|
||||
fn add_children(node: Node<'_>, to_add: &mut Vec<(NodeId, InterfaceSet)>) {
|
||||
for child in node.filtered_children(&filter) {
|
||||
let child_id = child.id();
|
||||
let wrapper = NodeWrapper(&child);
|
||||
let interfaces = wrapper.interfaces();
|
||||
to_add.push((child_id, interfaces));
|
||||
add_children(child, to_add);
|
||||
}
|
||||
}
|
||||
|
||||
let mut objects_to_add = Vec::new();
|
||||
|
||||
let (adapter_index, root_id) = {
|
||||
let tree = self.context.read_tree();
|
||||
let tree_state = tree.state();
|
||||
let mut app_context = self.context.write_app_context();
|
||||
app_context.toolkit_name = Some(tree_state.toolkit_name().to_string());
|
||||
app_context.toolkit_version = tree_state.toolkit_version().map(|s| s.to_string());
|
||||
let adapter_index = app_context.adapter_index(self.id).unwrap();
|
||||
let root = tree_state.root();
|
||||
let root_id = root.id();
|
||||
let wrapper = NodeWrapper(&root);
|
||||
objects_to_add.push((root_id, wrapper.interfaces()));
|
||||
add_children(root, &mut objects_to_add);
|
||||
(adapter_index, root_id)
|
||||
};
|
||||
|
||||
for (id, interfaces) in objects_to_add {
|
||||
self.register_interfaces(id, interfaces);
|
||||
if id == root_id {
|
||||
self.window_created(adapter_index, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn platform_node(&self, id: NodeId) -> PlatformNode {
|
||||
PlatformNode::new(&self.context, self.id, id)
|
||||
}
|
||||
|
||||
pub fn root_id(&self) -> NodeId {
|
||||
self.context.read_tree().state().root_id()
|
||||
}
|
||||
|
||||
pub fn platform_root(&self) -> PlatformRoot {
|
||||
PlatformRoot::new(&self.context.app_context)
|
||||
}
|
||||
|
||||
fn register_interfaces(&self, id: NodeId, new_interfaces: InterfaceSet) {
|
||||
self.callback.register_interfaces(self, id, new_interfaces);
|
||||
}
|
||||
|
||||
fn unregister_interfaces(&self, id: NodeId, old_interfaces: InterfaceSet) {
|
||||
self.callback
|
||||
.unregister_interfaces(self, id, old_interfaces);
|
||||
}
|
||||
|
||||
pub(crate) fn emit_object_event(&self, target: NodeId, event: ObjectEvent) {
|
||||
let target = NodeIdOrRoot::Node(target);
|
||||
self.callback
|
||||
.emit_event(self, Event::Object { target, event });
|
||||
}
|
||||
|
||||
fn emit_root_object_event(&self, event: ObjectEvent) {
|
||||
let target = NodeIdOrRoot::Root;
|
||||
self.callback
|
||||
.emit_event(self, Event::Object { target, event });
|
||||
}
|
||||
|
||||
fn emit_cache_added(&self, target: NodeId) {
|
||||
self.callback
|
||||
.emit_event(self, Event::Cache(CacheEvent::Added(target)));
|
||||
}
|
||||
|
||||
fn emit_cache_removed(&self, target: NodeId) {
|
||||
self.callback
|
||||
.emit_event(self, Event::Cache(CacheEvent::Removed(target)));
|
||||
}
|
||||
|
||||
pub fn set_root_window_bounds(&mut self, new_bounds: WindowBounds) {
|
||||
let mut bounds = self.context.root_window_bounds.write().unwrap();
|
||||
*bounds = new_bounds;
|
||||
}
|
||||
|
||||
pub fn update(&mut self, update: TreeUpdate) {
|
||||
let mut handler = AdapterChangeHandler::new(self);
|
||||
let mut tree = self.context.tree.write().unwrap();
|
||||
tree.update_and_process_changes(update, &mut handler);
|
||||
drop(tree);
|
||||
handler.emit_selection_changed();
|
||||
}
|
||||
|
||||
pub fn update_window_focus_state(&mut self, is_focused: bool) {
|
||||
let mut handler = AdapterChangeHandler::new(self);
|
||||
let mut tree = self.context.tree.write().unwrap();
|
||||
tree.update_host_focus_state_and_process_changes(is_focused, &mut handler);
|
||||
}
|
||||
|
||||
fn window_created(&self, adapter_index: usize, window: NodeId) {
|
||||
self.emit_root_object_event(ObjectEvent::ChildAdded(adapter_index, window));
|
||||
}
|
||||
|
||||
fn window_activated(&self, window: &NodeWrapper<'_>) {
|
||||
self.callback.emit_event(
|
||||
self,
|
||||
Event::Window {
|
||||
target: window.id(),
|
||||
name: window.name().unwrap_or_default(),
|
||||
event: WindowEvent::Activated,
|
||||
},
|
||||
);
|
||||
self.emit_object_event(window.id(), ObjectEvent::StateChanged(State::Active, true));
|
||||
self.emit_root_object_event(ObjectEvent::ActiveDescendantChanged(window.id()));
|
||||
}
|
||||
|
||||
fn window_deactivated(&self, window: &NodeWrapper<'_>) {
|
||||
self.callback.emit_event(
|
||||
self,
|
||||
Event::Window {
|
||||
target: window.id(),
|
||||
name: window.name().unwrap_or_default(),
|
||||
event: WindowEvent::Deactivated,
|
||||
},
|
||||
);
|
||||
self.emit_object_event(window.id(), ObjectEvent::StateChanged(State::Active, false));
|
||||
}
|
||||
|
||||
fn window_destroyed(&self, window: NodeId) {
|
||||
self.emit_root_object_event(ObjectEvent::ChildRemoved(window));
|
||||
}
|
||||
|
||||
pub fn id(&self) -> usize {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn is_window_focused(&self) -> bool {
|
||||
self.context.read_tree().state().is_host_focused()
|
||||
}
|
||||
|
||||
pub fn root_window_bounds(&self) -> WindowBounds {
|
||||
*self.context.read_root_window_bounds()
|
||||
}
|
||||
|
||||
/// This is an implementation detail of `accesskit_unix`, required for
|
||||
/// robust state transitions with minimal overhead.
|
||||
pub fn wrapped_action_handler(&self) -> Arc<dyn ActionHandlerNoMut + Send + Sync> {
|
||||
Arc::clone(&self.context.action_handler)
|
||||
}
|
||||
}
|
||||
|
||||
fn root_window(current_state: &TreeState) -> Option<Node<'_>> {
|
||||
const WINDOW_ROLES: &[Role] = &[Role::AlertDialog, Role::Dialog, Role::Window];
|
||||
let root = current_state.root();
|
||||
if WINDOW_ROLES.contains(&root.role()) {
|
||||
Some(root)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Adapter {
|
||||
fn drop(&mut self) {
|
||||
let root_id = self.context.read_tree().state().root_id();
|
||||
self.window_destroyed(root_id);
|
||||
// Note: We deliberately do the following here, not in a Drop
|
||||
// implementation on context, because AppContext owns a second
|
||||
// strong reference to Context, and we need that to be released.
|
||||
self.context.write_app_context().remove_adapter(self.id);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Adapter;
|
||||
use crate::{AdapterCallback, AppContext, CacheEvent, Event, InterfaceSet, WindowBounds};
|
||||
use accesskit::{
|
||||
ActionHandler, ActionRequest, Node, NodeId as LocalNodeId, Role, Tree, TreeId, TreeUpdate,
|
||||
};
|
||||
use accesskit_consumer::NodeId;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum CacheOp {
|
||||
Added(NodeId),
|
||||
Removed(NodeId),
|
||||
}
|
||||
|
||||
struct CapturingCallback {
|
||||
ops: Arc<Mutex<Vec<CacheOp>>>,
|
||||
}
|
||||
|
||||
impl AdapterCallback for CapturingCallback {
|
||||
fn register_interfaces(&self, _: &Adapter, _: NodeId, _: InterfaceSet) {}
|
||||
fn unregister_interfaces(&self, _: &Adapter, _: NodeId, _: InterfaceSet) {}
|
||||
fn emit_event(&self, _: &Adapter, event: Event) {
|
||||
let mut ops = self.ops.lock().unwrap();
|
||||
match event {
|
||||
Event::Cache(CacheEvent::Added(id)) => ops.push(CacheOp::Added(id)),
|
||||
Event::Cache(CacheEvent::Removed(id)) => ops.push(CacheOp::Removed(id)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NoOpActionHandler;
|
||||
impl ActionHandler for NoOpActionHandler {
|
||||
fn do_action(&mut self, _request: ActionRequest) {}
|
||||
}
|
||||
|
||||
fn with_children(role: Role, children: &[LocalNodeId]) -> Node {
|
||||
let mut node = Node::new(role);
|
||||
node.set_children(children.to_vec());
|
||||
node
|
||||
}
|
||||
|
||||
fn build(initial: TreeUpdate) -> (Adapter, Arc<Mutex<Vec<CacheOp>>>) {
|
||||
let ops = Arc::new(Mutex::new(Vec::new()));
|
||||
let app_context = AppContext::new(None);
|
||||
let adapter = Adapter::new(
|
||||
&app_context,
|
||||
CapturingCallback { ops: ops.clone() },
|
||||
initial,
|
||||
false,
|
||||
WindowBounds::default(),
|
||||
NoOpActionHandler,
|
||||
);
|
||||
(adapter, ops)
|
||||
}
|
||||
|
||||
fn initial_tree() -> TreeUpdate {
|
||||
TreeUpdate {
|
||||
nodes: vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1)]),
|
||||
),
|
||||
(LocalNodeId(1), Node::new(Role::Button)),
|
||||
],
|
||||
tree: Some(Tree::new(LocalNodeId(0))),
|
||||
tree_id: TreeId::ROOT,
|
||||
focus: LocalNodeId(0),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(nodes: Vec<(LocalNodeId, Node)>) -> TreeUpdate {
|
||||
TreeUpdate {
|
||||
nodes,
|
||||
tree: None,
|
||||
tree_id: TreeId::ROOT,
|
||||
focus: LocalNodeId(0),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_cache_events_on_construction() {
|
||||
let (_adapter, ops) = build(initial_tree());
|
||||
assert!(ops.lock().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_node_emits_one_added() {
|
||||
let (mut adapter, ops) = build(initial_tree());
|
||||
ops.lock().unwrap().clear();
|
||||
adapter.update(update(vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1), LocalNodeId(2)]),
|
||||
),
|
||||
(LocalNodeId(2), Node::new(Role::Button)),
|
||||
]));
|
||||
let ops = ops.lock().unwrap();
|
||||
assert_eq!(ops.len(), 1);
|
||||
assert!(matches!(ops[0], CacheOp::Added(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_node_emits_removed_for_same_id() {
|
||||
let (mut adapter, ops) = build(initial_tree());
|
||||
adapter.update(update(vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1), LocalNodeId(2)]),
|
||||
),
|
||||
(LocalNodeId(2), Node::new(Role::Button)),
|
||||
]));
|
||||
let added_id = match ops.lock().unwrap().as_slice() {
|
||||
[CacheOp::Added(id)] => *id,
|
||||
other => panic!("expected exactly one Added, got {other:?}"),
|
||||
};
|
||||
ops.lock().unwrap().clear();
|
||||
adapter.update(update(vec![(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1)]),
|
||||
)]));
|
||||
assert_eq!(*ops.lock().unwrap(), vec![CacheOp::Removed(added_id)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subtree_add_emits_added_per_node() {
|
||||
let (mut adapter, ops) = build(initial_tree());
|
||||
ops.lock().unwrap().clear();
|
||||
adapter.update(update(vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1), LocalNodeId(2)]),
|
||||
),
|
||||
(
|
||||
LocalNodeId(2),
|
||||
with_children(Role::Group, &[LocalNodeId(3), LocalNodeId(4)]),
|
||||
),
|
||||
(LocalNodeId(3), Node::new(Role::Button)),
|
||||
(LocalNodeId(4), Node::new(Role::Button)),
|
||||
]));
|
||||
let ops = ops.lock().unwrap();
|
||||
assert_eq!(ops.len(), 3);
|
||||
assert!(ops.iter().all(|op| matches!(op, CacheOp::Added(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subtree_remove_emits_removed_per_node() {
|
||||
let (mut adapter, ops) = build(initial_tree());
|
||||
adapter.update(update(vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1), LocalNodeId(2)]),
|
||||
),
|
||||
(
|
||||
LocalNodeId(2),
|
||||
with_children(Role::Group, &[LocalNodeId(3), LocalNodeId(4)]),
|
||||
),
|
||||
(LocalNodeId(3), Node::new(Role::Button)),
|
||||
(LocalNodeId(4), Node::new(Role::Button)),
|
||||
]));
|
||||
ops.lock().unwrap().clear();
|
||||
adapter.update(update(vec![(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1)]),
|
||||
)]));
|
||||
let ops = ops.lock().unwrap();
|
||||
assert_eq!(ops.len(), 3);
|
||||
assert!(ops.iter().all(|op| matches!(op, CacheOp::Removed(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_transition_into_tree_emits_added() {
|
||||
let mut hidden = Node::new(Role::Button);
|
||||
hidden.set_hidden();
|
||||
let (mut adapter, ops) = build(TreeUpdate {
|
||||
nodes: vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1), LocalNodeId(2)]),
|
||||
),
|
||||
(LocalNodeId(1), Node::new(Role::Button)),
|
||||
(LocalNodeId(2), hidden),
|
||||
],
|
||||
tree: Some(Tree::new(LocalNodeId(0))),
|
||||
tree_id: TreeId::ROOT,
|
||||
focus: LocalNodeId(0),
|
||||
});
|
||||
ops.lock().unwrap().clear();
|
||||
adapter.update(update(vec![(LocalNodeId(2), Node::new(Role::Button))]));
|
||||
let ops = ops.lock().unwrap();
|
||||
assert_eq!(ops.len(), 1);
|
||||
assert!(matches!(ops[0], CacheOp::Added(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_transition_out_of_tree_emits_removed() {
|
||||
let (mut adapter, ops) = build(initial_tree());
|
||||
ops.lock().unwrap().clear();
|
||||
let mut hidden = Node::new(Role::Button);
|
||||
hidden.set_hidden();
|
||||
adapter.update(update(vec![(LocalNodeId(1), hidden)]));
|
||||
let ops = ops.lock().unwrap();
|
||||
assert_eq!(ops.len(), 1);
|
||||
assert!(matches!(ops[0], CacheOp::Removed(_)));
|
||||
}
|
||||
}
|
||||
15
patches/accesskit_atspi_common/src/callback.rs
vendored
Normal file
15
patches/accesskit_atspi_common/src/callback.rs
vendored
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright 2023 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use accesskit_consumer::NodeId;
|
||||
use atspi_common::InterfaceSet;
|
||||
|
||||
use crate::{Adapter, Event};
|
||||
|
||||
pub trait AdapterCallback {
|
||||
fn register_interfaces(&self, adapter: &Adapter, id: NodeId, interfaces: InterfaceSet);
|
||||
fn unregister_interfaces(&self, adapter: &Adapter, id: NodeId, interfaces: InterfaceSet);
|
||||
fn emit_event(&self, adapter: &Adapter, event: Event);
|
||||
}
|
||||
122
patches/accesskit_atspi_common/src/context.rs
vendored
Normal file
122
patches/accesskit_atspi_common/src/context.rs
vendored
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
// Copyright 2023 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use accesskit::{ActionHandler, ActionRequest};
|
||||
use accesskit_consumer::Tree;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
use crate::WindowBounds;
|
||||
|
||||
/// This is an implementation detail of `accesskit_unix`, required for robust
|
||||
/// state transitions with minimal overhead.
|
||||
pub trait ActionHandlerNoMut {
|
||||
fn do_action(&self, request: ActionRequest);
|
||||
}
|
||||
|
||||
/// This is an implementation detail of `accesskit_unix`, required for robust
|
||||
/// state transitions with minimal overhead.
|
||||
pub struct ActionHandlerWrapper<H: ActionHandler + Send>(Mutex<H>);
|
||||
|
||||
impl<H: 'static + ActionHandler + Send> ActionHandlerWrapper<H> {
|
||||
pub fn new(inner: H) -> Self {
|
||||
Self(Mutex::new(inner))
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: ActionHandler + Send> ActionHandlerNoMut for ActionHandlerWrapper<H> {
|
||||
fn do_action(&self, request: ActionRequest) {
|
||||
self.0.lock().unwrap().do_action(request)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Context {
|
||||
pub(crate) app_context: Arc<RwLock<AppContext>>,
|
||||
pub(crate) tree: RwLock<Tree>,
|
||||
pub(crate) action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
|
||||
pub(crate) root_window_bounds: RwLock<WindowBounds>,
|
||||
}
|
||||
|
||||
impl Debug for Context {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Context")
|
||||
.field("app_context", &self.app_context)
|
||||
.field("tree", &self.tree)
|
||||
.field("action_handler", &"ActionHandler")
|
||||
.field("root_window_bounds", &self.root_window_bounds)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub(crate) fn new(
|
||||
app_context: &Arc<RwLock<AppContext>>,
|
||||
tree: Tree,
|
||||
action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
|
||||
root_window_bounds: WindowBounds,
|
||||
) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
app_context: Arc::clone(app_context),
|
||||
tree: RwLock::new(tree),
|
||||
action_handler,
|
||||
root_window_bounds: RwLock::new(root_window_bounds),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn read_tree(&self) -> RwLockReadGuard<'_, Tree> {
|
||||
self.tree.read().unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn read_root_window_bounds(&self) -> RwLockReadGuard<'_, WindowBounds> {
|
||||
self.root_window_bounds.read().unwrap()
|
||||
}
|
||||
|
||||
pub fn do_action(&self, request: ActionRequest) {
|
||||
self.action_handler.do_action(request);
|
||||
}
|
||||
|
||||
pub(crate) fn read_app_context(&self) -> RwLockReadGuard<'_, AppContext> {
|
||||
self.app_context.read().unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn write_app_context(&self) -> RwLockWriteGuard<'_, AppContext> {
|
||||
self.app_context.write().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AppContext {
|
||||
pub(crate) name: Option<String>,
|
||||
pub(crate) toolkit_name: Option<String>,
|
||||
pub(crate) toolkit_version: Option<String>,
|
||||
pub(crate) id: Option<i32>,
|
||||
pub(crate) adapters: Vec<(usize, Arc<Context>)>,
|
||||
}
|
||||
|
||||
impl AppContext {
|
||||
pub fn new(name: Option<String>) -> Arc<RwLock<Self>> {
|
||||
Arc::new(RwLock::new(Self {
|
||||
name,
|
||||
toolkit_name: None,
|
||||
toolkit_version: None,
|
||||
id: None,
|
||||
adapters: Vec::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub(crate) fn adapter_index(&self, id: usize) -> Result<usize, usize> {
|
||||
self.adapters.binary_search_by(|adapter| adapter.0.cmp(&id))
|
||||
}
|
||||
|
||||
pub(crate) fn push_adapter(&mut self, id: usize, context: &Arc<Context>) {
|
||||
self.adapters.push((id, Arc::clone(context)));
|
||||
}
|
||||
|
||||
pub(crate) fn remove_adapter(&mut self, id: usize) {
|
||||
if let Ok(index) = self.adapter_index(id) {
|
||||
self.adapters.remove(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
patches/accesskit_atspi_common/src/error.rs
vendored
Normal file
33
patches/accesskit_atspi_common/src/error.rs
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2022 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Defunct,
|
||||
UnsupportedInterface,
|
||||
TooManyChildren,
|
||||
IndexOutOfRange,
|
||||
TooManyCharacters,
|
||||
UnsupportedTextGranularity,
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Defunct => "defunct",
|
||||
Self::UnsupportedInterface => "unsupported interface",
|
||||
Self::TooManyChildren => "too many children",
|
||||
Self::IndexOutOfRange => "index out of range",
|
||||
Self::TooManyCharacters => "too many characters",
|
||||
Self::UnsupportedTextGranularity => "unsupported text granularity",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
69
patches/accesskit_atspi_common/src/events.rs
vendored
Normal file
69
patches/accesskit_atspi_common/src/events.rs
vendored
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
// Copyright 2022 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use accesskit_consumer::NodeId;
|
||||
use atspi_common::{Politeness, Role, State};
|
||||
|
||||
use crate::{NodeIdOrRoot, Rect};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Event {
|
||||
Object {
|
||||
target: NodeIdOrRoot,
|
||||
event: ObjectEvent,
|
||||
},
|
||||
Window {
|
||||
target: NodeId,
|
||||
name: String,
|
||||
event: WindowEvent,
|
||||
},
|
||||
Cache(CacheEvent),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CacheEvent {
|
||||
Added(NodeId),
|
||||
Removed(NodeId),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Property {
|
||||
Name(String),
|
||||
Description(String),
|
||||
Parent(NodeIdOrRoot),
|
||||
Role(Role),
|
||||
Value(f64),
|
||||
}
|
||||
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
#[derive(Debug)]
|
||||
pub enum ObjectEvent {
|
||||
ActiveDescendantChanged(NodeId),
|
||||
Announcement(String, Politeness),
|
||||
BoundsChanged(Rect),
|
||||
CaretMoved(i32),
|
||||
ChildAdded(usize, NodeId),
|
||||
ChildRemoved(NodeId),
|
||||
PropertyChanged(Property),
|
||||
SelectionChanged,
|
||||
StateChanged(State, bool),
|
||||
TextInserted {
|
||||
start_index: i32,
|
||||
length: i32,
|
||||
content: String,
|
||||
},
|
||||
TextRemoved {
|
||||
start_index: i32,
|
||||
length: i32,
|
||||
content: String,
|
||||
},
|
||||
TextSelectionChanged,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WindowEvent {
|
||||
Activated,
|
||||
Deactivated,
|
||||
}
|
||||
6
patches/accesskit_atspi_common/src/filters.rs
vendored
Normal file
6
patches/accesskit_atspi_common/src/filters.rs
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Copyright 2023 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
pub(crate) use accesskit_consumer::common_filter as filter;
|
||||
33
patches/accesskit_atspi_common/src/lib.rs
vendored
Normal file
33
patches/accesskit_atspi_common/src/lib.rs
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2022 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
mod action;
|
||||
mod adapter;
|
||||
mod callback;
|
||||
mod context;
|
||||
mod error;
|
||||
mod events;
|
||||
mod filters;
|
||||
mod node;
|
||||
mod rect;
|
||||
#[cfg(feature = "simplified-api")]
|
||||
pub mod simplified;
|
||||
mod text_attributes;
|
||||
mod util;
|
||||
|
||||
pub use accesskit_consumer::NodeId;
|
||||
pub use atspi_common::{
|
||||
CoordType, Granularity, InterfaceSet, Layer, RelationType, Role, ScrollType, State, StateSet,
|
||||
};
|
||||
|
||||
pub use action::*;
|
||||
pub use adapter::{Adapter, next_adapter_id};
|
||||
pub use callback::AdapterCallback;
|
||||
pub use context::{ActionHandlerNoMut, ActionHandlerWrapper, AppContext};
|
||||
pub use error::*;
|
||||
pub use events::*;
|
||||
pub use node::{CacheNode, NodeIdOrRoot, PlatformNode, PlatformRoot};
|
||||
pub use rect::*;
|
||||
pub use util::WindowBounds;
|
||||
1944
patches/accesskit_atspi_common/src/node.rs
vendored
Normal file
1944
patches/accesskit_atspi_common/src/node.rs
vendored
Normal file
File diff suppressed because it is too large
Load diff
37
patches/accesskit_atspi_common/src/rect.rs
vendored
Normal file
37
patches/accesskit_atspi_common/src/rect.rs
vendored
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2022 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zbus::wire::{OwnedVariant as OwnedValue, Type, Variant as Value};
|
||||
|
||||
#[derive(
|
||||
Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, OwnedValue, Type, Value,
|
||||
)]
|
||||
pub struct Rect {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
pub const INVALID: Rect = Rect {
|
||||
x: -1,
|
||||
y: -1,
|
||||
width: -1,
|
||||
height: -1,
|
||||
};
|
||||
}
|
||||
|
||||
impl From<accesskit::Rect> for Rect {
|
||||
fn from(value: accesskit::Rect) -> Rect {
|
||||
Rect {
|
||||
x: value.x0 as i32,
|
||||
y: value.y0 as i32,
|
||||
width: value.width() as i32,
|
||||
height: value.height() as i32,
|
||||
}
|
||||
}
|
||||
}
|
||||
901
patches/accesskit_atspi_common/src/simplified.rs
vendored
Normal file
901
patches/accesskit_atspi_common/src/simplified.rs
vendored
Normal file
|
|
@ -0,0 +1,901 @@
|
|||
// Copyright 2024 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
//! API that corresponds more closely to the libatspi client API,
|
||||
//! intended to be used by bindings to languages with less rich
|
||||
//! type systems.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
Adapter, CacheEvent, Event as EventEnum, NodeIdOrRoot, ObjectEvent, PlatformNode, PlatformRoot,
|
||||
Property, WindowEvent,
|
||||
};
|
||||
|
||||
pub use crate::{
|
||||
CoordType, Error, Granularity, Layer, Rect, RelationType, Result, Role, ScrollType, StateSet,
|
||||
};
|
||||
|
||||
#[derive(Clone, Hash, PartialEq)]
|
||||
pub enum Accessible {
|
||||
Node(PlatformNode),
|
||||
Root(PlatformRoot),
|
||||
}
|
||||
|
||||
impl Accessible {
|
||||
pub fn role(&self) -> Result<Role> {
|
||||
match self {
|
||||
Self::Node(node) => node.role(),
|
||||
Self::Root(root) => Ok(root.role()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn localized_role_name(&self) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.localized_role_name(),
|
||||
Self::Root(_) => Ok("".into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.name(),
|
||||
Self::Root(root) => root.name(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn description(&self) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.description(),
|
||||
Self::Root(root) => root.description(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn state(&self) -> StateSet {
|
||||
match self {
|
||||
Self::Node(node) => node.state(),
|
||||
Self::Root(root) => root.state(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn attributes(&self) -> Result<HashMap<&'static str, String>> {
|
||||
match self {
|
||||
Self::Node(node) => node.attributes(),
|
||||
Self::Root(_) => Ok(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parent(&self) -> Result<Option<Self>> {
|
||||
match self {
|
||||
Self::Node(node) => match node.parent()? {
|
||||
NodeIdOrRoot::Node(id) => Ok(Some(Self::Node(node.relative(id)))),
|
||||
NodeIdOrRoot::Root => node.root().map(|root| Some(Self::Root(root))),
|
||||
},
|
||||
Self::Root(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn index_in_parent(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.index_in_parent(),
|
||||
Self::Root(root) => Ok(root.index_in_parent()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn child_count(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.child_count(),
|
||||
Self::Root(root) => root.child_count(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn accessible_id(&self) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.accessible_id(),
|
||||
Self::Root(_) => Ok(String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn child_at_index(&self, index: usize) -> Result<Option<Self>> {
|
||||
match self {
|
||||
Self::Node(node) => node
|
||||
.child_at_index(index)
|
||||
.map(|id| id.map(|id| Self::Node(node.relative(id)))),
|
||||
Self::Root(root) => root
|
||||
.child_at_index(index)
|
||||
.map(|child| child.map(Self::Node)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map_children<T, I>(&self, f: impl Fn(Self) -> I) -> Result<T>
|
||||
where
|
||||
T: FromIterator<I>,
|
||||
{
|
||||
match self {
|
||||
Self::Node(node) => node.map_children(|id| f(Self::Node(node.relative(id)))),
|
||||
Self::Root(root) => root.map_children(|node| f(Self::Node(node))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn relation_set(&self) -> Result<HashMap<RelationType, Vec<Self>>> {
|
||||
match self {
|
||||
Self::Node(node) => node.relation_set(|id| Self::Node(node.relative(id))),
|
||||
Self::Root(_) => Ok(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn application(&self) -> Result<Self> {
|
||||
match self {
|
||||
Self::Node(node) => node.root().map(Self::Root),
|
||||
Self::Root(root) => Ok(Self::Root(root.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toolkit_name(&self) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.toolkit_name(),
|
||||
Self::Root(root) => root.toolkit_name(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toolkit_version(&self) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.toolkit_version(),
|
||||
Self::Root(root) => root.toolkit_version(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supports_action(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.supports_action(),
|
||||
Self::Root(_) => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n_actions(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.n_actions(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action_name(&self, index: i32) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.action_name(index),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_action(&self, index: i32) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.do_action(index),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supports_component(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.supports_component(),
|
||||
Self::Root(_) => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contains(&self, x: i32, y: i32, coord_type: CoordType) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.contains(x, y, coord_type),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn accessible_at_point(
|
||||
&self,
|
||||
x: i32,
|
||||
y: i32,
|
||||
coord_type: CoordType,
|
||||
) -> Result<Option<Self>> {
|
||||
match self {
|
||||
Self::Node(node) => node
|
||||
.accessible_at_point(x, y, coord_type)
|
||||
.map(|id| id.map(|id| Self::Node(node.relative(id)))),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extents(&self, coord_type: CoordType) -> Result<Rect> {
|
||||
match self {
|
||||
Self::Node(node) => node.extents(coord_type),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layer(&self) -> Result<Layer> {
|
||||
match self {
|
||||
Self::Node(node) => node.layer(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn grab_focus(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.grab_focus(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scroll_to(&self, scroll_type: ScrollType) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.scroll_to(scroll_type),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scroll_to_point(&self, coord_type: CoordType, x: i32, y: i32) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.scroll_to_point(coord_type, x, y),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supports_hyperlink(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.supports_hyperlink(),
|
||||
Self::Root(_) => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n_anchors(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.n_anchors(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hyperlink_start_index(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.hyperlink_start_index(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hyperlink_end_index(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.hyperlink_end_index(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hyperlink_object(&self, index: i32) -> Result<Option<Self>> {
|
||||
match self {
|
||||
Self::Node(node) => node
|
||||
.hyperlink_object(index)
|
||||
.map(|id| id.map(|id| Self::Node(node.relative(id)))),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uri(&self, index: i32) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.uri(index),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hyperlink_is_valid(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.hyperlink_is_valid(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supports_selection(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.supports_selection(),
|
||||
Self::Root(_) => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n_selected_children(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.n_selected_children(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selected_child(&self, selected_child_index: usize) -> Result<Option<Self>> {
|
||||
match self {
|
||||
Self::Node(node) => node
|
||||
.selected_child(selected_child_index)
|
||||
.map(|id| id.map(|id| Self::Node(node.relative(id)))),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_child(&self, child_index: usize) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.select_child(child_index),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deselect_selected_child(&self, selected_child_index: usize) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.deselect_selected_child(selected_child_index),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_child_selected(&self, child_index: usize) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.is_child_selected(child_index),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_all(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.select_all(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_selection(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.clear_selection(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deselect_child(&self, child_index: usize) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.deselect_child(child_index),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supports_text(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.supports_text(),
|
||||
Self::Root(_) => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn character_count(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.character_count(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn caret_offset(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.caret_offset(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn string_at_offset(
|
||||
&self,
|
||||
offset: i32,
|
||||
granularity: Granularity,
|
||||
) -> Result<(String, i32, i32)> {
|
||||
match self {
|
||||
Self::Node(node) => node.string_at_offset(offset, granularity),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text(&self, start_offset: i32, end_offset: i32) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.text(start_offset, end_offset),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_caret_offset(&self, offset: i32) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.set_caret_offset(offset),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_attribute_value(&self, offset: i32, attribute_name: &str) -> Result<String> {
|
||||
match self {
|
||||
Self::Node(node) => node.text_attribute_value(offset, attribute_name),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_attributes(
|
||||
&self,
|
||||
offset: i32,
|
||||
) -> Result<(HashMap<&'static str, String>, i32, i32)> {
|
||||
match self {
|
||||
Self::Node(node) => node.text_attributes(offset),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_text_attributes(&self) -> Result<HashMap<&'static str, String>> {
|
||||
match self {
|
||||
Self::Node(node) => node.default_text_attributes(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn character_extents(&self, offset: i32, coord_type: CoordType) -> Result<Rect> {
|
||||
match self {
|
||||
Self::Node(node) => node.character_extents(offset, coord_type),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn offset_at_point(&self, x: i32, y: i32, coord_type: CoordType) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.offset_at_point(x, y, coord_type),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n_selections(&self) -> Result<i32> {
|
||||
match self {
|
||||
Self::Node(node) => node.n_selections(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selection(&self, selection_num: i32) -> Result<(i32, i32)> {
|
||||
match self {
|
||||
Self::Node(node) => node.selection(selection_num),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_selection(&self, start_offset: i32, end_offset: i32) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.add_selection(start_offset, end_offset),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_selection(&self, selection_num: i32) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.remove_selection(selection_num),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_selection(
|
||||
&self,
|
||||
selection_num: i32,
|
||||
start_offset: i32,
|
||||
end_offset: i32,
|
||||
) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.set_selection(selection_num, start_offset, end_offset),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn range_extents(
|
||||
&self,
|
||||
start_offset: i32,
|
||||
end_offset: i32,
|
||||
coord_type: CoordType,
|
||||
) -> Result<Rect> {
|
||||
match self {
|
||||
Self::Node(node) => node.range_extents(start_offset, end_offset, coord_type),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_attribute_run(
|
||||
&self,
|
||||
offset: i32,
|
||||
include_defaults: bool,
|
||||
) -> Result<(HashMap<&'static str, String>, i32, i32)> {
|
||||
match self {
|
||||
Self::Node(node) => node.text_attribute_run(offset, include_defaults),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scroll_substring_to(
|
||||
&self,
|
||||
start_offset: i32,
|
||||
end_offset: i32,
|
||||
scroll_type: ScrollType,
|
||||
) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.scroll_substring_to(start_offset, end_offset, scroll_type),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scroll_substring_to_point(
|
||||
&self,
|
||||
start_offset: i32,
|
||||
end_offset: i32,
|
||||
coord_type: CoordType,
|
||||
x: i32,
|
||||
y: i32,
|
||||
) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => {
|
||||
node.scroll_substring_to_point(start_offset, end_offset, coord_type, x, y)
|
||||
}
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supports_value(&self) -> Result<bool> {
|
||||
match self {
|
||||
Self::Node(node) => node.supports_value(),
|
||||
Self::Root(_) => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn minimum_value(&self) -> Result<f64> {
|
||||
match self {
|
||||
Self::Node(node) => node.minimum_value(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maximum_value(&self) -> Result<f64> {
|
||||
match self {
|
||||
Self::Node(node) => node.maximum_value(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn minimum_increment(&self) -> Result<f64> {
|
||||
match self {
|
||||
Self::Node(node) => node.minimum_increment(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_value(&self) -> Result<f64> {
|
||||
match self {
|
||||
Self::Node(node) => node.current_value(),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_current_value(&self, value: f64) -> Result<()> {
|
||||
match self {
|
||||
Self::Node(node) => node.set_current_value(value),
|
||||
Self::Root(_) => Err(Error::UnsupportedInterface),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Cache {
|
||||
root: PlatformRoot,
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
pub fn new(root: PlatformRoot) -> Self {
|
||||
Self { root }
|
||||
}
|
||||
|
||||
pub fn items(&self) -> Result<Vec<Accessible>> {
|
||||
let descendants: Vec<Accessible> = self
|
||||
.root
|
||||
.map_descendants(|node, _, _| Accessible::Node(node))?;
|
||||
let mut items = Vec::with_capacity(descendants.len() + 1);
|
||||
items.push(Accessible::Root(self.root.clone()));
|
||||
items.extend(descendants);
|
||||
Ok(items)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum EventData {
|
||||
U32(u32),
|
||||
F64(f64),
|
||||
String(String),
|
||||
Rect(Rect),
|
||||
Accessible(Accessible),
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct Event {
|
||||
pub kind: String,
|
||||
pub source: Accessible,
|
||||
pub detail1: i32,
|
||||
pub detail2: i32,
|
||||
pub data: Option<EventData>,
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn new(adapter: &Adapter, event: EventEnum) -> Self {
|
||||
match event {
|
||||
EventEnum::Object { target, event } => {
|
||||
let source = match target {
|
||||
NodeIdOrRoot::Node(target) => Accessible::Node(adapter.platform_node(target)),
|
||||
NodeIdOrRoot::Root => Accessible::Root(adapter.platform_root()),
|
||||
};
|
||||
match event {
|
||||
ObjectEvent::ActiveDescendantChanged(child) => {
|
||||
let child = Accessible::Node(adapter.platform_node(child));
|
||||
Self {
|
||||
kind: "object:active-descendant-changed".into(),
|
||||
source,
|
||||
detail1: 0,
|
||||
detail2: 0,
|
||||
data: Some(EventData::Accessible(child)),
|
||||
}
|
||||
}
|
||||
ObjectEvent::Announcement(message, politeness) => Self {
|
||||
kind: "object:announcement".into(),
|
||||
source,
|
||||
detail1: politeness as i32,
|
||||
detail2: 0,
|
||||
data: Some(EventData::String(message)),
|
||||
},
|
||||
ObjectEvent::BoundsChanged(bounds) => Self {
|
||||
kind: "object:bounds-changed".into(),
|
||||
source,
|
||||
detail1: 0,
|
||||
detail2: 0,
|
||||
data: Some(EventData::Rect(bounds)),
|
||||
},
|
||||
ObjectEvent::CaretMoved(offset) => Self {
|
||||
kind: "object:text-caret-moved".into(),
|
||||
source,
|
||||
detail1: offset,
|
||||
detail2: 0,
|
||||
data: None,
|
||||
},
|
||||
ObjectEvent::ChildAdded(index, child) => {
|
||||
let child = Accessible::Node(adapter.platform_node(child));
|
||||
Self {
|
||||
kind: "object:children-changed:add".into(),
|
||||
source,
|
||||
detail1: index as i32,
|
||||
detail2: 0,
|
||||
data: Some(EventData::Accessible(child)),
|
||||
}
|
||||
}
|
||||
ObjectEvent::ChildRemoved(child) => {
|
||||
let child = Accessible::Node(adapter.platform_node(child));
|
||||
Self {
|
||||
kind: "object:children-changed:remove".into(),
|
||||
source,
|
||||
detail1: -1,
|
||||
detail2: 0,
|
||||
data: Some(EventData::Accessible(child)),
|
||||
}
|
||||
}
|
||||
ObjectEvent::PropertyChanged(property) => Self {
|
||||
kind: match property {
|
||||
Property::Name(_) => "object:property-change:accessible-name",
|
||||
Property::Description(_) => {
|
||||
"object:property-change:accessible-description"
|
||||
}
|
||||
Property::Parent(_) => "object:property-change:accessible-parent",
|
||||
Property::Role(_) => "object:property-change:accessible-role",
|
||||
Property::Value(_) => "object:property-change:accessible-value",
|
||||
}
|
||||
.into(),
|
||||
source,
|
||||
detail1: 0,
|
||||
detail2: 0,
|
||||
data: Some(match property {
|
||||
Property::Name(value) => EventData::String(value),
|
||||
Property::Description(value) => EventData::String(value),
|
||||
Property::Parent(parent) => {
|
||||
let parent = match parent {
|
||||
NodeIdOrRoot::Node(parent) => {
|
||||
Accessible::Node(adapter.platform_node(parent))
|
||||
}
|
||||
NodeIdOrRoot::Root => Accessible::Root(adapter.platform_root()),
|
||||
};
|
||||
EventData::Accessible(parent)
|
||||
}
|
||||
Property::Role(value) => EventData::U32(value as u32),
|
||||
Property::Value(value) => EventData::F64(value),
|
||||
}),
|
||||
},
|
||||
ObjectEvent::SelectionChanged => Self {
|
||||
kind: "object:selection-changed".into(),
|
||||
source,
|
||||
detail1: 0,
|
||||
detail2: 0,
|
||||
data: None,
|
||||
},
|
||||
ObjectEvent::StateChanged(state, value) => Self {
|
||||
kind: format!("object:state-changed:{}", state.to_static_str()),
|
||||
source,
|
||||
detail1: value as i32,
|
||||
detail2: 0,
|
||||
data: None,
|
||||
},
|
||||
ObjectEvent::TextInserted {
|
||||
start_index,
|
||||
length,
|
||||
content,
|
||||
} => Self {
|
||||
kind: "object:text-changed:insert".into(),
|
||||
source,
|
||||
detail1: start_index,
|
||||
detail2: length,
|
||||
data: Some(EventData::String(content)),
|
||||
},
|
||||
ObjectEvent::TextRemoved {
|
||||
start_index,
|
||||
length,
|
||||
content,
|
||||
} => Self {
|
||||
kind: "object:text-changed:delete".into(),
|
||||
source,
|
||||
detail1: start_index,
|
||||
detail2: length,
|
||||
data: Some(EventData::String(content)),
|
||||
},
|
||||
ObjectEvent::TextSelectionChanged => Self {
|
||||
kind: "object:text-selection-changed".into(),
|
||||
source,
|
||||
detail1: 0,
|
||||
detail2: 0,
|
||||
data: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
EventEnum::Window {
|
||||
target,
|
||||
name,
|
||||
event,
|
||||
} => {
|
||||
let kind = match event {
|
||||
WindowEvent::Activated => "window:activate",
|
||||
WindowEvent::Deactivated => "window:deactivate",
|
||||
};
|
||||
let source = Accessible::Node(adapter.platform_node(target));
|
||||
Self {
|
||||
kind: kind.into(),
|
||||
source,
|
||||
detail1: 0,
|
||||
detail2: 0,
|
||||
data: Some(EventData::String(name)),
|
||||
}
|
||||
}
|
||||
EventEnum::Cache(cache_event) => {
|
||||
let (kind, target) = match cache_event {
|
||||
CacheEvent::Added(target) => ("cache:add", target),
|
||||
CacheEvent::Removed(target) => ("cache:remove", target),
|
||||
};
|
||||
Self {
|
||||
kind: kind.into(),
|
||||
source: Accessible::Node(adapter.platform_node(target)),
|
||||
detail1: 0,
|
||||
detail2: 0,
|
||||
data: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Accessible, Cache, Event as SimplifiedEvent};
|
||||
use crate::{Adapter, AdapterCallback, AppContext, Event, WindowBounds};
|
||||
use accesskit::{
|
||||
ActionHandler, ActionRequest, Node, NodeId as LocalNodeId, Role, Tree, TreeId, TreeUpdate,
|
||||
};
|
||||
use accesskit_consumer::NodeId;
|
||||
use atspi_common::InterfaceSet;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
struct NoOpActionHandler;
|
||||
impl ActionHandler for NoOpActionHandler {
|
||||
fn do_action(&mut self, _request: ActionRequest) {}
|
||||
}
|
||||
|
||||
struct CacheKindCallback {
|
||||
kinds: Arc<Mutex<Vec<String>>>,
|
||||
}
|
||||
|
||||
impl AdapterCallback for CacheKindCallback {
|
||||
fn register_interfaces(&self, _: &Adapter, _: NodeId, _: InterfaceSet) {}
|
||||
fn unregister_interfaces(&self, _: &Adapter, _: NodeId, _: InterfaceSet) {}
|
||||
fn emit_event(&self, adapter: &Adapter, event: Event) {
|
||||
if matches!(event, Event::Cache(_)) {
|
||||
let simplified = SimplifiedEvent::new(adapter, event);
|
||||
self.kinds.lock().unwrap().push(simplified.kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NoOpCallback;
|
||||
impl AdapterCallback for NoOpCallback {
|
||||
fn register_interfaces(&self, _: &Adapter, _: NodeId, _: InterfaceSet) {}
|
||||
fn unregister_interfaces(&self, _: &Adapter, _: NodeId, _: InterfaceSet) {}
|
||||
fn emit_event(&self, _: &Adapter, _: Event) {}
|
||||
}
|
||||
|
||||
fn with_children(role: Role, children: &[LocalNodeId]) -> Node {
|
||||
let mut node = Node::new(role);
|
||||
node.set_children(children.to_vec());
|
||||
node
|
||||
}
|
||||
|
||||
fn initial_tree() -> TreeUpdate {
|
||||
TreeUpdate {
|
||||
nodes: vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1)]),
|
||||
),
|
||||
(LocalNodeId(1), Node::new(Role::Button)),
|
||||
],
|
||||
tree: Some(Tree::new(LocalNodeId(0))),
|
||||
tree_id: TreeId::ROOT,
|
||||
focus: LocalNodeId(0),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_events_map_to_kind_strings() {
|
||||
let kinds = Arc::new(Mutex::new(Vec::new()));
|
||||
let app_context = AppContext::new(None);
|
||||
let mut adapter = Adapter::new(
|
||||
&app_context,
|
||||
CacheKindCallback {
|
||||
kinds: kinds.clone(),
|
||||
},
|
||||
initial_tree(),
|
||||
false,
|
||||
WindowBounds::default(),
|
||||
NoOpActionHandler,
|
||||
);
|
||||
adapter.update(TreeUpdate {
|
||||
nodes: vec![
|
||||
(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1), LocalNodeId(2)]),
|
||||
),
|
||||
(LocalNodeId(2), Node::new(Role::Button)),
|
||||
],
|
||||
tree: None,
|
||||
tree_id: TreeId::ROOT,
|
||||
focus: LocalNodeId(0),
|
||||
});
|
||||
adapter.update(TreeUpdate {
|
||||
nodes: vec![(
|
||||
LocalNodeId(0),
|
||||
with_children(Role::Window, &[LocalNodeId(1)]),
|
||||
)],
|
||||
tree: None,
|
||||
tree_id: TreeId::ROOT,
|
||||
focus: LocalNodeId(0),
|
||||
});
|
||||
assert_eq!(*kinds.lock().unwrap(), vec!["cache:add", "cache:remove"]);
|
||||
}
|
||||
|
||||
fn build_cache() -> (Adapter, Cache) {
|
||||
let app_context = AppContext::new(None);
|
||||
let adapter = Adapter::new(
|
||||
&app_context,
|
||||
NoOpCallback,
|
||||
initial_tree(),
|
||||
false,
|
||||
WindowBounds::default(),
|
||||
NoOpActionHandler,
|
||||
);
|
||||
let cache = Cache::new(adapter.platform_root());
|
||||
(adapter, cache)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn items_prepends_application_root() {
|
||||
let (_adapter, cache) = build_cache();
|
||||
let items = cache.items().unwrap();
|
||||
assert_eq!(items.len(), 3);
|
||||
assert!(matches!(items[0], Accessible::Root(_)));
|
||||
}
|
||||
}
|
||||
79
patches/accesskit_atspi_common/src/text_attributes.rs
vendored
Normal file
79
patches/accesskit_atspi_common/src/text_attributes.rs
vendored
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// Copyright 2026 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use accesskit::{Color, TextAlign, TextDecorationStyle};
|
||||
use accesskit_consumer::Node;
|
||||
use phf::phf_map;
|
||||
|
||||
fn color_to_string(color: Color) -> String {
|
||||
format!("{},{},{}", color.red, color.green, color.blue)
|
||||
}
|
||||
|
||||
fn family_name(node: &Node) -> Option<String> {
|
||||
node.font_family().map(String::from)
|
||||
}
|
||||
|
||||
fn size(node: &Node) -> Option<String> {
|
||||
node.font_size().map(|value| value.to_string())
|
||||
}
|
||||
|
||||
fn weight(node: &Node) -> Option<String> {
|
||||
node.font_weight().map(|value| value.to_string())
|
||||
}
|
||||
|
||||
fn style(node: &Node) -> Option<String> {
|
||||
node.is_italic().then(|| "italic".into())
|
||||
}
|
||||
|
||||
fn strikethrough(node: &Node) -> Option<String> {
|
||||
node.strikethrough().map(|_| "true".into())
|
||||
}
|
||||
|
||||
fn underline(node: &Node) -> Option<String> {
|
||||
node.underline().map(|deco| {
|
||||
match deco.style {
|
||||
TextDecorationStyle::Double => "double",
|
||||
_ => "single",
|
||||
}
|
||||
.into()
|
||||
})
|
||||
}
|
||||
|
||||
fn bg_color(node: &Node) -> Option<String> {
|
||||
node.background_color().map(color_to_string)
|
||||
}
|
||||
|
||||
fn fg_color(node: &Node) -> Option<String> {
|
||||
node.foreground_color().map(color_to_string)
|
||||
}
|
||||
|
||||
fn language(node: &Node) -> Option<String> {
|
||||
node.language().map(String::from)
|
||||
}
|
||||
|
||||
fn justification(node: &Node) -> Option<String> {
|
||||
node.text_align().map(|align| {
|
||||
match align {
|
||||
TextAlign::Left => "left",
|
||||
TextAlign::Center => "center",
|
||||
TextAlign::Right => "right",
|
||||
TextAlign::Justify => "fill",
|
||||
}
|
||||
.into()
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) const ATTRIBUTE_GETTERS: phf::Map<&'static str, fn(&Node) -> Option<String>> = phf_map! {
|
||||
"family-name" => family_name,
|
||||
"size" => size,
|
||||
"weight" => weight,
|
||||
"style" => style,
|
||||
"strikethrough" => strikethrough,
|
||||
"underline" => underline,
|
||||
"bg-color" => bg_color,
|
||||
"fg-color" => fg_color,
|
||||
"language" => language,
|
||||
"justification" => justification,
|
||||
};
|
||||
134
patches/accesskit_atspi_common/src/util.rs
vendored
Normal file
134
patches/accesskit_atspi_common/src/util.rs
vendored
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
// Copyright 2022 The AccessKit Authors. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (found in
|
||||
// the LICENSE-APACHE file) or the MIT license (found in
|
||||
// the LICENSE-MIT file), at your option.
|
||||
|
||||
use accesskit::{Point, Rect, ScrollHint};
|
||||
use accesskit_consumer::{Node, TextPosition, TextRange};
|
||||
use atspi_common::{CoordType, Granularity, ScrollType};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
#[derive(Clone, Copy, Default, Debug)]
|
||||
pub struct WindowBounds {
|
||||
pub outer: Rect,
|
||||
pub inner: Rect,
|
||||
}
|
||||
|
||||
impl WindowBounds {
|
||||
pub fn new(outer: Rect, inner: Rect) -> Self {
|
||||
Self { outer, inner }
|
||||
}
|
||||
|
||||
pub(crate) fn accesskit_point_to_atspi_point(
|
||||
&self,
|
||||
point: Point,
|
||||
parent: Option<Node>,
|
||||
coord_type: CoordType,
|
||||
) -> Point {
|
||||
let origin = self.origin(parent, coord_type);
|
||||
Point::new(origin.x + point.x, origin.y + point.y)
|
||||
}
|
||||
|
||||
pub(crate) fn atspi_point_to_accesskit_point(
|
||||
&self,
|
||||
point: Point,
|
||||
parent: Option<Node>,
|
||||
coord_type: CoordType,
|
||||
) -> Point {
|
||||
let origin = self.origin(parent, coord_type);
|
||||
Point::new(point.x - origin.x, point.y - origin.y)
|
||||
}
|
||||
|
||||
fn origin(&self, parent: Option<Node>, coord_type: CoordType) -> Point {
|
||||
match coord_type {
|
||||
CoordType::Screen => self.inner.origin(),
|
||||
CoordType::Window => Point::ZERO,
|
||||
CoordType::Parent => {
|
||||
if let Some(parent) = parent {
|
||||
let parent_origin = parent.bounding_box().unwrap_or_default().origin();
|
||||
Point::new(-parent_origin.x, -parent_origin.y)
|
||||
} else {
|
||||
self.inner.origin()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn text_position_from_offset<'a>(
|
||||
node: &'a Node,
|
||||
offset: i32,
|
||||
) -> Option<TextPosition<'a>> {
|
||||
let index = offset.try_into().ok()?;
|
||||
node.text_position_from_global_usv_index(index)
|
||||
}
|
||||
|
||||
pub(crate) fn text_range_from_offset<'a>(
|
||||
node: &'a Node,
|
||||
offset: i32,
|
||||
granularity: Granularity,
|
||||
) -> Result<TextRange<'a>, Error> {
|
||||
let start_offset = text_position_from_offset(node, offset).ok_or(Error::IndexOutOfRange)?;
|
||||
let start = match granularity {
|
||||
Granularity::Char => start_offset,
|
||||
Granularity::Line if start_offset.is_line_start() => start_offset,
|
||||
Granularity::Line => start_offset.backward_to_line_start(),
|
||||
Granularity::Paragraph if start_offset.is_paragraph_start() => start_offset,
|
||||
Granularity::Paragraph => start_offset.backward_to_paragraph_start(),
|
||||
Granularity::Sentence => return Err(Error::UnsupportedTextGranularity),
|
||||
Granularity::Word if start_offset.is_word_start() => start_offset,
|
||||
Granularity::Word => start_offset.backward_to_word_start(),
|
||||
};
|
||||
let end = match granularity {
|
||||
Granularity::Char if start_offset.is_document_end() => start_offset,
|
||||
Granularity::Char => start.forward_to_character_end(),
|
||||
Granularity::Line => start.forward_to_line_end(),
|
||||
Granularity::Paragraph => start.forward_to_paragraph_end(),
|
||||
Granularity::Sentence => return Err(Error::UnsupportedTextGranularity),
|
||||
Granularity::Word => start.forward_to_word_end(),
|
||||
};
|
||||
let mut range = start.to_degenerate_range();
|
||||
range.set_end(end);
|
||||
Ok(range)
|
||||
}
|
||||
|
||||
pub(crate) fn text_range_from_offsets<'a>(
|
||||
node: &'a Node,
|
||||
start_offset: i32,
|
||||
end_offset: i32,
|
||||
) -> Option<TextRange<'a>> {
|
||||
let start = text_position_from_offset(node, start_offset)?;
|
||||
let end = if end_offset == -1 {
|
||||
node.document_range().end()
|
||||
} else {
|
||||
text_position_from_offset(node, end_offset)?
|
||||
};
|
||||
|
||||
let mut range = start.to_degenerate_range();
|
||||
range.set_end(end);
|
||||
Some(range)
|
||||
}
|
||||
|
||||
pub(crate) fn text_range_bounds_from_offsets(
|
||||
node: &Node,
|
||||
start_offset: i32,
|
||||
end_offset: i32,
|
||||
) -> Option<Rect> {
|
||||
text_range_from_offsets(node, start_offset, end_offset)?
|
||||
.bounding_boxes()
|
||||
.into_iter()
|
||||
.reduce(|rect1, rect2| rect1.union(rect2))
|
||||
}
|
||||
|
||||
pub(crate) fn atspi_scroll_type_to_scroll_hint(scroll_type: ScrollType) -> Option<ScrollHint> {
|
||||
match scroll_type {
|
||||
ScrollType::TopLeft => Some(ScrollHint::TopLeft),
|
||||
ScrollType::BottomRight => Some(ScrollHint::BottomRight),
|
||||
ScrollType::TopEdge => Some(ScrollHint::TopEdge),
|
||||
ScrollType::BottomEdge => Some(ScrollHint::BottomEdge),
|
||||
ScrollType::LeftEdge => Some(ScrollHint::LeftEdge),
|
||||
ScrollType::RightEdge => Some(ScrollHint::RightEdge),
|
||||
ScrollType::Anywhere => None,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue