layer_shika_composition/
selection.rs

1use crate::{
2    Error, LayerSurfaceHandle, Shell,
3    selector::{Selector, SurfaceInfo},
4    slint_interpreter::{ComponentInstance, Value},
5};
6use layer_shika_domain::errors::DomainError;
7
8/// A selection of surfaces matching a selector
9///
10/// Provides methods to interact with all matching surfaces at once.
11/// Created via `Shell::select()`.
12pub struct Selection<'a> {
13    shell: &'a Shell,
14    selector: Selector,
15}
16
17impl<'a> Selection<'a> {
18    pub(crate) fn new(shell: &'a Shell, selector: Selector) -> Self {
19        Self { shell, selector }
20    }
21
22    /// Registers a callback handler for all matching surfaces
23    ///
24    /// Handler receives a `CallbackContext` with surface identity and shell control.
25    pub fn on_callback<F, R>(&mut self, callback_name: &str, handler: F) -> &mut Self
26    where
27        F: Fn(crate::CallbackContext) -> R + Clone + 'static,
28        R: crate::IntoValue,
29    {
30        self.shell
31            .on_internal(&self.selector, callback_name, handler);
32        self
33    }
34
35    /// Registers a callback handler that receives arguments for all matching surfaces
36    pub fn on_callback_with_args<F, R>(&mut self, callback_name: &str, handler: F) -> &mut Self
37    where
38        F: Fn(&[Value], crate::CallbackContext) -> R + Clone + 'static,
39        R: crate::IntoValue,
40    {
41        self.shell
42            .on_with_args_internal(&self.selector, callback_name, handler);
43        self
44    }
45
46    /// Executes a function with each matching component instance
47    pub fn with_component<F>(&self, mut f: F)
48    where
49        F: FnMut(&ComponentInstance),
50    {
51        self.shell.with_selected(&self.selector, |_, component| {
52            f(component);
53        });
54    }
55
56    /// Sets a property value on all matching surfaces
57    pub fn set_property(&self, name: &str, value: &Value) -> Result<(), Error> {
58        let mut result = Ok(());
59        self.shell.with_selected(&self.selector, |_, component| {
60            if let Err(e) = component.set_property(name, value.clone()) {
61                log::error!("Failed to set property '{}': {}", name, e);
62                result = Err(Error::Domain(DomainError::Configuration {
63                    message: format!("Failed to set property '{}': {}", name, e),
64                }));
65            }
66        });
67        result
68    }
69
70    /// Gets property values from all matching surfaces
71    pub fn get_property(&self, name: &str) -> Result<Vec<Value>, Error> {
72        let mut values = Vec::new();
73        let mut result = Ok(());
74        self.shell.with_selected(&self.selector, |_, component| {
75            match component.get_property(name) {
76                Ok(value) => values.push(value),
77                Err(e) => {
78                    log::error!("Failed to get property '{}': {}", name, e);
79                    result = Err(Error::Domain(DomainError::Configuration {
80                        message: format!("Failed to get property '{}': {}", name, e),
81                    }));
82                }
83            }
84        });
85        result.map(|()| values)
86    }
87
88    /// Executes a configuration function with component and surface handle for matching surfaces
89    pub fn configure<F>(&self, mut f: F)
90    where
91        F: FnMut(&ComponentInstance, LayerSurfaceHandle<'_>),
92    {
93        self.shell
94            .configure_selected(&self.selector, |component, handle| {
95                f(component, handle);
96            });
97    }
98
99    /// Returns the number of surfaces matching the selector
100    pub fn count(&self) -> usize {
101        self.shell.count_selected(&self.selector)
102    }
103
104    /// Checks if no surfaces match the selector
105    pub fn is_empty(&self) -> bool {
106        self.count() == 0
107    }
108
109    /// Returns information about all matching surfaces
110    pub fn info(&self) -> Vec<SurfaceInfo> {
111        self.shell.get_selected_info(&self.selector)
112    }
113}