use crate::{ Error, LayerSurfaceHandle, Shell, selector::{Selector, SurfaceInfo}, slint_interpreter::{ComponentInstance, Value}, }; use layer_shika_domain::errors::DomainError; pub struct Selection<'a> { shell: &'a Shell, selector: Selector, } impl<'a> Selection<'a> { pub(crate) fn new(shell: &'a Shell, selector: Selector) -> Self { Self { shell, selector } } pub fn on_callback(&self, callback_name: &str, handler: F) where F: Fn(crate::ShellControl) -> R + Clone + 'static, R: crate::IntoValue, { self.shell .on_internal(&self.selector, callback_name, handler); } pub fn on_callback_with_args(&self, callback_name: &str, handler: F) where F: Fn(&[Value], crate::ShellControl) -> R + Clone + 'static, R: crate::IntoValue, { self.shell .on_with_args_internal(&self.selector, callback_name, handler); } pub fn with_component(&self, mut f: F) where F: FnMut(&ComponentInstance), { self.shell.with_selected(&self.selector, |_, component| { f(component); }); } pub fn set_property(&self, name: &str, value: &Value) -> Result<(), Error> { let mut result = Ok(()); self.shell.with_selected(&self.selector, |_, component| { if let Err(e) = component.set_property(name, value.clone()) { log::error!("Failed to set property '{}': {}", name, e); result = Err(Error::Domain(DomainError::Configuration { message: format!("Failed to set property '{}': {}", name, e), })); } }); result } pub fn get_property(&self, name: &str) -> Result, Error> { let mut values = Vec::new(); let mut result = Ok(()); self.shell.with_selected(&self.selector, |_, component| { match component.get_property(name) { Ok(value) => values.push(value), Err(e) => { log::error!("Failed to get property '{}': {}", name, e); result = Err(Error::Domain(DomainError::Configuration { message: format!("Failed to get property '{}': {}", name, e), })); } } }); result.map(|()| values) } pub fn configure(&self, mut f: F) where F: FnMut(&ComponentInstance, LayerSurfaceHandle<'_>), { self.shell .configure_selected(&self.selector, |component, handle| { f(component, handle); }); } pub fn count(&self) -> usize { self.shell.count_selected(&self.selector) } pub fn is_empty(&self) -> bool { self.count() == 0 } pub fn info(&self) -> Vec { self.shell.get_selected_info(&self.selector) } }