From e768d8a83fe8d20d64c94295453c079026b71791 Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Fri, 1 Mar 2024 16:25:05 +0100 Subject: [PATCH] notifications settings --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/backend/notifications.rs | 47 +++++++++++++++++++++++++++++------- src/config.rs | 2 +- src/gui/modular/button.rs | 39 ++++++++++++++++++++++++++++++ src/overlays/toast.rs | 2 +- src/res/settings.yaml | 37 ++++++++++++++++++++++++++-- 7 files changed, 116 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e24bd86..f131ea3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3722,7 +3722,7 @@ dependencies = [ [[package]] name = "wlx-overlay-s" -version = "0.2.2" +version = "0.2.3" dependencies = [ "anyhow", "ash", diff --git a/Cargo.toml b/Cargo.toml index 9a41b42..15ea20a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ debug = true [package] name = "wlx-overlay-s" -version = "0.2.2" +version = "0.2.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/backend/notifications.rs b/src/backend/notifications.rs index 520e878..7bd3630 100644 --- a/src/backend/notifications.rs +++ b/src/backend/notifications.rs @@ -1,14 +1,12 @@ use dbus::{blocking::Connection, channel::MatchingReceiver, message::MatchRule}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::{ - sync::{ - mpsc::{self}, - Arc, - }, + path::PathBuf, + sync::{mpsc, Arc}, time::Duration, }; -use crate::{overlays::toast::Toast, state::AppState}; +use crate::{config::def_true, config_io, overlays::toast::Toast, state::AppState}; pub struct NotificationManager { rx_toast: mpsc::Receiver, @@ -31,9 +29,14 @@ impl NotificationManager { let _ = c.process(Duration::ZERO); } - self.rx_toast.try_iter().for_each(|toast| { - toast.submit(app); - }); + if app.session.config.notifications_enabled { + self.rx_toast.try_iter().for_each(|toast| { + toast.submit(app); + }); + } else { + // consume without submitting + self.rx_toast.try_iter().last(); + } } pub fn run_dbus(&mut self) { @@ -208,3 +211,29 @@ struct XsoMessage { sourceApp: Option>, alwaysShow: Option, } + +#[derive(Deserialize, Serialize)] +pub struct NotifiConf { + #[serde(default = "def_true")] + pub notifications_enabled: bool, + + #[serde(default = "def_true")] + pub notifications_sound_enabled: bool, +} + +fn get_config_path() -> PathBuf { + let mut path = config_io::get_conf_d_path(); + path.push("notifications.yaml"); + path +} +pub fn save_notifications(app: &mut AppState) -> anyhow::Result<()> { + let conf = NotifiConf { + notifications_enabled: app.session.config.notifications_enabled, + notifications_sound_enabled: app.session.config.notifications_sound_enabled, + }; + + let yaml = serde_yaml::to_string(&conf)?; + std::fs::write(get_config_path(), yaml)?; + + Ok(()) +} diff --git a/src/config.rs b/src/config.rs index 696fb90..ed7660f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -30,7 +30,7 @@ fn def_click_freeze_time_ms() -> u32 { 300 } -fn def_true() -> bool { +pub fn def_true() -> bool { true } diff --git a/src/gui/modular/button.rs b/src/gui/modular/button.rs index 4d18009..5b98687 100644 --- a/src/gui/modular/button.rs +++ b/src/gui/modular/button.rs @@ -15,6 +15,7 @@ use crate::{ backend::{ common::{ColorChannel, OverlaySelector, SystemTask, TaskType}, input::PointerMode, + notifications::save_notifications, overlay::RelativeTo, }, overlays::{ @@ -44,6 +45,8 @@ pub enum Axis { #[derive(Deserialize, Clone)] pub enum SystemAction { + ToggleNotificationSounds, + ToggleNotifications, PlayspaceResetOffset, PlayspaceFixFloor, RecalculateExtent, @@ -315,10 +318,46 @@ fn run_system(action: &SystemAction, app: &mut AppState) { SystemAction::RecalculateExtent => { todo!() } + SystemAction::ToggleNotifications => { + app.session.config.notifications_enabled = !app.session.config.notifications_enabled; + Toast::new( + format!( + "Notifications are {}.", + if app.session.config.notifications_enabled { + "enabled" + } else { + "disabled" + } + ) + .into(), + "".into(), + ) + .submit(app); + } + SystemAction::ToggleNotificationSounds => { + app.session.config.notifications_sound_enabled = + !app.session.config.notifications_sound_enabled; + Toast::new( + format!( + "Notification sounds are {}.", + if app.session.config.notifications_sound_enabled { + "enabled" + } else { + "disabled" + } + ) + .into(), + "".into(), + ) + .submit(app); + } SystemAction::PersistConfig => { if let Err(e) = save_watch(app) { log::error!("Failed to save watch config: {:?}", e); }; + if let Err(e) = save_notifications(app) { + log::error!("Failed to save notifications config: {:?}", e); + } } } } diff --git a/src/overlays/toast.rs b/src/overlays/toast.rs index aae17c7..c6ed9ca 100644 --- a/src/overlays/toast.rs +++ b/src/overlays/toast.rs @@ -65,7 +65,7 @@ impl Toast { let destroy_at = instant.add(std::time::Duration::from_secs_f32(self.timeout)); - let has_sound = self.sound; + let has_sound = self.sound && app.session.config.notifications_sound_enabled; app.tasks.enqueue_at( TaskType::CreateOverlay( diff --git a/src/res/settings.yaml b/src/res/settings.yaml index 6fb0012..c56d09c 100644 --- a/src/res/settings.yaml +++ b/src/res/settings.yaml @@ -4,7 +4,7 @@ width: 0.3 -size: [600, 520] +size: [600, 700] # +X: right, +Y: up, +Z: back spawn_pos: [0, -0.1, -0.5] @@ -502,12 +502,45 @@ elements: - type: System action: PlayspaceResetOffset + ####### Notifications Section ####### + - type: Panel rect: [50, 460, 500, 1] bg_color: "#c0c0c0" + - type: Label + rect: [325, 480, 90, 24] + font_size: 18 + fg_color: "#ffffff" + source: Static + text: Notifications + + - type: Button + rect: [330, 495, 220, 30] + font_size: 12 + fg_color: "#ffffff" + bg_color: "#606020" + text: "Enabled" + click_down: + - type: System + action: ToggleNotifications + + - type: Button + rect: [330, 545, 220, 30] + font_size: 12 + fg_color: "#ffffff" + bg_color: "#606020" + text: "Sound Enabled" + click_down: + - type: System + action: ToggleNotificationSounds + + - type: Panel + rect: [50, 595, 500, 1] + bg_color: "#c0c0c0" + - type: Button - rect: [330, 480, 220, 30] + rect: [330, 615, 220, 30] font_size: 12 fg_color: "#ffffff" bg_color: "#206060"