use crate::uia::pattern::{PatternCreator, PatternError};
use std::fmt::{Debug, Formatter};
use windows::Win32::UI::Accessibility::{
IUIAutomationTogglePattern, ToggleState_Indeterminate, ToggleState_Off, ToggleState_On,
UIA_TogglePatternId, UIA_PATTERN_ID,
};
pub struct UiAutomationTogglePattern(IUIAutomationTogglePattern);
impl TryFrom<IUIAutomationTogglePattern> for UiAutomationTogglePattern {
type Error = PatternError;
fn try_from(value: IUIAutomationTogglePattern) -> Result<Self, Self::Error> {
Ok(Self(value))
}
}
impl PatternCreator<IUIAutomationTogglePattern> for UiAutomationTogglePattern {
const PATTERN: UIA_PATTERN_ID = UIA_TogglePatternId;
}
impl UiAutomationTogglePattern {
#[allow(non_upper_case_globals)]
pub fn get_toggle_state(&self) -> ToggleState {
let state = unsafe { self.0.CurrentToggleState() };
if state.is_err() {
return ToggleState::Indeterminate;
}
match state.unwrap() {
ToggleState_On => ToggleState::On,
ToggleState_Off => ToggleState::Off,
ToggleState_Indeterminate => ToggleState::Indeterminate,
_ => ToggleState::Indeterminate,
}
}
pub fn toggle(&self) {
unsafe { self.0.Toggle().unwrap_or(()) }
}
}
impl Debug for UiAutomationTogglePattern {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "UiAutomationTogglePattern()")
}
}
pub enum ToggleState {
On,
Off,
Indeterminate,
}