Skip to content

Commit

Permalink
refactor!: String indices are always in UTF-8 code units (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcampbell authored Jul 22, 2022
1 parent d48c31b commit 386ca0a
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 9,174 deletions.
30 changes: 4 additions & 26 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,6 @@ pub enum TextDecoration {
Wavy,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(crate = "serde"))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub enum StringEncoding {
Utf8,
Utf16,
}

// This is NonZeroU128 because we regularly store Option<NodeId>.
// 128-bit to handle UUIDs.
pub type NodeIdContent = NonZeroU128;
Expand All @@ -607,8 +597,7 @@ impl From<NonZeroU64> for NodeId {
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct TextMarker {
pub marker_type: MarkerType,
/// Indices are in code units for the encoding specified in
/// [`Tree::source_string_encoding`].
/// Indices are in UTF-8 code units.
pub range: Range<usize>,
}

Expand Down Expand Up @@ -639,8 +628,7 @@ fn is_empty<T>(slice: &[T]) -> bool {
slice.is_empty()
}

/// Offsets are in code units for the encoding specified in
/// [`Tree::source_string_encoding`].
/// Offsets are in UTF-8 code units.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
Expand Down Expand Up @@ -947,8 +935,7 @@ pub struct Node {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "is_empty"))]
pub character_offsets: Box<[f32]>,

/// For inline text. The indices of each word, in code units for
/// the encoding specified in [`Tree::source_string_encoding`].
/// For inline text. The indices of each word, in UTF-8 code units.
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "is_empty"))]
pub words: Box<[Range<usize>]>,
Expand Down Expand Up @@ -1337,14 +1324,6 @@ impl Node {
pub struct Tree {
pub root: NodeId,

/// The string encoding used by the tree source. This is required
/// to disambiguate string indices, e.g. in [`Node::words`].
/// When the tree is serialized, the encoding specified by
/// the target serialization format is always used. But this way,
/// the tree source doesn't have to convert string indices;
/// the platform adapter will do this if needed.
pub source_string_encoding: StringEncoding,

/// The node that's used as the root scroller, if any. On some platforms
/// like Android we need to ignore accessibility scroll offsets for
/// that node and get them from the viewport instead.
Expand All @@ -1353,10 +1332,9 @@ pub struct Tree {
}

impl Tree {
pub fn new(root: NodeId, source_string_encoding: StringEncoding) -> Tree {
pub fn new(root: NodeId) -> Tree {
Tree {
root,
source_string_encoding,
root_scroller: None,
}
}
Expand Down
6 changes: 2 additions & 4 deletions consumer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ pub use iterators::{
#[cfg(test)]
mod tests {
use accesskit::kurbo::{Affine, Rect, Vec2};
use accesskit::{
ActionHandler, ActionRequest, Node, NodeId, Role, StringEncoding, Tree, TreeUpdate,
};
use accesskit::{ActionHandler, ActionRequest, Node, NodeId, Role, Tree, TreeUpdate};
use std::num::NonZeroU128;
use std::sync::Arc;

Expand Down Expand Up @@ -145,7 +143,7 @@ mod tests {
button_3_2,
empty_container_3_3_ignored,
],
tree: Some(Tree::new(ROOT_ID, StringEncoding::Utf8)),
tree: Some(Tree::new(ROOT_ID)),
focus: None,
};
crate::tree::Tree::new(initial_update, Box::new(NullActionHandler {}))
Expand Down
6 changes: 3 additions & 3 deletions consumer/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl Node<'_> {
#[cfg(test)]
mod tests {
use accesskit::kurbo::{Point, Rect};
use accesskit::{Node, NodeId, Role, StringEncoding, Tree, TreeUpdate};
use accesskit::{Node, NodeId, Role, Tree, TreeUpdate};
use std::num::NonZeroU128;

use crate::tests::*;
Expand Down Expand Up @@ -792,7 +792,7 @@ mod tests {
},
Node::new(NODE_ID_2, Role::Button),
],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: None,
};
let tree = super::Tree::new(update, Box::new(NullActionHandler {}));
Expand Down Expand Up @@ -829,7 +829,7 @@ mod tests {
..Node::new(NODE_ID_5, Role::StaticText)
},
],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: None,
};
let tree = super::Tree::new(update, Box::new(NullActionHandler {}));
Expand Down
16 changes: 8 additions & 8 deletions consumer/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl Tree {

#[cfg(test)]
mod tests {
use accesskit::{Node, NodeId, Role, StringEncoding, Tree, TreeUpdate};
use accesskit::{Node, NodeId, Role, Tree, TreeUpdate};
use std::num::NonZeroU128;

use crate::tests::NullActionHandler;
Expand All @@ -358,7 +358,7 @@ mod tests {
fn init_tree_with_root_node() {
let update = TreeUpdate {
nodes: vec![Node::new(NODE_ID_1, Role::Window)],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: None,
};
let tree = super::Tree::new(update, Box::new(NullActionHandler {}));
Expand All @@ -378,7 +378,7 @@ mod tests {
Node::new(NODE_ID_2, Role::Button),
Node::new(NODE_ID_3, Role::Button),
],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: None,
};
let tree = super::Tree::new(update, Box::new(NullActionHandler {}));
Expand All @@ -399,7 +399,7 @@ mod tests {
let root_node = Node::new(NODE_ID_1, Role::Window);
let first_update = TreeUpdate {
nodes: vec![root_node.clone()],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: None,
};
let tree = super::Tree::new(first_update, Box::new(NullActionHandler {}));
Expand Down Expand Up @@ -457,7 +457,7 @@ mod tests {
},
Node::new(NODE_ID_2, Role::RootWebArea),
],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: None,
};
let tree = super::Tree::new(first_update, Box::new(NullActionHandler {}));
Expand Down Expand Up @@ -504,7 +504,7 @@ mod tests {
Node::new(NODE_ID_2, Role::Button),
Node::new(NODE_ID_3, Role::Button),
],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: Some(NODE_ID_2),
};
let tree = super::Tree::new(first_update, Box::new(NullActionHandler {}));
Expand Down Expand Up @@ -569,7 +569,7 @@ mod tests {
..child_node.clone()
},
],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: None,
};
let tree = super::Tree::new(first_update, Box::new(NullActionHandler {}));
Expand Down Expand Up @@ -620,7 +620,7 @@ mod tests {
Node::new(NODE_ID_2, Role::Button),
Node::new(NODE_ID_3, Role::Button),
],
tree: Some(Tree::new(NODE_ID_1, StringEncoding::Utf8)),
tree: Some(Tree::new(NODE_ID_1)),
focus: Some(NODE_ID_2),
};
let tree = super::Tree::new(update.clone(), Box::new(NullActionHandler {}));
Expand Down
5 changes: 2 additions & 3 deletions platforms/windows/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::{cell::RefCell, convert::TryInto, mem::drop, num::NonZeroU128, rc::Rc};

use accesskit::kurbo::Rect;
use accesskit::{
Action, ActionHandler, ActionRequest, DefaultActionVerb, Node, NodeId, Role, StringEncoding,
Tree, TreeUpdate,
Action, ActionHandler, ActionRequest, DefaultActionVerb, Node, NodeId, Role, Tree, TreeUpdate,
};
use lazy_static::lazy_static;
use windows::{
Expand Down Expand Up @@ -105,7 +104,7 @@ fn get_initial_state() -> TreeUpdate {
let button_2 = make_button(BUTTON_2_ID, "Button 2");
TreeUpdate {
nodes: vec![root, button_1, button_2],
tree: Some(Tree::new(WINDOW_ID, StringEncoding::Utf8)),
tree: Some(Tree::new(WINDOW_ID)),
focus: None,
}
}
Expand Down
6 changes: 2 additions & 4 deletions platforms/windows/examples/winit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use accesskit::{
Action, ActionHandler, ActionRequest, Node, NodeId, Role, StringEncoding, Tree, TreeUpdate,
};
use accesskit::{Action, ActionHandler, ActionRequest, Node, NodeId, Role, Tree, TreeUpdate};
use accesskit_windows::{Adapter, SubclassingAdapter};
use std::{
num::NonZeroU128,
Expand Down Expand Up @@ -67,7 +65,7 @@ fn initial_tree_update(state: &State) -> TreeUpdate {
let button_2 = make_button(BUTTON_2_ID, "Button 2");
TreeUpdate {
nodes: vec![root, button_1, button_2],
tree: Some(Tree::new(WINDOW_ID, StringEncoding::Utf8)),
tree: Some(Tree::new(WINDOW_ID)),
focus: state.is_window_focused.then_some(state.focus),
}
}
Expand Down
6 changes: 2 additions & 4 deletions platforms/windows/src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

use std::{convert::TryInto, num::NonZeroU128};

use accesskit::{
ActionHandler, ActionRequest, Node, NodeId, Role, StringEncoding, Tree, TreeUpdate,
};
use accesskit::{ActionHandler, ActionRequest, Node, NodeId, Role, Tree, TreeUpdate};
use windows::{core::*, Win32::UI::Accessibility::*};

use super::*;
Expand Down Expand Up @@ -36,7 +34,7 @@ fn get_initial_state() -> TreeUpdate {
let button_2 = make_button(BUTTON_2_ID, "Button 2");
TreeUpdate {
nodes: vec![root, button_1, button_2],
tree: Some(Tree::new(WINDOW_ID, StringEncoding::Utf8)),
tree: Some(Tree::new(WINDOW_ID)),
focus: None,
}
}
Expand Down
6 changes: 2 additions & 4 deletions platforms/windows/src/tests/subclassed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

use std::num::NonZeroU128;

use accesskit::{
ActionHandler, ActionRequest, Node, NodeId, Role, StringEncoding, Tree, TreeUpdate,
};
use accesskit::{ActionHandler, ActionRequest, Node, NodeId, Role, Tree, TreeUpdate};
use windows::Win32::{Foundation::*, UI::Accessibility::*};
use winit::{
event_loop::EventLoop,
Expand Down Expand Up @@ -41,7 +39,7 @@ fn get_initial_state() -> TreeUpdate {
let button_2 = make_button(BUTTON_2_ID, "Button 2");
TreeUpdate {
nodes: vec![root, button_1, button_2],
tree: Some(Tree::new(WINDOW_ID, StringEncoding::Utf8)),
tree: Some(Tree::new(WINDOW_ID)),
focus: None,
}
}
Expand Down
3 changes: 0 additions & 3 deletions testdata/README.md

This file was deleted.

Loading

0 comments on commit 386ca0a

Please sign in to comment.