mochi_protocol/bubbles.rs
1//! Bubbles: small, long-lived status items in the five areas along the
2//! island's edge, next to the island or at the screen's sides.
3
4use std::fmt;
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8
9/// One of the five places along the edge, from left to right. The island
10/// sits in one of them too.
11#[derive(
12 Debug,
13 Clone,
14 Copy,
15 PartialEq,
16 Eq,
17 PartialOrd,
18 Ord,
19 Hash,
20 Default,
21 Serialize,
22 Deserialize,
23 schemars::JsonSchema,
24)]
25#[serde(rename_all = "kebab-case")]
26pub enum Area {
27 /// Against the left side of the screen.
28 Left,
29 /// Just left of whatever is in the center.
30 CenterLeft,
31 /// The middle of the edge.
32 #[default]
33 Center,
34 /// Just right of whatever is in the center.
35 CenterRight,
36 /// Against the right side of the screen.
37 Right,
38}
39
40impl Area {
41 pub const ALL: [Self; 5] = [
42 Self::Left,
43 Self::CenterLeft,
44 Self::Center,
45 Self::CenterRight,
46 Self::Right,
47 ];
48}
49
50/// Identifies one bubble for its whole life, across the daemon and the UI.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
52#[serde(transparent)]
53pub struct BubbleId(pub u64);
54
55impl fmt::Display for BubbleId {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "{}", self.0)
58 }
59}
60
61/// A bubble as the UI draws it. Snapshots list bubbles in drawing order:
62/// area by area, each from left to right. Consecutive bubbles with the same
63/// `group` share one pill.
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct Bubble {
66 pub id: BubbleId,
67 pub module: String,
68 /// The module's replacement key. A bubble with the same module and key
69 /// continues the previous one, so the UI keeps its view and updates the
70 /// payload.
71 #[serde(default, skip_serializing_if = "Option::is_none")]
72 pub key: Option<String>,
73 /// The view to load: `modules/<module>/<view>.qml`.
74 pub view: String,
75 /// Whether `view` is a wide view with text, drawn in a pill. Otherwise it
76 /// is a small one, drawn in a round bubble.
77 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
78 pub wide: bool,
79 pub payload: Value,
80 pub area: Area,
81 #[serde(default, skip_serializing_if = "Option::is_none")]
82 pub group: Option<String>,
83 /// Higher is more important: the front of a stack.
84 #[serde(default, skip_serializing_if = "is_zero")]
85 pub priority: u8,
86 /// Goes up whenever the bubble has news, like when it appears or its
87 /// module says something new happened; payload updates alone leave it.
88 /// A stack brings a bubble whose news went up to the front for a while.
89 #[serde(default, skip_serializing_if = "is_zero")]
90 pub news: u64,
91 /// What the pointer resting on it shows beside it, unless its view says
92 /// more with a `tooltip` property of its own.
93 #[serde(default, skip_serializing_if = "Option::is_none")]
94 pub tooltip: Option<String>,
95 /// Keeps a pill of its own in an area that merges its bubbles, beside
96 /// the merged shape instead of in it.
97 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
98 pub apart: bool,
99}
100
101fn is_zero<T: Default + PartialEq>(value: &T) -> bool {
102 *value == T::default()
103}
104
105/// How stacked areas behave, when any area stacks.
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
107pub struct Stacking {
108 /// How long a bubble with news stays in front, in milliseconds.
109 pub news_ms: u64,
110}
111
112/// How an area draws its bubbles: `[bubbles] left = "merged"`.
113#[derive(
114 Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, schemars::JsonSchema,
115)]
116#[serde(rename_all = "kebab-case")]
117pub enum Arrangement {
118 /// A pill for each bubble, or for each group.
119 #[default]
120 Apart,
121 /// One shape around all of the area's bubbles.
122 Merged,
123 /// One bubble in front, the others peeking out behind it.
124 Stacked,
125 /// One shape around the bubbles and the island, when the island sits in
126 /// this area or next to it; merged otherwise.
127 Island,
128}
129
130/// An area that doesn't lay its bubbles apart.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
132pub struct AreaArrangement {
133 pub area: Area,
134 pub arrangement: Arrangement,
135}
136
137/// How many bubbles an area has beyond its maximum.
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
139pub struct Overflow {
140 pub area: Area,
141 pub hidden: u32,
142}