egui_charts/services/notifications.rs
1//! The host's log of alert triggers and other events for the user.
2
3use chrono::{DateTime, Utc};
4
5use super::{Events, ProfileId};
6use crate::data::MaybeSend;
7
8/// Events the host tells the user about, such as alert triggers, filled orders or a lost
9/// connection, for the notifications panel. The host keeps the log; the panel shows what the
10/// service pushes, newest first.
11///
12/// [`watch`](Self::watch) is called on the UI thread and must return at once. Push from any
13/// thread, runtime or browser task.
14///
15/// ```
16/// use egui_charts::services::{Events, Notification, Notifications};
17///
18/// /// Tells the user once that the session started.
19/// struct Session;
20///
21/// impl Notifications for Session {
22/// fn watch(&self, events: Events<Notification>) {
23/// events.push(Notification::new(chrono::Utc::now(), "Session started"));
24/// }
25/// }
26/// ```
27pub trait Notifications: MaybeSend + 'static {
28 /// Pushes the recent notifications, oldest first, then each new one until `events` is
29 /// inactive. Called when the panel starts and again after a lagged stream, each time with a
30 /// new stream that starts from none.
31 ///
32 /// The stream takes at most 10,000 pushes between two frames: push at most that many recent
33 /// notifications at once, or the stream lags on every call and none shows.
34 fn watch(&self, events: Events<Notification>);
35}
36
37/// One entry of the log.
38#[derive(Clone, Debug, PartialEq)]
39#[non_exhaustive]
40pub struct Notification {
41 /// UTC timestamp supplied by the host.
42 pub time: DateTime<Utc>,
43 /// Display title.
44 pub title: String,
45 /// Detail shown under the title. May be empty.
46 pub body: String,
47 /// The symbol the entry is about, which the panel offers to open.
48 pub symbol: Option<String>,
49 /// The profile or chat room a social entry is about, which the panel offers to open.
50 pub target: Option<NotificationTarget>,
51}
52
53/// What a social notification opens.
54#[derive(Clone, Debug, PartialEq, Eq)]
55#[non_exhaustive]
56pub enum NotificationTarget {
57 /// The profile of a user, as [`Profiles`](super::Profiles) knows it.
58 Profile(ProfileId),
59 /// A chat room, by the identifier [`Chats`](super::Chats) uses.
60 Room(String),
61}
62
63impl Notification {
64 /// An entry with a title and nothing else: set the other fields as known.
65 pub fn new(time: DateTime<Utc>, title: impl Into<String>) -> Self {
66 Self {
67 time,
68 title: title.into(),
69 body: String::new(),
70 symbol: None,
71 target: None,
72 }
73 }
74}