Skip to content

Commit

Permalink
Merge branch 'main' into danirabbit/autoconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Dec 10, 2024
2 parents 3012a93 + 096d829 commit 6b4ccb4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 60 deletions.
54 changes: 9 additions & 45 deletions src/Views/WifiPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class Network.WifiInterface : Network.Widgets.Page {
protected Gtk.ScrolledWindow scrolled;
protected Gtk.Box hotspot_mode_alert;
protected Gtk.Revealer top_revealer;
protected Gtk.Button? disconnect_btn;
protected Gtk.Button? settings_btn;
protected Gtk.Button? hidden_btn;

public WifiInterface (NM.Device device) {
Expand Down Expand Up @@ -181,9 +179,7 @@ public class Network.WifiInterface : Network.Widgets.Page {

/* Sometimes network manager sends a (fake?) AP without a valid ssid. */
if (!found && ap.ssid != null) {
var item = new WifiMenuItem (ap) {
visible = true
};
var item = new WifiMenuItem (ap);
item.user_action.connect (wifi_activate_cb);

wifi_list.append (item);
Expand Down Expand Up @@ -263,14 +259,6 @@ public class Network.WifiInterface : Network.Widgets.Page {

public override void update () {
bool sensitive = (device.get_state () == NM.DeviceState.ACTIVATED);
if (disconnect_btn != null) {
disconnect_btn.sensitive = sensitive;
}

if (settings_btn != null) {
settings_btn.sensitive = sensitive;
}

if (hidden_btn != null) {
hidden_btn.sensitive = (state != NM.DeviceState.UNAVAILABLE);
}
Expand Down Expand Up @@ -342,8 +330,6 @@ public class Network.WifiInterface : Network.Widgets.Page {
if (connected_frame != null && connected_frame.get_child () != null) {
connected_frame.get_child ().destroy ();
}

disconnect_btn = settings_btn = null;
} else if (active_access_point != null && active_wifi_item != old_active) {

if (old_active != null) {
Expand All @@ -357,43 +343,17 @@ public class Network.WifiInterface : Network.Widgets.Page {
active_wifi_item.visible = false;

var top_item = new WifiMenuItem (active_access_point) {
hexpand = true,
state = NM.DeviceState.ACTIVATED
};

disconnect_btn = new Gtk.Button.with_label (_("Disconnect")) {
sensitive = (device.get_state () == NM.DeviceState.ACTIVATED)
};
disconnect_btn.add_css_class (Granite.STYLE_CLASS_DESTRUCTIVE_ACTION);
disconnect_btn.clicked.connect (() => {
try {
device.disconnect (null);
} catch (Error e) {
warning (e.message);
}
});

settings_btn = new Gtk.Button.with_label (_("Settings…")) {
sensitive = (device.get_state () == NM.DeviceState.ACTIVATED)
};

var button_box = new Gtk.Box (HORIZONTAL, 6) {
homogeneous = true,
valign = CENTER
};
button_box.append (settings_btn);
button_box.append (disconnect_btn);

var connected_box = new Gtk.Box (HORIZONTAL, 12);
connected_box.append (top_item);
connected_box.append (button_box);

// Create a single item listbox to match styles with main listbox
var connected_listbox = new Gtk.ListBox () {
selection_mode = NONE
};
connected_listbox.add_css_class (Granite.STYLE_CLASS_RICH_LIST);
connected_listbox.append (connected_box);
connected_listbox.append (top_item);
connected_listbox.get_first_child ().focusable = false;

connected_frame.child = connected_listbox;
Expand All @@ -412,9 +372,8 @@ public class Network.WifiInterface : Network.Widgets.Page {
settings_dialog.add_button (_("Close"), Gtk.ResponseType.CLOSE);
settings_dialog.custom_bin.append (info_box);

settings_btn.clicked.connect (() => {
settings_dialog.present ();
});
top_item.user_action.connect (wifi_activate_cb);
top_item.settings_request.connect (settings_dialog.present);

settings_dialog.response.connect ((response) => {
if (response == 0) {
Expand Down Expand Up @@ -477,6 +436,11 @@ public class Network.WifiInterface : Network.Widgets.Page {

/* Do not activate connection if it is already activated */
if (wifi_device.get_active_access_point () == row.ap) {
try {
device.disconnect (null);
} catch (Error e) {
warning (e.message);
}
return;
}

Expand Down
50 changes: 35 additions & 15 deletions src/Widgets/WifiMenuItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

public class Network.WifiMenuItem : Gtk.ListBoxRow {
public signal void user_action ();
public signal void settings_request ();

public bool is_secured { get; private set; }
public bool active { get; set; }
Expand Down Expand Up @@ -50,11 +51,19 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {
public Gtk.Label ssid_label { get; private set; }
public Gtk.Label status_label { get; private set; }

private static Gtk.SizeGroup button_sizegroup;

private Gtk.Box button_box;
private Gtk.Button connect_button;
private Gtk.Image lock_img;
private Gtk.Image error_img;
private Gtk.Revealer connect_button_revealer;
private Gtk.Revealer settings_button_revealer;
private Gtk.Spinner spinner;

static construct {
button_sizegroup = new Gtk.SizeGroup (HORIZONTAL);
}

public WifiMenuItem (NM.AccessPoint ap) {
img_strength = new Gtk.Image () {
icon_size = LARGE
Expand All @@ -77,16 +86,25 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {

spinner = new Gtk.Spinner ();

var connect_button = new Gtk.Button.with_label (_("Connect")) {
halign = Gtk.Align.END,
hexpand = true,
valign = Gtk.Align.CENTER
var settings_button = new Gtk.Button.with_label (_("Settings…"));

connect_button = new Gtk.Button ();

button_sizegroup.add_widget (connect_button);

settings_button_revealer = new Gtk.Revealer () {
child = settings_button,
overflow = VISIBLE
};

connect_button_revealer = new Gtk.Revealer () {
reveal_child = true,
child = connect_button
button_box = new Gtk.Box (HORIZONTAL, 6) {
hexpand = true,
halign = END,
homogeneous = true,
valign = CENTER
};
button_box.append (settings_button_revealer);
button_box.append (connect_button);

var grid = new Gtk.Grid () {
valign = Gtk.Align.CENTER,
Expand All @@ -98,7 +116,7 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {
grid.attach (lock_img, 2, 0);
grid.attach (error_img, 3, 0, 1, 2);
grid.attach (spinner, 4, 0, 1, 2);
grid.attach (connect_button_revealer, 5, 0, 1, 2);
grid.attach (button_box, 5, 0, 1, 2);

_ap = new Gee.LinkedList<NM.AccessPoint> ();

Expand All @@ -110,9 +128,8 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {
notify["state"].connect (update);
notify["active"].connect (update);

connect_button.clicked.connect (() => {
user_action ();
});
connect_button.clicked.connect (() => user_action ());
settings_button.clicked.connect (() => settings_request ());

update ();
}
Expand Down Expand Up @@ -159,8 +176,9 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {

hide_item (error_img);
spinner.spinning = false;

connect_button_revealer.reveal_child = true;
button_box.sensitive = true;
settings_button_revealer.reveal_child = false;
connect_button.label = _("Connect");

switch (state) {
case NM.DeviceState.FAILED:
Expand All @@ -169,10 +187,12 @@ public class Network.WifiMenuItem : Gtk.ListBoxRow {
break;
case NM.DeviceState.PREPARE:
spinner.spinning = true;
button_box.sensitive = false;
state_string = _("Connecting");
break;
case NM.DeviceState.ACTIVATED:
connect_button_revealer.reveal_child = false;
settings_button_revealer.reveal_child = true;
connect_button.label = _("Disconnect");
break;
}

Expand Down

0 comments on commit 6b4ccb4

Please sign in to comment.