Skip to content

Commit

Permalink
refactor!: Drop Tree::app_name (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcampbell authored Dec 8, 2024
1 parent b4a89a3 commit 089794c
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 48 deletions.
3 changes: 0 additions & 3 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2240,8 +2240,6 @@ impl JsonSchema for Properties {
pub struct Tree {
/// The identifier of the tree's root node.
pub root: NodeId,
/// The name of the application this tree belongs to.
pub app_name: Option<String>,
/// The name of the UI toolkit in use.
pub toolkit_name: Option<String>,
/// The version of the UI toolkit.
Expand All @@ -2253,7 +2251,6 @@ impl Tree {
pub fn new(root: NodeId) -> Tree {
Tree {
root,
app_name: None,
toolkit_name: None,
toolkit_version: None,
}
Expand Down
14 changes: 5 additions & 9 deletions consumer/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// the LICENSE-MIT file), at your option.

use accesskit::{FrozenNode as NodeData, NodeId, Tree as TreeData, TreeUpdate};
use alloc::{string::String, sync::Arc, vec};
use alloc::{sync::Arc, vec};
use core::fmt;
use hashbrown::{HashMap, HashSet};
use immutable_chunkmap::map::MapM as ChunkMap;
Expand Down Expand Up @@ -213,16 +213,12 @@ impl State {
self.focus_id().map(|id| self.node_by_id(id).unwrap())
}

pub fn app_name(&self) -> Option<String> {
self.data.app_name.clone()
pub fn toolkit_name(&self) -> Option<&str> {
self.data.toolkit_name.as_deref()
}

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

pub fn toolkit_version(&self) -> Option<String> {
self.data.toolkit_version.clone()
pub fn toolkit_version(&self) -> Option<&str> {
self.data.toolkit_version.as_deref()
}
}

Expand Down
5 changes: 2 additions & 3 deletions platforms/atspi-common/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,8 @@ impl Adapter {
let tree = self.context.read_tree();
let tree_state = tree.state();
let mut app_context = self.context.write_app_context();
app_context.name = tree_state.app_name();
app_context.toolkit_name = tree_state.toolkit_name();
app_context.toolkit_version = tree_state.toolkit_version();
app_context.toolkit_name = tree_state.toolkit_name().map(|s| s.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();
Expand Down
4 changes: 2 additions & 2 deletions platforms/atspi-common/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ pub struct AppContext {
}

impl AppContext {
pub fn new() -> Arc<RwLock<Self>> {
pub fn new(name: Option<String>) -> Arc<RwLock<Self>> {
Arc::new(RwLock::new(Self {
name: None,
name,
toolkit_name: None,
toolkit_version: None,
id: None,
Expand Down
4 changes: 2 additions & 2 deletions platforms/atspi-common/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,11 @@ impl PlatformNode {
}

pub fn toolkit_name(&self) -> Result<String> {
self.with_tree_state(|state| Ok(state.toolkit_name().unwrap_or_default()))
self.with_tree_state(|state| Ok(state.toolkit_name().unwrap_or_default().to_string()))
}

pub fn toolkit_version(&self) -> Result<String> {
self.with_tree_state(|state| Ok(state.toolkit_version().unwrap_or_default()))
self.with_tree_state(|state| Ok(state.toolkit_version().unwrap_or_default().to_string()))
}

pub fn parent(&self) -> Result<NodeIdOrRoot> {
Expand Down
1 change: 0 additions & 1 deletion platforms/macos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ objc2-app-kit = { version = "0.2.0", features = [
"NSView",
"NSWindow",
] }

9 changes: 8 additions & 1 deletion platforms/unix/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ use crate::{
static APP_CONTEXT: OnceLock<Arc<RwLock<AppContext>>> = OnceLock::new();
static MESSAGES: OnceLock<Sender<Message>> = OnceLock::new();

fn app_name() -> Option<String> {
std::env::current_exe().ok().and_then(|path| {
path.file_name()
.map(|name| name.to_string_lossy().to_string())
})
}

pub(crate) fn get_or_init_app_context<'a>() -> &'a Arc<RwLock<AppContext>> {
APP_CONTEXT.get_or_init(AppContext::new)
APP_CONTEXT.get_or_init(|| AppContext::new(app_name()))
}

pub(crate) fn get_or_init_messages() -> Sender<Message> {
Expand Down
3 changes: 1 addition & 2 deletions platforms/windows/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ impl ActivationHandler for InnerWindowState {
let root = self.build_root();
let button_1 = build_button(BUTTON_1_ID, "Button 1");
let button_2 = build_button(BUTTON_2_ID, "Button 2");
let mut tree = Tree::new(WINDOW_ID);
tree.app_name = Some("hello_world".to_string());
let tree = Tree::new(WINDOW_ID);

let mut result = TreeUpdate {
nodes: vec![
Expand Down
4 changes: 1 addition & 3 deletions platforms/windows/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,7 @@ impl IRawElementProviderSimple_Impl for PlatformNode_Impl {
}
match property_id {
UIA_FrameworkIdPropertyId => result = state.toolkit_name().into(),
UIA_ProviderDescriptionPropertyId => {
result = app_and_toolkit_description(state).into()
}
UIA_ProviderDescriptionPropertyId => result = toolkit_description(state).into(),
_ => (),
}
}
Expand Down
25 changes: 7 additions & 18 deletions platforms/windows/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,14 @@ pub(crate) fn window_title(hwnd: WindowHandle) -> Option<BSTR> {
Some(BSTR::from_wide(&buffer).unwrap())
}

pub(crate) fn app_and_toolkit_description(state: &TreeState) -> Option<String> {
let app_name = state.app_name();
let toolkit_name = state.toolkit_name();
let toolkit_version = state.toolkit_version();
match (&app_name, &toolkit_name, &toolkit_version) {
(Some(app_name), Some(toolkit_name), Some(toolkit_version)) => Some(format!(
"{} <{} {}>",
app_name, toolkit_name, toolkit_version
)),
(Some(app_name), Some(toolkit_name), None) => {
Some(format!("{} <{}>", app_name, toolkit_name))
pub(crate) fn toolkit_description(state: &TreeState) -> Option<String> {
state.toolkit_name().map(|name| {
if let Some(version) = state.toolkit_version() {
format!("{} {}", name, version)
} else {
name.to_string()
}
(None, Some(toolkit_name), Some(toolkit_version)) => {
Some(format!("{} {}", toolkit_name, toolkit_version))
}
_ if toolkit_name.is_some() => toolkit_name,
_ if app_name.is_some() => app_name,
_ => None,
}
})
}

pub(crate) fn upgrade<T>(weak: &Weak<T>) -> Result<Arc<T>> {
Expand Down
3 changes: 1 addition & 2 deletions platforms/winit/examples/mixed_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ impl UiState {
let root = self.build_root();
let button_1 = build_button(BUTTON_1_ID, "Button 1");
let button_2 = build_button(BUTTON_2_ID, "Button 2");
let mut tree = Tree::new(WINDOW_ID);
tree.app_name = Some("simple".to_string());
let tree = Tree::new(WINDOW_ID);
let mut result = TreeUpdate {
nodes: vec![
(WINDOW_ID, root),
Expand Down
3 changes: 1 addition & 2 deletions platforms/winit/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ impl UiState {
let root = self.build_root();
let button_1 = build_button(BUTTON_1_ID, "Button 1");
let button_2 = build_button(BUTTON_2_ID, "Button 2");
let mut tree = Tree::new(WINDOW_ID);
tree.app_name = Some("simple".to_string());
let tree = Tree::new(WINDOW_ID);
let mut result = TreeUpdate {
nodes: vec![
(WINDOW_ID, root),
Expand Down

0 comments on commit 089794c

Please sign in to comment.