Skip to content

Commit dd8ba3b

Browse files
committed
Create SettingsToggle widget
1 parent 0c9a8ef commit dd8ba3b

5 files changed

Lines changed: 100 additions & 45 deletions

File tree

data/Indicator.css

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@
33
* SPDX-FileCopyrightText: 2023 elementary, Inc. (https://elementary.io)
44
*/
55

6-
network .image-button.toggle {
6+
network .image-button.toggle,
7+
settings-toggle .image-button.toggle {
78
border-radius: 1em;
89
}
910

10-
network .image-button {
11+
network .image-button,
12+
settings-toggle .image-button {
1113
border: none;
1214
box-shadow: none;
1315
min-height: 2.1666rem; /* 26px */
1416
min-width: 2.1666rem; /* 26px */
1517
}
1618

17-
network .image-button {
19+
network .image-button,
20+
settings-toggle .image-button {
1821
background: alpha(@text_color, 0.1);
1922
}
2023

21-
network .image-button:checked {
24+
network.image-button:checked,
25+
settings-toggle .image-button:checked {
2226
background: @selected_bg_color;
2327
color: @selected_fg_color;
2428
}
2529

26-
network .image-button:disabled {
30+
network .image-button:disabled,
31+
settings-toggle .image-button:disabled {
2732
background: @insensitive_bg_color;
2833
}

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ shared_module(
5050
'src/Widgets/EtherInterface.vala',
5151
'src/Widgets/ModemInterface.vala',
5252
'src/Widgets/PopoverWidget.vala',
53+
'src/Widgets/SettingsToggle.vala',
5354
'src/Widgets/VpnInterface.vala',
5455
'src/Widgets/VpnMenuItem.vala',
5556
'src/Widgets/WidgetNMInterface.vala',

src/Indicator.vala

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Network.Indicator : Wingpanel.Indicator {
2323
NetworkMonitor network_monitor;
2424

2525
private Gtk.GestureMultiPress gesture_click;
26+
private SimpleAction airplane_action;
2627

2728
public bool is_in_session { get; set; default = false; }
2829

@@ -60,20 +61,30 @@ public class Network.Indicator : Wingpanel.Indicator {
6061
};
6162

6263
gesture_click.pressed.connect (() => {
63-
popover_widget.nm_client.dbus_call.begin (
64-
NM.DBUS_PATH, NM.DBUS_INTERFACE,
65-
"Enable", new Variant.tuple ({new Variant.boolean (!popover_widget.nm_client.networking_get_enabled ())}),
66-
null, -1, null, (obj, res) => {
67-
try {
68-
((NM.Client) obj).dbus_set_property.end (res);
69-
} catch (Error e) {
70-
warning ("Error setting airplane mode: %s", e.message);
71-
}
72-
}
73-
);
64+
airplane_action.activate (null);
7465
});
7566
}
7667

68+
airplane_action = new SimpleAction.stateful ("airplane-mode", null, new Variant.boolean (popover_widget.nm_client.networking_get_enabled ()));
69+
airplane_action.activate.connect (() => {
70+
popover_widget.nm_client.dbus_call.begin (
71+
NM.DBUS_PATH, NM.DBUS_INTERFACE,
72+
"Enable", new Variant.tuple ({new Variant.boolean (!popover_widget.nm_client.networking_get_enabled ())}),
73+
null, -1, null, (obj, res) => {
74+
try {
75+
((NM.Client) obj).dbus_set_property.end (res);
76+
} catch (Error e) {
77+
warning ("Error setting airplane mode: %s", e.message);
78+
}
79+
}
80+
);
81+
});
82+
83+
var action_group = new SimpleActionGroup ();
84+
action_group.add_action (airplane_action);
85+
86+
popover_widget.insert_action_group ("network", action_group);
87+
7788
update_tooltip ();
7889
on_state_changed ();
7990
start_monitor ();
@@ -93,6 +104,8 @@ public class Network.Indicator : Wingpanel.Indicator {
93104

94105
display_widget.update_state (popover_widget.state, popover_widget.secure, popover_widget.extra_info);
95106

107+
airplane_action.set_state (new Variant.boolean (!popover_widget.nm_client.networking_get_enabled ()));
108+
96109
update_tooltip ();
97110
}
98111

src/Widgets/PopoverWidget.vala

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,43 +56,20 @@ public class Network.Widgets.PopoverWidget : Gtk.Box {
5656
}
5757

5858
if (is_in_session) {
59-
var airplane_toggle = new Gtk.ToggleButton () {
60-
halign = Gtk.Align.CENTER,
61-
image = new Gtk.Image.from_icon_name ("airplane-mode-symbolic", Gtk.IconSize.MENU)
59+
var airplane_toggle = new SettingsToggle () {
60+
action_name = "network.airplane-mode",
61+
icon_name = "airplane-mode-symbolic",
62+
settings_uri = "settings://network",
63+
text = _("Airplane Mode")
6264
};
6365

64-
var airplane_label = new Gtk.Label (_("Airplane Mode"));
65-
airplane_label.get_style_context ().add_class (Granite.STYLE_CLASS_SMALL_LABEL);
66-
67-
var airplane_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 3);
68-
airplane_box.add (airplane_toggle);
69-
airplane_box.add (airplane_label);
70-
7166
var airplane_child = new Gtk.FlowBoxChild () {
7267
// Prevent weird double focus border
7368
can_focus = false,
74-
child = airplane_box
69+
child = airplane_toggle
7570
};
7671

7772
other_box.add (airplane_child);
78-
79-
airplane_toggle.toggled.connect (() => {
80-
nm_client.dbus_call.begin (
81-
NM.DBUS_PATH, NM.DBUS_INTERFACE,
82-
"Enable", new Variant.tuple ({new Variant.boolean (!airplane_toggle.active)}),
83-
null, -1, null, (obj, res) => {
84-
try {
85-
((NM.Client) obj).dbus_set_property.end (res);
86-
} catch (Error e) {
87-
warning ("Error setting airplane mode: %s", e.message);
88-
}
89-
}
90-
);
91-
});
92-
93-
if (!airplane_toggle.active && !nm_client.networking_get_enabled ()) {
94-
airplane_toggle.activate ();
95-
}
9673
}
9774

9875
var other_sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL) {

src/Widgets/SettingsToggle.vala

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2023 elementary, Inc. (https://elementary.io)
4+
*/
5+
6+
public class Network.SettingsToggle : Gtk.Box {
7+
public string action_name { get; set; }
8+
public string icon_name { get; set; }
9+
public string text { get; set; }
10+
public string settings_uri { get; set; default = "settings://"; }
11+
12+
private Gtk.GestureMultiPress middle_click_gesture;
13+
14+
class construct {
15+
set_css_name ("settings-toggle");
16+
}
17+
18+
construct {
19+
var image = new Gtk.Image ();
20+
21+
var button = new Gtk.ToggleButton () {
22+
halign = CENTER,
23+
image = image
24+
};
25+
26+
var label = new Gtk.Label (null) {
27+
ellipsize = MIDDLE,
28+
justify = CENTER,
29+
lines = 2,
30+
max_width_chars = 13,
31+
mnemonic_widget = button
32+
};
33+
label.get_style_context ().add_class (Granite.STYLE_CLASS_SMALL_LABEL);
34+
35+
halign = CENTER;
36+
orientation = VERTICAL;
37+
spacing = 3;
38+
add (button);
39+
add (label);
40+
41+
bind_property ("action-name", button, "action-name");
42+
bind_property ("icon-name", image, "icon-name");
43+
bind_property ("text", label, "label");
44+
45+
middle_click_gesture = new Gtk.GestureMultiPress (this) {
46+
button = Gdk.BUTTON_MIDDLE
47+
};
48+
middle_click_gesture.pressed.connect (() => {
49+
try {
50+
AppInfo.launch_default_for_uri (settings_uri, null);
51+
52+
var popover = (Gtk.Popover) get_ancestor (typeof (Gtk.Popover));
53+
popover.popdown ();
54+
} catch (Error e) {
55+
critical ("Failed to open system settings: %s", e.message);
56+
}
57+
});
58+
}
59+
}

0 commit comments

Comments
 (0)