Compare commits

3 Commits
main ... dev-2

9 changed files with 152 additions and 14 deletions

View File

@@ -370,6 +370,16 @@ config file to be able to detect config errors
type: string ++
default: "" ++
description: "Command to be executed on click" ++
type: ++
type: string ++
default: "normal" ++
description: Type of the button. ++
Toggle buttons receive the '.active' css class ++
enum: ["normal", "toggle"] ++
active: ++
type: string ++
default: "" ++
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 button to reveal a dropdown with action-buttons ++
buttons#<name>: ++
@@ -395,6 +405,16 @@ config file to be able to detect config errors
type: string ++
default: "" ++
description: "Command to be executed on click" ++
type: ++
type: string ++
default: "normal" ++
description: Type of the button ++
Toggle buttons receive the '.active' css class ++
enum: ["normal", "toggle"] ++
active: ++
type: string ++
default: "" ++
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 buttons to be displayed in the menu-button-bar ++
*buttons-grid*++
@@ -415,6 +435,16 @@ config file to be able to detect config errors
type: string ++
default: "" ++
description: "Command to be executed on click" ++
type: ++
type: string ++
default: "normal" ++
description: Type of the button ++
Toggle buttons receive the '.active' css class ++
enum: ["normal", "toggle"] ++
active: ++
type: string ++
default: "" ++
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 grid of buttons that execute shell commands ++
#START pulse-audio

View File

@@ -422,6 +422,17 @@
"type": "string",
"description": "Command to be executed on click",
"default": ""
},
"type": {
"type": "string",
"description": "Type of the button; toggle buttons receive the .active css class",
"default": "normal",
"enum": ["normal", "toggle"]
},
"active": {
"type": "string",
"description": "Command to run to test if button should be active",
"default": ""
}
}
}

View File

@@ -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);
string active = actions.get_object_element (i).get_string_member_with_default ("active", "");
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) {

View File

@@ -10,6 +10,7 @@ namespace SwayNotificationCenter.Widgets {
}
Action[] actions;
ToggleButton[] toggle_buttons;
public ButtonsGrid (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) {
base (suffix, swaync_daemon, noti_daemon);
@@ -26,14 +27,28 @@ 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) {
ToggleButton toggle_button = new ToggleButton (act.label, act.command, act.active);
toggle_buttons += toggle_button;
Gtk.ToggleButton tb = toggle_button;
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 ();
}
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

@@ -25,6 +25,8 @@ namespace SwayNotificationCenter.Widgets {
public struct Action {
string ? label;
string ? command;
BaseWidget.ButtonType ? type;
string ? 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:

View File

@@ -18,7 +18,7 @@
<object class="GtkImage" id="album_art">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="pixel-size">96</property>
<property name="pixel-size">48</property>
<property name="icon-name">audio-x-generic-symbolic</property>
<property name="use-fallback">True</property>
<property name="icon_size">0</property>

View File

@@ -0,0 +1,56 @@
namespace SwayNotificationCenter.Widgets {
class ToggleButton : Gtk.ToggleButton {
private bool _active;
private string command;
private string active_test;
public ToggleButton (string label, string command, string active) {
this.command = command;
this.active_test = active;
this.label = label;
this._active = false;
debug("ToggleButton created with test: %s", active);
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) {
debug("on_toggle");
this.set_active(!this._active);
BaseWidget.execute_command (command);
}
private bool check_should_be_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;
}
}
}

View File

@@ -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

View File

@@ -282,6 +282,11 @@
border-radius: 12px;
}
/* style given to the active toggle button */
.widget-buttons-grid>flowbox>flowboxchild>.active {
background: @noti-bg-focus;
}
.widget-buttons-grid>flowbox>flowboxchild>button:hover {
background: @noti-bg-hover;
}