win_wrap/uia/pattern/
toggle.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
14use crate::uia::pattern::{PatternCreator, PatternError};
15use std::fmt::{Debug, Formatter};
16use windows::Win32::UI::Accessibility::{
17    IUIAutomationTogglePattern, ToggleState_Indeterminate, ToggleState_Off, ToggleState_On,
18    UIA_TogglePatternId, UIA_PATTERN_ID,
19};
20
21/**
22提供对控件的访问,该控件可以在一组状态之间循环,并在设置状态后保持状态。
23*/
24pub struct UiAutomationTogglePattern(IUIAutomationTogglePattern);
25
26impl TryFrom<IUIAutomationTogglePattern> for UiAutomationTogglePattern {
27    type Error = PatternError;
28
29    fn try_from(value: IUIAutomationTogglePattern) -> Result<Self, Self::Error> {
30        Ok(Self(value))
31    }
32}
33
34impl PatternCreator<IUIAutomationTogglePattern> for UiAutomationTogglePattern {
35    const PATTERN: UIA_PATTERN_ID = UIA_TogglePatternId;
36}
37
38/// <https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationtogglepattern>
39impl UiAutomationTogglePattern {
40    /**
41    检索控件的状态。
42    */
43    #[allow(non_upper_case_globals)]
44    pub fn get_toggle_state(&self) -> ToggleState {
45        let state = unsafe { self.0.CurrentToggleState() };
46        if state.is_err() {
47            return ToggleState::Indeterminate;
48        }
49        match state.unwrap() {
50            ToggleState_On => ToggleState::On,
51            ToggleState_Off => ToggleState::Off,
52            ToggleState_Indeterminate => ToggleState::Indeterminate,
53            _ => ToggleState::Indeterminate,
54        }
55    }
56
57    /**
58    在控件的切换状态之间循环。
59    控件按以下顺序循环其状态:ToggleState::On、ToggleState::Off,以及(如果支持)ToggleState::Indeterminate。
60    */
61    pub fn toggle(&self) {
62        unsafe { self.0.Toggle().unwrap_or(()) }
63    }
64}
65
66impl Debug for UiAutomationTogglePattern {
67    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
68        write!(f, "UiAutomationTogglePattern()")
69    }
70}
71
72pub enum ToggleState {
73    On,
74    Off,
75    Indeterminate,
76}