win_wrap/uia/pattern/
value.rs1use crate::uia::pattern::{PatternCreator, PatternError};
15use std::fmt::{Debug, Formatter};
16use windows::{
17 core::BSTR,
18 Win32::UI::Accessibility::{IUIAutomationValuePattern, UIA_ValuePatternId, UIA_PATTERN_ID},
19};
20
21pub struct UiAutomationValuePattern(IUIAutomationValuePattern);
24
25impl TryFrom<IUIAutomationValuePattern> for UiAutomationValuePattern {
26 type Error = PatternError;
27
28 fn try_from(value: IUIAutomationValuePattern) -> Result<Self, Self::Error> {
29 Ok(Self(value))
30 }
31}
32
33impl PatternCreator<IUIAutomationValuePattern> for UiAutomationValuePattern {
34 const PATTERN: UIA_PATTERN_ID = UIA_ValuePatternId;
35}
36
37impl UiAutomationValuePattern {
39 pub fn get_value(&self) -> Result<String, String> {
43 let value = unsafe { self.0.CurrentValue() };
44 match value {
45 Ok(v) => Ok(v.to_string()),
46 Err(ee) => Err(format!("Can't get the value. ({})", ee)),
47 }
48 }
49
50 pub fn set_value(&self, value: &str) -> Result<(), String> {
54 let value = BSTR::from(value);
55 let value = unsafe { self.0.SetValue(&value) };
56 if let Err(e) = value {
57 return Err(format!("Can't set the value. ({})", e));
58 }
59 Ok(())
60 }
61
62 pub fn is_readonly(&self) -> Result<bool, String> {
66 let value = unsafe { self.0.CurrentIsReadOnly() };
67 match value {
68 Ok(v) => Ok(v.0 != 0),
69 Err(ee) => Err(format!("Can't get the value. ({})", ee)),
70 }
71 }
72}
73
74impl Debug for UiAutomationValuePattern {
75 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
76 write!(f, "UiAutomationValuePattern()")
77 }
78}
79
80unsafe impl Send for UiAutomationValuePattern {}
81
82unsafe impl Sync for UiAutomationValuePattern {}