1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! System notifications and the app's badge (`nsh`'s `Notifications`).
//!
//! The app asks the core ([`crate::element::notification_permission`],
//! [`crate::element::show_notification`], ...), and the core asks the
//! platform with [`Command::Notification`](crate::Command::Notification)s on
//! the root ([`NotificationCommand`]), which it answers with the root's
//! [`EventKind::Notification`](crate::EventKind::Notification)
//! ([`NotificationEvent`] gives its payload's fields). Where
//! [`Environment::notifications`](crate::platform::Environment::notifications)
//! is false the core answers itself: `unavailable`, nothing shown.
//!
//! A notification the user pressed is reported the same way, without a
//! request. The press can start the app, so the platform keeps what it has
//! until the core sends [`NotificationCommand::Listen`], which it does once
//! the app listens, and the core keeps presses until the app's listener
//! takes them.
//!
//! The badge is a command of its own and is not answered: it works without a
//! notification on macOS (the Dock's tile) and in an installed web app; iOS
//! needs the user's permission, and Android has no count to set (launchers
//! mark an app that has notifications showing).
use crate::element::PropEnum;
/// Whether the user lets the app show notifications.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PropEnum)]
pub enum NotificationPermission {
/// The user was not asked yet.
#[default]
Default,
/// Notifications show.
Granted,
/// The user refused: only the system's settings change that.
Denied,
/// This platform or build shows none.
Unavailable,
}
impl NotificationPermission {
/// Its name in the platforms' answers.
pub const fn as_str(self) -> &'static str {
match self {
Self::Default => "default",
Self::Granted => "granted",
Self::Denied => "denied",
Self::Unavailable => "unavailable",
}
}
/// The permission named `name` ([`Self::as_str`]).
pub fn from_name(name: &str) -> Option<Self> {
[
Self::Default,
Self::Granted,
Self::Denied,
Self::Unavailable,
]
.into_iter()
.find(|p| p.as_str() == name)
}
}
/// A notification to show.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Notification {
/// The app's name for it: showing another with the same id replaces
/// it, and a press reports it.
pub id: String,
/// Its title.
pub title: String,
/// Its text, `""` for none.
pub body: String,
/// Shows it without a sound or vibration, as the web's `silent` does.
pub silent: bool,
}
impl Notification {
/// A notification called `id`, titled `title`, with `body`.
pub fn new(id: impl Into<String>, title: impl Into<String>, body: impl Into<String>) -> Self {
Self {
id: id.into(),
title: title.into(),
body: body.into(),
silent: false,
}
}
/// Shows it without a sound or vibration ([`Self::silent`]).
pub fn silent(mut self, silent: bool) -> Self {
self.silent = silent;
self
}
}
/// What the core asks of the platform (on the root,
/// [`crate::Command::Notification`]). Sent only where
/// [`Environment::notifications`](crate::platform::Environment::notifications)
/// holds, but for [`Self::SetBadge`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum NotificationCommand {
/// Report the permission as request `id`; with `ask`, ask the user
/// first when they were not asked yet.
Permission {
/// The request.
id: u32,
/// Show the system's prompt when it has not shown yet.
ask: bool,
},
/// Show `notification` and report whether the system took it as
/// request `id`.
Show {
/// The request.
id: u32,
/// What to show.
notification: Notification,
},
/// Remove the notification called this, shown or waiting to show.
Close(String),
/// Report the notifications the user presses, those pressed before now
/// (which started the app) first.
Listen,
/// Shows `count` on the app's icon; 0 clears it. Where the platform has
/// no badge, nothing happens.
SetBadge(u32),
}
/// The fields of a platform's [`EventKind::Notification`](crate::EventKind::Notification)
/// payload (an [`EventPayload::Map`](crate::EventPayload::Map)).
#[non_exhaustive]
pub struct NotificationEvent;
impl NotificationEvent {
/// The answered request (a number), with [`Self::PERMISSION`] or
/// [`Self::SHOWN`].
pub const ID: &'static str = "id";
/// The permission's name ([`NotificationPermission::as_str`]).
pub const PERMISSION: &'static str = "permission";
/// Whether the system took the notification (a boolean).
pub const SHOWN: &'static str = "shown";
/// The id of the notification the user pressed.
pub const PRESSED: &'static str = "pressed";
}