-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtoggleButton.vala
More file actions
51 lines (44 loc) · 1.76 KB
/
toggleButton.vala
File metadata and controls
51 lines (44 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
namespace SwayNotificationCenter.Widgets {
class ToggleButton : Gtk.ToggleButton {
private string command;
private string update_command;
private ulong handler_id;
public ToggleButton (string label, string command, string update_command, bool active) {
this.command = command;
this.update_command = update_command;
this.label = label;
this.set_has_frame (true);
if (active) {
this.active = true;
}
this.handler_id = this.toggled.connect (on_toggle);
}
private async void on_toggle () {
string msg = "";
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, "");
GLib.SignalHandler.block (this, this.handler_id);
if (res.up () == "TRUE") {
this.active = true;
} else {
this.active = false;
}
GLib.SignalHandler.unblock (this, this.handler_id);
} catch (RegexError e) {
warning ("RegexError: %s", e.message);
}
}
}
}