accesskit_atspi_common/
context.rs

1// Copyright 2023 The AccessKit Authors. All rights reserved.
2// Licensed under the Apache License, Version 2.0 (found in
3// the LICENSE-APACHE file) or the MIT license (found in
4// the LICENSE-MIT file), at your option.
5
6use accesskit::{ActionHandler, ActionRequest};
7use accesskit_consumer::Tree;
8use std::fmt::{Debug, Formatter};
9use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
10
11use crate::WindowBounds;
12
13/// This is an implementation detail of `accesskit_unix`, required for robust
14/// state transitions with minimal overhead.
15pub trait ActionHandlerNoMut {
16    fn do_action(&self, request: ActionRequest);
17}
18
19/// This is an implementation detail of `accesskit_unix`, required for robust
20/// state transitions with minimal overhead.
21pub struct ActionHandlerWrapper<H: ActionHandler + Send>(Mutex<H>);
22
23impl<H: 'static + ActionHandler + Send> ActionHandlerWrapper<H> {
24    pub fn new(inner: H) -> Self {
25        Self(Mutex::new(inner))
26    }
27}
28
29impl<H: ActionHandler + Send> ActionHandlerNoMut for ActionHandlerWrapper<H> {
30    fn do_action(&self, request: ActionRequest) {
31        self.0.lock().unwrap().do_action(request)
32    }
33}
34
35pub(crate) struct Context {
36    pub(crate) app_context: Arc<RwLock<AppContext>>,
37    pub(crate) tree: RwLock<Tree>,
38    pub(crate) action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
39    pub(crate) root_window_bounds: RwLock<WindowBounds>,
40}
41
42impl Debug for Context {
43    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("Context")
45            .field("app_context", &self.app_context)
46            .field("tree", &self.tree)
47            .field("action_handler", &"ActionHandler")
48            .field("root_window_bounds", &self.root_window_bounds)
49            .finish()
50    }
51}
52
53impl Context {
54    pub(crate) fn new(
55        app_context: &Arc<RwLock<AppContext>>,
56        tree: Tree,
57        action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
58        root_window_bounds: WindowBounds,
59    ) -> Arc<Self> {
60        Arc::new(Self {
61            app_context: Arc::clone(app_context),
62            tree: RwLock::new(tree),
63            action_handler,
64            root_window_bounds: RwLock::new(root_window_bounds),
65        })
66    }
67
68    pub(crate) fn read_tree(&self) -> RwLockReadGuard<'_, Tree> {
69        self.tree.read().unwrap()
70    }
71
72    pub(crate) fn read_root_window_bounds(&self) -> RwLockReadGuard<'_, WindowBounds> {
73        self.root_window_bounds.read().unwrap()
74    }
75
76    pub fn do_action(&self, request: ActionRequest) {
77        self.action_handler.do_action(request);
78    }
79
80    pub(crate) fn read_app_context(&self) -> RwLockReadGuard<'_, AppContext> {
81        self.app_context.read().unwrap()
82    }
83
84    pub(crate) fn write_app_context(&self) -> RwLockWriteGuard<'_, AppContext> {
85        self.app_context.write().unwrap()
86    }
87}
88
89#[derive(Debug)]
90pub struct AppContext {
91    pub(crate) name: Option<String>,
92    pub(crate) toolkit_name: Option<String>,
93    pub(crate) toolkit_version: Option<String>,
94    pub(crate) id: Option<i32>,
95    pub(crate) adapters: Vec<(usize, Arc<Context>)>,
96}
97
98impl AppContext {
99    pub fn new(name: Option<String>) -> Arc<RwLock<Self>> {
100        Arc::new(RwLock::new(Self {
101            name,
102            toolkit_name: None,
103            toolkit_version: None,
104            id: None,
105            adapters: Vec::new(),
106        }))
107    }
108
109    pub(crate) fn adapter_index(&self, id: usize) -> Result<usize, usize> {
110        self.adapters.binary_search_by(|adapter| adapter.0.cmp(&id))
111    }
112
113    pub(crate) fn push_adapter(&mut self, id: usize, context: &Arc<Context>) {
114        self.adapters.push((id, Arc::clone(context)));
115    }
116
117    pub(crate) fn remove_adapter(&mut self, id: usize) {
118        if let Ok(index) = self.adapter_index(id) {
119            self.adapters.remove(index);
120        }
121    }
122}