Skip to content

Commit

Permalink
Add state update command for toggle buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
JannisPetschenka committed Oct 12, 2023
1 parent c3f1c1d commit 09bfebb
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 15 deletions.
12 changes: 12 additions & 0 deletions man/swaync.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ config file to be able to detect config errors
description: Type of the button. ++
Toggle buttons receive the '.active' css class ++
enum: ["normal", "toggle"] ++
update-command: ++
type: string ++
default: "" ++
description: "Command to be executed on visibility change of ++
cc to update the active state of the toggle button (should ++
echo true or false)" ++
active: ++
type: bool ++
default: false ++
Expand Down Expand Up @@ -420,6 +426,12 @@ config file to be able to detect config errors
variable "SWAYNC_TOGGLE_STATE" is set. See example usage in the ++
default config.json ++
enum: ["normal", "toggle"] ++
update-command: ++
type: string ++
default: "" ++
description: "Command to be executed on visibility change of ++
cc to update the active state of the toggle button (should ++
echo true or false)" ++
active: ++
type: bool ++
default: false ++
Expand Down
3 changes: 2 additions & 1 deletion src/config.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"label": "直",
"type": "toggle",
"active": true,
"command": "sh -c '[[ $SWAYNC_TOGGLE_STATE == true ]] && nmcli radio wifi on || nmcli radio wifi off'"
"command": "sh -c '[[ $SWAYNC_TOGGLE_STATE == true ]] && nmcli radio wifi on || nmcli radio wifi off'",
"update_command": "sh -c '[[ $(nmcli radio wifi) == \"enabled\" ]] && echo true || echo false'"
}
]
}
Expand Down
5 changes: 5 additions & 0 deletions src/configSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@
"default": "normal",
"enum": ["normal", "toggle"]
},
"update-command": {
"type": "string",
"description": "Command to be executed on visibility change of cc to update the active state of the toggle button (should echo true or false)",
"default": ""
},
"active": {
"type": "bool",
"description": "Wether the toggle button is active as default or not",
Expand Down
2 changes: 2 additions & 0 deletions src/controlCenter/widgets/baseWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ namespace SwayNotificationCenter.Widgets {
string command = actions.get_object_element (i).get_string_member_with_default ("command", "");
string t = actions.get_object_element (i).get_string_member_with_default ("type", "normal");
ButtonType type = ButtonType.parse (t);
string update_command = actions.get_object_element (i).get_string_member_with_default ("update-command", "");
bool active = actions.get_object_element (i).get_boolean_member_with_default ("active", false);
res[i] = Action () {
label = label,
command = command,
type = type,
update_command = update_command,
active = active
};
}
Expand Down
12 changes: 11 additions & 1 deletion src/controlCenter/widgets/buttonsGrid/buttonsGrid.vala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace SwayNotificationCenter.Widgets {
}

Action[] actions;
List<ToggleButton> toggle_buttons;

public ButtonsGrid (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) {
base (suffix, swaync_daemon, noti_daemon);
Expand All @@ -27,8 +28,9 @@ namespace SwayNotificationCenter.Widgets {
// add action to container
foreach (var act in actions) {
if (act.type == ButtonType.TOGGLE) {
Gtk.ToggleButton tb = new ToggleButton (act.label, act.command, act.active);
ToggleButton tb = new ToggleButton (act.label, act.command, act.update_command, act.active);
container.insert (tb, -1);
toggle_buttons.append (tb);
} else {
Gtk.Button b = new Gtk.Button.with_label (act.label);
b.clicked.connect (() => execute_command.begin (act.command));
Expand All @@ -38,5 +40,13 @@ namespace SwayNotificationCenter.Widgets {

show_all ();
}

public override void on_cc_visibility_change (bool value) {
if (!value) {
foreach (var tb in toggle_buttons) {
tb.on_update.begin ();
}
}
}
}
}
22 changes: 14 additions & 8 deletions src/controlCenter/widgets/menubar/menubar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace SwayNotificationCenter.Widgets {
string ? label;
string ? command;
BaseWidget.ButtonType ? type;
string ? update_command;
bool ? active;
}

Expand All @@ -40,6 +41,7 @@ namespace SwayNotificationCenter.Widgets {
Gtk.Box topbar_container;

List<ConfigObject ?> menu_objects;
List<ToggleButton> toggle_buttons;

public Menubar (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) {
base (suffix, swaync_daemon, noti_daemon);
Expand Down Expand Up @@ -71,14 +73,15 @@ namespace SwayNotificationCenter.Widgets {

void add_menu (ref unowned ConfigObject ? obj) {
switch (obj.type) {
case MenuType.BUTTONS:
case MenuType.BUTTONS :
Gtk.Box container = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
if (obj.name != null) container.get_style_context ().add_class (obj.name);

foreach (Action a in obj.actions) {
if (a.type == ButtonType.TOGGLE) {
ToggleButton tb = new ToggleButton (a.label, a.command, a.active);
ToggleButton tb = new ToggleButton (a.label, a.command, a.update_command, a.active);
container.add (tb);
toggle_buttons.append (tb);
} else {
Gtk.Button b = new Gtk.Button.with_label (a.label);
b.clicked.connect (() => execute_command.begin (a.command));
Expand Down Expand Up @@ -107,12 +110,12 @@ namespace SwayNotificationCenter.Widgets {
obj.revealer = r;

show_button.clicked.connect (() => {
bool visible = !r.get_reveal_child ();
foreach (var o in menu_objects) {
o.revealer ?.set_reveal_child (false);
}
r.set_reveal_child (visible);
});
bool visible = !r.get_reveal_child ();
foreach (var o in menu_objects) {
o.revealer ?.set_reveal_child (false);
}
r.set_reveal_child (visible);
});

foreach (var a in obj.actions) {
Gtk.Button b = new Gtk.Button.with_label (a.label);
Expand Down Expand Up @@ -215,6 +218,9 @@ namespace SwayNotificationCenter.Widgets {
foreach (var obj in menu_objects) {
obj.revealer ?.set_reveal_child (false);
}
foreach (var tb in toggle_buttons) {
tb.on_update.begin ();
}
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/controlCenter/widgets/shared/toggleButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ namespace SwayNotificationCenter.Widgets {
class ToggleButton : Gtk.ToggleButton {

private string command;
private string update_command;

public ToggleButton (string label, string command, bool active) {
public ToggleButton (string label, string command, string update_command, bool active) {
this.command = command;
this.update_command = update_command;
this.label = label;

if (active) {
Expand All @@ -19,5 +21,24 @@ namespace SwayNotificationCenter.Widgets {
string[] env_additions = { "SWAYNC_TOGGLE_STATE=" + this.active.to_string () };
yield Functions.execute_command (this.command, env_additions, out msg);
}

public async void on_update () {
if (update_command == "") return;
string msg = "";
string[] env_additions = { "SWAYNC_TOGGLE_STATE=" + this.active.to_string () };
yield Functions.execute_command (this.update_command, env_additions, out msg);
try {
// remove trailing whitespaces
Regex regex = new Regex ("\\s+$");
string res = regex.replace(msg, msg.length, 0, "");
if (res.up() == "TRUE") {
this.active = true;
} else {
this.active = false;
}
} catch (RegexError e) {
stderr.printf ("RegexError: %s\n", e.message);
}
}
}
}
34 changes: 30 additions & 4 deletions src/functions.vala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace SwayNotificationCenter {
if (custom_path != null && custom_path.length > 0) {
// Replaces the home directory relative path with a absolute path
if (custom_path.get (0) == '~') {
custom_path = Environment.get_home_dir () + custom_path[1:];
custom_path = Environment.get_home_dir () + custom_path[1 :];
}
paths += custom_path;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ namespace SwayNotificationCenter {
if (custom_path != null && (custom_path = custom_path.strip ()).length > 0) {
// Replaces the home directory relative path with a absolute path
if (custom_path.get (0) == '~') {
custom_path = Environment.get_home_dir () + custom_path[1:];
custom_path = Environment.get_home_dir () + custom_path[1 :];
}

if (File.new_for_path (custom_path).query_exists ()) {
Expand Down Expand Up @@ -313,13 +313,38 @@ namespace SwayNotificationCenter {
Shell.parse_argv (cmd, out argvp);

Pid child_pid;
Process.spawn_async (
int std_input;
int std_output;
int std_err;
Process.spawn_async_with_pipes (
"/",
argvp,
spawn_env,
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
null,
out child_pid);
out child_pid,
out std_input,
out std_output,
out std_err);

// stdout:
string res = "";
IOChannel output = new IOChannel.unix_new (std_output);
output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
if (condition == IOCondition.HUP) {
return false;
}
try {
channel.read_line (out res, null, null);
return true;
} catch (IOChannelError e) {
stderr.printf ("stdout: IOChannelError: %s\n", e.message);
return false;
} catch (ConvertError e) {
stderr.printf ("stdout: ConvertError: %s\n", e.message);
return false;
}
});

// Close the child when the spawned process is idling
int end_status = 0;
Expand All @@ -330,6 +355,7 @@ namespace SwayNotificationCenter {
});
// Waits until `run_script.callback()` is called above
yield;
msg = res;
return end_status == 0;
} catch (Error e) {
stderr.printf ("Run_Script Error: %s\n", e.message);
Expand Down

0 comments on commit 09bfebb

Please sign in to comment.