Hide visible notifications when activating dnd (#233)

* Hide visible notifications when activating dnd

* Fix linting issues

---------

Co-authored-by: Erik Reider <erik.reider@protonmail.com>
This commit is contained in:
Erik Reider
2023-03-08 10:19:24 +01:00
committed by GitHub
parent f7daa7ac45
commit 81bf4bdb39
3 changed files with 26 additions and 9 deletions

View File

@@ -17,6 +17,13 @@ namespace SwayNotificationCenter {
this.notify["dnd"].connect (() => on_dnd_toggle (dnd));
on_dnd_toggle.connect ((dnd) => {
if (!dnd || NotificationWindow.is_null) return;
NotificationWindow.instance.close_all_notifications ((noti) => {
return noti.param.urgency != UrgencyLevels.CRITICAL;
});
});
// Init dnd from gsettings
self_settings.bind ("dnd-state", this, "dnd", SettingsBindFlags.DEFAULT);
@@ -34,14 +41,12 @@ namespace SwayNotificationCenter {
/** Toggles the current Do Not Disturb state */
public bool toggle_dnd () throws DBusError, IOError {
on_dnd_toggle (dnd = !dnd);
return dnd;
return dnd = !dnd;
}
/** Sets the current Do Not Disturb state */
[DBus (name = "SetDnd")]
public void set_do_not_disturb (bool state) throws DBusError, IOError {
on_dnd_toggle (state);
dnd = state;
}
@@ -63,9 +68,9 @@ namespace SwayNotificationCenter {
NotificationClosed (id, ClosedReasons.DISMISSED);
swaync_daemon.subscribe_v2 (control_center.notification_count (),
dnd,
control_center.get_visibility (),
swaync_daemon.inhibited);
dnd,
control_center.get_visibility (),
swaync_daemon.inhibited);
}
}

View File

@@ -1,6 +1,6 @@
namespace SwayNotificationCenter {
[GtkTemplate (ui = "/org/erikreider/sway-notification-center/notification/notification.ui")]
private class Notification : Gtk.ListBoxRow {
public class Notification : Gtk.ListBoxRow {
[GtkChild]
unowned Gtk.Revealer revealer;
[GtkChild]

View File

@@ -23,6 +23,12 @@ namespace SwayNotificationCenter {
}
}
public static bool is_null {
get {
return window == null;
}
}
[GtkChild]
unowned Gtk.ScrolledWindow scrolled_window;
[GtkChild]
@@ -122,10 +128,16 @@ namespace SwayNotificationCenter {
}
}
public void close_all_notifications () {
/** Return true to remove notification, false to skip */
public delegate bool remove_iter_func (Notification notification);
public void close_all_notifications (remove_iter_func ? func = null) {
if (!this.get_realized ()) return;
foreach (var w in box.get_children ()) {
remove_notification ((Notification) w, false);
Notification notification = (Notification) w;
if (func == null || func (notification)) {
remove_notification (notification, false);
}
}
}