toggleButton: change "active" field to be a command

This commit is contained in:
2023-09-15 13:52:54 +00:00
parent d9a0d938b8
commit f5d9405e04
6 changed files with 67 additions and 24 deletions

View File

@@ -377,9 +377,9 @@ config file to be able to detect config errors
Toggle buttons receive the '.active' css class ++ Toggle buttons receive the '.active' css class ++
enum: ["normal", "toggle"] ++ enum: ["normal", "toggle"] ++
active: ++ active: ++
type: bool ++ type: string ++
default: false ++ default: "" ++
description: Wether the toggle button is active as default or not ++ description: Command to run to test if button should be active (return code 0) or not ++
description: A list of actions containing a label and a command ++ description: A list of actions containing a label and a command ++
description: A button to reveal a dropdown with action-buttons ++ description: A button to reveal a dropdown with action-buttons ++
buttons#<name>: ++ buttons#<name>: ++
@@ -412,9 +412,9 @@ config file to be able to detect config errors
Toggle buttons receive the '.active' css class ++ Toggle buttons receive the '.active' css class ++
enum: ["normal", "toggle"] ++ enum: ["normal", "toggle"] ++
active: ++ active: ++
type: bool ++ type: string ++
default: false ++ default: "" ++
description: Wether the toggle button is active as default or not ++ description: Command to run to test if button should be active (return code 0) or not ++
description: A list of actions containing a label and a command ++ description: A list of actions containing a label and a command ++
description: A list of buttons to be displayed in the menu-button-bar ++ description: A list of buttons to be displayed in the menu-button-bar ++
*buttons-grid*++ *buttons-grid*++
@@ -442,9 +442,9 @@ config file to be able to detect config errors
Toggle buttons receive the '.active' css class ++ Toggle buttons receive the '.active' css class ++
enum: ["normal", "toggle"] ++ enum: ["normal", "toggle"] ++
active: ++ active: ++
type: bool ++ type: string ++
default: false ++ default: "" ++
description: Wether the toggle button is active as default or not ++ description: Command to run to test if button should be active (return code 0) or not ++
description: A list of actions containing a label and a command ++ description: A list of actions containing a label and a command ++
description: A grid of buttons that execute shell commands ++ description: A grid of buttons that execute shell commands ++
#START pulse-audio #START pulse-audio

View File

@@ -430,9 +430,9 @@
"enum": ["normal", "toggle"] "enum": ["normal", "toggle"]
}, },
"active": { "active": {
"type": "bool", "type": "string",
"description": "Wether the toggle button is active as default or not", "description": "Command to run to test if button should be active",
"default": false "default": ""
} }
} }
} }

View File

@@ -105,7 +105,7 @@ namespace SwayNotificationCenter.Widgets {
string command = actions.get_object_element (i).get_string_member_with_default ("command", ""); 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"); string t = actions.get_object_element (i).get_string_member_with_default ("type", "normal");
ButtonType type = ButtonType.parse (t); ButtonType type = ButtonType.parse (t);
bool active = actions.get_object_element (i).get_boolean_member_with_default ("active", false); string active = actions.get_object_element (i).get_string_member_with_default ("active", "");
res[i] = Action () { res[i] = Action () {
label = label, label = label,
command = command, command = command,

View File

@@ -10,6 +10,7 @@ namespace SwayNotificationCenter.Widgets {
} }
Action[] actions; Action[] actions;
ToggleButton[] toggle_buttons;
public ButtonsGrid (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) { public ButtonsGrid (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) {
base (suffix, swaync_daemon, noti_daemon); base (suffix, swaync_daemon, noti_daemon);
@@ -27,7 +28,9 @@ namespace SwayNotificationCenter.Widgets {
// add action to container // add action to container
foreach (var act in actions) { foreach (var act in actions) {
if (act.type == ButtonType.TOGGLE) { if (act.type == ButtonType.TOGGLE) {
Gtk.ToggleButton tb = new ToggleButton (act.label, act.command, act.active); ToggleButton toggle_button = new ToggleButton (act.label, act.command, act.active);
toggle_buttons += toggle_button;
Gtk.ToggleButton tb = toggle_button;
container.insert (tb, -1); container.insert (tb, -1);
} else { } else {
Gtk.Button b = new Gtk.Button.with_label (act.label); Gtk.Button b = new Gtk.Button.with_label (act.label);
@@ -38,5 +41,14 @@ namespace SwayNotificationCenter.Widgets {
show_all (); show_all ();
} }
public override void on_cc_visibility_change (bool val) {
debug("buttonsGrid:on_cc_visibility_change %i", (int)val);
if (val) {
foreach (var tb in toggle_buttons) {
tb.compute_state();
}
}
}
} }
} }

View File

@@ -26,7 +26,7 @@ namespace SwayNotificationCenter.Widgets {
string ? label; string ? label;
string ? command; string ? command;
BaseWidget.ButtonType ? type; BaseWidget.ButtonType ? type;
bool ? active; string ? active;
} }
public class Menubar : BaseWidget { public class Menubar : BaseWidget {

View File

@@ -1,25 +1,56 @@
namespace SwayNotificationCenter.Widgets { namespace SwayNotificationCenter.Widgets {
class ToggleButton : Gtk.ToggleButton { class ToggleButton : Gtk.ToggleButton {
private bool _active;
private string command; private string command;
public ToggleButton (string label, string command, bool active) { private string active_test;
public ToggleButton (string label, string command, string active) {
this.command = command; this.command = command;
this.active_test = active;
this.label = label; this.label = label;
this._active = false;
if (active) { debug("ToggleButton created with test: %s", active);
this.get_style_context ().add_class ("active");
this.active = true;
}
this.toggled.connect (on_toggle); this.toggled.connect (on_toggle);
} }
public void compute_state() {
debug("compute_state");
this.set_active(this.check_should_be_active());
}
private void set_active(bool active) {
debug("set_active: %i", (int)active);
if (active) {
this.get_style_context ().add_class ("active");
this._active = true;
} else {
this.get_style_context ().remove_class ("active");
this._active = false;
}
}
private void on_toggle (Gtk.ToggleButton tb) { private void on_toggle (Gtk.ToggleButton tb) {
debug("on_toggle");
this.set_active(!this._active);
BaseWidget.execute_command (command); BaseWidget.execute_command (command);
if (tb.active) }
tb.get_style_context ().add_class ("active");
else private bool check_should_be_active () {
tb.get_style_context ().remove_class ("active"); string stdout;
string stderr;
int end_status;
Process.spawn_sync (
"/",
this.active_test.split (" "),
Environ.get(),
SpawnFlags.SEARCH_PATH,
null,
out stdout,
out stderr,
out end_status);
return end_status == 0;
} }
} }
} }