Skip to content

Commit

Permalink
Invoke action from swaync-client (implements #437) (#443)
Browse files Browse the repository at this point in the history
* Invoke action from swaync-client

* Rename ACTION_ID to ACTION_INDEX

* Fix formatting

* Change noti_id to action_index

* Change invoke_action to latest_invoke_action

* Fixed nits

---------

Co-authored-by: Erik Reider <[email protected]>
  • Loading branch information
SuperDuperDeou and ErikReider authored Mar 3, 2025
1 parent 20f41dd commit 96eca86
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions man/swaync-client.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ swaync-client - Client executable
*-C, --close-all*
Closes all notifications

*-a, --action [ACTION_INDEX]*
Invokes the action [ACTION_INDEX] (or 0) of the latest notification

*-sw, --skip-wait*
Doesn't wait when swaync hasn't been started

Expand Down
11 changes: 11 additions & 0 deletions src/client.vala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface CcDaemon : Object {

public abstract void set_visibility (bool value) throws DBusError, IOError;

public abstract void latest_invoke_action (uint32 action_index) throws DBusError, IOError;

[DBus (name = "GetSubscribeData")]
public abstract SwayncDaemonData get_subscribe_data () throws Error;

Expand Down Expand Up @@ -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_INDEX]\t Invokes the action [ACTION_INDEX] 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 "
Expand Down Expand Up @@ -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_index = 0;
if ( args.length >= 3 ) {
action_index = int.parse (args[2]);
}
cc_daemon.latest_invoke_action ((uint32) action_index);
break;
case "--get-inhibited":
case "-I":
print (cc_daemon.is_inhibited ().to_string ());
Expand Down
6 changes: 6 additions & 0 deletions src/notiDaemon/notiDaemon.vala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ namespace SwayNotificationCenter {
manually_close_notification (id, !close);
}

/** Activates the `action_index` action of the latest notification */
public void latest_invoke_action (uint32 action_index)
throws DBusError, IOError {
NotificationWindow.instance.latest_notification_action (action_index);
}

/*
* D-Bus Specification
* https://specifications.freedesktop.org/notification-spec/latest/ar01s09.html
Expand Down
17 changes: 17 additions & 0 deletions src/notificationWindow/notificationWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,22 @@ namespace SwayNotificationCenter {
Notification noti = (Notification) child;
return noti.param.applied_id;
}

public void latest_notification_action (uint32 action) {
List<weak Gtk.Widget> 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 ();
}
}
}
6 changes: 6 additions & 0 deletions src/swayncDaemon/swayncDaemon.vala
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ namespace SwayNotificationCenter {
noti_daemon.control_center.close_notification (id, true);
}

/** Activates the `action_index` action of the latest notification */
public void latest_invoke_action (uint32 action_index)
throws DBusError, IOError {
noti_daemon.latest_invoke_action (action_index);
}

/**
* Adds an inhibitor with the Application ID
* (ex: "org.erikreider.swaysettings", "swayidle", etc...).
Expand Down

0 comments on commit 96eca86

Please sign in to comment.