relm4/factory/async/
traits.rs

1// Prevent false positive in nightly
2#![allow(unused_qualifications)]
3
4//! Traits for for managing and updating factories.
5
6use crate::Sender;
7use crate::channel::AsyncFactorySender;
8use crate::factory::{AsyncPosition, DynamicIndex, FactoryView};
9use crate::loading_widgets::LoadingWidgets;
10
11use std::fmt::Debug;
12
13/// A component that's stored inside a factory.
14/// Similar to [`Component`](crate::Component) but adjusted to fit the life cycle
15/// of factories.
16pub trait AsyncFactoryComponent:
17    AsyncPosition<<Self::ParentWidget as FactoryView>::Position> + Sized + 'static
18{
19    /// Container widget to which all widgets of the factory will be added.
20    type ParentWidget: FactoryView + 'static;
21
22    /// Messages which are received from commands executing in the background.
23    type CommandOutput: Debug + Send + 'static;
24
25    /// The message type that the factory component accepts as inputs.
26    type Input: Debug + 'static;
27
28    /// The message type that the factory component provides as outputs.
29    type Output: Debug + 'static;
30
31    /// The parameter used to initialize the factory component.
32    type Init;
33
34    /// The top-level widget of the factory component.
35    type Root: AsRef<<Self::ParentWidget as FactoryView>::Children> + Debug + Clone;
36
37    /// The type that's used for storing widgets created for this factory component.
38    type Widgets: 'static;
39
40    /// Initializes the model.
41    fn init_model(
42        init: Self::Init,
43        index: &DynamicIndex,
44        sender: AsyncFactorySender<Self>,
45    ) -> impl std::future::Future<Output = Self>;
46
47    /// Initializes the root widget
48    fn init_root() -> Self::Root;
49
50    /// Allows you to initialize the root widget with a temporary value
51    /// as a placeholder until the [`init_model()`] future completes.
52    ///
53    /// This method does nothing by default.
54    ///
55    /// [`init_model()`]: AsyncFactoryComponent::init_model
56    #[must_use]
57    fn init_loading_widgets(_root: Self::Root) -> Option<LoadingWidgets> {
58        None
59    }
60
61    /// Initializes the widgets.
62    fn init_widgets(
63        &mut self,
64        index: &DynamicIndex,
65        root: Self::Root,
66        returned_widget: &<Self::ParentWidget as FactoryView>::ReturnedWidget,
67        sender: AsyncFactorySender<Self>,
68    ) -> Self::Widgets;
69
70    /// Processes inputs received by the component.
71    #[allow(unused)]
72    fn update(
73        &mut self,
74        message: Self::Input,
75        sender: AsyncFactorySender<Self>,
76    ) -> impl std::future::Future<Output = ()> {
77        async {}
78    }
79
80    /// Defines how the component should respond to command updates.
81    #[allow(unused)]
82    fn update_cmd(
83        &mut self,
84        message: Self::CommandOutput,
85        sender: AsyncFactorySender<Self>,
86    ) -> impl std::future::Future<Output = ()> {
87        async {}
88    }
89
90    /// Handles updates from a command.
91    fn update_cmd_with_view(
92        &mut self,
93        widgets: &mut Self::Widgets,
94        message: Self::CommandOutput,
95        sender: AsyncFactorySender<Self>,
96    ) -> impl std::future::Future<Output = ()> {
97        async {
98            self.update_cmd(message, sender.clone()).await;
99            self.update_view(widgets, sender);
100        }
101    }
102
103    /// Updates the view after the model has been updated.
104    #[allow(unused)]
105    fn update_view(&self, widgets: &mut Self::Widgets, sender: AsyncFactorySender<Self>) {}
106
107    /// Updates the model and view. Optionally returns a command to run.
108    fn update_with_view(
109        &mut self,
110        widgets: &mut Self::Widgets,
111        message: Self::Input,
112        sender: AsyncFactorySender<Self>,
113    ) -> impl std::future::Future<Output = ()> {
114        async {
115            self.update(message, sender.clone()).await;
116            self.update_view(widgets, sender);
117        }
118    }
119
120    /// Last method called before a factory component is shut down.
121    #[allow(unused)]
122    fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {}
123
124    /// An identifier for the component used for debug logging.
125    ///
126    /// The default implementation of this method uses the address of the component, but
127    /// implementations are free to provide more meaningful identifiers.
128    fn id(&self) -> String {
129        format!("{:p}", &self)
130    }
131}