Use ListBoxes for the available and current networks

This commit is contained in:
Lucy 2022-02-09 17:23:36 -05:00
parent 8ef033f39e
commit 770dffa729
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
2 changed files with 20 additions and 16 deletions

View file

@ -9,32 +9,34 @@ use futures_util::StreamExt;
use gtk4::{ use gtk4::{
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender}, glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
prelude::*, prelude::*,
Align, Image, Separator, Align, Image, ListBox, ListBoxRow, Separator,
}; };
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use zbus::Connection; use zbus::Connection;
pub fn add_available_wifi(target: &gtk4::Box, separator: Separator) { pub fn add_available_wifi(target: &gtk4::Box, separator: Separator) {
let ap_entries = Rc::<RefCell<Vec<SettingsEntry>>>::default(); let ap_entries = Rc::<RefCell<Vec<ListBoxRow>>>::default();
let (tx, rx) = MainContext::channel::<Vec<AccessPoint>>(PRIORITY_DEFAULT); let (tx, rx) = MainContext::channel::<Vec<AccessPoint>>(PRIORITY_DEFAULT);
task::spawn(async move { task::spawn(async move {
if let Err(err) = scan_for_wifi(tx).await { if let Err(err) = scan_for_wifi(tx).await {
eprintln!("scan_for_wifi failed: {}", err); eprintln!("scan_for_wifi failed: {}", err);
} }
}); });
let wifi_list = ListBox::new();
rx.attach( rx.attach(
None, None,
clone!(@strong ap_entries, @weak target, @weak separator, => @default-return Continue(true), move |aps| { clone!(@strong ap_entries, @weak wifi_list, @weak separator, => @default-return Continue(true), move |aps| {
build_aps_list(ap_entries.clone(), target, aps); build_aps_list(ap_entries.clone(), &wifi_list, aps);
separator.set_visible(!ap_entries.borrow().is_empty()); separator.set_visible(!ap_entries.borrow().is_empty());
Continue(true) Continue(true)
}), }),
); );
target.append(&wifi_list);
} }
fn build_aps_list( fn build_aps_list(
ap_entries: Rc<RefCell<Vec<SettingsEntry>>>, ap_entries: Rc<RefCell<Vec<ListBoxRow>>>,
target: gtk4::Box, target: &ListBox,
aps: Vec<AccessPoint>, aps: Vec<AccessPoint>,
) { ) {
let mut ap_entries = ap_entries.borrow_mut(); let mut ap_entries = ap_entries.borrow_mut();
@ -51,8 +53,9 @@ fn build_aps_list(
} }
} }
ap_entry.align_child(Align::Start); // view! seems to reorder everything in alphabetical order, but align_child must always come after set_child ap_entry.align_child(Align::Start); // view! seems to reorder everything in alphabetical order, but align_child must always come after set_child
target.append(&ap_entry); let entry = ListBoxRow::builder().child(&ap_entry).build();
ap_entries.push(ap_entry); target.append(&entry);
ap_entries.push(entry);
} }
} }

View file

@ -9,31 +9,31 @@ use cosmic_dbus_networkmanager::{
use gtk4::{ use gtk4::{
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender}, glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
prelude::*, prelude::*,
Image, Orientation, Image, ListBox, ListBoxRow, Orientation,
}; };
use std::{cell::RefCell, net::IpAddr, rc::Rc}; use std::{cell::RefCell, net::IpAddr, rc::Rc};
use zbus::Connection; use zbus::Connection;
pub fn add_current_networks(target: &gtk4::Box) { pub fn add_current_networks(target: &gtk4::Box) {
let our_box = gtk4::Box::new(Orientation::Vertical, 8); let networks_list = ListBox::new();
let entries = Rc::<RefCell<Vec<gtk4::Box>>>::default(); let entries = Rc::<RefCell<Vec<ListBoxRow>>>::default();
let (tx, rx) = MainContext::channel::<Vec<ActiveConnectionInfo>>(PRIORITY_DEFAULT); let (tx, rx) = MainContext::channel::<Vec<ActiveConnectionInfo>>(PRIORITY_DEFAULT);
crate::task::spawn(handle_devices(tx)); crate::task::spawn(handle_devices(tx));
rx.attach( rx.attach(
None, None,
clone!(@weak our_box, @strong entries => @default-return Continue(true), move |connections| { clone!(@weak networks_list, @strong entries => @default-return Continue(true), move |connections| {
let mut entries = entries.borrow_mut(); let mut entries = entries.borrow_mut();
display_active_connections(connections, &our_box, &mut *entries); display_active_connections(connections, &networks_list, &mut *entries);
Continue(true) Continue(true)
}), }),
); );
target.append(&our_box); target.append(&networks_list);
} }
fn display_active_connections( fn display_active_connections(
connections: Vec<ActiveConnectionInfo>, connections: Vec<ActiveConnectionInfo>,
target: &gtk4::Box, target: &ListBox,
entries: &mut Vec<gtk4::Box>, entries: &mut Vec<ListBoxRow>,
) { ) {
for old_entry in entries.drain(..) { for old_entry in entries.drain(..) {
target.remove(&old_entry); target.remove(&old_entry);
@ -54,6 +54,7 @@ fn display_active_connections(
wpa_flags, wpa_flags,
} => todo!(), } => todo!(),
}; };
let entry = ListBoxRow::builder().child(&entry).build();
target.append(&entry); target.append(&entry);
entries.push(entry); entries.push(entry);
} }