Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support text fields without a value property #274

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions consumer/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl<'a> Node<'a> {
is_focused: self.is_focused(),
is_root: self.is_root(),
name: self.name(),
value: self.value(),
live: self.live(),
supports_text_ranges: self.supports_text_ranges(),
}
Expand Down Expand Up @@ -379,10 +380,6 @@ impl NodeState {
self.data().checked_state()
}

pub fn value(&self) -> Option<&str> {
self.data().value()
}

pub fn numeric_value(&self) -> Option<f64> {
self.data().numeric_value()
}
Expand Down Expand Up @@ -534,6 +531,20 @@ impl<'a> Node<'a> {
(!names.is_empty()).then(move || names.join(" "))
}
}

pub fn value(&self) -> Option<String> {
if let Some(value) = &self.data().value() {
Some(value.to_string())
} else if self.supports_text_ranges() && !self.is_multiline() {
Some(self.document_range().text())
} else {
None
}
}

pub fn has_value(&self) -> bool {
self.data().value().is_some() || (self.supports_text_ranges() && !self.is_multiline())
}
}

impl NodeState {
Expand Down Expand Up @@ -606,6 +617,10 @@ impl NodeState {
pub fn raw_text_selection(&self) -> Option<&TextSelection> {
self.data().text_selection()
}

pub fn raw_value(&self) -> Option<&str> {
self.data().value()
}
}

impl<'a> Node<'a> {
Expand Down Expand Up @@ -680,6 +695,7 @@ pub struct DetachedNode {
pub(crate) is_focused: bool,
pub(crate) is_root: bool,
pub(crate) name: Option<String>,
pub(crate) value: Option<String>,
pub(crate) live: Live,
pub(crate) supports_text_ranges: bool,
}
Expand All @@ -697,6 +713,14 @@ impl DetachedNode {
self.name.clone()
}

pub fn value(&self) -> Option<String> {
self.value.clone()
}

pub fn has_value(&self) -> bool {
self.value.is_some()
}

pub fn live(&self) -> Live {
self.live
}
Expand Down
8 changes: 4 additions & 4 deletions consumer/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> InnerPosition<'a> {
}

fn is_paragraph_end(&self) -> bool {
self.is_line_end() && self.node.value().unwrap().ends_with('\n')
self.is_line_end() && self.node.data().value().unwrap().ends_with('\n')
}

fn is_document_start(&self, root_node: &Node) -> bool {
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<'a> Position<'a> {
pub fn to_global_utf16_index(&self) -> usize {
let mut total_length = 0usize;
for node in self.root_node.inline_text_boxes() {
let node_text = node.value().unwrap();
let node_text = node.data().value().unwrap();
if node.id() == self.inner.node.id() {
let character_lengths = node.data().character_lengths();
let slice_end = character_lengths[..self.inner.character_index]
Expand Down Expand Up @@ -538,7 +538,7 @@ impl<'a> Range<'a> {
} else {
character_lengths.len()
};
let value = node.value().unwrap();
let value = node.data().value().unwrap();
let s = if start_index == end_index {
""
} else if start_index == 0 && end_index == character_lengths.len() {
Expand Down Expand Up @@ -983,7 +983,7 @@ impl<'a> Node<'a> {
pub fn text_position_from_global_utf16_index(&self, index: usize) -> Option<Position> {
let mut total_length = 0usize;
for node in self.inline_text_boxes() {
let node_text = node.value().unwrap();
let node_text = node.data().value().unwrap();
let node_text_length = node_text.chars().map(char::len_utf16).sum::<usize>();
let new_total_length = total_length + node_text_length;
if index >= total_length && index < new_total_length {
Expand Down
1 change: 1 addition & 0 deletions consumer/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ impl State {
is_focused: old_focus_id == Some(id),
is_root: old_root_id == id,
name: None,
value: None,
live: Live::Off,
supports_text_ranges: false,
};
Expand Down
59 changes: 55 additions & 4 deletions platforms/macos/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

use accesskit::{Live, NodeId};
use accesskit::{Live, NodeId, Role};
use accesskit_consumer::{DetachedNode, FilterResult, Node, TreeChangeHandler, TreeState};
use objc2::{
foundation::{NSInteger, NSMutableDictionary, NSNumber, NSObject, NSString},
msg_send, Message,
};
use std::rc::Rc;
use std::{collections::HashSet, rc::Rc};

use crate::{
appkit::*,
Expand Down Expand Up @@ -132,13 +132,15 @@ impl QueuedEvents {
pub(crate) struct EventGenerator {
context: Rc<Context>,
events: Vec<QueuedEvent>,
text_changed: HashSet<NodeId>,
}

impl EventGenerator {
pub(crate) fn new(context: Rc<Context>) -> Self {
Self {
context,
events: Vec::new(),
text_changed: HashSet::new(),
}
}

Expand All @@ -148,10 +150,56 @@ impl EventGenerator {
events: self.events,
}
}

fn insert_text_change_if_needed_parent(&mut self, node: Node) {
if !node.supports_text_ranges() {
return;
}
let id = node.id();
if self.text_changed.contains(&id) {
return;
}
// Text change events must come before selection change
// events. It doesn't matter if text change events come
// before other events.
self.events.insert(
0,
QueuedEvent::Generic {
node_id: id,
notification: unsafe { NSAccessibilityValueChangedNotification },
},
);
self.text_changed.insert(id);
}

fn insert_text_change_if_needed(&mut self, node: &Node) {
if node.role() != Role::InlineTextBox {
return;
}
if let Some(node) = node.filtered_parent(&filter) {
self.insert_text_change_if_needed_parent(node);
}
}

fn insert_text_change_if_needed_for_removed_node(
&mut self,
node: &DetachedNode,
current_state: &TreeState,
) {
if node.role() != Role::InlineTextBox {
return;
}
if let Some(id) = node.parent_id() {
if let Some(node) = current_state.node_by_id(id) {
self.insert_text_change_if_needed_parent(node);
}
}
}
}

impl TreeChangeHandler for EventGenerator {
fn node_added(&mut self, node: &Node) {
self.insert_text_change_if_needed(node);
if filter(node) != FilterResult::Include {
return;
}
Expand All @@ -162,7 +210,9 @@ impl TreeChangeHandler for EventGenerator {
}

fn node_updated(&mut self, old_node: &DetachedNode, new_node: &Node) {
// TODO: text changes, live regions
if old_node.raw_value() != new_node.raw_value() {
self.insert_text_change_if_needed(new_node);
}
if filter(new_node) != FilterResult::Include {
return;
}
Expand Down Expand Up @@ -218,7 +268,8 @@ impl TreeChangeHandler for EventGenerator {
}
}

fn node_removed(&mut self, node: &DetachedNode, _current_state: &TreeState) {
fn node_removed(&mut self, node: &DetachedNode, current_state: &TreeState) {
self.insert_text_change_if_needed_for_removed_node(node, current_state);
self.events.push(QueuedEvent::NodeDestroyed(node.id()));
}
}
13 changes: 10 additions & 3 deletions platforms/macos/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,20 @@ impl<'a> NodeWrapper<'a> {
}
}

fn node_value(&self) -> Option<String> {
match self {
Self::Node(node) => node.value(),
Self::DetachedNode(node) => node.value(),
}
}

// TODO: implement proper logic for title, description, and value;
// see Chromium's content/browser/accessibility/browser_accessibility_cocoa.mm
// and figure out how this is different in the macOS 10.10+ protocol

pub(crate) fn title(&self) -> Option<String> {
let state = self.node_state();
if state.role() == Role::StaticText && state.value().is_none() {
if state.role() == Role::StaticText && state.raw_value().is_none() {
// In this case, macOS wants the text to be the value, not title.
return None;
}
Expand All @@ -321,8 +328,8 @@ impl<'a> NodeWrapper<'a> {
if let Some(state) = state.checked_state() {
return Some(Value::Bool(state != CheckedState::False));
}
if let Some(value) = state.value() {
return Some(Value::String(value.into()));
if let Some(value) = self.node_value() {
return Some(Value::String(value));
}
if let Some(value) = state.numeric_value() {
return Some(Value::Number(value));
Expand Down
2 changes: 1 addition & 1 deletion platforms/windows/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Adapter {
}
}
fn node_updated(&mut self, old_node: &DetachedNode, new_node: &Node) {
if old_node.value() != new_node.value() {
if old_node.raw_value() != new_node.raw_value() {
self.insert_text_change_if_needed(new_node);
}
if filter(new_node) != FilterResult::Include {
Expand Down
12 changes: 9 additions & 3 deletions platforms/windows/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,21 @@ impl<'a> NodeWrapper<'a> {
}

fn is_value_pattern_supported(&self) -> bool {
self.node_state().value().is_some()
match self {
Self::Node(node) => node.has_value(),
Self::DetachedNode(node) => node.has_value(),
}
}

fn is_range_value_pattern_supported(&self) -> bool {
self.node_state().numeric_value().is_some()
}

fn value(&self) -> &str {
self.node_state().value().unwrap()
fn value(&self) -> String {
match self {
Self::Node(node) => node.value().unwrap(),
Self::DetachedNode(node) => node.value().unwrap(),
}
}

fn is_read_only(&self) -> bool {
Expand Down