relm4/component/async/
controller.rs

1// Copyright 2021-2022 Aaron Erhardt <aaron.erhardt@t-online.de>
2// Copyright 2022 System76 <info@system76.com>
3// SPDX-License-Identifier: MIT or Apache-2.0
4
5use std::fmt::{self, Debug};
6
7use crate::{Sender, ShutdownOnDrop};
8
9use super::AsyncComponent;
10
11/// Shared behavior of component controller types.
12pub trait AsyncComponentController<C: AsyncComponent> {
13    /// Emits an input to the component.
14    fn emit(&self, event: C::Input) {
15        self.sender().send(event).unwrap();
16    }
17
18    /// Provides access to the component's sender.
19    fn sender(&self) -> &Sender<C::Input>;
20
21    /// Returns the root widget of the component.
22    fn widget(&self) -> &C::Root;
23
24    /// Dropping this type will usually stop the runtime of the component.
25    /// With this method you can give the runtime a static lifetime.
26    /// In other words, dropping the controller or connector will not stop
27    /// the runtime anymore, instead it will run until the app is closed.
28    fn detach_runtime(&mut self);
29}
30
31/// Controls the component from afar.
32pub struct AsyncController<C: AsyncComponent> {
33    /// The widget that this component manages.
34    pub(super) widget: C::Root,
35
36    /// Used for emitting events to the component.
37    pub(super) sender: Sender<C::Input>,
38
39    /// Type used to destroy the async component when it's dropped.
40    pub(super) shutdown_on_drop: ShutdownOnDrop,
41}
42
43impl<C: AsyncComponent> AsyncComponentController<C> for AsyncController<C> {
44    fn sender(&self) -> &Sender<C::Input> {
45        &self.sender
46    }
47
48    fn widget(&self) -> &C::Root {
49        &self.widget
50    }
51
52    fn detach_runtime(&mut self) {
53        self.shutdown_on_drop.deactivate();
54    }
55}
56
57impl<C> Debug for AsyncController<C>
58where
59    C: AsyncComponent + Debug,
60    C::Widgets: Debug,
61{
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        f.debug_struct("Controller")
64            .field("widget", &self.widget)
65            .field("sender", &self.sender)
66            .finish()
67    }
68}