Action scripts (#194)

Co-authored-by: auctumnus <auctumnus@users.noreply.github.com>
Closes https://github.com/ErikReider/SwayNotificationCenter/issues/192
This commit is contained in:
autumn
2023-01-03 11:56:59 -06:00
committed by GitHub
parent 18b72b9b8c
commit 8a1ffcfd6f
7 changed files with 46 additions and 2 deletions

View File

@@ -180,6 +180,8 @@ Scripting rules and logic:
. If any of the properties doesn't match, the script will be skipped
. If a notification doesn't include one of the properties, that property will
be skipped
· If a script has `run-on` set to `action`, the script will only run when an
action is taken on the notification
More information can be found in the `swaync(5)` man page

View File

@@ -333,6 +333,13 @@ config file to be able to detect config errors
type: string ++
optional: true ++
description: Which category the notification belongs to. Uses Regex.++
*run-on*++
type: string ++
optional: true ++
values: action, receive ++
default: receive ++
description: Whether to run this action when the notification is ++
received, or when an action is taken on it. ++
description: Which scripts to check and potentially run for every ++
notification. If the notification doesn't include one of the properties, ++
that property will be ignored. All properties (except for exec) use regex. ++

View File

@@ -28,6 +28,11 @@
"example-script": {
"exec": "echo 'Do something...'",
"urgency": "Normal"
},
"example-action-script": {
"exec": "echo 'Do something actionable!'",
"urgency": "Normal",
"run-on": "action"
}
},
"notification-visibility": {

View File

@@ -204,9 +204,15 @@ namespace SwayNotificationCenter {
public NotificationStatusEnum state { get; set; }
}
public enum ScriptRunOnType {
ACTION,
RECEIVE;
}
#if WANT_SCRIPTING
public class Script : NotificationMatching {
public string ? exec { get; set; default = null; }
public ScriptRunOnType run_on { get; set; default = ScriptRunOnType.RECEIVE; }
public async bool run_script (NotifyParams param, out string msg) {
msg = "";

View File

@@ -176,6 +176,12 @@
"category": {
"type": "string",
"description": "Which category the notification belongs to. Uses Regex."
},
"run-on": {
"type": "string",
"description": "Whether to run the script on an action being activated, or when the notification is received.",
"enum": ["action", "receive"],
"default": "receive"
}
}
}

View File

@@ -195,9 +195,28 @@ namespace SwayNotificationCenter {
// Run the first script if notification meets requirements
HashTable<string, Script> scripts = ConfigModel.instance.scripts;
if (scripts.length == 0) return id;
this.run_scripts (param, ScriptRunOnType.RECEIVE);
#endif
return id;
}
/**
* Runs scripts that meet the requirements of the given `param`.
*/
[DBus (visible = false)]
public void run_scripts (NotifyParams param, ScriptRunOnType run_on) {
#if WANT_SCRIPTING
if (param.swaync_no_script) {
debug ("Skipped action scripts for this notification\n");
return;
}
// Run the first script if notification meets requirements
HashTable<string, Script> scripts = ConfigModel.instance.scripts;
if (scripts.length == 0) return;
foreach (string key in scripts.get_keys ()) {
unowned Script script = scripts[key];
if (!script.matches_notification (param)) continue;
if (script.run_on != run_on) continue;
script.run_script.begin (param, (obj, res) => {
// Gets the end status
@@ -238,8 +257,6 @@ namespace SwayNotificationCenter {
break;
}
#endif
return id;
}
/**

View File

@@ -298,6 +298,7 @@ namespace SwayNotificationCenter {
}
private void action_clicked (Action ? action, bool is_default = false) {
noti_daemon.run_scripts (param, ScriptRunOnType.ACTION);
if (action != null
&& action.identifier != null
&& action.identifier != "") {