relm4/factory/sync/
traits.rs

1//! Traits for for managing and updating factories.
2
3use crate::Sender;
4use crate::factory::{FactorySender, FactoryView, Position};
5
6use std::fmt::Debug;
7
8/// A component that's stored inside a factory.
9/// Similar to [`Component`](crate::Component) but adjusted to fit the life cycle
10/// of factories.
11pub trait FactoryComponent:
12    Position<<Self::ParentWidget as FactoryView>::Position, Self::Index> + Sized + 'static
13{
14    /// Container widget to which all widgets of the factory will be added.
15    type ParentWidget: FactoryView + 'static;
16
17    /// Messages which are received from commands executing in the background.
18    type CommandOutput: Debug + Send + 'static;
19
20    /// The message type that the factory component accepts as inputs.
21    type Input: Debug + 'static;
22
23    /// The message type that the factory component provides as outputs.
24    type Output: Debug + 'static;
25
26    /// The parameter used to initialize the factory component.
27    type Init;
28
29    /// The top-level widget of the factory component.
30    type Root: AsRef<<Self::ParentWidget as FactoryView>::Children> + Debug + Clone;
31
32    /// The type that's used for storing widgets created for this factory component.
33    type Widgets: 'static;
34
35    /// The type that's used by a factory collection as index.
36    ///
37    /// For example, for [`FactoryVecDeque`](crate::factory::FactoryVecDeque), this type
38    /// is [`DynamicIndex`](crate::factory::DynamicIndex).
39    /// For [`FactoryHashMap`](crate::factory::FactoryHashMap), this type is equal to the key
40    /// you use for inserting values.
41    type Index;
42
43    /// Initializes the model.
44    fn init_model(init: Self::Init, index: &Self::Index, sender: FactorySender<Self>) -> Self;
45
46    /// Initializes the root widget
47    fn init_root(&self) -> Self::Root;
48
49    /// Initializes the widgets.
50    fn init_widgets(
51        &mut self,
52        index: &Self::Index,
53        root: Self::Root,
54        returned_widget: &<Self::ParentWidget as FactoryView>::ReturnedWidget,
55        sender: FactorySender<Self>,
56    ) -> Self::Widgets;
57
58    /// Processes inputs received by the component.
59    #[allow(unused)]
60    fn update(&mut self, message: Self::Input, sender: FactorySender<Self>) {}
61
62    /// Defines how the component should respond to command updates.
63    #[allow(unused)]
64    fn update_cmd(&mut self, message: Self::CommandOutput, sender: FactorySender<Self>) {}
65
66    /// Handles updates from a command.
67    fn update_cmd_with_view(
68        &mut self,
69        widgets: &mut Self::Widgets,
70        message: Self::CommandOutput,
71        sender: FactorySender<Self>,
72    ) {
73        self.update_cmd(message, sender.clone());
74        self.update_view(widgets, sender);
75    }
76
77    /// Updates the view after the model has been updated.
78    #[allow(unused)]
79    fn update_view(&self, widgets: &mut Self::Widgets, sender: FactorySender<Self>) {}
80
81    /// Updates the model and view. Optionally returns a command to run.
82    fn update_with_view(
83        &mut self,
84        widgets: &mut Self::Widgets,
85        message: Self::Input,
86        sender: FactorySender<Self>,
87    ) {
88        self.update(message, sender.clone());
89        self.update_view(widgets, sender);
90    }
91
92    /// Last method called before a component is shut down.
93    #[allow(unused)]
94    fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {}
95
96    /// An identifier for the component used for debug logging.
97    ///
98    /// The default implementation of this method uses the address of the component, but
99    /// implementations are free to provide more meaningful identifiers.
100    fn id(&self) -> String {
101        format!("{:p}", &self)
102    }
103}
104
105/// Extension for [`FactoryComponent`] that makes elements cloneable.
106pub trait CloneableFactoryComponent: FactoryComponent {
107    /// Retrieve the initialization data from an initialized factory component.
108    /// This is necessary for cloning the factory.
109    fn get_init(&self) -> Self::Init;
110}