1616 */
1717
1818public 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