use std::{
fmt::{Debug, Formatter},
sync::Weak,
};
use crate::uia::{
element::UiAutomationElement,
pattern::{PatternCreatorWithAutomation, PatternError},
property::UiaPropertyId,
};
use windows::Win32::{
System::Variant::VARIANT,
UI::Accessibility::{
IUIAutomation6, IUIAutomationItemContainerPattern, UIA_ItemContainerPatternId,
UIA_PATTERN_ID,
},
};
pub struct UiAutomationItemContainerPattern(
Weak<IUIAutomation6>,
IUIAutomationItemContainerPattern,
);
impl TryFrom<(IUIAutomationItemContainerPattern, Weak<IUIAutomation6>)>
for UiAutomationItemContainerPattern
{
type Error = PatternError;
fn try_from(
value: (IUIAutomationItemContainerPattern, Weak<IUIAutomation6>),
) -> Result<Self, Self::Error> {
Ok(Self(value.1, value.0))
}
}
impl PatternCreatorWithAutomation<IUIAutomationItemContainerPattern>
for UiAutomationItemContainerPattern
{
const PATTERN: UIA_PATTERN_ID = UIA_ItemContainerPatternId;
}
impl UiAutomationItemContainerPattern {
pub fn find_item_by_property<T>(
&self,
start_after: Option<&UiAutomationElement>,
property_id: UiaPropertyId,
value: T,
) -> Option<UiAutomationElement>
where
VARIANT: From<T>,
{
unsafe {
let Ok(r) = (match start_after {
None => self.1.FindItemByProperty(
None,
property_id.into(),
&VARIANT::try_from(value).unwrap(),
),
Some(x) => self.1.FindItemByProperty(
x.get_raw(),
property_id.into(),
&VARIANT::try_from(value).unwrap(),
),
}) else {
return None;
};
Some(UiAutomationElement::obtain(self.0.clone(), r))
}
}
}
impl Debug for UiAutomationItemContainerPattern {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "UiAutomationItemContainerPattern()")
}
}