mod policy;
use std::collections::HashMap;
use gpui_kit::{App, Global, SharedString};
pub use policy::{Mode, Preferences, Sound, Style};
pub const SOUNDS_KEY: &str = "notifications.sounds";
pub fn sounds_enabled(cx: &App) -> bool {
crate::prefs::get(cx, SOUNDS_KEY).as_deref() != Some("0")
}
pub struct Sink(pub fn(&str, u64, Notice, &mut App));
impl Global for Sink {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Notice {
text: SharedString,
system_body: Option<SharedString>,
}
impl Notice {
pub fn new(text: impl Into<SharedString>) -> Self {
Self {
text: text.into(),
system_body: None,
}
}
pub fn described(text: impl Into<SharedString>, body: impl Into<SharedString>) -> Self {
Self {
text: text.into(),
system_body: Some(body.into()),
}
}
pub fn text(&self) -> &SharedString {
&self.text
}
pub fn system_body(&self) -> Option<&SharedString> {
self.system_body.as_ref()
}
}
#[derive(Default)]
pub struct Defaults(HashMap<String, Preferences>);
impl Global for Defaults {}
pub fn install_defaults<'a>(
boards: impl IntoIterator<Item = (&'a str, Preferences)>,
cx: &mut App,
) {
let declared = boards
.into_iter()
.map(|(source, preferences)| (source.to_owned(), preferences))
.collect();
cx.set_global(Defaults(declared));
}
pub fn request(source: &str, target: u64, notice: Notice, cx: &mut App) {
if preferences(source, cx).mode == Mode::Off {
return;
}
if let Some(sink) = cx.try_global::<Sink>().map(|sink| sink.0) {
sink(source, target, notice, cx);
}
}
pub fn preferences(source: &str, cx: &App) -> Preferences {
let stored = |name| crate::prefs::get(cx, &key(source, name));
let saved = stored("mode").map(|mode| {
Preferences::decode(
Some(&mode),
stored("style").as_deref(),
stored("sound").as_deref(),
)
});
let marked = stored(CHOSEN).is_some();
chosen(saved, marked)
.unwrap_or_else(|| declared(source, cx))
.playable()
}
const CHOSEN: &str = "chosen";
fn chosen(saved: Option<Preferences>, marked: bool) -> Option<Preferences> {
saved.filter(|saved| marked || *saved != Preferences::default())
}
fn declared(source: &str, cx: &App) -> Preferences {
cx.try_global::<Defaults>()
.and_then(|defaults| defaults.0.get(source).copied())
.unwrap_or_default()
}
fn key(source: &str, name: &str) -> String {
format!("notifications.{source}.{name}")
}
impl Preferences {
pub fn save(self, source: &str, cx: &mut App) {
for (name, value) in [
("mode", self.mode.value()),
("style", self.style.value()),
("sound", self.sound.value()),
(CHOSEN, "1"),
] {
crate::prefs::set(cx, &key(source, name), value);
}
}
}
#[cfg(test)]
mod tests {
use super::{Mode, Notice, Preferences, Sound, Style};
#[gpui_kit::test]
fn board_defaults_apply_until_a_choice_is_saved(cx: &mut gpui_kit::TestAppContext) {
let declared = Preferences {
mode: Mode::System,
style: Style::Compact,
sound: Sound::Silent,
};
cx.update(|cx| {
crate::prefs::init_file(None, cx);
super::install_defaults([("fixture", declared)], cx);
assert_eq!(super::preferences("fixture", cx), declared);
assert_eq!(super::preferences("other", cx), Preferences::default());
Preferences::default().save("fixture", cx);
assert_eq!(
super::preferences("fixture", cx),
Preferences::default(),
"a saved Off wins over the board's default"
);
});
}
#[gpui_kit::test]
fn an_unmarked_old_default_reads_as_no_choice(cx: &mut gpui_kit::TestAppContext) {
let declared = Preferences {
mode: Mode::System,
..Preferences::default()
};
cx.update(|cx| {
crate::prefs::init_file(None, cx);
super::install_defaults([("fixture", declared)], cx);
for (name, value) in [("mode", "off"), ("style", "standard"), ("sound", "Silent")] {
crate::prefs::set(cx, &super::key("fixture", name), value);
}
assert_eq!(super::preferences("fixture", cx), declared);
crate::prefs::set(cx, &super::key("fixture", "mode"), "in-app");
assert_eq!(super::preferences("fixture", cx).mode, Mode::InApp);
});
}
#[test]
fn only_described_notices_carry_text_to_the_operating_system() {
let plain = Notice::new(String::from("Notes: three new notes"));
assert_eq!(plain.text().as_ref(), "Notes: three new notes");
assert_eq!(plain.system_body(), None);
let described = Notice::described("alice mentioned you in #subspace", "Local Ergo");
assert_eq!(
described.text().as_ref(),
"alice mentioned you in #subspace"
);
assert_eq!(
described.system_body().map(|body| body.as_ref()),
Some("Local Ergo")
);
}
#[gpui_kit::test]
fn off_requests_never_reach_the_process_sink(cx: &mut gpui_kit::TestAppContext) {
struct Delivered(Vec<u64>);
impl gpui_kit::Global for Delivered {}
cx.update(|cx| {
crate::prefs::init_file(None, cx);
cx.set_global(Delivered(Vec::new()));
cx.set_global(super::Sink(|_, target, _, cx| {
cx.global_mut::<Delivered>().0.push(target)
}));
super::request("fixture", 1, Notice::new("Generic activity"), cx);
assert!(cx.global::<Delivered>().0.is_empty());
Preferences {
mode: Mode::InApp,
..Preferences::default()
}
.save("fixture", cx);
super::request("fixture", 2, Notice::new("Generic activity"), cx);
assert_eq!(cx.global::<Delivered>().0, [2]);
Preferences::default().save("fixture", cx);
super::request("fixture", 3, Notice::new("Generic activity"), cx);
assert_eq!(cx.global::<Delivered>().0, [2]);
});
}
}