diff --git a/src/controlCenter/widgets/baseWidget.vala b/src/controlCenter/widgets/baseWidget.vala index 2728485c..5f687353 100644 --- a/src/controlCenter/widgets/baseWidget.vala +++ b/src/controlCenter/widgets/baseWidget.vala @@ -16,6 +16,16 @@ namespace SwayNotificationCenter.Widgets { public unowned SwayncDaemon swaync_daemon; public unowned NotiDaemon noti_daemon; + public enum ButtonType { + TOGGLE, + NORMAL; + + public static ButtonType parse (string value) { + if (value == "toggle") return ButtonType.TOGGLE; + return ButtonType.NORMAL; + } + } + protected BaseWidget (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) { this.suffix = suffix; this.key = widget_name + (suffix.length > 0 ? "#%s".printf (suffix) : ""); @@ -93,15 +103,20 @@ namespace SwayNotificationCenter.Widgets { for (int i = 0; i < actions.get_length (); i++) { string label = actions.get_object_element (i).get_string_member_with_default ("label", "label"); 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); + bool active = actions.get_object_element (i).get_boolean_member_with_default ("active", false); res[i] = Action () { label = label, - command = command + command = command, + type = type, + active = active }; } return res; } - protected void execute_command (string cmd) { + public static void execute_command (string cmd) { pid_t pid; int status; if ((pid = fork ()) < 0) { diff --git a/src/controlCenter/widgets/buttonsGrid/buttonsGrid.vala b/src/controlCenter/widgets/buttonsGrid/buttonsGrid.vala index d8f88b01..05fd7842 100644 --- a/src/controlCenter/widgets/buttonsGrid/buttonsGrid.vala +++ b/src/controlCenter/widgets/buttonsGrid/buttonsGrid.vala @@ -26,11 +26,14 @@ namespace SwayNotificationCenter.Widgets { // add action to container foreach (var act in actions) { - Gtk.Button b = new Gtk.Button.with_label (act.label); - - b.clicked.connect (() => execute_command (act.command)); - - container.insert (b, -1); + if (act.type == ButtonType.TOGGLE) { + Gtk.ToggleButton tb = new ToggleButton (act.label, act.command, act.active); + container.insert (tb, -1); + } else { + Gtk.Button b = new Gtk.Button.with_label (act.label); + b.clicked.connect (() => execute_command (act.command)); + container.insert (b, -1); + } } show_all (); diff --git a/src/controlCenter/widgets/menubar/menubar.vala b/src/controlCenter/widgets/menubar/menubar.vala index a5d55e28..17af3758 100644 --- a/src/controlCenter/widgets/menubar/menubar.vala +++ b/src/controlCenter/widgets/menubar/menubar.vala @@ -25,6 +25,8 @@ namespace SwayNotificationCenter.Widgets { public struct Action { string ? label; string ? command; + BaseWidget.ButtonType ? type; + bool ? active; } public class Menubar : BaseWidget { @@ -69,16 +71,19 @@ 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) { - Gtk.Button b = new Gtk.Button.with_label (a.label); - - b.clicked.connect (() => execute_command (a.command)); - - container.add (b); + if (a.type == ButtonType.TOGGLE) { + ToggleButton tb = new ToggleButton (a.label, a.command, a.active); + container.add (tb); + } else { + Gtk.Button b = new Gtk.Button.with_label (a.label); + b.clicked.connect (() => execute_command (a.command)); + container.add (b); + } } switch (obj.position) { case Position.LEFT: diff --git a/src/controlCenter/widgets/shared/toggleButton.vala b/src/controlCenter/widgets/shared/toggleButton.vala new file mode 100644 index 00000000..893a5837 --- /dev/null +++ b/src/controlCenter/widgets/shared/toggleButton.vala @@ -0,0 +1,25 @@ +namespace SwayNotificationCenter.Widgets { + class ToggleButton : Gtk.ToggleButton { + + private string command; + public ToggleButton (string label, string command, bool active) { + this.command = command; + this.label = label; + + if (active) { + this.get_style_context ().add_class ("active"); + this.active = true; + } + + this.toggled.connect (on_toggle); + } + + private void on_toggle (Gtk.ToggleButton tb) { + BaseWidget.execute_command (command); + if (tb.active) + tb.get_style_context ().add_class ("active"); + else + tb.get_style_context ().remove_class ("active"); + } + } +} diff --git a/src/meson.build b/src/meson.build index b3d200bc..8a62739a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -26,6 +26,7 @@ widget_sources = [ # Helpers 'controlCenter/widgets/baseWidget.vala', 'controlCenter/widgets/factory.vala', + 'controlCenter/widgets/shared/toggleButton.vala', # Widget: Title 'controlCenter/widgets/title/title.vala', # Widget: Dnd