Skip to content

Commit

Permalink
notifications settings
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed Mar 1, 2024
1 parent 6acfc82 commit e768d8a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 38 additions & 9 deletions src/backend/notifications.rs
Original file line number Diff line number Diff line change
@@ -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<Toast>,
Expand All @@ -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) {
Expand Down Expand Up @@ -208,3 +211,29 @@ struct XsoMessage {
sourceApp: Option<Arc<str>>,
alwaysShow: Option<bool>,
}

#[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(())
}
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn def_click_freeze_time_ms() -> u32 {
300
}

fn def_true() -> bool {
pub fn def_true() -> bool {
true
}

Expand Down
39 changes: 39 additions & 0 deletions src/gui/modular/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
backend::{
common::{ColorChannel, OverlaySelector, SystemTask, TaskType},
input::PointerMode,
notifications::save_notifications,
overlay::RelativeTo,
},
overlays::{
Expand Down Expand Up @@ -44,6 +45,8 @@ pub enum Axis {

#[derive(Deserialize, Clone)]
pub enum SystemAction {
ToggleNotificationSounds,
ToggleNotifications,
PlayspaceResetOffset,
PlayspaceFixFloor,
RecalculateExtent,
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/overlays/toast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
37 changes: 35 additions & 2 deletions src/res/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e768d8a

Please sign in to comment.