use crate::ia2::{ia2_lib::AccessibleRelation::IAccessibleRelation, object::Accessible2Object};
use std::ffi::c_void;
use windows::core::Type;
pub struct AccessibleRelation(IAccessibleRelation);
impl AccessibleRelation {
pub(crate) fn from_raw(raw: &IAccessibleRelation) -> Self {
Self(raw.clone())
}
pub fn relation_type(&self) -> String {
unsafe {
let mut res = std::mem::zeroed();
if self.0.relationType(&mut res).is_err() {
return String::new();
}
res.to_string()
}
}
pub fn localized_relation_type(&self) -> String {
unsafe {
let mut res = std::mem::zeroed();
if self.0.localizedRelationType(&mut res).is_err() {
return String::new();
}
res.to_string()
}
}
pub fn n_targets(&self) -> i32 {
unsafe {
let mut num = std::mem::zeroed();
if self.0.nTargets(&mut num).is_err() {
return 0;
}
num
}
}
pub fn target(&self, target_index: i32) -> Option<Accessible2Object> {
unsafe {
let mut target = std::mem::zeroed();
if self.0.target(target_index, &mut target).is_err() {
return None;
}
match Accessible2Object::from_raw(&Type::from_abi(target as *mut c_void).unwrap()) {
Ok(x) => Some(x),
Err(_) => None,
}
}
}
pub fn targets(&self, max_targets: i32) -> Vec<Accessible2Object> {
unsafe {
let mut targets = std::mem::zeroed();
let mut num = 0;
if self.0.targets(max_targets, &mut targets, &mut num).is_err() {
return vec![];
}
let mut v = vec![];
for i in 0..num {
let Ok(o) = Accessible2Object::from_raw(
&Type::from_abi(targets.wrapping_add(i as usize) as *mut c_void).unwrap(),
) else {
continue;
};
v.push(o);
}
v
}
}
}