-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDisplayWidget.vala
More file actions
297 lines (268 loc) · 11.9 KB
/
DisplayWidget.vala
File metadata and controls
297 lines (268 loc) · 11.9 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright 2015-2020 elementary, Inc. (https://elementary.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
public class Network.Widgets.DisplayWidget : Gtk.Grid {
private ConnectionRevealer cellular_revealer;
private ConnectionRevealer vpn_revealer;
private ConnectionRevealer wifi_revealer;
private ConnectionRevealer wired_revealer;
private ConnectionRevealer network_revealer;
private Gtk.Label extra_info_label;
private Gtk.Revealer extra_info_revealer;
private uint wifi_animation_timeout;
private int wifi_animation_state = 0;
private uint cellular_animation_timeout;
private int cellular_animation_state = 0;
private enum ConnectionState {
DISCONNECTED = 0,
CONNECTING = 1,
CONNECTED = 2
}
private ConnectionState cellular_connection_state = ConnectionState.DISCONNECTED;
private ConnectionState wifi_connection_state = ConnectionState.DISCONNECTED;
private ConnectionState wired_connection_state = ConnectionState.DISCONNECTED;
construct {
cellular_revealer = new ConnectionRevealer.from_icon_name ("network-cellular-offline-symbolic");
vpn_revealer = new ConnectionRevealer.from_icon_name ("network-vpn-symbolic");
wifi_revealer = new ConnectionRevealer.from_icon_name ("network-wireless-offline-symbolic");
wired_revealer = new ConnectionRevealer.from_icon_name ("network-wired-offline-symbolic");
network_revealer = new ConnectionRevealer.from_icon_name ("network-offline-symbolic") {
reveal_child = true
};
extra_info_label = new Gtk.Label (null) {
margin_start = 4,
valign = Gtk.Align.CENTER,
vexpand = true
};
extra_info_revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT
};
extra_info_revealer.add (extra_info_label);
add (vpn_revealer);
add (wired_revealer);
add (wifi_revealer);
add (cellular_revealer);
add (network_revealer);
add (extra_info_revealer);
update_icons ();
}
public void update_state (Network.State state, bool secure, string? extra_info = null) {
info ("Network state changed to \"%s\"\n", state.to_string ());
extra_info_revealer.reveal_child = extra_info != null;
extra_info_label.label = extra_info;
if (wifi_animation_timeout > 0) {
Source.remove (wifi_animation_timeout);
wifi_animation_timeout = 0;
}
if (cellular_animation_timeout > 0) {
Source.remove (cellular_animation_timeout);
cellular_animation_timeout = 0;
}
switch (state) {
case Network.State.DISCONNECTED:
network_revealer.image.icon_name = "network-offline-symbolic";
cellular_connection_state = ConnectionState.DISCONNECTED;
wifi_connection_state = ConnectionState.DISCONNECTED;
wired_connection_state = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.DISCONNECTED_AIRPLANE_MODE:
network_revealer.image.icon_name = "airplane-mode-symbolic";
cellular_connection_state = ConnectionState.DISCONNECTED;
wifi_connection_state = ConnectionState.DISCONNECTED;
wired_connection_state = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.DISCONNECTED_WIRED:
wired_revealer.image.icon_name = "network-wired-disconnected";
wired_connection_state = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_VPN:
vpn_revealer.reveal_child = true;
break;
case Network.State.FAILED_VPN:
vpn_revealer.reveal_child = false;
break;
case Network.State.CONNECTING_WIRED:
wired_revealer.image.icon_name = "network-wired-acquiring-symbolic";
wired_connection_state = ConnectionState.CONNECTING;
update_icons ();
break;
case Network.State.CONNECTED_WIRED:
wired_revealer.image.icon_name = "network-wired-%ssymbolic".printf (secure? "secure-" : "");
wired_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI:
wifi_revealer.image.icon_name = "network-wireless-connected-symbolic";
wifi_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_WEAK:
wifi_revealer.image.icon_name = "network-wireless-signal-weak-%ssymbolic".printf (secure? "secure-" : "");
wifi_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_OK:
wifi_revealer.image.icon_name = "network-wireless-signal-ok-%ssymbolic".printf (secure? "secure-" : "");
wifi_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_GOOD:
wifi_revealer.image.icon_name = "network-wireless-signal-good-%ssymbolic".printf (secure? "secure-" : "");
wifi_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_EXCELLENT:
wifi_revealer.image.icon_name = "network-wireless-signal-excellent-%ssymbolic".printf (secure? "secure-" : "");
wifi_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTING_WIFI:
wifi_animation_timeout = Timeout.add (300, () => {
wifi_animation_state = (wifi_animation_state + 1) % 4;
string strength = "";
switch (wifi_animation_state) {
case 0:
strength = "weak";
break;
case 1:
strength = "ok";
break;
case 2:
strength = "good";
break;
case 3:
strength = "excellent";
break;
}
wifi_revealer.image.icon_name = "network-wireless-signal-" + strength + (secure? "-secure" : "") + "-symbolic";
wifi_connection_state = ConnectionState.CONNECTING;
update_icons ();
return true;
});
break;
case Network.State.CONNECTED_MOBILE_WEAK:
cellular_revealer.image.icon_name = "network-cellular-signal-weak-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_MOBILE_OK:
cellular_revealer.image.icon_name = "network-cellular-signal-ok-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_MOBILE_GOOD:
cellular_revealer.image.icon_name = "network-cellular-signal-good-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_MOBILE_EXCELLENT:
cellular_revealer.image.icon_name = "network-cellular-signal-excellent-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connection_state = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTING_MOBILE:
cellular_animation_timeout = Timeout.add (300, () => {
cellular_animation_state = (cellular_animation_state + 1) % 4;
string strength = "";
switch (cellular_animation_state) {
case 0:
strength = "weak";
break;
case 1:
strength = "ok";
break;
case 2:
strength = "good";
break;
case 3:
strength = "excellent";
break;
}
cellular_revealer.image.icon_name = "network-cellular-signal-" + strength + (secure ? "secure-" : "") + "-symbolic";
cellular_connection_state = ConnectionState.CONNECTING;
update_icons ();
return true;
});
break;
case Network.State.FAILED_MOBILE:
cellular_revealer.image.icon_name = "network-cellular-offline-symbolic";
cellular_connection_state = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.FAILED_WIFI:
wifi_revealer.image.icon_name = "network-wireless-offline-symbolic";
wifi_connection_state = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.FAILED_WIRED:
case Network.State.WIRED_UNPLUGGED:
wired_revealer.image.icon_name = "network-wired-offline-symbolic";
wired_connection_state = ConnectionState.DISCONNECTED;
update_icons ();
break;
default:
network_revealer.image.icon_name = "network-offline-symbolic";
cellular_connection_state = ConnectionState.DISCONNECTED;
wifi_connection_state = ConnectionState.DISCONNECTED;
wired_connection_state = ConnectionState.DISCONNECTED;
update_icons ();
critical ("Unknown network state, cannot show the good icon: %s", state.to_string ());
break;
}
}
private void update_icons () {
if ((cellular_connection_state + wifi_connection_state + wired_connection_state) > 0) {
network_revealer.reveal_child = false;
} else {
cellular_revealer.reveal_child = false;
wifi_revealer.reveal_child = false;
wired_revealer.reveal_child = false;
network_revealer.reveal_child = true;
return;
}
if (cellular_connection_state > 0) {
cellular_revealer.reveal_child = true;
} else {
cellular_revealer.reveal_child = false;
}
if (wifi_connection_state > 0) {
wifi_revealer.reveal_child = true;
} else {
wifi_revealer.reveal_child = false;
}
if (wired_connection_state > 0) {
wired_revealer.reveal_child = true;
} else {
wired_revealer.reveal_child = false;
}
}
private class ConnectionRevealer : Gtk.Revealer {
public Gtk.Image image { get; construct set; }
private string _icon_name;
public ConnectionRevealer.from_icon_name (string icon_name) {
_icon_name = icon_name;
}
construct {
image = new Gtk.Image.from_icon_name (_icon_name, Gtk.IconSize.LARGE_TOOLBAR);
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT;
add (image);
}
}
}