From 0848fa01cfa57e98176c76af7b6635394b10e965 Mon Sep 17 00:00:00 2001 From: SuperDuperDeou <87223140+SuperDuperDeou@users.noreply.github.com> Date: Wed, 19 Jun 2024 09:49:15 +0200 Subject: [PATCH] Invoke action from swaync-client --- man/swaync-client.1.scd | 3 +++ src/client.vala | 11 +++++++++++ src/notiDaemon/notiDaemon.vala | 6 ++++++ src/notificationWindow/notificationWindow.vala | 17 +++++++++++++++++ src/swayncDaemon/swayncDaemon.vala | 6 ++++++ 5 files changed, 43 insertions(+) diff --git a/man/swaync-client.1.scd b/man/swaync-client.1.scd index 9803b07f..761803bb 100644 --- a/man/swaync-client.1.scd +++ b/man/swaync-client.1.scd @@ -70,6 +70,9 @@ swaync-client - Client executable *-C, --close-all* Closes all notifications +*-a, --action [ACTION_ID]* + Invokes the action [ACTION_ID] (or 0) of the latest notification + *-sw, --skip-wait* Doesn't wait when swaync hasn't been started diff --git a/src/client.vala b/src/client.vala index 6bd3189c..8ba0a5c1 100644 --- a/src/client.vala +++ b/src/client.vala @@ -30,6 +30,8 @@ interface CcDaemon : Object { public abstract void set_visibility (bool value) throws DBusError, IOError; + public abstract void invoke_action (uint32 action) throws DBusError, IOError; + [DBus (name = "GetSubscribeData")] public abstract SwayncDaemonData get_subscribe_data () throws Error; @@ -69,6 +71,7 @@ private void print_help (string[] args) { print (" \t --hide-latest \t\t\t Hides latest notification. Still shown in Control Center\n"); print (" \t --close-latest \t\t Closes latest notification\n"); print (" -C, \t --close-all \t\t\t Closes all notifications\n"); + print (" -a, \t --action [ACTION_ID]\t\t Invokes the action [ACTION_ID] of the latest notification\n"); print (" -sw, \t --skip-wait \t\t\t Doesn't wait when swaync hasn't been started\n"); print (" -s, \t --subscribe \t\t\t Subscribe to notification add and close events\n"); print (" -swb, --subscribe-waybar \t\t Subscribe to notification add and close events " @@ -190,6 +193,14 @@ public int command_line (string[] args) { cc_daemon.set_dnd (false); print (cc_daemon.get_dnd ().to_string ()); break; + case "--action": + case "-a": + int action = 0; + if ( args.length >= 3 ) { + action = int.parse(args[2]); + } + cc_daemon.invoke_action ((uint32) action); + break; case "--get-inhibited": case "-I": print (cc_daemon.is_inhibited ().to_string ()); diff --git a/src/notiDaemon/notiDaemon.vala b/src/notiDaemon/notiDaemon.vala index 6bac7f45..570f201f 100644 --- a/src/notiDaemon/notiDaemon.vala +++ b/src/notiDaemon/notiDaemon.vala @@ -88,6 +88,12 @@ namespace SwayNotificationCenter { manually_close_notification (id, !close); } + /** Activates the `noti_id` action of the latest notification */ + public void invoke_action (uint32 noti_id) + throws DBusError, IOError { + NotificationWindow.instance.latest_notification_action (noti_id); + } + /* * D-Bus Specification * https://specifications.freedesktop.org/notification-spec/latest/ar01s09.html diff --git a/src/notificationWindow/notificationWindow.vala b/src/notificationWindow/notificationWindow.vala index d35c7813..8e41b9d7 100644 --- a/src/notificationWindow/notificationWindow.vala +++ b/src/notificationWindow/notificationWindow.vala @@ -270,5 +270,22 @@ namespace SwayNotificationCenter { Notification noti = (Notification) child; return noti.param.applied_id; } + + public void latest_notification_action (uint32 action) { + List children = box.get_children (); + if (children.is_empty ()) return; + + Gtk.Widget ? child = null; + if (list_reverse) { + child = children.last ().data; + } else { + child = children.first ().data; + } + + if (child == null || !(child is Notification)) return; + Notification noti = (Notification) child; + noti.click_alt_action (action); + noti.close_notification (); + } } } diff --git a/src/swayncDaemon/swayncDaemon.vala b/src/swayncDaemon/swayncDaemon.vala index 70808758..0d96d875 100644 --- a/src/swayncDaemon/swayncDaemon.vala +++ b/src/swayncDaemon/swayncDaemon.vala @@ -254,6 +254,12 @@ namespace SwayNotificationCenter { noti_daemon.control_center.close_notification (id, true); } + /** Activates the `noti_id` action of the latest notification */ + public void invoke_action (uint32 noti_id) + throws DBusError, IOError { + noti_daemon.invoke_action (noti_id); + } + /** * Adds an inhibitor with the Application ID * (ex: "org.erikreider.swaysettings", "swayidle", etc...).