Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

587
vendor/accesskit_windows/src/adapter.rs vendored Normal file
View File

@@ -0,0 +1,587 @@
// Copyright 2022 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, ActivationHandler, Live, Node as NodeProvider, NodeId, Role, Tree as TreeData,
TreeUpdate,
};
use accesskit_consumer::{FilterResult, Node, Tree, TreeChangeHandler};
use hashbrown::{HashMap, HashSet};
use std::fmt::{Debug, Formatter};
use std::sync::{atomic::Ordering, Arc};
use windows::Win32::{
Foundation::*,
UI::{Accessibility::*, WindowsAndMessaging::*},
};
use crate::{
context::{ActionHandlerNoMut, ActionHandlerWrapper, Context},
filters::filter,
node::{NodeWrapper, PlatformNode},
util::QueuedEvent,
window_handle::WindowHandle,
};
fn focus_event(context: &Arc<Context>, node_id: NodeId) -> QueuedEvent {
let platform_node = PlatformNode::new(context, node_id);
let element: IRawElementProviderSimple = platform_node.into();
QueuedEvent::Simple {
element,
event_id: UIA_AutomationFocusChangedEventId,
}
}
struct AdapterChangeHandler<'a> {
context: &'a Arc<Context>,
queue: Vec<QueuedEvent>,
text_changed: HashSet<NodeId>,
selection_changed: HashMap<NodeId, SelectionChanges>,
}
impl<'a> AdapterChangeHandler<'a> {
fn new(context: &'a Arc<Context>) -> Self {
Self {
context,
queue: Vec::new(),
text_changed: HashSet::new(),
selection_changed: HashMap::new(),
}
}
}
impl AdapterChangeHandler<'_> {
fn insert_text_change_if_needed_parent(&mut self, node: Node) {
if !node.supports_text_ranges() {
return;
}
let id = node.id();
if self.text_changed.contains(&id) {
return;
}
let platform_node = PlatformNode::new(self.context, node.id());
let element: IRawElementProviderSimple = platform_node.into();
// Text change events must come before selection change
// events. It doesn't matter if text change events come
// before other events.
self.queue.insert(
0,
QueuedEvent::Simple {
element,
event_id: UIA_Text_TextChangedEventId,
},
);
self.text_changed.insert(id);
}
fn insert_text_change_if_needed(&mut self, node: &Node) {
if node.role() != Role::TextRun {
return;
}
if let Some(node) = node.filtered_parent(&filter) {
self.insert_text_change_if_needed_parent(node);
}
}
fn handle_selection_state_change(&mut self, node: &Node, is_selected: bool) {
// If `node` belongs to a selection container, then map the events with the
// selection container as the key because |FinalizeSelectionEvents| needs to
// determine whether or not there is only one element selected in order to
// optimize what platform events are sent.
let key = if let Some(container) = node.selection_container(&filter) {
container.id()
} else {
node.id()
};
let changes = self
.selection_changed
.entry(key)
.or_insert_with(|| SelectionChanges {
added_items: HashSet::new(),
removed_items: HashSet::new(),
});
if is_selected {
changes.added_items.insert(node.id());
} else {
changes.removed_items.insert(node.id());
}
}
fn enqueue_selection_changes(&mut self, tree: &Tree) {
let tree_state = tree.state();
for (id, changes) in self.selection_changed.iter() {
let Some(node) = tree_state.node_by_id(*id) else {
continue;
};
// Determine if `node` is a selection container with one selected child in
// order to optimize what platform events are sent.
let mut container = None;
let mut only_selected_child = None;
if node.is_container_with_selectable_children() {
container = Some(node);
for child in node.filtered_children(filter) {
if let Some(true) = child.is_selected() {
if only_selected_child.is_none() {
only_selected_child = Some(child);
} else {
only_selected_child = None;
break;
}
}
}
}
if let Some(only_selected_child) = only_selected_child {
self.queue.push(QueuedEvent::Simple {
element: PlatformNode::new(self.context, only_selected_child.id()).into(),
event_id: UIA_SelectionItem_ElementSelectedEventId,
});
self.queue.push(QueuedEvent::PropertyChanged {
element: PlatformNode::new(self.context, only_selected_child.id()).into(),
property_id: UIA_SelectionItemIsSelectedPropertyId,
old_value: false.into(),
new_value: true.into(),
});
for child_id in changes.removed_items.iter() {
let platform_node = PlatformNode::new(self.context, *child_id);
self.queue.push(QueuedEvent::PropertyChanged {
element: platform_node.into(),
property_id: UIA_SelectionItemIsSelectedPropertyId,
old_value: true.into(),
new_value: false.into(),
});
}
} else {
// Per UIA documentation, beyond the "invalidate limit" we're supposed to
// fire a 'SelectionInvalidated' event. The exact value isn't specified,
// but System.Windows.Automation.Provider uses a value of 20.
const INVALIDATE_LIMIT: usize = 20;
if let Some(container) = container.filter(|_| {
changes.added_items.len() + changes.removed_items.len() > INVALIDATE_LIMIT
}) {
let platform_node = PlatformNode::new(self.context, container.id());
self.queue.push(QueuedEvent::Simple {
element: platform_node.into(),
event_id: UIA_Selection_InvalidatedEventId,
});
} else {
let container_is_multiselectable =
container.is_some_and(|c| c.is_multiselectable());
for added_id in changes.added_items.iter() {
self.queue.push(QueuedEvent::Simple {
element: PlatformNode::new(self.context, *added_id).into(),
event_id: match container_is_multiselectable {
true => UIA_SelectionItem_ElementAddedToSelectionEventId,
false => UIA_SelectionItem_ElementSelectedEventId,
},
});
self.queue.push(QueuedEvent::PropertyChanged {
element: PlatformNode::new(self.context, *added_id).into(),
property_id: UIA_SelectionItemIsSelectedPropertyId,
old_value: false.into(),
new_value: true.into(),
});
}
for removed_id in changes.removed_items.iter() {
self.queue.push(QueuedEvent::Simple {
element: PlatformNode::new(self.context, *removed_id).into(),
event_id: UIA_SelectionItem_ElementRemovedFromSelectionEventId,
});
self.queue.push(QueuedEvent::PropertyChanged {
element: PlatformNode::new(self.context, *removed_id).into(),
property_id: UIA_SelectionItemIsSelectedPropertyId,
old_value: true.into(),
new_value: false.into(),
});
}
}
}
}
}
}
struct SelectionChanges {
added_items: HashSet<NodeId>,
removed_items: HashSet<NodeId>,
}
impl TreeChangeHandler for AdapterChangeHandler<'_> {
fn node_added(&mut self, node: &Node) {
self.insert_text_change_if_needed(node);
if filter(node) != FilterResult::Include {
return;
}
let wrapper = NodeWrapper(node);
if wrapper.name().is_some() && node.live() != Live::Off {
let platform_node = PlatformNode::new(self.context, node.id());
let element: IRawElementProviderSimple = platform_node.into();
self.queue.push(QueuedEvent::Simple {
element,
event_id: UIA_LiveRegionChangedEventId,
});
}
if wrapper.is_selection_item_pattern_supported() && wrapper.is_selected() {
self.handle_selection_state_change(node, true);
}
}
fn node_updated(&mut self, old_node: &Node, new_node: &Node) {
if old_node.raw_value() != new_node.raw_value() {
self.insert_text_change_if_needed(new_node);
}
let old_node_was_filtered_out = filter(old_node) != FilterResult::Include;
if filter(new_node) != FilterResult::Include {
if !old_node_was_filtered_out {
let old_wrapper = NodeWrapper(old_node);
if old_wrapper.is_selection_item_pattern_supported() && old_wrapper.is_selected() {
self.handle_selection_state_change(old_node, false);
}
}
return;
}
let platform_node = PlatformNode::new(self.context, new_node.id());
let element: IRawElementProviderSimple = platform_node.into();
let old_wrapper = NodeWrapper(old_node);
let new_wrapper = NodeWrapper(new_node);
new_wrapper.enqueue_property_changes(&mut self.queue, &element, &old_wrapper);
let new_name = new_wrapper.name();
if new_name.is_some()
&& new_node.live() != Live::Off
&& (new_node.live() != old_node.live()
|| old_node_was_filtered_out
|| new_name != old_wrapper.name())
{
self.queue.push(QueuedEvent::Simple {
element,
event_id: UIA_LiveRegionChangedEventId,
});
}
if new_wrapper.is_selection_item_pattern_supported()
&& (new_wrapper.is_selected() != old_wrapper.is_selected()
|| (old_node_was_filtered_out && new_wrapper.is_selected()))
{
self.handle_selection_state_change(new_node, new_wrapper.is_selected());
}
}
fn focus_moved(&mut self, _old_node: Option<&Node>, new_node: Option<&Node>) {
if let Some(new_node) = new_node {
self.queue.push(focus_event(self.context, new_node.id()));
}
}
fn node_removed(&mut self, node: &Node) {
self.insert_text_change_if_needed(node);
if filter(node) != FilterResult::Include {
return;
}
let wrapper = NodeWrapper(node);
if wrapper.is_selection_item_pattern_supported() {
self.handle_selection_state_change(node, false);
}
}
// TODO: handle other events (#20)
}
const PLACEHOLDER_ROOT_ID: NodeId = NodeId(0);
enum State {
Inactive {
hwnd: WindowHandle,
is_window_focused: bool,
action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
},
Placeholder(Arc<Context>),
Active(Arc<Context>),
}
impl Debug for State {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
State::Inactive {
hwnd,
is_window_focused,
action_handler: _,
} => f
.debug_struct("Inactive")
.field("hwnd", hwnd)
.field("is_window_focused", is_window_focused)
.field("action_handler", &"ActionHandler")
.finish(),
State::Placeholder(context) => f.debug_tuple("Placeholder").field(context).finish(),
State::Active(context) => f.debug_tuple("Active").field(context).finish(),
}
}
}
#[derive(Debug)]
pub struct Adapter {
state: State,
}
impl Adapter {
/// Creates a new Windows platform adapter.
///
/// The action handler may or may not be called on the thread that owns
/// the window.
///
/// This must not be called while handling the `WM_GETOBJECT` message,
/// because this function must initialize UI Automation before
/// that message is handled. This is necessary to prevent a race condition
/// that leads to nested `WM_GETOBJECT` messages and, in some cases,
/// assistive technologies not realizing that the window natively implements.
/// UIA. See [AccessKit issue #37](https://github.com/AccessKit/accesskit/issues/37)
/// for more details.
pub fn new(
hwnd: HWND,
is_window_focused: bool,
action_handler: impl 'static + ActionHandler + Send,
) -> Self {
Self::with_wrapped_action_handler(
hwnd,
is_window_focused,
Arc::new(ActionHandlerWrapper::new(action_handler)),
)
}
// Currently required by the test infrastructure
pub(crate) fn with_wrapped_action_handler(
hwnd: HWND,
is_window_focused: bool,
action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
) -> Self {
init_uia();
let state = State::Inactive {
hwnd: hwnd.into(),
is_window_focused,
action_handler,
};
Self { state }
}
/// If and only if the tree has been initialized, call the provided function
/// and apply the resulting update. Note: If the caller's implementation of
/// [`ActivationHandler::request_initial_tree`] initially returned `None`,
/// the [`TreeUpdate`] returned by the provided function must contain
/// a full tree.
///
/// If a [`QueuedEvents`] instance is returned, the caller must call
/// [`QueuedEvents::raise`] on it.
///
/// This method may be safely called on any thread, but refer to
/// [`QueuedEvents::raise`] for restrictions on the context in which
/// it should be called.
pub fn update_if_active(
&mut self,
update_factory: impl FnOnce() -> TreeUpdate,
) -> Option<QueuedEvents> {
match &self.state {
State::Inactive { .. } => None,
State::Placeholder(context) => {
let is_window_focused = context.read_tree().state().is_host_focused();
let tree = Tree::new(update_factory(), is_window_focused);
*context.tree.write().unwrap() = tree;
context.is_placeholder.store(false, Ordering::SeqCst);
let result = context
.read_tree()
.state()
.focus_id()
.map(|id| QueuedEvents(vec![focus_event(context, id)]));
self.state = State::Active(Arc::clone(context));
result
}
State::Active(context) => {
let mut handler = AdapterChangeHandler::new(context);
let mut tree = context.tree.write().unwrap();
tree.update_and_process_changes(update_factory(), &mut handler);
handler.enqueue_selection_changes(&tree);
Some(QueuedEvents(handler.queue))
}
}
}
/// Update the tree state based on whether the window is focused.
///
/// If a [`QueuedEvents`] instance is returned, the caller must call
/// [`QueuedEvents::raise`] on it.
///
/// This method may be safely called on any thread, but refer to
/// [`QueuedEvents::raise`] for restrictions on the context in which
/// it should be called.
pub fn update_window_focus_state(&mut self, is_focused: bool) -> Option<QueuedEvents> {
match &mut self.state {
State::Inactive {
is_window_focused, ..
} => {
*is_window_focused = is_focused;
None
}
State::Placeholder(context) => {
let mut handler = AdapterChangeHandler::new(context);
let mut tree = context.tree.write().unwrap();
tree.update_host_focus_state_and_process_changes(is_focused, &mut handler);
Some(QueuedEvents(handler.queue))
}
State::Active(context) => {
let mut handler = AdapterChangeHandler::new(context);
let mut tree = context.tree.write().unwrap();
tree.update_host_focus_state_and_process_changes(is_focused, &mut handler);
Some(QueuedEvents(handler.queue))
}
}
}
/// Handle the `WM_GETOBJECT` window message. The accessibility tree
/// is lazily initialized if necessary using the provided
/// [`ActivationHandler`] implementation.
///
/// This returns an `Option` so the caller can pass the message
/// to `DefWindowProc` if AccessKit decides not to handle it.
/// The optional value is an `Into<LRESULT>` rather than simply an `LRESULT`
/// so the necessary call to UIA, which may lead to a nested `WM_GETOBJECT`
/// message, can be done outside of any lock that the caller might hold
/// on the `Adapter` or window state, while still abstracting away
/// the details of that call to UIA.
pub fn handle_wm_getobject<H: ActivationHandler + ?Sized>(
&mut self,
wparam: WPARAM,
lparam: LPARAM,
activation_handler: &mut H,
) -> Option<impl Into<LRESULT>> {
// Don't bother with MSAA object IDs that are asking for something other
// than the client area of the window. DefWindowProc can handle those.
// First, cast the lparam to i32, to handle inconsistent conversion
// behavior in senders.
let objid = normalize_objid(lparam);
if objid < 0 && objid != UiaRootObjectId && objid != OBJID_CLIENT.0 {
return None;
}
let (hwnd, platform_node) = match &self.state {
State::Inactive {
hwnd,
is_window_focused,
action_handler,
} => match activation_handler.request_initial_tree() {
Some(initial_state) => {
let hwnd = *hwnd;
let tree = Tree::new(initial_state, *is_window_focused);
let context = Context::new(hwnd, tree, Arc::clone(action_handler), false);
let node_id = context.read_tree().state().root_id();
let platform_node = PlatformNode::new(&context, node_id);
self.state = State::Active(context);
(hwnd, platform_node)
}
None => {
let hwnd = *hwnd;
let placeholder_update = TreeUpdate {
nodes: vec![(PLACEHOLDER_ROOT_ID, NodeProvider::new(Role::Window))],
tree: Some(TreeData::new(PLACEHOLDER_ROOT_ID)),
focus: PLACEHOLDER_ROOT_ID,
};
let placeholder_tree = Tree::new(placeholder_update, *is_window_focused);
let context =
Context::new(hwnd, placeholder_tree, Arc::clone(action_handler), true);
let platform_node = PlatformNode::unspecified_root(&context);
self.state = State::Placeholder(context);
(hwnd, platform_node)
}
},
State::Placeholder(context) => (context.hwnd, PlatformNode::unspecified_root(context)),
State::Active(context) => {
let node_id = context.read_tree().state().root_id();
(context.hwnd, PlatformNode::new(context, node_id))
}
};
let el: IRawElementProviderSimple = platform_node.into();
Some(WmGetObjectResult {
hwnd,
wparam,
lparam,
el,
})
}
}
fn init_uia() {
// `UiaLookupId` is a cheap way of forcing UIA to initialize itself.
unsafe {
UiaLookupId(
AutomationIdentifierType_Property,
&ControlType_Property_GUID,
)
};
}
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
fn normalize_objid(lparam: LPARAM) -> i32 {
(lparam.0 & 0xFFFFFFFF) as _
}
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
fn normalize_objid(lparam: LPARAM) -> i32 {
lparam.0 as _
}
struct WmGetObjectResult {
hwnd: WindowHandle,
wparam: WPARAM,
lparam: LPARAM,
el: IRawElementProviderSimple,
}
impl From<WmGetObjectResult> for LRESULT {
fn from(this: WmGetObjectResult) -> Self {
unsafe { UiaReturnRawElementProvider(this.hwnd.0, this.wparam, this.lparam, &this.el) }
}
}
/// Events generated by a tree update.
#[must_use = "events must be explicitly raised"]
pub struct QueuedEvents(Vec<QueuedEvent>);
impl QueuedEvents {
/// Raise all queued events synchronously.
///
/// The window may receive `WM_GETOBJECT` messages during this call.
/// This means that any locks required by the `WM_GETOBJECT` handler
/// must not be held when this method is called.
///
/// This method should be called on the thread that owns the window.
/// It's not clear whether this is a strict requirement of UIA itself,
/// but based on the known behavior of UIA, MSAA, and some ATs,
/// it's strongly recommended.
pub fn raise(self) {
for event in self.0 {
match event {
QueuedEvent::Simple { element, event_id } => {
unsafe { UiaRaiseAutomationEvent(&element, event_id) }.unwrap();
}
QueuedEvent::PropertyChanged {
element,
property_id,
old_value,
new_value,
} => {
unsafe {
UiaRaiseAutomationPropertyChangedEvent(
&element,
property_id,
&old_value,
&new_value,
)
}
.unwrap();
}
}
}
}
}
// We explicitly want to allow the queued events to be sent to the UI thread,
// so implement Send even though windows-rs doesn't implement it for all
// contained types. This is safe because we're not using COM threading.
unsafe impl Send for QueuedEvents {}

75
vendor/accesskit_windows/src/context.rs vendored Normal file
View File

@@ -0,0 +1,75 @@
// 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::Tree;
use std::fmt::{Debug, Formatter};
use std::sync::{atomic::AtomicBool, Arc, Mutex, RwLock, RwLockReadGuard};
use crate::{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,
}
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),
})
}
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);
}
}

View File

@@ -0,0 +1,8 @@
// 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.
pub(crate) use accesskit_consumer::{
common_filter as filter, common_filter_with_root_exception as filter_with_root_exception,
};

22
vendor/accesskit_windows/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,22 @@
// Copyright 2021 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.
mod context;
mod filters;
mod node;
mod text;
mod util;
mod window_handle;
mod adapter;
pub use adapter::{Adapter, QueuedEvents};
mod subclass;
pub use subclass::SubclassingAdapter;
pub use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
#[cfg(test)]
mod tests;

1079
vendor/accesskit_windows/src/node.rs vendored Normal file

File diff suppressed because it is too large Load Diff

188
vendor/accesskit_windows/src/subclass.rs vendored Normal file
View File

@@ -0,0 +1,188 @@
// Copyright 2022 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, ActivationHandler, TreeUpdate};
use std::{
cell::{Cell, RefCell},
ffi::c_void,
mem::transmute,
};
use windows::{
core::*,
Win32::{Foundation::*, UI::WindowsAndMessaging::*},
};
use crate::{Adapter, QueuedEvents};
fn win32_error() -> ! {
panic!("{}", Error::from_win32())
}
// Work around a difference between the SetWindowLongPtrW API definition
// in windows-rs on 32-bit and 64-bit Windows.
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
type LongPtr = isize;
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
type LongPtr = i32;
const PROP_NAME: PCWSTR = w!("AccessKitAdapter");
struct SubclassState {
adapter: Adapter,
activation_handler: Box<dyn ActivationHandler>,
}
struct SubclassImpl {
hwnd: HWND,
state: RefCell<SubclassState>,
prev_wnd_proc: WNDPROC,
window_destroyed: Cell<bool>,
}
extern "system" fn wnd_proc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
let handle = unsafe { GetPropW(window, PROP_NAME) };
let impl_ptr = handle.0 as *const SubclassImpl;
assert!(!impl_ptr.is_null());
let r#impl = unsafe { &*impl_ptr };
match message {
WM_GETOBJECT => {
let mut state = r#impl.state.borrow_mut();
let state_mut = &mut *state;
if let Some(result) = state_mut.adapter.handle_wm_getobject(
wparam,
lparam,
&mut *state_mut.activation_handler,
) {
drop(state);
return result.into();
}
}
WM_SETFOCUS | WM_EXITMENULOOP | WM_EXITSIZEMOVE => {
r#impl.update_window_focus_state(true);
}
WM_KILLFOCUS | WM_ENTERMENULOOP | WM_ENTERSIZEMOVE => {
r#impl.update_window_focus_state(false);
}
WM_NCDESTROY => {
r#impl.window_destroyed.set(true);
}
_ => (),
}
unsafe { CallWindowProcW(r#impl.prev_wnd_proc, window, message, wparam, lparam) }
}
impl SubclassImpl {
fn new(
hwnd: HWND,
activation_handler: impl 'static + ActivationHandler,
action_handler: impl 'static + ActionHandler + Send,
) -> Box<Self> {
let adapter = Adapter::new(hwnd, false, action_handler);
let state = RefCell::new(SubclassState {
adapter,
activation_handler: Box::new(activation_handler),
});
Box::new(Self {
hwnd,
state,
prev_wnd_proc: None,
window_destroyed: Cell::new(false),
})
}
fn install(&mut self) {
unsafe {
SetPropW(
self.hwnd,
PROP_NAME,
HANDLE(self as *const SubclassImpl as _),
)
}
.unwrap();
let result =
unsafe { SetWindowLongPtrW(self.hwnd, GWLP_WNDPROC, wnd_proc as *const c_void as _) };
if result == 0 {
win32_error();
}
self.prev_wnd_proc = unsafe { transmute::<LongPtr, WNDPROC>(result) };
}
fn update_window_focus_state(&self, is_focused: bool) {
let mut state = self.state.borrow_mut();
if let Some(events) = state.adapter.update_window_focus_state(is_focused) {
drop(state);
events.raise();
}
}
fn uninstall(&self) {
if self.window_destroyed.get() {
return;
}
let result = unsafe {
SetWindowLongPtrW(
self.hwnd,
GWLP_WNDPROC,
transmute::<WNDPROC, LongPtr>(self.prev_wnd_proc),
)
};
if result == 0 {
win32_error();
}
unsafe { RemovePropW(self.hwnd, PROP_NAME) }.unwrap();
}
}
/// Uses [Win32 subclassing] to handle `WM_GETOBJECT` messages on a window
/// that provides no other way of adding custom message handlers.
///
/// [Win32 subclassing]: https://docs.microsoft.com/en-us/windows/win32/controls/subclassing-overview
pub struct SubclassingAdapter(Box<SubclassImpl>);
impl SubclassingAdapter {
/// Creates a new Windows platform adapter using window subclassing.
/// This must be done before the window is shown or focused
/// for the first time.
///
/// This must be called on the thread that owns the window. The activation
/// handler will always be called on that thread. The action handler
/// may or may not be called on that thread.
pub fn new(
hwnd: HWND,
activation_handler: impl 'static + ActivationHandler,
action_handler: impl 'static + ActionHandler + Send,
) -> Self {
let mut r#impl = SubclassImpl::new(hwnd, activation_handler, action_handler);
r#impl.install();
Self(r#impl)
}
/// If and only if the tree has been initialized, call the provided function
/// and apply the resulting update. Note: If the caller's implementation of
/// [`ActivationHandler::request_initial_tree`] initially returned `None`,
/// the [`TreeUpdate`] returned by the provided function must contain
/// a full tree.
///
/// If a [`QueuedEvents`] instance is returned, the caller must call
/// [`QueuedEvents::raise`] on it.
pub fn update_if_active(
&mut self,
update_factory: impl FnOnce() -> TreeUpdate,
) -> Option<QueuedEvents> {
// SAFETY: We use `RefCell::borrow_mut` here, even though
// `RefCell::get_mut` is allowed (because this method takes
// a mutable self reference), just in case there's some way
// this method can be called from within the subclassed window
// procedure, e.g. via `ActivationHandler`.
let mut state = self.0.state.borrow_mut();
state.adapter.update_if_active(update_factory)
}
}
impl Drop for SubclassingAdapter {
fn drop(&mut self) {
self.0.uninstall();
}
}

View File

@@ -0,0 +1,336 @@
// Copyright 2022 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, ActivationHandler};
use once_cell::sync::Lazy;
use std::{
cell::RefCell,
sync::{Arc, Condvar, Mutex},
thread,
time::Duration,
};
use windows as Windows;
use windows::{
core::*,
Win32::{
Foundation::*,
Graphics::Gdi::ValidateRect,
System::{Com::*, LibraryLoader::GetModuleHandleW},
UI::{Accessibility::*, WindowsAndMessaging::*},
},
};
use crate::window_handle::WindowHandle;
use super::{
context::{ActionHandlerNoMut, ActionHandlerWrapper},
Adapter,
};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
static WINDOW_CLASS_ATOM: Lazy<u16> = Lazy::new(|| {
let class_name = w!("AccessKitTest");
let wc = WNDCLASSW {
hCursor: unsafe { LoadCursorW(None, IDC_ARROW) }.unwrap(),
hInstance: unsafe { GetModuleHandleW(None) }.unwrap().into(),
lpszClassName: class_name,
style: CS_HREDRAW | CS_VREDRAW,
lpfnWndProc: Some(wndproc),
..Default::default()
};
let atom = unsafe { RegisterClassW(&wc) };
if atom == 0 {
panic!("{}", Error::from_win32());
}
atom
});
struct WindowState {
activation_handler: RefCell<Box<dyn ActivationHandler>>,
adapter: RefCell<Adapter>,
}
unsafe fn get_window_state(window: HWND) -> *const WindowState {
GetWindowLongPtrW(window, GWLP_USERDATA) as _
}
fn update_window_focus_state(window: HWND, is_focused: bool) {
let state = unsafe { &*get_window_state(window) };
let mut adapter = state.adapter.borrow_mut();
if let Some(events) = adapter.update_window_focus_state(is_focused) {
events.raise();
}
}
struct WindowCreateParams {
activation_handler: Box<dyn ActivationHandler>,
action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
}
extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
match message {
WM_NCCREATE => {
let create_struct: &CREATESTRUCTW = unsafe { &mut *(lparam.0 as *mut _) };
let create_params: Box<WindowCreateParams> =
unsafe { Box::from_raw(create_struct.lpCreateParams as _) };
let WindowCreateParams {
activation_handler,
action_handler,
} = *create_params;
let adapter = Adapter::with_wrapped_action_handler(window, false, action_handler);
let state = Box::new(WindowState {
activation_handler: RefCell::new(activation_handler),
adapter: RefCell::new(adapter),
});
unsafe { SetWindowLongPtrW(window, GWLP_USERDATA, Box::into_raw(state) as _) };
unsafe { DefWindowProcW(window, message, wparam, lparam) }
}
WM_PAINT => {
unsafe { ValidateRect(window, None) }.unwrap();
LRESULT(0)
}
WM_DESTROY => {
let ptr = unsafe { SetWindowLongPtrW(window, GWLP_USERDATA, 0) };
if ptr != 0 {
drop(unsafe { Box::<WindowState>::from_raw(ptr as _) });
}
unsafe { PostQuitMessage(0) };
LRESULT(0)
}
WM_GETOBJECT => {
let state_ptr = unsafe { get_window_state(window) };
if state_ptr.is_null() {
// We need to be prepared to gracefully handle WM_GETOBJECT
// while the window is being destroyed; this can happen if
// the thread is using a COM STA.
return unsafe { DefWindowProcW(window, message, wparam, lparam) };
}
let state = unsafe { &*state_ptr };
let mut adapter = state.adapter.borrow_mut();
let mut activation_handler = state.activation_handler.borrow_mut();
let result = adapter.handle_wm_getobject(wparam, lparam, &mut **activation_handler);
drop(activation_handler);
drop(adapter);
result.map_or_else(
|| unsafe { DefWindowProcW(window, message, wparam, lparam) },
|result| result.into(),
)
}
WM_SETFOCUS | WM_EXITMENULOOP | WM_EXITSIZEMOVE => {
update_window_focus_state(window, true);
LRESULT(0)
}
WM_KILLFOCUS | WM_ENTERMENULOOP | WM_ENTERSIZEMOVE => {
update_window_focus_state(window, false);
LRESULT(0)
}
_ => unsafe { DefWindowProcW(window, message, wparam, lparam) },
}
}
fn create_window(
title: &str,
activation_handler: impl 'static + ActivationHandler,
action_handler: impl 'static + ActionHandler + Send,
) -> Result<HWND> {
let create_params = Box::new(WindowCreateParams {
activation_handler: Box::new(activation_handler),
action_handler: Arc::new(ActionHandlerWrapper::new(action_handler)),
});
let window = unsafe {
CreateWindowExW(
Default::default(),
PCWSTR(*WINDOW_CLASS_ATOM as usize as _),
&HSTRING::from(title),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
None,
None,
GetModuleHandleW(None).unwrap(),
Some(Box::into_raw(create_params) as _),
)?
};
if window.is_invalid() {
return Err(Error::from_win32());
}
Ok(window)
}
pub(crate) struct Scope {
pub(crate) uia: IUIAutomation,
pub(crate) window: WindowHandle,
}
impl Scope {
pub(crate) fn show_and_focus_window(&self) {
let _ = unsafe { ShowWindow(self.window.0, SW_SHOW) };
let _ = unsafe { SetForegroundWindow(self.window.0) };
}
}
// It's not safe to run these UI-related tests concurrently.
pub(crate) static MUTEX: Mutex<()> = Mutex::new(());
pub(crate) fn scope<F>(
window_title: &str,
activation_handler: impl 'static + ActivationHandler + Send,
action_handler: impl 'static + ActionHandler + Send,
f: F,
) -> Result<()>
where
F: FnOnce(&Scope) -> Result<()>,
{
let _lock_guard = MUTEX.lock().unwrap();
let window_mutex: Mutex<Option<WindowHandle>> = Mutex::new(None);
let window_cv = Condvar::new();
thread::scope(|thread_scope| {
thread_scope.spawn(|| {
// We explicitly don't want to initialize COM on the provider thread,
// because we want to make sure that the provider side of UIA works
// even if COM is never initialized on the provider thread
// (as is the case if the window is never shown), or if COM is
// initialized after the window is shown (as is the case,
// at least on some Windows 10 machines, due to IME support).
let window = create_window(window_title, activation_handler, action_handler).unwrap();
{
let mut state = window_mutex.lock().unwrap();
*state = Some(window.into());
window_cv.notify_one();
}
let mut message = MSG::default();
while unsafe { GetMessageW(&mut message, HWND::default(), 0, 0) }.into() {
let _ = unsafe { TranslateMessage(&message) };
unsafe { DispatchMessageW(&message) };
}
});
let window = {
let state = window_mutex.lock().unwrap();
let mut state = if state.is_none() {
window_cv.wait(state).unwrap()
} else {
state
};
state.take().unwrap()
};
let _window_guard = scopeguard::guard((), |_| {
unsafe { PostMessageW(window.0, WM_CLOSE, WPARAM(0), LPARAM(0)) }.unwrap()
});
// We must initialize COM before creating the UIA client. The MTA option
// is cleaner by far, especially when we want to wait for a UIA event
// handler to be called, and there's no reason not to use it here.
// Note that we don't initialize COM this way on the provider thread,
// as explained above. It's also important that we let the provider
// thread do its forced initialization of UIA, in an environment
// where COM has not been initialized, before we create the UIA client,
// which also triggers UIA initialization, in a thread where COM
// _has_ been initialized. This way, we ensure that the provider side
// of UIA works even if it is set up in an environment where COM
// has not been initialized, and that this sequence of events
// doesn't prevent the UIA client from working.
unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) }.unwrap();
let _com_guard = scopeguard::guard((), |_| unsafe { CoUninitialize() });
let uia: IUIAutomation =
unsafe { CoCreateInstance(&CUIAutomation8, None, CLSCTX_INPROC_SERVER) }?;
let s = Scope { uia, window };
f(&s)
})
}
/// This must only be used to wrap UIA elements returned by a UIA client
/// that was created in the MTA. Those are safe to send between threads.
struct SendableUiaElement(IUIAutomationElement);
unsafe impl Send for SendableUiaElement {}
pub(crate) struct ReceivedFocusEvent {
mutex: Mutex<Option<SendableUiaElement>>,
cv: Condvar,
}
impl ReceivedFocusEvent {
fn new() -> Arc<Self> {
Arc::new(Self {
mutex: Mutex::new(None),
cv: Condvar::new(),
})
}
pub(crate) fn wait<F>(&self, f: F) -> IUIAutomationElement
where
F: Fn(&IUIAutomationElement) -> bool,
{
let mut received = self.mutex.lock().unwrap();
loop {
if let Some(SendableUiaElement(element)) = received.take() {
if f(&element) {
return element;
}
}
let (lock, result) = self.cv.wait_timeout(received, DEFAULT_TIMEOUT).unwrap();
assert!(!result.timed_out());
received = lock;
}
}
fn put(&self, element: IUIAutomationElement) {
let mut received = self.mutex.lock().unwrap();
*received = Some(SendableUiaElement(element));
self.cv.notify_one();
}
}
#[implement(Windows::Win32::UI::Accessibility::IUIAutomationFocusChangedEventHandler)]
pub(crate) struct FocusEventHandler {
received: Arc<ReceivedFocusEvent>,
}
// Because we create a UIA client in the COM MTA, this event handler
// _will_ be called from a different thread, and possibly multiple threads
// at once.
static_assertions::assert_impl_all!(FocusEventHandler: Send, Sync);
impl FocusEventHandler {
#[allow(clippy::new_ret_no_self)] // it does return self, but wrapped
pub(crate) fn new() -> (
IUIAutomationFocusChangedEventHandler,
Arc<ReceivedFocusEvent>,
) {
let received = ReceivedFocusEvent::new();
(
Self {
received: Arc::clone(&received),
}
.into(),
received,
)
}
}
#[allow(non_snake_case)]
impl IUIAutomationFocusChangedEventHandler_Impl for FocusEventHandler_Impl {
fn HandleFocusChangedEvent(&self, sender: Option<&IUIAutomationElement>) -> Result<()> {
self.received.put(sender.unwrap().clone());
Ok(())
}
}
mod simple;
mod subclassed;

View File

@@ -0,0 +1,196 @@
// Copyright 2022 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::{
Action, ActionHandler, ActionRequest, ActivationHandler, Node, NodeId, Role, Tree, TreeUpdate,
};
use windows::{core::*, Win32::UI::Accessibility::*};
use super::*;
const WINDOW_TITLE: &str = "Simple test";
const WINDOW_ID: NodeId = NodeId(0);
const BUTTON_1_ID: NodeId = NodeId(1);
const BUTTON_2_ID: NodeId = NodeId(2);
fn make_button(label: &str) -> Node {
let mut node = Node::new(Role::Button);
node.set_label(label);
node.add_action(Action::Focus);
node
}
fn get_initial_state() -> TreeUpdate {
let mut root = Node::new(Role::Window);
root.set_children(vec![BUTTON_1_ID, BUTTON_2_ID]);
let button_1 = make_button("Button 1");
let button_2 = make_button("Button 2");
TreeUpdate {
nodes: vec![
(WINDOW_ID, root),
(BUTTON_1_ID, button_1),
(BUTTON_2_ID, button_2),
],
tree: Some(Tree::new(WINDOW_ID)),
focus: BUTTON_1_ID,
}
}
pub struct NullActionHandler;
impl ActionHandler for NullActionHandler {
fn do_action(&mut self, _request: ActionRequest) {}
}
struct SimpleActivationHandler;
impl ActivationHandler for SimpleActivationHandler {
fn request_initial_tree(&mut self) -> Option<TreeUpdate> {
Some(get_initial_state())
}
}
fn scope<F>(f: F) -> Result<()>
where
F: FnOnce(&Scope) -> Result<()>,
{
super::scope(
WINDOW_TITLE,
SimpleActivationHandler {},
NullActionHandler {},
f,
)
}
#[test]
fn has_native_uia() -> Result<()> {
scope(|s| {
let has_native_uia: bool = unsafe { UiaHasServerSideProvider(s.window.0) }.into();
assert!(has_native_uia);
Ok(())
})
}
fn is_button_named(element: &IUIAutomationElement, expected_name: &str) -> bool {
let control_type = unsafe { element.CurrentControlType() }.unwrap();
let name = unsafe { element.CurrentName() }.unwrap();
let name: String = name.try_into().unwrap();
control_type == UIA_ButtonControlTypeId && name == expected_name
}
fn is_button_1(element: &IUIAutomationElement) -> bool {
is_button_named(element, "Button 1")
}
fn is_button_2(element: &IUIAutomationElement) -> bool {
is_button_named(element, "Button 2")
}
#[test]
fn navigation() -> Result<()> {
scope(|s| {
let root = unsafe { s.uia.ElementFromHandle(s.window.0) }?;
let walker = unsafe { s.uia.ControlViewWalker() }?;
// The children of the window include the children that we provide,
// but also the title bar provided by the OS. We know that our own
// children are in the order we specified, but we don't know
// their position relative to the title bar. In fact, Windows
// has changed this in the past.
//
// Note that a UIA client would normally use the UIA condition feature
// to traverse the tree looking for an element that meets
// some condition. But we want to be explicit about navigating
// forward (and backward below) through only the immediate children.
// We'll accept the performance hit of multiple cross-thread calls
// (insignificant in this case) to achieve that.
let mut button_1_forward: Option<IUIAutomationElement> = None;
let mut wrapped_child = unsafe { walker.GetFirstChildElement(&root) };
while let Ok(child) = wrapped_child {
if is_button_1(&child) {
button_1_forward = Some(child);
break;
}
wrapped_child = unsafe { walker.GetNextSiblingElement(&child) };
}
let button_1_forward = button_1_forward.unwrap();
let mut button_2_forward: Option<IUIAutomationElement> = None;
let wrapped_child = unsafe { walker.GetNextSiblingElement(&button_1_forward) };
if let Ok(child) = wrapped_child {
if is_button_2(&child) {
button_2_forward = Some(child);
}
}
let _button_2_forward = button_2_forward.unwrap();
let mut button_2_backward: Option<IUIAutomationElement> = None;
let mut wrapped_child = unsafe { walker.GetLastChildElement(&root) };
while let Ok(child) = wrapped_child {
if is_button_2(&child) {
button_2_backward = Some(child);
break;
}
wrapped_child = unsafe { walker.GetPreviousSiblingElement(&child) };
}
let button_2_backward = button_2_backward.unwrap();
let mut button_1_backward: Option<IUIAutomationElement> = None;
let wrapped_child = unsafe { walker.GetPreviousSiblingElement(&button_2_backward) };
if let Ok(child) = wrapped_child {
if is_button_1(&child) {
button_1_backward = Some(child);
}
}
let button_1_backward = button_1_backward.unwrap();
let equal: bool =
unsafe { s.uia.CompareElements(&button_1_forward, &button_1_backward) }?.into();
assert!(equal);
let parent = unsafe { walker.GetParentElement(&button_1_forward) }?;
let equal: bool = unsafe { s.uia.CompareElements(&parent, &root) }?.into();
assert!(equal);
let desktop_root = unsafe { s.uia.GetRootElement() }?;
let parent = unsafe { walker.GetParentElement(&root) }?;
let equal: bool = unsafe { s.uia.CompareElements(&parent, &desktop_root) }?.into();
assert!(equal);
let wrapped_child = unsafe { walker.GetFirstChildElement(&button_1_forward) };
assert_eq!(Err(Error::empty()), wrapped_child);
let wrapped_child = unsafe { walker.GetLastChildElement(&button_1_forward) };
assert_eq!(Err(Error::empty()), wrapped_child);
Ok(())
})
}
#[test]
fn focus() -> Result<()> {
scope(|s| {
let (focus_event_handler, received_focus_event) = FocusEventHandler::new();
unsafe {
s.uia
.AddFocusChangedEventHandler(None, &focus_event_handler)
}?;
s.show_and_focus_window();
let focus_from_event = received_focus_event.wait(is_button_1);
let has_focus: bool = unsafe { focus_from_event.CurrentHasKeyboardFocus() }?.into();
assert!(has_focus);
let is_focusable: bool = unsafe { focus_from_event.CurrentIsKeyboardFocusable() }?.into();
assert!(is_focusable);
let focus_on_demand = unsafe { s.uia.GetFocusedElement() }?;
let equal: bool =
unsafe { s.uia.CompareElements(&focus_from_event, &focus_on_demand) }?.into();
assert!(equal);
Ok(())
})
}

View File

@@ -0,0 +1,103 @@
// Copyright 2022 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::{
Action, ActionHandler, ActionRequest, ActivationHandler, Node, NodeId, Role, Tree, TreeUpdate,
};
use windows::Win32::{Foundation::*, UI::Accessibility::*};
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event_loop::{ActiveEventLoop, EventLoop},
platform::windows::EventLoopBuilderExtWindows,
raw_window_handle::{HasWindowHandle, RawWindowHandle},
window::{Window, WindowId},
};
use super::MUTEX;
use crate::SubclassingAdapter;
const WINDOW_TITLE: &str = "Simple test";
const WINDOW_ID: NodeId = NodeId(0);
const BUTTON_1_ID: NodeId = NodeId(1);
const BUTTON_2_ID: NodeId = NodeId(2);
fn make_button(label: &str) -> Node {
let mut node = Node::new(Role::Button);
node.set_label(label);
node.add_action(Action::Focus);
node
}
fn get_initial_state() -> TreeUpdate {
let mut root = Node::new(Role::Window);
root.set_children(vec![BUTTON_1_ID, BUTTON_2_ID]);
let button_1 = make_button("Button 1");
let button_2 = make_button("Button 2");
TreeUpdate {
nodes: vec![
(WINDOW_ID, root),
(BUTTON_1_ID, button_1),
(BUTTON_2_ID, button_2),
],
tree: Some(Tree::new(WINDOW_ID)),
focus: BUTTON_1_ID,
}
}
pub struct NullActionHandler;
impl ActionHandler for NullActionHandler {
fn do_action(&mut self, _request: ActionRequest) {}
}
struct SimpleActivationHandler;
impl ActivationHandler for SimpleActivationHandler {
fn request_initial_tree(&mut self) -> Option<TreeUpdate> {
Some(get_initial_state())
}
}
// This module uses winit for the purpose of testing with a real third-party
// window implementation that we don't control.
struct TestApplication;
impl ApplicationHandler<()> for TestApplication {
fn window_event(&mut self, _: &ActiveEventLoop, _: WindowId, _: WindowEvent) {}
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let window_attributes = Window::default_attributes()
.with_title(WINDOW_TITLE)
.with_visible(false);
let window = event_loop.create_window(window_attributes).unwrap();
let hwnd = match window.window_handle().unwrap().as_raw() {
RawWindowHandle::Win32(handle) => HWND(handle.hwnd.get() as *mut core::ffi::c_void),
RawWindowHandle::WinRt(_) => unimplemented!(),
_ => unreachable!(),
};
let adapter =
SubclassingAdapter::new(hwnd, SimpleActivationHandler {}, NullActionHandler {});
assert!(unsafe { UiaHasServerSideProvider(hwnd) }.as_bool());
drop(window);
drop(adapter);
event_loop.exit();
}
}
#[test]
fn has_native_uia() {
// This test is simple enough that we know it's fine to run entirely
// on one thread, so we don't need a full multithreaded test harness.
// Still, we must prevent this test from running concurrently with other
// tests, especially the focus test.
let _lock_guard = MUTEX.lock().unwrap();
let event_loop = EventLoop::builder().with_any_thread(true).build().unwrap();
let mut state = TestApplication {};
event_loop.run_app(&mut state).unwrap();
}

601
vendor/accesskit_windows/src/text.rs vendored Normal file
View File

@@ -0,0 +1,601 @@
// Copyright 2022 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.
#![allow(non_upper_case_globals)]
use accesskit::{Action, ActionData, ActionRequest};
use accesskit_consumer::{
Node, TextPosition as Position, TextRange as Range, TreeState, WeakTextRange as WeakRange,
};
use std::sync::{Arc, RwLock, Weak};
use windows::{
core::*,
Win32::{Foundation::*, System::Com::*, UI::Accessibility::*},
};
use crate::{context::Context, node::PlatformNode, util::*};
fn upgrade_range<'a>(weak: &WeakRange, tree_state: &'a TreeState) -> Result<Range<'a>> {
if let Some(range) = weak.upgrade(tree_state) {
Ok(range)
} else {
Err(element_not_available())
}
}
fn upgrade_range_node<'a>(weak: &WeakRange, tree_state: &'a TreeState) -> Result<Node<'a>> {
if let Some(node) = weak.upgrade_node(tree_state) {
Ok(node)
} else {
Err(element_not_available())
}
}
fn weak_comparable_position_from_endpoint(
range: &WeakRange,
endpoint: TextPatternRangeEndpoint,
) -> Result<&(Vec<usize>, usize)> {
match endpoint {
TextPatternRangeEndpoint_Start => Ok(range.start_comparable()),
TextPatternRangeEndpoint_End => Ok(range.end_comparable()),
_ => Err(invalid_arg()),
}
}
fn position_from_endpoint<'a>(
range: &Range<'a>,
endpoint: TextPatternRangeEndpoint,
) -> Result<Position<'a>> {
match endpoint {
TextPatternRangeEndpoint_Start => Ok(range.start()),
TextPatternRangeEndpoint_End => Ok(range.end()),
_ => Err(invalid_arg()),
}
}
fn set_endpoint_position<'a>(
range: &mut Range<'a>,
endpoint: TextPatternRangeEndpoint,
pos: Position<'a>,
) -> Result<()> {
match endpoint {
TextPatternRangeEndpoint_Start => {
range.set_start(pos);
}
TextPatternRangeEndpoint_End => {
range.set_end(pos);
}
_ => {
return Err(invalid_arg());
}
}
Ok(())
}
fn back_to_unit_start(start: Position, unit: TextUnit) -> Result<Position> {
match unit {
TextUnit_Character => {
// If we get here, this position is at the start of a non-degenerate
// range, so it's always at the start of a character.
debug_assert!(!start.is_document_end());
Ok(start)
}
TextUnit_Format => {
if start.is_format_start() {
Ok(start)
} else {
Ok(start.backward_to_format_start())
}
}
TextUnit_Word => {
if start.is_word_start() {
Ok(start)
} else {
Ok(start.backward_to_word_start())
}
}
TextUnit_Line => {
if start.is_line_start() {
Ok(start)
} else {
Ok(start.backward_to_line_start())
}
}
TextUnit_Paragraph => {
if start.is_paragraph_start() {
Ok(start)
} else {
Ok(start.backward_to_paragraph_start())
}
}
TextUnit_Page => {
if start.is_page_start() {
Ok(start)
} else {
Ok(start.backward_to_page_start())
}
}
TextUnit_Document => {
if start.is_document_start() {
Ok(start)
} else {
Ok(start.document_start())
}
}
_ => Err(invalid_arg()),
}
}
fn move_forward_to_start(pos: Position, unit: TextUnit) -> Result<Position> {
match unit {
TextUnit_Character => Ok(pos.forward_to_character_start()),
TextUnit_Format => Ok(pos.forward_to_format_start()),
TextUnit_Word => Ok(pos.forward_to_word_start()),
TextUnit_Line => Ok(pos.forward_to_line_start()),
TextUnit_Paragraph => Ok(pos.forward_to_paragraph_start()),
TextUnit_Page => Ok(pos.forward_to_page_start()),
TextUnit_Document => Ok(pos.document_end()),
_ => Err(invalid_arg()),
}
}
fn move_forward_to_end(pos: Position, unit: TextUnit) -> Result<Position> {
match unit {
TextUnit_Character => Ok(pos.forward_to_character_end()),
TextUnit_Format => Ok(pos.forward_to_format_end()),
TextUnit_Word => Ok(pos.forward_to_word_end()),
TextUnit_Line => Ok(pos.forward_to_line_end()),
TextUnit_Paragraph => Ok(pos.forward_to_paragraph_end()),
TextUnit_Page => Ok(pos.forward_to_page_end()),
TextUnit_Document => Ok(pos.document_end()),
_ => Err(invalid_arg()),
}
}
fn move_backward(pos: Position, unit: TextUnit) -> Result<Position> {
match unit {
TextUnit_Character => Ok(pos.backward_to_character_start()),
TextUnit_Format => Ok(pos.backward_to_format_start()),
TextUnit_Word => Ok(pos.backward_to_word_start()),
TextUnit_Line => Ok(pos.backward_to_line_start()),
TextUnit_Paragraph => Ok(pos.backward_to_paragraph_start()),
TextUnit_Page => Ok(pos.backward_to_page_start()),
TextUnit_Document => Ok(pos.document_start()),
_ => Err(invalid_arg()),
}
}
fn move_position(
mut pos: Position,
unit: TextUnit,
to_end: bool,
count: i32,
) -> Result<(Position, i32)> {
let forward = count > 0;
let count = count.abs();
let mut moved = 0i32;
for _ in 0..count {
let at_end = if forward {
pos.is_document_end()
} else {
pos.is_document_start()
};
if at_end {
break;
}
pos = if forward {
if to_end {
move_forward_to_end(pos, unit)
} else {
move_forward_to_start(pos, unit)
}
} else {
move_backward(pos, unit)
}?;
moved += 1;
}
if !forward {
moved = -moved;
}
Ok((pos, moved))
}
#[implement(ITextRangeProvider)]
pub(crate) struct PlatformRange {
context: Weak<Context>,
state: RwLock<WeakRange>,
}
impl PlatformRange {
pub(crate) fn new(context: &Weak<Context>, range: Range) -> Self {
Self {
context: context.clone(),
state: RwLock::new(range.downgrade()),
}
}
fn upgrade_context(&self) -> Result<Arc<Context>> {
upgrade(&self.context)
}
fn with_tree_state_and_context<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(&TreeState, &Context) -> Result<T>,
{
let context = self.upgrade_context()?;
let tree = context.read_tree();
f(tree.state(), &context)
}
fn with_tree_state<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(&TreeState) -> Result<T>,
{
self.with_tree_state_and_context(|state, _| f(state))
}
fn upgrade_node<'a>(&self, tree_state: &'a TreeState) -> Result<Node<'a>> {
let state = self.state.read().unwrap();
upgrade_range_node(&state, tree_state)
}
fn with_node<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(Node) -> Result<T>,
{
self.with_tree_state(|tree_state| {
let node = self.upgrade_node(tree_state)?;
f(node)
})
}
fn upgrade_for_read<'a>(&self, tree_state: &'a TreeState) -> Result<Range<'a>> {
let state = self.state.read().unwrap();
upgrade_range(&state, tree_state)
}
fn read_with_context<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(Range, &Context) -> Result<T>,
{
self.with_tree_state_and_context(|tree_state, context| {
let range = self.upgrade_for_read(tree_state)?;
f(range, context)
})
}
fn read<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(Range) -> Result<T>,
{
self.read_with_context(|range, _| f(range))
}
fn write<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(&mut Range) -> Result<T>,
{
self.with_tree_state(|tree_state| {
let mut state = self.state.write().unwrap();
let mut range = upgrade_range(&state, tree_state)?;
let result = f(&mut range);
*state = range.downgrade();
result
})
}
fn do_action<F>(&self, f: F) -> Result<()>
where
for<'a> F: FnOnce(Range<'a>) -> ActionRequest,
{
let context = self.upgrade_context()?;
let tree = context.read_tree();
let range = self.upgrade_for_read(tree.state())?;
let request = f(range);
drop(tree);
context.do_action(request);
Ok(())
}
fn require_same_context(&self, other: &PlatformRange) -> Result<()> {
if self.context.ptr_eq(&other.context) {
Ok(())
} else {
Err(invalid_arg())
}
}
}
impl Clone for PlatformRange {
fn clone(&self) -> Self {
PlatformRange {
context: self.context.clone(),
state: RwLock::new(self.state.read().unwrap().clone()),
}
}
}
// Some text range methods take another text range interface pointer as a
// parameter. We need to cast these interface pointers to their underlying
// implementations. We assume that AccessKit is the only UIA provider
// within this process. This seems a safe assumption for most AccessKit users.
#[allow(non_snake_case)]
impl ITextRangeProvider_Impl for PlatformRange_Impl {
fn Clone(&self) -> Result<ITextRangeProvider> {
Ok(self.this.clone().into())
}
fn Compare(&self, other: Option<&ITextRangeProvider>) -> Result<BOOL> {
let other = unsafe { required_param(other)?.as_impl() };
Ok((self.context.ptr_eq(&other.context)
&& *self.state.read().unwrap() == *other.state.read().unwrap())
.into())
}
fn CompareEndpoints(
&self,
endpoint: TextPatternRangeEndpoint,
other: Option<&ITextRangeProvider>,
other_endpoint: TextPatternRangeEndpoint,
) -> Result<i32> {
let other = unsafe { required_param(other)?.as_impl() };
if std::ptr::eq(other as *const _, &self.this as *const _) {
// Comparing endpoints within the same range can be done
// safely without upgrading the range. This allows ATs
// to determine whether an old range is degenerate even if
// that range is no longer valid.
let state = self.state.read().unwrap();
let other_state = other.state.read().unwrap();
let pos = weak_comparable_position_from_endpoint(&state, endpoint)?;
let other_pos = weak_comparable_position_from_endpoint(&other_state, other_endpoint)?;
let result = pos.cmp(other_pos);
return Ok(result as i32);
}
self.require_same_context(other)?;
self.with_tree_state(|tree_state| {
let range = self.upgrade_for_read(tree_state)?;
let other_range = other.upgrade_for_read(tree_state)?;
if range.node().id() != other_range.node().id() {
return Err(invalid_arg());
}
let pos = position_from_endpoint(&range, endpoint)?;
let other_pos = position_from_endpoint(&other_range, other_endpoint)?;
let result = pos.partial_cmp(&other_pos).unwrap();
Ok(result as i32)
})
}
fn ExpandToEnclosingUnit(&self, unit: TextUnit) -> Result<()> {
if unit == TextUnit_Document {
// Handle document as a special case so we can get to a document
// range even if the current endpoints are now invalid.
// Based on observed behavior, Narrator needs this ability.
return self.with_tree_state(|tree_state| {
let mut state = self.state.write().unwrap();
let node = upgrade_range_node(&state, tree_state)?;
*state = node.document_range().downgrade();
Ok(())
});
}
self.write(|range| {
let start = range.start();
if unit == TextUnit_Character && start.is_document_end() {
// We know from experimentation that some Windows ATs
// expect ExpandToEnclosingUnit(TextUnit_Character)
// to do nothing if the range is degenerate at the end
// of the document.
return Ok(());
}
let start = back_to_unit_start(start, unit)?;
range.set_start(start);
if !start.is_document_end() {
let end = move_forward_to_end(start, unit)?;
range.set_end(end);
}
Ok(())
})
}
fn FindAttribute(
&self,
_id: UIA_TEXTATTRIBUTE_ID,
_value: &VARIANT,
_backward: BOOL,
) -> Result<ITextRangeProvider> {
// TODO: implement when we support variable formatting (part of rich text)
// Justification: JUCE doesn't implement this.
Err(Error::empty())
}
fn FindText(
&self,
_text: &BSTR,
_backward: BOOL,
_ignore_case: BOOL,
) -> Result<ITextRangeProvider> {
// TODO: implement when there's a real-world use case that requires it
// Justification: Quorum doesn't implement this and is being used
// by blind students.
Err(Error::empty())
}
fn GetAttributeValue(&self, id: UIA_TEXTATTRIBUTE_ID) -> Result<VARIANT> {
match id {
UIA_IsReadOnlyAttributeId => {
// TBD: do we ever want to support mixed read-only/editable text?
self.with_node(|node| {
let value = node.is_read_only();
Ok(value.into())
})
}
UIA_CaretPositionAttributeId => self.read(|range| {
let mut value = CaretPosition_Unknown;
if range.is_degenerate() {
let pos = range.start();
if pos.is_line_start() {
value = CaretPosition_BeginningOfLine;
} else if pos.is_line_end() {
value = CaretPosition_EndOfLine;
}
}
Ok(value.0.into())
}),
// TODO: implement more attributes
_ => {
let value = unsafe { UiaGetReservedNotSupportedValue() }.unwrap();
Ok(value.into())
}
}
}
fn GetBoundingRectangles(&self) -> Result<*mut SAFEARRAY> {
self.read_with_context(|range, context| {
let rects = range.bounding_boxes();
if rects.is_empty() {
return Ok(std::ptr::null_mut());
}
let client_top_left = context.client_top_left();
let mut result = Vec::<f64>::with_capacity(rects.len() * 4);
for rect in rects {
result.push(rect.x0 + client_top_left.x);
result.push(rect.y0 + client_top_left.y);
result.push(rect.width());
result.push(rect.height());
}
Ok(safe_array_from_f64_slice(&result))
})
}
fn GetEnclosingElement(&self) -> Result<IRawElementProviderSimple> {
self.with_node(|node| {
// Revisit this if we eventually support embedded objects.
Ok(PlatformNode {
context: self.context.clone(),
node_id: Some(node.id()),
}
.into())
})
}
fn GetText(&self, _max_length: i32) -> Result<BSTR> {
// The Microsoft docs imply that the provider isn't _required_
// to truncate text at the max length, so we just ignore it.
self.read(|range| {
let mut result = WideString::default();
range.write_text(&mut result).unwrap();
Ok(result.into())
})
}
fn Move(&self, unit: TextUnit, count: i32) -> Result<i32> {
self.write(|range| {
let degenerate = range.is_degenerate();
let start = range.start();
let start = if degenerate {
start
} else {
back_to_unit_start(start, unit)?
};
let (start, moved) = move_position(start, unit, false, count)?;
if moved != 0 {
range.set_start(start);
let end = if degenerate || start.is_document_end() {
start
} else {
move_forward_to_end(start, unit)?
};
range.set_end(end);
}
Ok(moved)
})
}
fn MoveEndpointByUnit(
&self,
endpoint: TextPatternRangeEndpoint,
unit: TextUnit,
count: i32,
) -> Result<i32> {
self.write(|range| {
let pos = position_from_endpoint(range, endpoint)?;
let (pos, moved) =
move_position(pos, unit, endpoint == TextPatternRangeEndpoint_End, count)?;
set_endpoint_position(range, endpoint, pos)?;
Ok(moved)
})
}
fn MoveEndpointByRange(
&self,
endpoint: TextPatternRangeEndpoint,
other: Option<&ITextRangeProvider>,
other_endpoint: TextPatternRangeEndpoint,
) -> Result<()> {
let other = unsafe { required_param(other)?.as_impl() };
self.require_same_context(other)?;
// We have to obtain the tree state and ranges manually to avoid
// lifetime issues, and work with the two locks in a specific order
// to avoid deadlock.
self.with_tree_state(|tree_state| {
let other_range = other.upgrade_for_read(tree_state)?;
let mut state = self.state.write().unwrap();
let mut range = upgrade_range(&state, tree_state)?;
if range.node().id() != other_range.node().id() {
return Err(invalid_arg());
}
let pos = position_from_endpoint(&other_range, other_endpoint)?;
set_endpoint_position(&mut range, endpoint, pos)?;
*state = range.downgrade();
Ok(())
})
}
fn Select(&self) -> Result<()> {
self.do_action(|range| ActionRequest {
action: Action::SetTextSelection,
target: range.node().id(),
data: Some(ActionData::SetTextSelection(range.to_text_selection())),
})
}
fn AddToSelection(&self) -> Result<()> {
// AccessKit doesn't support multiple text selections.
Err(invalid_operation())
}
fn RemoveFromSelection(&self) -> Result<()> {
// AccessKit doesn't support multiple text selections.
Err(invalid_operation())
}
fn ScrollIntoView(&self, align_to_top: BOOL) -> Result<()> {
self.do_action(|range| {
let position = if align_to_top.into() {
range.start()
} else {
range.end()
};
ActionRequest {
action: Action::ScrollIntoView,
target: position.inner_node().id(),
data: None,
}
})
}
fn GetChildren(&self) -> Result<*mut SAFEARRAY> {
// We don't support embedded objects in text.
Ok(safe_array_from_com_slice(&[]))
}
}
// Ensures that `PlatformRange` is actually safe to use in the free-threaded
// manner that we advertise via `ProviderOptions`.
#[test]
fn platform_range_impl_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<PlatformRange>();
}

272
vendor/accesskit_windows/src/util.rs vendored Normal file
View File

@@ -0,0 +1,272 @@
// Copyright 2022 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::Point;
use accesskit_consumer::TreeState;
use std::{
fmt::{self, Write},
sync::{Arc, Weak},
};
use windows::{
core::*,
Win32::{
Foundation::*,
Graphics::Gdi::*,
System::{Com::*, Ole::*, Variant::*},
UI::{Accessibility::*, WindowsAndMessaging::*},
},
};
use crate::window_handle::WindowHandle;
#[derive(Clone, Default, PartialEq, Eq)]
pub(crate) struct WideString(Vec<u16>);
impl Write for WideString {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.0.extend(s.encode_utf16());
Ok(())
}
fn write_char(&mut self, c: char) -> fmt::Result {
self.0.extend_from_slice(c.encode_utf16(&mut [0; 2]));
Ok(())
}
}
impl From<WideString> for BSTR {
fn from(value: WideString) -> Self {
Self::from_wide(&value.0).unwrap()
}
}
pub(crate) struct Variant(VARIANT);
impl From<Variant> for VARIANT {
fn from(variant: Variant) -> Self {
variant.0
}
}
impl Variant {
pub(crate) fn empty() -> Self {
Self(VARIANT::default())
}
pub(crate) fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<BSTR> for Variant {
fn from(value: BSTR) -> Self {
Self(value.into())
}
}
impl From<WideString> for Variant {
fn from(value: WideString) -> Self {
BSTR::from(value).into()
}
}
impl From<&str> for Variant {
fn from(value: &str) -> Self {
let mut result = WideString::default();
result.write_str(value).unwrap();
result.into()
}
}
impl From<String> for Variant {
fn from(value: String) -> Self {
value.as_str().into()
}
}
impl From<IUnknown> for Variant {
fn from(value: IUnknown) -> Self {
Self(value.into())
}
}
impl From<i32> for Variant {
fn from(value: i32) -> Self {
Self(value.into())
}
}
impl From<f64> for Variant {
fn from(value: f64) -> Self {
Self(value.into())
}
}
impl From<ToggleState> for Variant {
fn from(value: ToggleState) -> Self {
Self(value.0.into())
}
}
impl From<LiveSetting> for Variant {
fn from(value: LiveSetting) -> Self {
Self(value.0.into())
}
}
impl From<CaretPosition> for Variant {
fn from(value: CaretPosition) -> Self {
Self(value.0.into())
}
}
impl From<UIA_CONTROLTYPE_ID> for Variant {
fn from(value: UIA_CONTROLTYPE_ID) -> Self {
Self(value.0.into())
}
}
impl From<OrientationType> for Variant {
fn from(value: OrientationType) -> Self {
Self(value.0.into())
}
}
impl From<bool> for Variant {
fn from(value: bool) -> Self {
Self(value.into())
}
}
impl<T: Into<Variant>> From<Option<T>> for Variant {
fn from(value: Option<T>) -> Self {
value.map_or_else(Self::empty, T::into)
}
}
fn safe_array_from_primitive_slice<T>(vt: VARENUM, slice: &[T]) -> *mut SAFEARRAY {
let sa = unsafe { SafeArrayCreateVector(VARENUM(vt.0), 0, slice.len().try_into().unwrap()) };
if sa.is_null() {
panic!("SAFEARRAY allocation failed");
}
for (i, item) in slice.iter().enumerate() {
let i: i32 = i.try_into().unwrap();
unsafe { SafeArrayPutElement(&*sa, &i, (item as *const T) as *const _) }.unwrap();
}
sa
}
pub(crate) fn safe_array_from_i32_slice(slice: &[i32]) -> *mut SAFEARRAY {
safe_array_from_primitive_slice(VT_I4, slice)
}
pub(crate) fn safe_array_from_f64_slice(slice: &[f64]) -> *mut SAFEARRAY {
safe_array_from_primitive_slice(VT_R8, slice)
}
pub(crate) fn safe_array_from_com_slice(slice: &[IUnknown]) -> *mut SAFEARRAY {
let sa = unsafe { SafeArrayCreateVector(VT_UNKNOWN, 0, slice.len().try_into().unwrap()) };
if sa.is_null() {
panic!("SAFEARRAY allocation failed");
}
for (i, item) in slice.iter().enumerate() {
let i: i32 = i.try_into().unwrap();
unsafe { SafeArrayPutElement(&*sa, &i, std::mem::transmute_copy(item)) }.unwrap();
}
sa
}
pub(crate) enum QueuedEvent {
Simple {
element: IRawElementProviderSimple,
event_id: UIA_EVENT_ID,
},
PropertyChanged {
element: IRawElementProviderSimple,
property_id: UIA_PROPERTY_ID,
old_value: VARIANT,
new_value: VARIANT,
},
}
pub(crate) fn not_implemented() -> Error {
E_NOTIMPL.into()
}
pub(crate) fn invalid_arg() -> Error {
E_INVALIDARG.into()
}
pub(crate) fn required_param<T>(param: Option<&T>) -> Result<&T> {
param.map_or_else(|| Err(invalid_arg()), Ok)
}
pub(crate) fn element_not_available() -> Error {
HRESULT(UIA_E_ELEMENTNOTAVAILABLE as _).into()
}
pub(crate) fn element_not_enabled() -> Error {
HRESULT(UIA_E_ELEMENTNOTENABLED as _).into()
}
pub(crate) fn invalid_operation() -> Error {
HRESULT(UIA_E_INVALIDOPERATION as _).into()
}
pub(crate) fn client_top_left(hwnd: WindowHandle) -> Point {
let mut result = POINT::default();
// If ClientToScreen fails, that means the window is gone.
// That's an unexpected condition, so we should fail loudly.
unsafe { ClientToScreen(hwnd.0, &mut result) }.unwrap();
Point::new(result.x.into(), result.y.into())
}
pub(crate) fn window_title(hwnd: WindowHandle) -> Option<BSTR> {
// The following is an old hack to get the window caption without ever
// sending messages to the window itself, even if the window is in
// the same process but possibly a separate thread. This prevents
// possible hangs and sluggishness. This hack has been proven to work
// over nearly 20 years on every version of Windows back to XP.
let result = unsafe { DefWindowProcW(hwnd.0, WM_GETTEXTLENGTH, WPARAM(0), LPARAM(0)) };
if result.0 <= 0 {
return None;
}
let capacity = (result.0 as usize) + 1; // make room for the null
let mut buffer = Vec::<u16>::with_capacity(capacity);
let result = unsafe {
DefWindowProcW(
hwnd.0,
WM_GETTEXT,
WPARAM(capacity),
LPARAM(buffer.as_mut_ptr() as _),
)
};
if result.0 <= 0 {
return None;
}
let len = result.0 as usize;
unsafe { buffer.set_len(len) };
Some(BSTR::from_wide(&buffer).unwrap())
}
pub(crate) fn toolkit_description(state: &TreeState) -> Option<WideString> {
state.toolkit_name().map(|name| {
let mut result = WideString::default();
result.write_str(name).unwrap();
if let Some(version) = state.toolkit_version() {
result.write_char(' ').unwrap();
result.write_str(version).unwrap();
}
result
})
}
pub(crate) fn upgrade<T>(weak: &Weak<T>) -> Result<Arc<T>> {
if let Some(strong) = weak.upgrade() {
Ok(strong)
} else {
Err(element_not_available())
}
}

View File

@@ -0,0 +1,32 @@
// Copyright 2024 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 windows::Win32::Foundation::HWND;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct WindowHandle(pub HWND);
unsafe impl Send for WindowHandle {}
unsafe impl Sync for WindowHandle {}
impl From<HWND> for WindowHandle {
fn from(value: HWND) -> Self {
Self(value)
}
}
impl From<WindowHandle> for HWND {
fn from(value: WindowHandle) -> Self {
value.0
}
}
#[cfg(test)]
mod tests {
use super::*;
static_assertions::assert_impl_all!(WindowHandle: Send, Sync);
}