win_wrap/uia/
pattern.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
14pub mod container;
15pub mod legacy;
16pub mod range;
17pub mod table;
18pub mod text;
19pub mod toggle;
20pub mod value;
21
22use crate::uia::element::UiAutomationElement;
23use std::{fmt::Formatter, sync::Weak};
24use windows::Win32::UI::Accessibility::{IUIAutomation6, UIA_PATTERN_ID};
25use windows_core::Interface;
26
27/// 模式错误结构
28pub struct PatternError {
29    /// 错误信息
30    message: String,
31}
32
33impl From<String> for PatternError {
34    /// 从字符串创建
35    fn from(value: String) -> Self {
36        Self { message: value }
37    }
38}
39
40impl std::fmt::Debug for PatternError {
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        write!(f, "{}", self.message)
43    }
44}
45
46pub trait PatternCreator<I: Interface>: TryFrom<I> + Sized {
47    /// 模式的ID
48    const PATTERN: UIA_PATTERN_ID;
49
50    /**
51    从UI元素获取此模式。
52    `value` UI元素。
53    */
54    fn obtain(value: &UiAutomationElement) -> Result<Self, PatternError>
55    where
56        <Self as TryFrom<I>>::Error: std::fmt::Debug,
57    {
58        let pattern = unsafe { value.get_raw().GetCurrentPattern(Self::PATTERN) };
59        let pattern = pattern
60            .map_err(|e| -> PatternError {
61                format!("Can't get the Pattern({:?}). ({})", Self::PATTERN, e).into()
62            })?
63            .cast::<I>()
64            .unwrap();
65        Self::try_from(pattern).map_err(|e| format!("Can't create pattern. ({:?})", e).into())
66    }
67}
68
69pub trait PatternCreatorWithAutomation<I: Interface>:
70    TryFrom<(I, Weak<IUIAutomation6>)> + Sized
71{
72    /// 模式的ID
73    const PATTERN: UIA_PATTERN_ID;
74
75    /**
76    从UI元素获取此模式。
77    `value` UI元素。
78    */
79    fn obtain(value: &UiAutomationElement) -> Result<Self, PatternError>
80    where
81        <Self as TryFrom<(I, Weak<IUIAutomation6>)>>::Error: std::fmt::Debug,
82    {
83        let pattern = unsafe { value.get_raw().GetCurrentPattern(Self::PATTERN) };
84        let pattern = pattern
85            .map_err(|e| -> PatternError {
86                format!("Can't get the Pattern({:?}). ({})", Self::PATTERN, e).into()
87            })?
88            .cast::<I>()
89            .unwrap();
90        Self::try_from((pattern, value.get_aut()))
91            .map_err(|e| format!("Can't create pattern. ({:?})", e).into())
92    }
93}