rsiot/components/cmp_slint/config.rs
1use std::time::Duration;
2
3use slint::ComponentHandle;
4use tokio::sync::mpsc;
5
6use crate::message::{Message, MsgDataBound};
7
8pub type FnInput<TMsg, TMainWindow> = fn(TMsg, TMainWindow);
9pub type FnOutput<TMsg, TMainWindow> = fn(TMainWindow, mpsc::Sender<TMsg>);
10
11/// Настройки компонента cmp_slint
12pub struct Config<TMsg, TMainWindow>
13where
14 Self: Sync,
15 TMsg: MsgDataBound,
16 TMainWindow: ComponentHandle,
17{
18 /// Ссылка на главное окно
19 pub slint_window: super::SlintWindow<TMainWindow>,
20
21 /// Функция обработки входящих сообщений
22 ///
23 /// *Пример:*
24 ///
25 /// ```rust
26 /// fn_input: |msg, w| {
27 /// let input_data = w.global::<Input>();
28 /// let Some(msg) = msg.get_custom_data() else {
29 /// return;
30 /// };
31 /// match msg {
32 /// Custom::LiveCounter(msg) => match msg {
33 /// Livecounter::Counter(c) => input_data.set_value_from_phone(c as i32),
34 /// },
35 /// _ => (),
36 /// };
37 /// },
38 /// ```
39 pub fn_input: FnInput<TMsg, TMainWindow>,
40
41 /// Функция генерирования исходящих сообщений
42 ///
43 /// *Пример:*
44 ///
45 /// ```rust
46 /// fn_output: |w, tx| {
47 /// let output_data = w.global::<Output>();
48 /// output_data.on_slider(move |value| {
49 /// let msg = Message::new_custom(Custom::Slint(Slint::Slider(value as f64)));
50 /// tx.blocking_send(msg).unwrap();
51 /// })
52 /// },
53 /// ```
54 pub fn_output: FnOutput<TMsg, TMainWindow>,
55
56 /// Период фильтрации исходящих сообщений
57 pub output_period: Duration,
58}
59
60impl<TMsg, TMainWindow> Clone for Config<TMsg, TMainWindow>
61where
62 TMsg: MsgDataBound,
63 TMainWindow: ComponentHandle,
64{
65 fn clone(&self) -> Self {
66 Self {
67 slint_window: self.slint_window.clone(),
68 fn_input: self.fn_input,
69 fn_output: self.fn_output,
70 output_period: Duration::from_millis(100),
71 }
72 }
73}