Skip to content

Commit

Permalink
impl TryFrom for enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
leexgone committed Jan 17, 2025
1 parent 81decc6 commit a1e2457
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,7 @@
## v0.15.1

+ Update deps & append docs.

## v0.16.0

+ Impl `TryFrom` for enum types, instead of `From`.
6 changes: 2 additions & 4 deletions crates/uiautomation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uiautomation"
version = "0.15.1"
version = "0.16.0"
edition = "2021"
license = "Apache-2.0"
authors = ["Steven Lee <[email protected]>"]
Expand All @@ -25,9 +25,8 @@ log = ["dep:log"]
[dependencies]

chrono = "0.4.39"
# phf = { version = "0.11.2", features = ["macros"] }
log = { version = "0.4.25", optional = true }
uiautomation_derive = { version = "0.3.14", path = "../uiautomation_derive" }
uiautomation_derive = { version = "0.4.0", path = "../uiautomation_derive" }

[dependencies.windows-core]
version = "0.59.0"
Expand All @@ -46,5 +45,4 @@ features = [
"Win32_Security",
"Win32_UI_Shell_PropertiesSystem",
"UI_UIAutomation",
# "implement",
]
21 changes: 14 additions & 7 deletions crates/uiautomation/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ impl UIElement {
self.element.CurrentOrientation()?
};

Ok(orientation.into())
// Ok(orientation.into())
orientation.try_into()
}

/// Retrieves a cached value that indicates the orientation of the element.
Expand All @@ -844,7 +845,8 @@ impl UIElement {
self.element.CachedOrientation()?
};

Ok(orientation.into())
// Ok(orientation.into())
orientation.try_into()
}

/// Retrieves the name of the underlying UI framework.
Expand Down Expand Up @@ -1348,7 +1350,8 @@ impl UICacheRequest {
let mode = unsafe {
self.request.AutomationElementMode()?
};
Ok(mode.into())
// Ok(mode.into())
mode.try_into()
}

/// Sets whether returned elements contain full references to the underlying UI, or only cached information.
Expand Down Expand Up @@ -1380,7 +1383,8 @@ impl UICacheRequest {
let scope = unsafe {
self.request.TreeScope()?
};
Ok(scope.into())
// Ok(scope.into())
scope.try_into()
}

/// Sets the scope of caching.
Expand Down Expand Up @@ -2249,7 +2253,8 @@ impl UIPropertyCondition {
let property_id = unsafe {
self.0.PropertyId()?
};
Ok(property_id.into())
// Ok(property_id.into())
property_id.try_into()
}

/// Retrieves the property value that must be matched for the condition to be true.
Expand All @@ -2262,9 +2267,11 @@ impl UIPropertyCondition {

/// Retrieves a set of flags that specify how the condition is applied.
pub fn get_property_condition_flags(&self) -> Result<PropertyConditionFlags> {
Ok(unsafe {
let flags = unsafe {
self.0.PropertyConditionFlags()?
}.into())
};

flags.try_into()
}
}

Expand Down
19 changes: 15 additions & 4 deletions crates/uiautomation/src/events/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use windows::Win32::UI::Accessibility::UIA_EVENT_ID;
use windows::Win32::UI::Accessibility::UIA_PROPERTY_ID;
use windows_core::implement;

use crate::types::StructureChangeType;
use crate::variants::SafeArray;
use crate::variants::Variant;
use crate::UIElement;
Expand All @@ -30,7 +31,10 @@ impl IUIAutomationEventHandler_Impl for AutomationEventHandler_Impl {
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())
match eventid.try_into() {
Ok(event_id) => handler(&element, event_id).map_err(|e| e.into()),
Err(e) => Err(e.into())
}
} else {
Ok(())
}
Expand All @@ -56,7 +60,10 @@ impl IUIAutomationPropertyChangedEventHandler_Impl for AutomationPropertyChanged
let element = UIElement::from(e);
let value = Variant::from(newvalue);
let handler = &self.handler;
handler(&element, propertyid.into(), value).map_err(|e| e.into())
match propertyid.try_into() {
Ok(property_id) => handler(&element, property_id, value).map_err(|e| e.into()),
Err(e) => Err(e.into()),
}
} else {
Ok(())
}
Expand All @@ -82,14 +89,18 @@ impl IUIAutomationStructureChangedEventHandler_Impl for AutomationStructureChang
let handler = &self.handler;
let element = UIElement::from(e);
let arr = SafeArray::from(runtimeid);
let change_type: StructureChangeType = match changetype.try_into() {
Ok(change_type) => change_type,
Err(e) => return Err(e.into()),
};
let ret = if arr.is_null() {
handler(&element, changetype.into(), None)
handler(&element, change_type, None)
} else {
let runtime_id: Vec<i32> = match arr.try_into() {
Ok(arr) => arr,
Err(e) => return Err(e.into())
};
handler(&element, changetype.into(), Some(&runtime_id))
handler(&element, change_type, Some(&runtime_id))
};
ret.map_err(|e| e.into())
} else {
Expand Down
21 changes: 17 additions & 4 deletions crates/uiautomation/src/events/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use windows::Win32::UI::Accessibility::UIA_EVENT_ID;
use windows::Win32::UI::Accessibility::UIA_PROPERTY_ID;
use windows_core::implement;

use crate::types::StructureChangeType;
use crate::variants::SafeArray;
use crate::variants::Variant;
use crate::UIElement;
Expand All @@ -29,7 +30,11 @@ impl IUIAutomationEventHandler_Impl for AutomationEventHandler_Impl {
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())
match eventid.try_into() {
Ok(event_id) => self.handler.handle(&element, event_id).map_err(|e| e.into()),
Err(e) => Err(e.into())
}
// self.handler.handle(&element, eventid.into()).map_err(|e| e.into())
} else {
Ok(())
}
Expand All @@ -54,7 +59,11 @@ impl IUIAutomationPropertyChangedEventHandler_Impl for AutomationPropertyChanged
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())
match propertyid.try_into() {
Ok(property_id) => self.handler.handle(&element, property_id, value).map_err(|e| e.into()),
Err(e) => Err(e.into()),
}
// self.handler.handle(&element, propertyid.into(), value).map_err(|e| e.into())
} else {
Ok(())
}
Expand All @@ -79,14 +88,18 @@ impl IUIAutomationStructureChangedEventHandler_Impl for AutomationStructureChang
if let Some(e) = sender.as_ref() {
let element = UIElement::from(e);
let arr = SafeArray::from(runtimeid);
let change_type: StructureChangeType = match changetype.try_into() {
Ok(change_type) => change_type,
Err(e) => return Err(e.into())
};
let ret = if arr.is_null() {
self.handler.handle(&element, changetype.into(), None)
self.handler.handle(&element, change_type, None)
} else {
let runtime_id: Vec<i32> = match arr.try_into() {
Ok(arr) => arr,
Err(e) => return Err(e.into())
};
self.handler.handle(&element, changetype.into(), Some(&runtime_id))
self.handler.handle(&element, change_type, Some(&runtime_id))
};
ret.map_err(|e| e.into())
} else {
Expand Down
51 changes: 34 additions & 17 deletions crates/uiautomation/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,16 @@ impl UIAnnotationPattern {
let id = unsafe {
self.pattern.CurrentAnnotationTypeId()?
};
Ok(id.into())
// Ok(id.into())
id.try_into()
}

pub fn get_cached_type(&self) -> Result<AnnotationType> {
let id = unsafe {
self.pattern.CachedAnnotationTypeId()?
};
Ok(id.into())
// Ok(id.into())
id.try_into()
}

pub fn get_type_nane(&self) -> Result<String> {
Expand Down Expand Up @@ -369,14 +371,16 @@ impl UIDockPattern {
let pos = unsafe {
self.pattern.CurrentDockPosition()?
};
Ok(pos.into())
// Ok(pos.into())
pos.try_into()
}

pub fn get_cached_dock_position(&self) -> Result<DockPosition> {
let pos = unsafe {
self.pattern.CachedDockPosition()?
};
Ok(pos.into())
// Ok(pos.into())
pos.try_into()
}

pub fn set_dock_position(&self, position: DockPosition) -> Result<()> {
Expand Down Expand Up @@ -628,14 +632,16 @@ impl UIExpandCollapsePattern {
let state = unsafe {
self.pattern.CurrentExpandCollapseState()?
};
Ok(state.into())
// Ok(state.into())
state.try_into()
}

pub fn get_cached_state(&self) -> Result<ExpandCollapseState> {
let state = unsafe {
self.pattern.CachedExpandCollapseState()?
};
Ok(state.into())
// Ok(state.into())
state.try_into()
}
}

Expand Down Expand Up @@ -1815,14 +1821,16 @@ impl UIStylesPattern {
let style_id = unsafe {
self.pattern.CurrentStyleId()?
};
Ok(style_id.into())
// Ok(style_id.into())
style_id.try_into()
}

pub fn get_cached_style(&self) -> Result<StyleType> {
let style_id = unsafe {
self.pattern.CachedStyleId()?
};
Ok(style_id.into())
// Ok(style_id.into())
style_id.try_into()
}

pub fn get_style_name(&self) -> Result<String> {
Expand Down Expand Up @@ -2037,14 +2045,16 @@ impl UITablePattern {
let major = unsafe {
self.pattern.CurrentRowOrColumnMajor()?
};
Ok(major.into())
// Ok(major.into())
major.try_into()
}

pub fn get_cached_row_or_column_major(&self) -> Result<RowOrColumnMajor> {
let major = unsafe {
self.pattern.CachedRowOrColumnMajor()?
};
Ok(major.into())
// Ok(major.into())
major.try_into()
}
}

Expand Down Expand Up @@ -2266,7 +2276,8 @@ impl UITextPattern {
let selection = unsafe {
self.pattern.SupportedTextSelection()?
};
Ok(selection.into())
// Ok(selection.into())
selection.try_into()
}

pub fn get_range_from_annotation(&self, annotation: &UIElement) -> Result<UITextRange> {
Expand Down Expand Up @@ -2563,14 +2574,16 @@ impl UITogglePattern {
let state = unsafe {
self.pattern.CurrentToggleState()?
};
Ok(state.into())
// Ok(state.into())
state.try_into()
}

pub fn get_cached_toggle_state(&self) -> Result<ToggleState> {
let state = unsafe {
self.pattern.CachedToggleState()?
};
Ok(state.into())
// Ok(state.into())
state.try_into()
}

pub fn toggle(&self) -> Result<()> {
Expand Down Expand Up @@ -2942,15 +2955,17 @@ impl UIWindowPattern {
let state = unsafe {
self.pattern.CurrentWindowVisualState()?
};
Ok(state.into())
// Ok(state.into())
state.try_into()
}

pub fn get_cached_window_visual_state(&self) -> Result<WindowVisualState> {
let state = unsafe {
self.pattern.CachedWindowVisualState()?
};

Ok(state.into())
// Ok(state.into())
state.try_into()
}

pub fn set_window_visual_state(&self, state: WindowVisualState) -> Result<()> {
Expand Down Expand Up @@ -3020,15 +3035,17 @@ impl UIWindowPattern {
self.pattern.CurrentWindowInteractionState()?
};

Ok(state.into())
// Ok(state.into())
state.try_into()
}

pub fn get_cached_window_interaction_state(&self) -> Result<WindowInteractionState> {
let state = unsafe {
self.pattern.CachedWindowInteractionState()?
};

Ok(state.into())
// Ok(state.into())
state.try_into()
}
}

Expand Down
Loading

0 comments on commit a1e2457

Please sign in to comment.