layer_shika_composition/
selection.rs1use crate::{
2 Error, LayerSurfaceHandle, Shell,
3 selector::{Selector, SurfaceInfo},
4 slint_interpreter::{ComponentInstance, Value},
5};
6use layer_shika_domain::errors::DomainError;
7
8pub 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 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 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 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 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 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 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 pub fn count(&self) -> usize {
101 self.shell.count_selected(&self.selector)
102 }
103
104 pub fn is_empty(&self) -> bool {
106 self.count() == 0
107 }
108
109 pub fn info(&self) -> Vec<SurfaceInfo> {
111 self.shell.get_selected_info(&self.selector)
112 }
113}