use wincom::{ComObject, Variant};
use windows::Win32::System::Com::IDispatch;
use crate::layer::IvgLayer;
pub struct IvgLayers {
disp: ComObject,
}
impl IvgLayers {
pub fn new(disp: IDispatch) -> Self {
Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
}
pub fn from_disp(disp: IDispatch) -> Self {
Self::new(disp)
}
pub fn as_variant(&self) -> Variant {
self.disp.as_variant()
}
pub fn raw(&self) -> &ComObject {
&self.disp
}
pub fn count(&self) -> Option<i64> {
self.disp.get_property("Count").ok()?.to_i64().ok()
}
pub fn application(&self) -> Option<crate::app::IvgApplication> {
self.disp
.get_property("Application")
.ok()?
.to_idispatch()
.ok()
.map(crate::app::IvgApplication::from_disp)
}
pub fn item(&self, index_or_name: Variant) -> Option<IvgLayer> {
let args = vec![index_or_name];
self.disp
.get_property_with("Item", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgLayer::new)
}
pub fn item_by_index(&self, i: i32) -> Option<IvgLayer> {
let v = self
.disp
.get_property_with("Item", vec![Variant::from_i64(i as i64)])
.or_else(|_| self.disp.invoke_method("Item", vec![Variant::from_i64(i as i64)]))
.ok()?;
v.to_idispatch().ok().map(IvgLayer::new)
}
pub fn item_by_name(&self, name: impl Into<String>) -> Option<IvgLayer> {
let name = name.into();
let v = self
.disp
.get_property_with("Item", vec![Variant::from_str(name.clone())])
.or_else(|_| self.disp.invoke_method("Item", vec![Variant::from_str(name)]))
.ok()?;
v.to_idispatch().ok().map(IvgLayer::new)
}
pub fn find(&self, layer_name: impl Into<String>) -> Option<IvgLayer> {
let args = vec![Variant::from_str(layer_name.into())];
self.disp
.invoke_method("Find", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgLayer::new)
}
pub fn top(&self) -> Option<IvgLayer> {
self.disp
.get_property("Top")
.ok()?
.to_idispatch()
.ok()
.map(IvgLayer::new)
}
pub fn bottom(&self) -> Option<IvgLayer> {
self.disp
.get_property("Bottom")
.ok()?
.to_idispatch()
.ok()
.map(IvgLayer::new)
}
pub fn all(&self) -> Vec<IvgLayer> {
let mut out = Vec::new();
let mut current = self.bottom();
while let Some(layer) = current {
current = layer.above(true);
out.push(layer);
}
out
}
}