Skip to content

Commit

Permalink
Merge pull request #94 from leexgone/dev
Browse files Browse the repository at this point in the history
v0.14.0
  • Loading branch information
leexgone authored Jan 8, 2025
2 parents a652748 + 0faa007 commit f14fa71
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 151 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,7 @@
## v0.13.4

+ Add get_(cached)_heading_level and is_(cached)_dialog. [#92](https://github.com/leexgone/uiautomation-rs/pull/92)

## v0.14.0

+ update to `windows v0.59.0`.
10 changes: 5 additions & 5 deletions crates/uiautomation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uiautomation"
version = "0.13.4"
version = "0.14.0"
edition = "2021"
license = "Apache-2.0"
authors = ["Steven Lee <[email protected]>"]
Expand All @@ -27,13 +27,13 @@ log = ["dep:log"]
chrono = "0.4.39"
# phf = { version = "0.11.2", features = ["macros"] }
log = { version = "0.4.22", optional = true }
uiautomation_derive = { version = "0.3.12", path = "../uiautomation_derive" }
uiautomation_derive = { version = "0.3.13", path = "../uiautomation_derive" }

[dependencies.windows-core]
version = "0.58.0"
version = "0.59.0"

[dependencies.windows]
version = "0.58.0"
version = "0.59.0"
features = [
"Win32_Foundation",
"Win32_System_Variant",
Expand All @@ -46,5 +46,5 @@ features = [
"Win32_Security",
"Win32_UI_Shell_PropertiesSystem",
"UI_UIAutomation",
"implement",
# "implement",
]
2 changes: 1 addition & 1 deletion crates/uiautomation/src/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ impl WindowControl {
pub fn set_foregrand(&self) -> Result<bool> {
let hwnd = self.control.get_native_window_handle()?;
let ret = unsafe {
SetForegroundWindow(hwnd)
SetForegroundWindow(hwnd.into())
};
Ok(ret.as_bool())
}
Expand Down
8 changes: 4 additions & 4 deletions crates/uiautomation/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl UIAutomation {
/// Retrieves a UI Automation element for the specified window.
pub fn element_from_handle(&self, hwnd: Handle) -> Result<UIElement> {
let element = unsafe {
self.automation.ElementFromHandle(hwnd)?
self.automation.ElementFromHandle(hwnd.into())?
};

Ok(UIElement::from(element))
Expand All @@ -118,7 +118,7 @@ impl UIAutomation {
/// Retrieves a UI Automation element for the specified window, prefetches the requested properties and control patterns, and stores the prefetched items in the cache.
pub fn element_from_handle_build_cache(&self, hwnd: Handle, cache_request: &UICacheRequest) -> Result<UIElement> {
let element = unsafe {
self.automation.ElementFromHandleBuildCache(hwnd, cache_request)?
self.automation.ElementFromHandleBuildCache(hwnd.into(), cache_request)?
};
Ok(element.into())
}
Expand Down Expand Up @@ -306,9 +306,9 @@ impl UIAutomation {
pub fn create_property_condition(&self, property: UIProperty, value: Variant, flags: Option<PropertyConditionFlags>) -> Result<UICondition> {
let condition = unsafe {
if let Some(flags) = flags {
self.automation.CreatePropertyConditionEx(property.into(), value, flags.into())?
self.automation.CreatePropertyConditionEx(property.into(), value.as_ref(), flags.into())?
} else {
self.automation.CreatePropertyCondition(property.into(), value)?
self.automation.CreatePropertyCondition(property.into(), value.as_ref())?
}
};
Ok(condition.into())
Expand Down
2 changes: 1 addition & 1 deletion crates/uiautomation/src/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ pub fn message_box(text: &str, caption: &str, styles: MESSAGEBOX_STYLE) -> MESSA

unsafe {
let hwnd = GetActiveWindow();
MessageBoxW(hwnd, lptext, lpcaption, styles)
MessageBoxW(hwnd.into(), lptext, lpcaption, styles)
}
}
31 changes: 14 additions & 17 deletions crates/uiautomation/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt::Display;
use std::string::FromUtf16Error;
use std::string::FromUtf8Error;

use windows::Win32::Foundation::GetLastError;
use windows::core::HRESULT;
Expand Down Expand Up @@ -39,18 +41,6 @@ impl Error {

pub fn last_os_error() -> Error {
let error = unsafe { GetLastError() };
// let code: i32 = if (error.0 as i32) < 0 {
// error.0 as _
// } else {
// ((error.0 & 0x0000FFFF) | 0x80070000) as _
// };

// HRESULT(code).into()
// if let Err(e) = error {
// e.into()
// } else {
// HRESULT(0).into()
// }
let result = HRESULT::from_win32(error.0);
result.into()
}
Expand Down Expand Up @@ -92,11 +82,6 @@ impl From<windows::core::Error> for Error {

impl Into<windows::core::Error> for Error {
fn into(self) -> windows::core::Error {
// if self.code < 0 {
// windows::core::Error::new(HRESULT(self.code), self.message)
// } else {
// windows::core::Error::new(E_FAIL, self.message)
// }
if let Some(result) = self.result() {
windows::core::Error::from_hresult(result)
} else {
Expand Down Expand Up @@ -132,4 +117,16 @@ impl From<&str> for Error {
}
}

impl From<FromUtf8Error> for Error {
fn from(value: FromUtf8Error) -> Self {
value.to_string().into()
}
}

impl From<FromUtf16Error> for Error {
fn from(value: FromUtf16Error) -> Self {
value.to_string().into()
}
}

pub type Result<T> = core::result::Result<T, Error>;
16 changes: 8 additions & 8 deletions crates/uiautomation/src/events/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct AutomationEventHandler {
}

impl IUIAutomationEventHandler_Impl for AutomationEventHandler_Impl {
fn HandleAutomationEvent(&self, sender: Option<&IUIAutomationElement>, eventid: UIA_EVENT_ID) -> windows_core::Result<()> {
if let Some(e) = sender {
fn HandleAutomationEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>, eventid: UIA_EVENT_ID) -> windows_core::Result<()> {
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
let handler = &self.handler;
handler(&element, eventid.into()).map_err(|e| e.into())
Expand All @@ -51,8 +51,8 @@ pub struct AutomationPropertyChangedEventHandler {
}

impl IUIAutomationPropertyChangedEventHandler_Impl for AutomationPropertyChangedEventHandler_Impl {
fn HandlePropertyChangedEvent(&self, sender: Option<&IUIAutomationElement>, propertyid: UIA_PROPERTY_ID, newvalue: &windows_core::VARIANT) -> windows_core::Result<()> {
if let Some(e) = sender {
fn HandlePropertyChangedEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>, propertyid: UIA_PROPERTY_ID, newvalue: &windows::Win32::System::Variant::VARIANT) -> windows_core::Result<()> {
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
let value = Variant::from(newvalue);
let handler = &self.handler;
Expand All @@ -77,8 +77,8 @@ pub struct AutomationStructureChangedEventHandler {
}

impl IUIAutomationStructureChangedEventHandler_Impl for AutomationStructureChangedEventHandler_Impl {
fn HandleStructureChangedEvent(&self, sender: Option<&IUIAutomationElement>, changetype: windows::Win32::UI::Accessibility::StructureChangeType, runtimeid: *const windows::Win32::System::Com::SAFEARRAY) -> windows_core::Result<()> {
if let Some(e) = sender {
fn HandleStructureChangedEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>, changetype: windows::Win32::UI::Accessibility::StructureChangeType, runtimeid: *const windows::Win32::System::Com::SAFEARRAY) -> windows_core::Result<()> {
if let Some(e) = sender.as_ref() {
let handler = &self.handler;
let element = UIElement::from(e);
let arr = SafeArray::from(runtimeid);
Expand Down Expand Up @@ -112,8 +112,8 @@ pub struct AutomationFocusChangedEventHandler {
}

impl IUIAutomationFocusChangedEventHandler_Impl for AutomationFocusChangedEventHandler_Impl {
fn HandleFocusChangedEvent(&self, sender: Option<&IUIAutomationElement>) -> windows_core::Result<()> {
if let Some(e) = sender {
fn HandleFocusChangedEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>) -> windows_core::Result<()> {
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
let handler = &self.handler;
handler(&element).map_err(|e| e.into())
Expand Down
16 changes: 8 additions & 8 deletions crates/uiautomation/src/events/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct AutomationEventHandler {
}

impl IUIAutomationEventHandler_Impl for AutomationEventHandler_Impl {
fn HandleAutomationEvent(&self, sender: Option<&IUIAutomationElement>, eventid: UIA_EVENT_ID) -> windows::core::Result<()> {
if let Some(e) = sender {
fn HandleAutomationEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>, eventid: UIA_EVENT_ID) -> windows::core::Result<()> {
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
self.handler.handle(&element, eventid.into()).map_err(|e| e.into())
} else {
Expand All @@ -50,8 +50,8 @@ pub struct AutomationPropertyChangedHandler {
}

impl IUIAutomationPropertyChangedEventHandler_Impl for AutomationPropertyChangedHandler_Impl {
fn HandlePropertyChangedEvent(&self, sender: Option<&IUIAutomationElement>, propertyid: UIA_PROPERTY_ID, newvalue: &windows::core::VARIANT) -> windows::core::Result<()> {
if let Some(e) = sender {
fn HandlePropertyChangedEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>, propertyid: UIA_PROPERTY_ID, newvalue: &windows::Win32::System::Variant::VARIANT) -> windows::core::Result<()> {
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
let value = Variant::from(newvalue);
self.handler.handle(&element, propertyid.into(), value).map_err(|e| e.into())
Expand All @@ -75,8 +75,8 @@ pub struct AutomationStructureChangedEventHandler {
}

impl IUIAutomationStructureChangedEventHandler_Impl for AutomationStructureChangedEventHandler_Impl {
fn HandleStructureChangedEvent(&self, sender: Option<&IUIAutomationElement>, changetype: windows::Win32::UI::Accessibility::StructureChangeType, runtimeid: *const windows::Win32::System::Com::SAFEARRAY) -> windows_core::Result<()> {
if let Some(e) = sender {
fn HandleStructureChangedEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>, changetype: windows::Win32::UI::Accessibility::StructureChangeType, runtimeid: *const windows::Win32::System::Com::SAFEARRAY) -> windows_core::Result<()> {
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
let arr = SafeArray::from(runtimeid);
let ret = if arr.is_null() {
Expand Down Expand Up @@ -109,8 +109,8 @@ pub struct AutomationFocusChangedEventHandler {
}

impl IUIAutomationFocusChangedEventHandler_Impl for AutomationFocusChangedEventHandler_Impl {
fn HandleFocusChangedEvent(&self, sender: Option<&IUIAutomationElement>) -> windows_core::Result<()> {
if let Some(e) = sender {
fn HandleFocusChangedEvent(&self, sender: windows_core::Ref<'_, IUIAutomationElement>) -> windows_core::Result<()> {
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
self.handler.handle(&element).map_err(|e| e.into())
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/uiautomation/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl UIPropertyChangedEventHandler {
/// Handles a Microsoft UI Automation property-changed event.
pub fn handle_property_changed_event(&self, sender: &UIElement, property_id: UIProperty, new_value: Variant) -> Result<()> {
unsafe {
self.handler.HandlePropertyChangedEvent(sender, property_id.into(), new_value)?
self.handler.HandlePropertyChangedEvent(sender, property_id.into(), new_value.as_ref())?
};
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/uiautomation/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ impl UIItemContainerPattern {
pub fn find_item_by_property(&self, start_after: UIElement, property: UIProperty, value: Variant) -> Result<UIElement> {
// let val: VARIANT = value.into();
let element = unsafe {
self.pattern.FindItemByProperty(start_after.as_ref(), property.into(), value)?
self.pattern.FindItemByProperty(start_after.as_ref(), property.into(), value.as_ref())?
};

Ok(element.into())
Expand Down Expand Up @@ -2426,7 +2426,7 @@ impl UITextRange {

pub fn find_attribute(&self, attr: TextAttribute, value: Variant, backward: bool) -> Result<UITextRange> {
let range = unsafe {
self.range.FindAttribute(attr.into(), value, backward)?
self.range.FindAttribute(attr.into(), value.as_ref(), backward)?
};
Ok(range.into())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/uiautomation/src/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Process {
unsafe {
CreateProcessW(
app.to_pcwstr(),
cmd.to_pwstr(),
Some(cmd.to_pwstr()),
None,
None,
true,
Expand Down
23 changes: 15 additions & 8 deletions crates/uiautomation/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::fmt::Display;

use uiautomation_derive::EnumConvert;
use uiautomation_derive::map_as;
use windows::core::Param;
// use windows::core::Param;
use windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::POINT;
use windows::Win32::Foundation::RECT;

/// A Point type stores the x and y position.
#[derive(Clone, Copy, PartialEq, Eq, Default)]
#[derive(Clone, Copy, PartialEq, Default)]
pub struct Point(POINT);

impl Point {
Expand Down Expand Up @@ -43,6 +43,9 @@ impl Point {
}
}

impl Eq for Point {
}

impl Debug for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Point").field("x", &self.0.x).field("y", &self.0.y).finish()
Expand Down Expand Up @@ -80,7 +83,7 @@ impl AsMut<POINT> for Point {
}

/// A Rect type stores the position and size of a rectangle.
#[derive(Clone, Copy, PartialEq, Eq, Default)]
#[derive(Clone, Copy, PartialEq, Default)]
pub struct Rect(RECT);

impl Rect {
Expand Down Expand Up @@ -155,6 +158,10 @@ impl Rect {
}
}

impl Eq for Rect {

}

impl Debug for Rect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Rect").field("left", &self.0.left).field("top", &self.0.top).field("right", &self.0.right).field("bottom", &self.0.bottom).finish()
Expand Down Expand Up @@ -228,11 +235,11 @@ impl AsRef<HWND> for Handle {
}
}

impl Param<HWND> for Handle {
unsafe fn param(self) -> windows::core::ParamValue<HWND> {
windows::core::ParamValue::Owned(self.0)
}
}
// impl Param<HWND> for Handle {
// unsafe fn param(self) -> windows::core::ParamValue<HWND> {
// windows::core::ParamValue::Owned(self.0)
// }
// }

impl From<isize> for Handle {
fn from(value: isize) -> Self {
Expand Down
Loading

0 comments on commit f14fa71

Please sign in to comment.