Repository navigation
Expand file tree
/
Copy pathcontext.rs
More file actions
98 lines (83 loc) · 2.99 KB
/
Copy pathcontext.rs
File metadata and controls
98 lines (83 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2023 The AccessKit Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (found in
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.
use accesskit::{ActionHandler, ActionRequest, Point};
use accesskit_consumer::{FullNodeId, Tree};
use hashbrown::HashMap;
use std::fmt::{Debug, Formatter};
use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard, atomic::AtomicBool};
use windows::core::ComObject;
use crate::{node::PlatformNode, util::*, window_handle::WindowHandle};
pub(crate) trait ActionHandlerNoMut {
fn do_action(&self, request: ActionRequest);
}
pub(crate) struct ActionHandlerWrapper<H: ActionHandler + Send>(Mutex<H>);
impl<H: 'static + ActionHandler + Send> ActionHandlerWrapper<H> {
pub(crate) fn new(inner: H) -> Self {
Self(Mutex::new(inner))
}
}
impl<H: ActionHandler + Send> ActionHandlerNoMut for ActionHandlerWrapper<H> {
fn do_action(&self, request: ActionRequest) {
self.0.lock().unwrap().do_action(request)
}
}
pub(crate) struct Context {
pub(crate) hwnd: WindowHandle,
pub(crate) tree: RwLock<Tree>,
pub(crate) action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
pub(crate) is_placeholder: AtomicBool,
platform_nodes: Mutex<HashMap<FullNodeId, ComObject<PlatformNode>>>,
}
impl Debug for Context {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Context")
.field("hwnd", &self.hwnd)
.field("tree", &self.tree)
.field("action_handler", &"ActionHandler")
.field("is_placeholder", &self.is_placeholder)
.finish()
}
}
impl Context {
pub(crate) fn new(
hwnd: WindowHandle,
tree: Tree,
action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
is_placeholder: bool,
) -> Arc<Self> {
Arc::new(Self {
hwnd,
tree: RwLock::new(tree),
action_handler,
is_placeholder: AtomicBool::new(is_placeholder),
platform_nodes: Mutex::new(HashMap::new()),
})
}
pub(crate) fn read_tree(&self) -> RwLockReadGuard<'_, Tree> {
self.tree.read().unwrap()
}
pub(crate) fn client_top_left(&self) -> Point {
client_top_left(self.hwnd)
}
pub(crate) fn do_action(&self, request: ActionRequest) {
self.action_handler.do_action(request);
}
pub(crate) fn get_or_create_platform_node(
self: &Arc<Self>,
id: FullNodeId,
) -> ComObject<PlatformNode> {
let mut platform_nodes = self.platform_nodes.lock().unwrap();
if let Some(result) = platform_nodes.get(&id) {
return result.clone();
}
let result = PlatformNode::new(self, id);
platform_nodes.insert(id, result.clone());
result
}
pub(crate) fn remove_platform_node(&self, id: FullNodeId) {
let mut platform_nodes = self.platform_nodes.lock().unwrap();
platform_nodes.remove(&id);
}
}