1pub 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
27pub struct PatternError {
29 message: String,
31}
32
33impl From<String> for PatternError {
34 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 const PATTERN: UIA_PATTERN_ID;
49
50 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 const PATTERN: UIA_PATTERN_ID;
74
75 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}