Skip to content

Commit 316aa94

Browse files
authored
WifiMenuItem: misc cleanup (#282)
1 parent 24e72ab commit 316aa94

2 files changed

Lines changed: 93 additions & 132 deletions

File tree

src/Widgets/WifiInterface.vala

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
3131
private NM.AccessPoint? active_ap;
3232
private Gtk.ListBox wifi_list;
3333
private WifiMenuItem? active_wifi_item;
34-
private WifiMenuItem? blank_item = null;
34+
private Gtk.RadioButton blank_item;
3535
private Gtk.Stack placeholder;
3636

3737
private bool locked;
@@ -47,7 +47,9 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
4747
device = _device;
4848

4949
wifi_device = (NM.DeviceWifi) device;
50-
blank_item = new WifiMenuItem.blank ();
50+
51+
blank_item = new Gtk.RadioButton (null);
52+
5153
active_wifi_item = null;
5254

5355
/* Monitor killswitch status */
@@ -68,29 +70,6 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
6870
}
6971

7072
update ();
71-
72-
wifi_item.text = display_title;
73-
notify["display-title"].connect ( () => {
74-
wifi_item.text = display_title;
75-
});
76-
77-
wifi_item.notify["active"].connect (() => {
78-
var active = wifi_item.active;
79-
if (active != !software_locked) {
80-
rfkill.set_software_lock (RFKillDeviceType.WLAN, !active);
81-
nm_client.dbus_set_property.begin (
82-
NM.DBUS_PATH, NM.DBUS_INTERFACE,
83-
"WirelessEnabled", active,
84-
-1, null, (obj, res) => {
85-
try {
86-
((NM.Client) obj).dbus_set_property.end (res);
87-
} catch (Error e) {
88-
warning ("Error activating wifi item: %s", e.message);
89-
}
90-
}
91-
);
92-
}
93-
});
9473
}
9574

9675
construct {
@@ -124,7 +103,7 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
124103
wifi_list.set_sort_func (sort_func);
125104
wifi_list.set_placeholder (placeholder);
126105

127-
wifi_item = new Granite.SwitchModelButton ("");
106+
wifi_item = new Granite.SwitchModelButton (display_title);
128107
wifi_item.get_style_context ().add_class (Granite.STYLE_CLASS_H4_LABEL);
129108

130109
var scrolled_box = new Gtk.ScrolledWindow (null, null) {
@@ -140,6 +119,32 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
140119
orientation = Gtk.Orientation.VERTICAL;
141120
pack_start (wifi_item);
142121
pack_start (revealer);
122+
123+
bind_property ("display-title", wifi_item, "text");
124+
125+
wifi_list.row_activated.connect ((row) => {
126+
if (row is WifiMenuItem) {
127+
wifi_activate_cb ((WifiMenuItem) row);
128+
}
129+
});
130+
131+
wifi_item.notify["active"].connect (() => {
132+
var active = wifi_item.active;
133+
if (active != !software_locked) {
134+
rfkill.set_software_lock (RFKillDeviceType.WLAN, !active);
135+
nm_client.dbus_set_property.begin (
136+
NM.DBUS_PATH, NM.DBUS_INTERFACE,
137+
"WirelessEnabled", active,
138+
-1, null, (obj, res) => {
139+
try {
140+
((NM.Client) obj).dbus_set_property.end (res);
141+
} catch (Error e) {
142+
warning ("Error activating wifi item: %s", e.message);
143+
}
144+
}
145+
);
146+
}
147+
});
143148
}
144149

145150
public override void update () {
@@ -415,7 +420,6 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
415420

416421
private void access_point_added_cb (Object ap_) {
417422
NM.AccessPoint ap = (NM.AccessPoint)ap_;
418-
WifiMenuItem? previous_wifi_item = blank_item;
419423
unowned GLib.Bytes ap_ssid = ap.ssid;
420424

421425
bool found = false;
@@ -429,17 +433,11 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
429433
menu_item.add_ap (ap);
430434
break;
431435
}
432-
433-
previous_wifi_item = menu_item;
434436
}
435437

436438
/* Sometimes network manager sends a (fake?) AP without a valid ssid. */
437439
if (!found && ap_ssid != null) {
438-
var item = new WifiMenuItem (ap, previous_wifi_item);
439-
440-
previous_wifi_item = item;
441-
item.set_visible (true);
442-
item.user_action.connect (wifi_activate_cb);
440+
var item = new WifiMenuItem (ap, blank_item);
443441

444442
wifi_list.add (item);
445443
wifi_list.show_all ();
@@ -463,7 +461,7 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
463461

464462
if (active_ap == null) {
465463
debug ("No active AP");
466-
blank_item.set_active (true);
464+
blank_item.active = true;
467465
} else {
468466
unowned GLib.Bytes active_ap_ssid = active_ap.ssid;
469467
active_ap_name = NM.Utils.ssid_to_utf8 (active_ap_ssid.get_data ());
@@ -475,7 +473,7 @@ public class Network.WifiInterface : Network.WidgetNMInterface {
475473

476474
if (active_ap_ssid.compare (menu_item.ssid) == 0) {
477475
found = true;
478-
menu_item.set_active (true);
476+
menu_item.active = true;
479477
active_wifi_item = menu_item;
480478
active_wifi_item.state = device.state;
481479
}

src/Widgets/WifiMenuItem.vala

Lines changed: 59 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,49 @@
1616
*/
1717

1818
public class Network.WifiMenuItem : Gtk.ListBoxRow {
19-
private List<NM.AccessPoint> _ap;
20-
public signal void user_action ();
19+
public NM.AccessPoint ap { get; private set; }
20+
public NM.DeviceState state { get; set; default = NM.DeviceState.DISCONNECTED; }
21+
2122
public GLib.Bytes ssid {
2223
get {
23-
return _tmp_ap.get_ssid ();
24+
return ap.get_ssid ();
2425
}
2526
}
2627

27-
public NM.DeviceState state { get; set; default = NM.DeviceState.DISCONNECTED; }
28-
2928
public uint8 strength {
3029
get {
3130
uint8 strength = 0;
32-
foreach (var ap in _ap) {
31+
foreach (unowned var ap in ap_list) {
3332
strength = uint8.max (strength, ap.get_strength ());
3433
}
3534
return strength;
3635
}
3736
}
3837

39-
public NM.AccessPoint ap { get { return _tmp_ap; } }
40-
private NM.AccessPoint _tmp_ap;
38+
public bool active {
39+
set {
40+
radio_button.active = value;
41+
}
42+
}
4143

42-
private Gtk.RadioButton radio_button;
44+
private Gtk.Image error_img;
4345
private Gtk.Image img_strength;
4446
private Gtk.Image lock_img;
45-
private Gtk.Image error_img;
46-
private Gtk.Spinner spinner;
4747
private Gtk.Label label;
48+
private Gtk.RadioButton radio_button;
49+
private Gtk.Spinner spinner;
50+
private List<NM.AccessPoint> ap_list;
4851

49-
public WifiMenuItem (NM.AccessPoint ap, WifiMenuItem? previous = null) {
52+
public WifiMenuItem (NM.AccessPoint ap, Gtk.RadioButton blank_radio) {
5053
label = new Gtk.Label (null) {
5154
ellipsize = Pango.EllipsizeMode.MIDDLE
5255
};
5356

54-
radio_button = new Gtk.RadioButton (null) {
57+
radio_button = new Gtk.RadioButton.from_widget (blank_radio) {
5558
hexpand = true
5659
};
5760
radio_button.add (label);
5861

59-
if (previous != null) {
60-
radio_button.set_group (previous.get_group ());
61-
}
62-
6362
img_strength = new Gtk.Image () {
6463
icon_size = Gtk.IconSize.MENU
6564
};
@@ -70,71 +69,44 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {
7069
tooltip_text = _("Unable to connect")
7170
};
7271

73-
spinner = new Gtk.Spinner () {
74-
no_show_all = true,
75-
visible = false
76-
};
77-
spinner.start ();
72+
spinner = new Gtk.Spinner ();
7873

79-
var grid = new Gtk.Grid () {
80-
column_spacing = 6
81-
};
82-
grid.add (radio_button);
83-
grid.add (spinner);
84-
grid.add (error_img);
85-
grid.add (lock_img);
86-
grid.add (img_strength);
74+
var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
75+
box.add (radio_button);
76+
box.add (spinner);
77+
box.add (error_img);
78+
box.add (lock_img);
79+
box.add (img_strength);
8780

88-
_ap = new List<NM.AccessPoint> ();
81+
add (box);
82+
83+
ap_list = new List<NM.AccessPoint> ();
8984

9085
/* Adding the access point triggers update */
9186
add_ap (ap);
9287

9388
notify["state"].connect (update);
94-
radio_button.notify["active"].connect (update);
9589

90+
// We can't use clicked because we get in a weird loop state
9691
radio_button.button_release_event.connect ((b, ev) => {
97-
user_action ();
98-
return false;
92+
activate ();
93+
return Gdk.EVENT_STOP;
9994
});
100-
101-
add (grid);
102-
103-
}
104-
105-
/**
106-
* Only used for an item which is not displayed: hacky way to have no radio button selected.
107-
**/
108-
public WifiMenuItem.blank () {
109-
radio_button = new Gtk.RadioButton (null);
11095
}
11196

11297
class construct {
11398
set_css_name (Gtk.STYLE_CLASS_MENUITEM);
11499
}
115100

116-
private void update_tmp_ap () {
101+
private void update_ap () {
117102
uint8 strength = 0;
118-
foreach (var ap in _ap) {
119-
_tmp_ap = strength > ap.get_strength () ? _tmp_ap : ap;
120-
strength = uint8.max (strength, ap.get_strength ());
103+
foreach (unowned var acess_point in ap_list) {
104+
ap = strength > acess_point.get_strength () ? ap : acess_point;
105+
strength = uint8.max (strength, acess_point.get_strength ());
121106
}
122-
}
123107

124-
public void set_active (bool active) {
125-
radio_button.set_active (active);
126-
}
127-
128-
private unowned SList get_group () {
129-
return radio_button.get_group ();
130-
}
131-
132-
private void update () {
133108
label.label = NM.Utils.ssid_to_utf8 (ap.get_ssid ().get_data ());
134109

135-
img_strength.icon_name = get_strength_symbolic_icon ();
136-
img_strength.show_all ();
137-
138110
var flags = ap.get_wpa_flags () | ap.get_rsn_flags ();
139111
var is_secured = false;
140112
if (NM.@80211ApSecurityFlags.GROUP_WEP40 in flags) {
@@ -158,63 +130,54 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {
158130

159131
lock_img.visible = !is_secured;
160132
lock_img.no_show_all = !lock_img.visible;
133+
}
134+
135+
private const string BASE_ICON_NAME = "panel-network-wireless-signal-%s-symbolic";
136+
private void update () {
137+
if (strength < 30) {
138+
img_strength.icon_name = BASE_ICON_NAME.printf ("weak");
139+
} else if (strength < 55) {
140+
img_strength.icon_name = BASE_ICON_NAME.printf ("ok");
141+
} else if (strength < 80) {
142+
img_strength.icon_name = BASE_ICON_NAME.printf ("good");
143+
} else {
144+
img_strength.icon_name = BASE_ICON_NAME.printf ("excellent");
145+
}
146+
147+
error_img.no_show_all = true;
148+
error_img.visible = false;
149+
error_img.hide ();
161150

162-
hide_item (error_img);
163-
hide_item (spinner);
151+
spinner.stop ();
164152

165153
switch (state) {
166154
case NM.DeviceState.FAILED:
167-
show_item (error_img);
155+
error_img.no_show_all = false;
156+
error_img.visible = true;
168157
break;
169158
case NM.DeviceState.PREPARE:
170159
case NM.DeviceState.CONFIG:
171160
case NM.DeviceState.NEED_AUTH:
172161
case NM.DeviceState.IP_CONFIG:
173162
case NM.DeviceState.IP_CHECK:
174163
case NM.DeviceState.SECONDARIES:
175-
show_item (spinner);
164+
spinner.start ();
176165
if (!radio_button.active) {
177166
critical ("An access point is being connected but not active.");
178167
}
179168
break;
180169
}
181170
}
182171

183-
private void show_item (Gtk.Widget w) {
184-
w.visible = true;
185-
w.no_show_all = !w.visible;
186-
}
187-
188-
private void hide_item (Gtk.Widget w) {
189-
w.visible = false;
190-
w.no_show_all = !w.visible;
191-
w.hide ();
192-
}
193-
194172
public void add_ap (NM.AccessPoint ap) {
195-
_ap.append (ap);
196-
update_tmp_ap ();
197-
173+
ap_list.append (ap);
174+
update_ap ();
198175
update ();
199176
}
200177

201-
private const string BASE_ICON_NAME = "panel-network-wireless-signal-";
202-
private const string SYMBOLIC = "-symbolic";
203-
private unowned string get_strength_symbolic_icon () {
204-
if (strength < 30) {
205-
return BASE_ICON_NAME + "weak" + SYMBOLIC;
206-
} else if (strength < 55) {
207-
return BASE_ICON_NAME + "ok" + SYMBOLIC;
208-
} else if (strength < 80) {
209-
return BASE_ICON_NAME + "good" + SYMBOLIC;
210-
} else {
211-
return BASE_ICON_NAME + "excellent" + SYMBOLIC;
212-
}
213-
}
214-
215178
public bool remove_ap (NM.AccessPoint ap) {
216-
_ap.remove (ap);
217-
update_tmp_ap ();
218-
return _ap.length () > 0;
179+
ap_list.remove (ap);
180+
update_ap ();
181+
return ap_list.length () > 0;
219182
}
220183
}

0 commit comments

Comments
 (0)