win_wrap/uia/pattern/
value.rsuse crate::uia::pattern::{PatternCreator, PatternError};
use std::fmt::{Debug, Formatter};
use windows::{
core::BSTR,
Win32::UI::Accessibility::{IUIAutomationValuePattern, UIA_ValuePatternId, UIA_PATTERN_ID},
};
pub struct UiAutomationValuePattern(IUIAutomationValuePattern);
impl TryFrom<IUIAutomationValuePattern> for UiAutomationValuePattern {
type Error = PatternError;
fn try_from(value: IUIAutomationValuePattern) -> Result<Self, Self::Error> {
Ok(Self(value))
}
}
impl PatternCreator<IUIAutomationValuePattern> for UiAutomationValuePattern {
const PATTERN: UIA_PATTERN_ID = UIA_ValuePatternId;
}
impl UiAutomationValuePattern {
pub fn get_value(&self) -> Result<String, String> {
let value = unsafe { self.0.CurrentValue() };
match value {
Ok(v) => Ok(v.to_string()),
Err(ee) => Err(format!("Can't get the value. ({})", ee)),
}
}
pub fn set_value(&self, value: &str) -> Result<(), String> {
let value = BSTR::from(value);
let value = unsafe { self.0.SetValue(&value) };
if let Err(e) = value {
return Err(format!("Can't set the value. ({})", e));
}
Ok(())
}
pub fn is_readonly(&self) -> Result<bool, String> {
let value = unsafe { self.0.CurrentIsReadOnly() };
match value {
Ok(v) => Ok(v.0 != 0),
Err(ee) => Err(format!("Can't get the value. ({})", ee)),
}
}
}
impl Debug for UiAutomationValuePattern {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "UiAutomationValuePattern()")
}
}
unsafe impl Send for UiAutomationValuePattern {}
unsafe impl Sync for UiAutomationValuePattern {}