win_wrap/uia/pattern/
table.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 std::{
15    fmt::{Debug, Formatter},
16    sync::Weak,
17};
18
19use crate::{
20    ext::VecExt,
21    uia::{
22        element::UiAutomationElement,
23        pattern::{PatternCreatorWithAutomation, PatternError},
24    },
25};
26use windows::Win32::UI::Accessibility::{
27    IUIAutomation6, IUIAutomationTableItemPattern, IUIAutomationTablePattern,
28    RowOrColumnMajor_ColumnMajor, RowOrColumnMajor_Indeterminate, RowOrColumnMajor_RowMajor,
29    UIA_TableItemPatternId, UIA_TablePatternId, UIA_PATTERN_ID,
30};
31
32/**
33提供对控件的访问,该控件充当子元素集合的容器。此元素的子元素支持 UiAutomationTableItemPattern,并按行和列遍历的二维逻辑坐标系进行组织。
34*/
35pub struct UiAutomationTablePattern(IUIAutomationTablePattern, Weak<IUIAutomation6>);
36
37impl TryFrom<(IUIAutomationTablePattern, Weak<IUIAutomation6>)> for UiAutomationTablePattern {
38    type Error = PatternError;
39
40    fn try_from(
41        value: (IUIAutomationTablePattern, Weak<IUIAutomation6>),
42    ) -> Result<Self, Self::Error> {
43        Ok(Self(value.0, value.1))
44    }
45}
46
47impl PatternCreatorWithAutomation<IUIAutomationTablePattern> for UiAutomationTablePattern {
48    const PATTERN: UIA_PATTERN_ID = UIA_TablePatternId;
49}
50
51/// <https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationtablepattern>
52impl UiAutomationTablePattern {
53    /**
54    查询表的主要遍历方向。此属性是只读的。
55    */
56    #[allow(non_upper_case_globals)]
57    pub fn row_or_column_major(&self) -> RowOrColumnMajor {
58        unsafe {
59            match self.0.CurrentRowOrColumnMajor() {
60                Ok(x) => match x {
61                    RowOrColumnMajor_ColumnMajor => RowOrColumnMajor::Column,
62                    RowOrColumnMajor_Indeterminate => RowOrColumnMajor::Indeterminate,
63                    RowOrColumnMajor_RowMajor => RowOrColumnMajor::Row,
64                    _ => RowOrColumnMajor::None,
65                },
66                Err(_) => RowOrColumnMajor::None,
67            }
68        }
69    }
70
71    /**
72    查询表示表中所有列标题的 UI 自动化元素的集合。
73    */
74    pub fn column_headers(&self) -> Vec<UiAutomationElement> {
75        unsafe {
76            match self.0.GetCurrentColumnHeaders() {
77                Ok(x) => x
78                    .to_vec()
79                    .iter()
80                    .map(|x| UiAutomationElement::obtain(self.1.clone(), x.clone()))
81                    .collect(),
82                Err(_) => vec![],
83            }
84        }
85    }
86}
87
88pub enum RowOrColumnMajor {
89    Row,
90    Column,
91    Indeterminate,
92    None,
93}
94
95/// 提供对支持 UiAutomationTablePattern 的容器中的子元素的访问。
96/// 支持此接口的元素还必须支持 UiAutomationGridItemPattern,以提供不特定于表的属性。
97pub struct UiAutomationTableItemPattern(Weak<IUIAutomation6>, IUIAutomationTableItemPattern);
98
99impl TryFrom<(IUIAutomationTableItemPattern, Weak<IUIAutomation6>)>
100    for UiAutomationTableItemPattern
101{
102    type Error = PatternError;
103
104    fn try_from(
105        value: (IUIAutomationTableItemPattern, Weak<IUIAutomation6>),
106    ) -> Result<Self, Self::Error> {
107        Ok(Self(value.1, value.0))
108    }
109}
110
111impl PatternCreatorWithAutomation<IUIAutomationTableItemPattern> for UiAutomationTableItemPattern {
112    const PATTERN: UIA_PATTERN_ID = UIA_TableItemPatternId;
113}
114
115/// <https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationtableitempattern>
116impl UiAutomationTableItemPattern {
117    /**
118    查询与表项或单元格关联的列标题。
119    */
120    pub fn get_column_header_items(&self) -> Option<Vec<UiAutomationElement>> {
121        unsafe {
122            if let Ok(arr) = self.1.GetCurrentColumnHeaderItems() {
123                return Some(
124                    arr.to_vec()
125                        .iter()
126                        .map(|i| UiAutomationElement::obtain(self.0.clone(), i.clone()))
127                        .collect(),
128                );
129            }
130            None
131        }
132    }
133
134    /**
135    查询与表项或单元格关联的行标题。
136    */
137    pub fn get_row_header_items(&self) -> Option<Vec<UiAutomationElement>> {
138        unsafe {
139            if let Ok(arr) = self.1.GetCurrentRowHeaderItems() {
140                return Some(
141                    arr.to_vec()
142                        .iter()
143                        .map(|i| UiAutomationElement::obtain(self.0.clone(), i.clone()))
144                        .collect(),
145                );
146            }
147            None
148        }
149    }
150}
151
152impl Debug for UiAutomationTableItemPattern {
153    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
154        write!(f, "UiAutomationTableItemPattern()")
155    }
156}