1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
* Copyright (c) 2024. The RigelA open source project team and
* its contributors reserve all rights.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
use crate::uia::element::UiAutomationElement;
use std::fmt::{Debug, Formatter};
use windows::{
core::{Interface, BSTR},
Win32::UI::Accessibility::{IUIAutomationValuePattern, UIA_ValuePatternId},
};
/// ValuePattern
/// 提供对控件的访问,该控件包含一个值,该值不跨越范围,并且可以表示为字符串。此字符串可能是可编辑的,也可能是不可编辑的,具体取决于控件及其设置。
pub struct UiAutomationValuePattern(IUIAutomationValuePattern);
/// <https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationvaluepattern>
impl UiAutomationValuePattern {
/**
从UI元素获取此模式。
`value` UI元素。
*/
pub fn obtain(value: &UiAutomationElement) -> Result<Self, String> {
let pattern = unsafe { value.get_raw().GetCurrentPattern(UIA_ValuePatternId) };
if let Err(e) = pattern {
return Err(format!("Can't get the ItemContainerPattern. ({})", e));
}
let pattern = pattern
.unwrap()
.cast::<IUIAutomationValuePattern>()
.unwrap();
Ok(Self(pattern))
}
/**
查询元素的值。
*/
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 {}