From 089794c8f74957e91a19ae3df508e2a892f39ebc Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Sun, 8 Dec 2024 15:33:14 -0600 Subject: [PATCH] refactor!: Drop `Tree::app_name` (#492) --- common/src/lib.rs | 3 --- consumer/src/tree.rs | 14 +++++------- platforms/atspi-common/src/adapter.rs | 5 ++--- platforms/atspi-common/src/context.rs | 4 ++-- platforms/atspi-common/src/node.rs | 4 ++-- platforms/macos/Cargo.toml | 1 - platforms/unix/src/context.rs | 9 +++++++- platforms/windows/examples/hello_world.rs | 3 +-- platforms/windows/src/node.rs | 4 +--- platforms/windows/src/util.rs | 25 ++++++---------------- platforms/winit/examples/mixed_handlers.rs | 3 +-- platforms/winit/examples/simple.rs | 3 +-- 12 files changed, 30 insertions(+), 48 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..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,16 +213,12 @@ 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<&str> { + self.data.toolkit_name.as_deref() } - pub fn toolkit_name(&self) -> Option { - self.data.toolkit_name.clone() - } - - 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/atspi-common/src/adapter.rs b/platforms/atspi-common/src/adapter.rs index e44de30e4..cdd79aef5 100644 --- a/platforms/atspi-common/src/adapter.rs +++ b/platforms/atspi-common/src/adapter.rs @@ -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(); 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/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 { 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/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 { 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..0bf096018 100644 --- a/platforms/windows/src/util.rs +++ b/platforms/windows/src/util.rs @@ -216,25 +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(); - 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 { + 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(weak: &Weak) -> Result> { 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),