From 96df8de48c3d9021cd13430f271ece5f0b5ec668 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Sun, 8 Dec 2024 10:51:27 -0600 Subject: [PATCH 1/5] refactor!: Drop `Tree::app_name` --- common/src/lib.rs | 3 --- consumer/src/tree.rs | 4 ---- platforms/atspi-common/src/adapter.rs | 1 - platforms/atspi-common/src/context.rs | 4 ++-- platforms/macos/Cargo.toml | 1 - platforms/windows/examples/hello_world.rs | 3 +-- platforms/windows/src/node.rs | 4 +--- platforms/windows/src/util.rs | 15 +++------------ platforms/winit/examples/mixed_handlers.rs | 3 +-- platforms/winit/examples/simple.rs | 3 +-- 10 files changed, 9 insertions(+), 32 deletions(-) diff --git a/common/src/lib.rs b/common/src/lib.rs index b630f21a8..68143a793 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -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, /// The name of the UI toolkit in use. pub toolkit_name: Option, /// The version of the UI toolkit. @@ -2253,7 +2251,6 @@ impl Tree { pub fn new(root: NodeId) -> Tree { Tree { root, - app_name: None, toolkit_name: None, toolkit_version: None, } diff --git a/consumer/src/tree.rs b/consumer/src/tree.rs index 24a23f9f9..fb881a049 100644 --- a/consumer/src/tree.rs +++ b/consumer/src/tree.rs @@ -213,10 +213,6 @@ impl State { self.focus_id().map(|id| self.node_by_id(id).unwrap()) } - pub fn app_name(&self) -> Option { - self.data.app_name.clone() - } - pub fn toolkit_name(&self) -> Option { self.data.toolkit_name.clone() } diff --git a/platforms/atspi-common/src/adapter.rs b/platforms/atspi-common/src/adapter.rs index e44de30e4..3fdf32d4a 100644 --- a/platforms/atspi-common/src/adapter.rs +++ b/platforms/atspi-common/src/adapter.rs @@ -405,7 +405,6 @@ 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(); let adapter_index = app_context.adapter_index(self.id).unwrap(); diff --git a/platforms/atspi-common/src/context.rs b/platforms/atspi-common/src/context.rs index dbf02c485..7a8f0b1cc 100644 --- a/platforms/atspi-common/src/context.rs +++ b/platforms/atspi-common/src/context.rs @@ -83,9 +83,9 @@ pub struct AppContext { } impl AppContext { - pub fn new() -> Arc> { + pub fn new(name: Option) -> Arc> { Arc::new(RwLock::new(Self { - name: None, + name, toolkit_name: None, toolkit_version: None, id: None, diff --git a/platforms/macos/Cargo.toml b/platforms/macos/Cargo.toml index d461a4a01..2af0abf6d 100644 --- a/platforms/macos/Cargo.toml +++ b/platforms/macos/Cargo.toml @@ -34,4 +34,3 @@ objc2-app-kit = { version = "0.2.0", features = [ "NSView", "NSWindow", ] } - diff --git a/platforms/windows/examples/hello_world.rs b/platforms/windows/examples/hello_world.rs index 2dc05f992..8872b7c16 100644 --- a/platforms/windows/examples/hello_world.rs +++ b/platforms/windows/examples/hello_world.rs @@ -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![ diff --git a/platforms/windows/src/node.rs b/platforms/windows/src/node.rs index bc6c29330..f9668273b 100644 --- a/platforms/windows/src/node.rs +++ b/platforms/windows/src/node.rs @@ -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(), _ => (), } } diff --git a/platforms/windows/src/util.rs b/platforms/windows/src/util.rs index efdb4da16..9ff3eda41 100644 --- a/platforms/windows/src/util.rs +++ b/platforms/windows/src/util.rs @@ -216,23 +216,14 @@ pub(crate) fn window_title(hwnd: WindowHandle) -> Option { Some(BSTR::from_wide(&buffer).unwrap()) } -pub(crate) fn app_and_toolkit_description(state: &TreeState) -> Option { - let app_name = state.app_name(); +pub(crate) fn toolkit_description(state: &TreeState) -> Option { 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)) - } - (None, Some(toolkit_name), Some(toolkit_version)) => { + match (&toolkit_name, &toolkit_version) { + (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, } } diff --git a/platforms/winit/examples/mixed_handlers.rs b/platforms/winit/examples/mixed_handlers.rs index affa58b6d..5219b9e10 100644 --- a/platforms/winit/examples/mixed_handlers.rs +++ b/platforms/winit/examples/mixed_handlers.rs @@ -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), diff --git a/platforms/winit/examples/simple.rs b/platforms/winit/examples/simple.rs index ec535d075..5005e1c85 100644 --- a/platforms/winit/examples/simple.rs +++ b/platforms/winit/examples/simple.rs @@ -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), From cc5815521016f6c949b1b72b9b236a74d8f7b631 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Sun, 8 Dec 2024 10:55:09 -0600 Subject: [PATCH 2/5] Simplify `toolkit_description` --- platforms/windows/src/util.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/platforms/windows/src/util.rs b/platforms/windows/src/util.rs index 9ff3eda41..f369b6cbb 100644 --- a/platforms/windows/src/util.rs +++ b/platforms/windows/src/util.rs @@ -217,15 +217,13 @@ pub(crate) fn window_title(hwnd: WindowHandle) -> Option { } pub(crate) fn toolkit_description(state: &TreeState) -> Option { - let toolkit_name = state.toolkit_name(); - let toolkit_version = state.toolkit_version(); - match (&toolkit_name, &toolkit_version) { - (Some(toolkit_name), Some(toolkit_version)) => { - Some(format!("{} {}", toolkit_name, toolkit_version)) + state.toolkit_name().map(|name| { + if let Some(version) = state.toolkit_version() { + format!("{} {}", name, version) + } else { + name } - _ if toolkit_name.is_some() => toolkit_name, - _ => None, - } + }) } pub(crate) fn upgrade(weak: &Weak) -> Result> { From 3d56fd76f35fc949879bc147791d200cb9020a2a Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Sun, 8 Dec 2024 11:05:49 -0600 Subject: [PATCH 3/5] Autoamtically set the app name in the Unix adapter --- platforms/unix/src/context.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/platforms/unix/src/context.rs b/platforms/unix/src/context.rs index f940cb673..a685718e0 100644 --- a/platforms/unix/src/context.rs +++ b/platforms/unix/src/context.rs @@ -33,8 +33,15 @@ use crate::{ static APP_CONTEXT: OnceLock>> = OnceLock::new(); static MESSAGES: OnceLock> = OnceLock::new(); +fn app_name() -> Option { + 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> { - APP_CONTEXT.get_or_init(AppContext::new) + APP_CONTEXT.get_or_init(|| AppContext::new(app_name())) } pub(crate) fn get_or_init_messages() -> Sender { From cb38582b2905bb8e487256306faee6267f1ebba3 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Sun, 8 Dec 2024 11:19:32 -0600 Subject: [PATCH 4/5] Work so far fixing `toolkit_name` and `toolkit_version` accessors --- consumer/src/tree.rs | 10 +++++----- platforms/windows/src/util.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/consumer/src/tree.rs b/consumer/src/tree.rs index fb881a049..d3c8635ad 100644 --- a/consumer/src/tree.rs +++ b/consumer/src/tree.rs @@ -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; @@ -213,12 +213,12 @@ impl State { self.focus_id().map(|id| self.node_by_id(id).unwrap()) } - pub fn toolkit_name(&self) -> Option { - self.data.toolkit_name.clone() + pub fn toolkit_name(&self) -> Option<&str> { + self.data.toolkit_name.as_deref() } - pub fn toolkit_version(&self) -> Option { - self.data.toolkit_version.clone() + pub fn toolkit_version(&self) -> Option<&str> { + self.data.toolkit_version.as_deref() } } diff --git a/platforms/windows/src/util.rs b/platforms/windows/src/util.rs index f369b6cbb..0bf096018 100644 --- a/platforms/windows/src/util.rs +++ b/platforms/windows/src/util.rs @@ -221,7 +221,7 @@ pub(crate) fn toolkit_description(state: &TreeState) -> Option { if let Some(version) = state.toolkit_version() { format!("{} {}", name, version) } else { - name + name.to_string() } }) } From c5720ee3b85a1be0c186db55a2abd6c29fbbece7 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Sun, 8 Dec 2024 11:28:33 -0600 Subject: [PATCH 5/5] Fix atspi-common --- platforms/atspi-common/src/adapter.rs | 4 ++-- platforms/atspi-common/src/node.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/platforms/atspi-common/src/adapter.rs b/platforms/atspi-common/src/adapter.rs index 3fdf32d4a..cdd79aef5 100644 --- a/platforms/atspi-common/src/adapter.rs +++ b/platforms/atspi-common/src/adapter.rs @@ -405,8 +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.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(); diff --git a/platforms/atspi-common/src/node.rs b/platforms/atspi-common/src/node.rs index 6948f9744..a9f9e1228 100644 --- a/platforms/atspi-common/src/node.rs +++ b/platforms/atspi-common/src/node.rs @@ -695,11 +695,11 @@ impl PlatformNode { } pub fn toolkit_name(&self) -> Result { - 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 { - 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 {