Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show an indicator icon for each active connection #201

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,57 @@ public enum Network.State {
return 8;
}
}

public string to_string () {
switch (this) {
case Network.State.DISCONNECTED:
return "DISCONNECTED";
case Network.State.WIRED_UNPLUGGED:
return "WIRED_UNPLUGGED";
case Network.State.DISCONNECTED_WIRED:
return "DISCONNECTED_WIRED";
case Network.State.DISCONNECTED_AIRPLANE_MODE:
return "DISCONNECTED_AIRPLANE_MODE";
case Network.State.CONNECTED_WIRED:
return "CONNECTED_WIRED";
case Network.State.CONNECTED_VPN:
return "CONNECTED_VPN";
case Network.State.CONNECTED_WIFI:
return "CONNECTED_WIFI";
case Network.State.CONNECTED_WIFI_WEAK:
return "CONNECTED_WIFI_WEAK";
case Network.State.CONNECTED_WIFI_OK:
return "CONNECTED_WIFI_OK";
case Network.State.CONNECTED_WIFI_GOOD:
return "CONNECTED_WIFI_GOOD";
case Network.State.CONNECTED_WIFI_EXCELLENT:
return "CONNECTED_WIFI_EXCELLENT";
case Network.State.CONNECTED_MOBILE_WEAK:
return "CONNECTED_MOBILE_WEAK";
case Network.State.CONNECTED_MOBILE_OK:
return "CONNECTED_MOBILE_OK";
case Network.State.CONNECTED_MOBILE_GOOD:
return "CONNECTED_MOBILE_GOOD";
case Network.State.CONNECTED_MOBILE_EXCELLENT:
return "CONNECTED_MOBILE_EXCELLENT";
case Network.State.CONNECTING_WIFI:
return "CONNECTING_WIFI";
case Network.State.CONNECTING_MOBILE:
return "CONNECTING_MOBILE";
case Network.State.CONNECTING_WIRED:
return "CONNECTING_WIRED";
case Network.State.CONNECTING_VPN:
return "CONNECTING_VPN";
case Network.State.FAILED_WIRED:
return "FAILED_WIRED";
case Network.State.FAILED_WIFI:
return "FAILED_WIFI";
case Network.State.FAILED_MOBILE:
return "FAILED_MOBILE";
case Network.State.FAILED_VPN:
return "FAILED_VPN";
}

return "UNKNOWN";
}
meisenzahl marked this conversation as resolved.
Show resolved Hide resolved
}
194 changes: 172 additions & 22 deletions src/Widgets/DisplayWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@
*/

public class Network.Widgets.DisplayWidget : Gtk.Grid {
private Gtk.Image image;
private Gtk.Image cellular_image;
private Gtk.Revealer cellular_revealer;

private Gtk.Image vpn_image;
private Gtk.Revealer vpn_revealer;

private Gtk.Image wifi_image;
private Gtk.Revealer wifi_revealer;

private Gtk.Image wired_image;
private Gtk.Revealer wired_revealer;

private Gtk.Image network_image;
private Gtk.Revealer network_revealer;

private Gtk.Label extra_info_label;
private Gtk.Revealer extra_info_revealer;

Expand All @@ -26,8 +40,52 @@ public class Network.Widgets.DisplayWidget : Gtk.Grid {
private uint cellular_animation_timeout;
private int cellular_animation_state = 0;

private enum ConnectionState {
DISCONNECTED = 0,
CONNECTING = 1,
CONNECTED = 2
}

private ConnectionState cellular_connected = ConnectionState.DISCONNECTED;
private ConnectionState wifi_connected = ConnectionState.DISCONNECTED;
private ConnectionState wired_connected = ConnectionState.DISCONNECTED;
meisenzahl marked this conversation as resolved.
Show resolved Hide resolved

construct {
image = new Gtk.Image.from_icon_name ("network-wired-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
cellular_image = new Gtk.Image.from_icon_name ("network-cellular-offline-symbolic", Gtk.IconSize.LARGE_TOOLBAR);

cellular_revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT
};
cellular_revealer.add (cellular_image);

vpn_image = new Gtk.Image.from_icon_name ("network-vpn", Gtk.IconSize.LARGE_TOOLBAR);
meisenzahl marked this conversation as resolved.
Show resolved Hide resolved

vpn_revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT
};
vpn_revealer.add (vpn_image);

wifi_image = new Gtk.Image.from_icon_name ("network-wireless-offline-symbolic", Gtk.IconSize.LARGE_TOOLBAR);

wifi_revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT
};
wifi_revealer.add (wifi_image);

wired_image = new Gtk.Image.from_icon_name ("network-wired-offline-symbolic", Gtk.IconSize.LARGE_TOOLBAR);

wired_revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT
};
wired_revealer.add (wired_image);

network_image = new Gtk.Image.from_icon_name ("network-offline-symbolic", Gtk.IconSize.LARGE_TOOLBAR);

network_revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT,
reveal_child = true
};
network_revealer.add (network_image);

meisenzahl marked this conversation as resolved.
Show resolved Hide resolved
extra_info_label = new Gtk.Label (null) {
margin_start = 4,
Expand All @@ -40,11 +98,19 @@ public class Network.Widgets.DisplayWidget : Gtk.Grid {
};
extra_info_revealer.add (extra_info_label);

add (image);
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;

Expand All @@ -59,29 +125,60 @@ public class Network.Widgets.DisplayWidget : Gtk.Grid {
}

switch (state) {
case Network.State.DISCONNECTED:
network_image.icon_name = "network-offline-symbolic";
cellular_connected = ConnectionState.DISCONNECTED;
wifi_connected = ConnectionState.DISCONNECTED;
wired_connected = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.DISCONNECTED_AIRPLANE_MODE:
image.icon_name = "airplane-mode-symbolic";
network_image.icon_name = "airplane-mode-symbolic";
cellular_connected = ConnectionState.DISCONNECTED;
wifi_connected = ConnectionState.DISCONNECTED;
wired_connected = 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:
image.icon_name = "network-wired-acquiring-symbolic";
wired_image.icon_name = "network-wired-acquiring-symbolic";
wired_connected = ConnectionState.CONNECTING;
update_icons ();
break;
case Network.State.CONNECTED_WIRED:
image.icon_name = "network-wired-%ssymbolic".printf (secure? "secure-" : "");
wired_image.icon_name = "network-wired-%ssymbolic".printf (secure? "secure-" : "");
wired_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI:
image.icon_name = "network-wireless-connected-symbolic";
wifi_image.icon_name = "network-wireless-connected-symbolic";
wifi_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_WEAK:
image.icon_name = "network-wireless-signal-weak-%ssymbolic".printf (secure? "secure-" : "");
wifi_image.icon_name = "network-wireless-signal-weak-%ssymbolic".printf (secure? "secure-" : "");
wifi_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_OK:
image.icon_name = "network-wireless-signal-ok-%ssymbolic".printf (secure? "secure-" : "");
wifi_image.icon_name = "network-wireless-signal-ok-%ssymbolic".printf (secure? "secure-" : "");
wifi_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_GOOD:
image.icon_name = "network-wireless-signal-good-%ssymbolic".printf (secure? "secure-" : "");
wifi_image.icon_name = "network-wireless-signal-good-%ssymbolic".printf (secure? "secure-" : "");
wifi_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_WIFI_EXCELLENT:
image.icon_name = "network-wireless-signal-excellent-%ssymbolic".printf (secure? "secure-" : "");
wifi_image.icon_name = "network-wireless-signal-excellent-%ssymbolic".printf (secure? "secure-" : "");
wifi_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTING_WIFI:
wifi_animation_timeout = Timeout.add (300, () => {
Expand All @@ -101,21 +198,31 @@ public class Network.Widgets.DisplayWidget : Gtk.Grid {
strength = "excellent";
break;
}
image.icon_name = "network-wireless-signal-" + strength + (secure? "-secure" : "") + "-symbolic";
wifi_image.icon_name = "network-wireless-signal-" + strength + (secure? "-secure" : "") + "-symbolic";
wifi_connected = ConnectionState.CONNECTING;
update_icons ();
return true;
});
break;
case Network.State.CONNECTED_MOBILE_WEAK:
image.icon_name = "network-cellular-signal-weak-%ssymbolic".printf (secure ? "secure-" : "");
cellular_image.icon_name = "network-cellular-signal-weak-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_MOBILE_OK:
image.icon_name = "network-cellular-signal-ok-%ssymbolic".printf (secure ? "secure-" : "");
cellular_image.icon_name = "network-cellular-signal-ok-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_MOBILE_GOOD:
image.icon_name = "network-cellular-signal-good-%ssymbolic".printf (secure ? "secure-" : "");
cellular_image.icon_name = "network-cellular-signal-good-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTED_MOBILE_EXCELLENT:
image.icon_name = "network-cellular-signal-excellent-%ssymbolic".printf (secure ? "secure-" : "");
cellular_image.icon_name = "network-cellular-signal-excellent-%ssymbolic".printf (secure ? "secure-" : "");
cellular_connected = ConnectionState.CONNECTED;
update_icons ();
break;
case Network.State.CONNECTING_MOBILE:
cellular_animation_timeout = Timeout.add (300, () => {
Expand All @@ -136,24 +243,67 @@ public class Network.Widgets.DisplayWidget : Gtk.Grid {
break;
}

image.icon_name = "network-cellular-signal-" + strength + (secure ? "secure-" : "") + "-symbolic";
cellular_image.icon_name = "network-cellular-signal-" + strength + (secure ? "secure-" : "") + "-symbolic";
cellular_connected = ConnectionState.CONNECTING;
update_icons ();
return true;
});
break;
case Network.State.FAILED_MOBILE:
image.icon_name = "network-cellular-offline-symbolic";
cellular_image.icon_name = "network-cellular-offline-symbolic";
cellular_connected = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.FAILED_WIFI:
case Network.State.DISCONNECTED:
image.icon_name = "network-wireless-offline-symbolic";
wifi_image.icon_name = "network-wireless-offline-symbolic";
wifi_connected = ConnectionState.DISCONNECTED;
update_icons ();
break;
case Network.State.FAILED_WIRED:
case Network.State.WIRED_UNPLUGGED:
image.icon_name = "network-wired-offline-symbolic";
wired_image.icon_name = "network-wired-offline-symbolic";
wired_connected = ConnectionState.DISCONNECTED;
update_icons ();
break;
default:
image.icon_name = "network-offline-symbolic";
network_image.icon_name = "network-offline-symbolic";
cellular_connected = ConnectionState.DISCONNECTED;
wifi_connected = ConnectionState.DISCONNECTED;
wired_connected = ConnectionState.DISCONNECTED;
update_icons ();
critical ("Unknown network state, cannot show the good icon: %s", state.to_string ());
break;
}
}

private void update_icons () {
if ((cellular_connected + wifi_connected + wired_connected) > 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_connected > 0) {
cellular_revealer.reveal_child = true;
} else {
cellular_revealer.reveal_child = false;
}

if (wifi_connected > 0) {
wifi_revealer.reveal_child = true;
} else {
wifi_revealer.reveal_child = false;
}

if (wired_connected > 0) {
wired_revealer.reveal_child = true;
} else {
wired_revealer.reveal_child = false;
}
}
}