win_wrap/uia/pattern/
legacy.rs

1/*
2 * Copyright (c) 2024. The RigelA open source project team and
3 * its contributors reserve all rights.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and limitations under the License.
12 */
13
14use crate::{
15    msaa::object::AccessibleObject,
16    uia::pattern::{PatternCreator, PatternError},
17};
18use std::fmt::{Debug, Formatter};
19use windows::{
20    core::{Result, BSTR},
21    Win32::UI::Accessibility::{
22        IUIAutomationLegacyIAccessiblePattern, UIA_LegacyIAccessiblePatternId, UIA_PATTERN_ID,
23    },
24};
25
26/**
27公开使 Microsoft UI 自动化客户端能够从 Microsoft Active Accessibility (MSAA) 服务器检索 UI 信息的方法和属性。
28此接口的获取方式与任何其他控制模式一样。它使 UI 自动化客户端能够利用缓存系统更有效地收集 MSAA 属性,还使 UI 自动化客户端能够与支持 IAccessible 接口的本机 Microsoft Active Accessibility 服务器进行交互。
29*/
30pub struct UiAutomationIAccessiblePattern(IUIAutomationLegacyIAccessiblePattern);
31
32impl TryFrom<IUIAutomationLegacyIAccessiblePattern> for UiAutomationIAccessiblePattern {
33    type Error = PatternError;
34
35    fn try_from(
36        value: IUIAutomationLegacyIAccessiblePattern,
37    ) -> std::result::Result<Self, Self::Error> {
38        Ok(Self(value))
39    }
40}
41
42impl PatternCreator<IUIAutomationLegacyIAccessiblePattern> for UiAutomationIAccessiblePattern {
43    const PATTERN: UIA_PATTERN_ID = UIA_LegacyIAccessiblePatternId;
44}
45
46/// <https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationlegacyiaccessiblepattern>
47impl UiAutomationIAccessiblePattern {
48    /**
49    获取元素名称。
50    */
51    pub fn get_name(&self) -> String {
52        unsafe { self.0.CurrentName() }
53            .unwrap_or(BSTR::default())
54            .to_string()
55    }
56
57    /**
58    获取元素描述。
59    */
60    pub fn get_description(&self) -> String {
61        unsafe { self.0.CurrentDescription() }
62            .unwrap_or(BSTR::default())
63            .to_string()
64    }
65
66    /**
67    获取元素帮助。
68    */
69    pub fn get_help(&self) -> String {
70        unsafe { self.0.CurrentHelp() }
71            .unwrap_or(BSTR::default())
72            .to_string()
73    }
74
75    /**
76    获取元素状态。
77    */
78    pub fn get_state(&self) -> u32 {
79        unsafe { self.0.CurrentState() }.unwrap_or(0)
80    }
81
82    /**
83    获取元素的角色。
84    */
85    pub fn get_role(&self) -> u32 {
86        unsafe { self.0.CurrentRole() }.unwrap_or(0)
87    }
88
89    /**
90    获取对应的MSAA对象。
91    */
92    pub fn get_object(&self) -> Result<AccessibleObject> {
93        match unsafe { self.0.GetIAccessible() } {
94            Ok(o) => Ok(AccessibleObject::from_raw(
95                o,
96                unsafe { self.0.CurrentChildId() }.unwrap_or(0),
97            )),
98            Err(e) => Err(e),
99        }
100    }
101}
102
103impl Debug for UiAutomationIAccessiblePattern {
104    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
105        write!(
106            f,
107            "UiAutomationIAccessiblePattern(name:{}, description:{}, role:{})",
108            self.get_name(),
109            self.get_description(),
110            self.get_role()
111        )
112    }
113}