Skip to content

Commit

Permalink
Add toggle button
Browse files Browse the repository at this point in the history
  • Loading branch information
JannisPetschenka committed Aug 14, 2023
1 parent 176e3b7 commit 76bc879
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
19 changes: 17 additions & 2 deletions src/controlCenter/widgets/baseWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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) : "");
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions src/controlCenter/widgets/buttonsGrid/buttonsGrid.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
17 changes: 11 additions & 6 deletions src/controlCenter/widgets/menubar/menubar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace SwayNotificationCenter.Widgets {
public struct Action {
string ? label;
string ? command;
BaseWidget.ButtonType ? type;
bool ? active;
}

public class Menubar : BaseWidget {
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions src/controlCenter/widgets/shared/toggleButton.vala
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76bc879

Please sign in to comment.