From c54bf8ba16d5558082d5337cf24a87f9956c067b Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Mon, 18 Apr 2022 00:18:46 +0200 Subject: [PATCH] Added hide and close latest notification to client (#113) --- completions/bash/swaync-client | 2 ++ completions/fish/swaync-client.fish | 2 ++ completions/zsh/_swaync-client | 4 +++- man/swaync-client.1.scd | 6 ++++++ src/ccDaemon/ccDaemon.vala | 6 ++++++ src/client.vala | 10 ++++++++++ src/notiDaemon/notiDaemon.vala | 8 ++++++++ src/notiWindow/notiWindow.vala | 21 +++++++++++++++++++++ 8 files changed, 58 insertions(+), 1 deletion(-) diff --git a/completions/bash/swaync-client b/completions/bash/swaync-client index 36247e0..30173d2 100644 --- a/completions/bash/swaync-client +++ b/completions/bash/swaync-client @@ -30,6 +30,8 @@ _swaync-client() { --toggle-dnd --get-dnd --count + --hide-latest + --close-latest --close-all --skip-wait --subscribe diff --git a/completions/fish/swaync-client.fish b/completions/fish/swaync-client.fish index e7a750e..253fb48 100644 --- a/completions/fish/swaync-client.fish +++ b/completions/fish/swaync-client.fish @@ -9,6 +9,8 @@ complete -c swaync-client -s cp -l close-panel --description "Closes the notific complete -c swaync-client -s d -l toggle-dnd --description "Toggle and print the current dnd state" -r complete -c swaync-client -s D -l get-dnd --description "Print the current dnd state" -r complete -c swaync-client -s c -l count --description "Print the current notificaion count" -r +complete -c swaync-client -l hide-latest --description "Hides latest notification. Still shown in Control Center" -r +complete -c swaync-client -l close-latest --description "Closes latest notification" -r complete -c swaync-client -s C -l close-all --description "Closes all notifications" -r complete -c swaync-client -s sw -l skip-wait --description "Doesn't wait when swaync hasn't been started" -r complete -c swaync-client -s s -l subscribe --description "Subscribe to notificaion add and close events" -r diff --git a/completions/zsh/_swaync-client b/completions/zsh/_swaync-client index 43b0a9a..89d45f6 100644 --- a/completions/zsh/_swaync-client +++ b/completions/zsh/_swaync-client @@ -11,7 +11,9 @@ _arguments -s \ '(-d --toggle-dnd)'{-d,--toggle-dnd}'[Toggle and print the current dnd state]' \ '(-D --get-dnd)'{-D,--get-dnd}'[Print the current dnd state]' \ '(-c --count)'{-c,--count}'[Print the current notificaion count]' \ - '(-C --close-all)'{-C,--close-all}'[Closes all notifications]' \ + '(--hide-latest)'{--hide-latest}'[Closes all notifications]' \ + '(--close-latest)'{--close-latest}'[Hides latest notification. Still shown in Control Center]' \ + '(-C --close-all)'{-C,--close-all}'[Closes latest notification]' \ '(-sw --skip-wait)'{-sw,--skip-wait}"[Doesn't wait when swaync hasn't been started]" \ '(-s --subscribe)'{-s,--subscribe}'[Subscribe to notificaion add and close events]' \ '(-swb --subscribe-waybar)'{-swb,--subscribe-waybar}'[Subscribe to notificaion add and close events with waybar support. Read README for example]' \ diff --git a/man/swaync-client.1.scd b/man/swaync-client.1.scd index d344a9c..9adfe9e 100644 --- a/man/swaync-client.1.scd +++ b/man/swaync-client.1.scd @@ -40,6 +40,12 @@ swaync-client - Client executable *-c, --count* Print the current notificaion count +*--hide-latest* + Hides latest notification. Still shown in Control Center + +*--close-latest* + Closes latest notification + *-C, --close-all* Closes all notifications diff --git a/src/ccDaemon/ccDaemon.vala b/src/ccDaemon/ccDaemon.vala index 2dd70b2..989a660 100644 --- a/src/ccDaemon/ccDaemon.vala +++ b/src/ccDaemon/ccDaemon.vala @@ -70,6 +70,12 @@ namespace SwayNotificationCenter { return controlCenter.get_visibility (); } + /** Closes latest popup notification */ + public void hide_latest_notifications (bool close) + throws DBusError, IOError { + notiDaemon.hide_latest_notification (close); + } + /** Closes all popup and controlcenter notifications */ public void close_all_notifications () throws DBusError, IOError { notiDaemon.close_all_notifications (); diff --git a/src/client.vala b/src/client.vala index 6d7a2e2..05f41e4 100644 --- a/src/client.vala +++ b/src/client.vala @@ -5,6 +5,8 @@ interface CcDaemon : GLib.Object { public abstract void reload_config () throws Error; + public abstract void hide_latest_notifications (bool close) throws DBusError, IOError; + public abstract void close_all_notifications () throws DBusError, IOError; public abstract uint notification_count () throws DBusError, IOError; @@ -39,6 +41,8 @@ private void print_help (string[] args) { print (@"\t -d, \t --toggle-dnd \t\t Toggle and print the current dnd state\n"); print (@"\t -D, \t --get-dnd \t\t Print the current dnd state\n"); print (@"\t -c, \t --count \t\t Print the current notificaion count\n"); + print (@"\t \t --hide-latest \t\t Hides latest notification. Still shown in Control Center\n"); + print (@"\t \t --close-latest \t Closes latest notification\n"); print (@"\t -C, \t --close-all \t\t Closes all notifications\n"); print (@"\t -sw, \t --skip-wait \t\t Doesn't wait when swaync hasn't been started\n"); print (@"\t -s, \t --subscribe \t\t Subscribe to notificaion add and close events\n"); @@ -118,6 +122,12 @@ public int command_line (string[] args) { case "-c": print (cc_daemon.notification_count ().to_string ()); break; + case "--close-latest": + cc_daemon.hide_latest_notifications (true); + break; + case "--hide-latest": + cc_daemon.hide_latest_notifications (false); + break; case "--close-all": case "-C": cc_daemon.close_all_notifications (); diff --git a/src/notiDaemon/notiDaemon.vala b/src/notiDaemon/notiDaemon.vala index 59a78ec..103a351 100644 --- a/src/notiDaemon/notiDaemon.vala +++ b/src/notiDaemon/notiDaemon.vala @@ -78,6 +78,14 @@ namespace SwayNotificationCenter { ccDaemon.controlCenter.close_all_notifications (); } + /** Closes latest popup notification */ + public void hide_latest_notification (bool close) + throws DBusError, IOError { + uint32 ? id = notiWindow.get_latest_notification (); + if (id == null) return; + manually_close_notification (id, !close); + } + /* * D-Bus Specification * https://specifications.freedesktop.org/notification-spec/latest/ar01s09.html diff --git a/src/notiWindow/notiWindow.vala b/src/notiWindow/notiWindow.vala index 6487d06..0ce9b1e 100644 --- a/src/notiWindow/notiWindow.vala +++ b/src/notiWindow/notiWindow.vala @@ -27,6 +27,11 @@ namespace SwayNotificationCenter { public void close_notification (uint32 id) { notificationWindow.close_notification (id); } + + public uint32 ? get_latest_notification () { + if (!notis.get_realized () || notis.closed) return null; + return notificationWindow.get_latest_notification (); + } } [GtkTemplate (ui = "/org/erikreider/sway-notification-center/notiWindow/notiWindow.ui")] @@ -169,5 +174,21 @@ namespace SwayNotificationCenter { } } } + + public uint32 ? get_latest_notification () { + List children = box.get_children (); + if (children.is_empty ()) return null; + + Gtk.Widget ? child = null; + if (list_reverse) { + child = children.last ().data; + } else { + child = children.first ().data; + } + + if (child == null || !(child is Notification)) return null; + Notification noti = (Notification) child; + return noti.param.applied_id; + } } }