Skip to main content

eave_api/
widget.rs

1use std::fmt::Debug;
2
3use serde::de::DeserializeOwned;
4
5use crate::ui::{Action, Item, Notice, Panel};
6use crate::{Effect, Subscription, Task};
7
8/// Something that sits in the bar, with an optional popover.
9///
10/// It follows the same Elm shape as the host: state lives in `Self`, changes arrive as messages in
11/// [`update`](Widget::update), and the views are pure functions of state. `update` must not block:
12/// do I/O in a [`Task`] or a [`Subscription`].
13pub trait Widget: Sized + 'static {
14    /// Options from this widget's entry in `config.toml`, minus the `widget` key.
15    ///
16    /// The entry may be empty, so use `#[serde(default)]` for options that have one.
17    type Config: DeserializeOwned;
18    type Message: Clone + Debug + Send + Sync + 'static;
19
20    fn new(config: Self::Config) -> (Self, Task<Self::Message>);
21
22    fn update(&mut self, message: Self::Message) -> Effect<Self::Message>;
23
24    fn subscription(&self) -> Subscription<Self::Message> {
25        Subscription::none()
26    }
27
28    /// What the widget shows in the bar.
29    fn item(&self) -> Item<Self::Message>;
30
31    /// The popover contents, if this widget has one.
32    ///
33    /// When it returns `Some` and the item has no `on_press`, a click on the item opens and
34    /// closes the popover. Returning `None` while the popover is open closes it.
35    fn popover(&self) -> Option<Panel<Self::Message>> {
36        None
37    }
38
39    /// The dialog contents, if this widget has one: a panel in the middle of the screen, wider
40    /// than a popover, for what needs room, like a conversation. It opens only when asked
41    /// ([`Effect::open_dialog`]), never on a click. Returning `None` while it's open closes it.
42    fn dialog(&self) -> Option<Panel<Self::Message>> {
43        None
44    }
45
46    /// Notifications to show on screen now, newest first. Only for a notification engine: the
47    /// widget that serves `org.freedesktop.Notifications` lists here what it received. Any other
48    /// widget sends its notifications with `Notify` on the session bus, like any app.
49    ///
50    /// While there are any, the bar's right island turns into a panel of them, like a popover. The
51    /// host takes each one away once its key is no longer in the list. Keep the notices you mean to show in your state, and drop
52    /// one when it's dismissed or expires.
53    fn notices(&self) -> Vec<Notice<Self::Message>> {
54        Vec::new()
55    }
56
57    /// What the widget can do, for the dormer: the host's search dialog, which a keybind opens.
58    /// Typing finds an action by its title, subtitle and keywords, and picking it sends its message
59    /// to this widget, like a click would.
60    ///
61    /// The host reads them only while the dormer is open, after each of your messages, so they can
62    /// follow your state: "Mute" while sound plays, "Unmute" while muted. Leave out what can't be
63    /// done right now. Keep it pure and cheap, like the views.
64    fn actions(&self) -> Vec<Action<Self::Message>> {
65        Vec::new()
66    }
67}