wip: network applet more like mockup

This commit is contained in:
Ashley Wulber 2023-01-04 22:12:46 -05:00
parent b5371f58f7
commit 13ccc03676
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
5 changed files with 376 additions and 139 deletions

View file

@ -6,15 +6,31 @@ use cosmic::{
popup::{destroy_popup, get_popup},
SurfaceIdWrapper,
},
widget::{column, container, row, scrollable, text},
widget::{column, container, row, scrollable, text, text_input},
Alignment, Application, Color, Command, Length, Subscription,
},
iced_native::window,
iced_style::{application, svg},
iced_native::{
alignment::{Horizontal, Vertical},
layout::Limits,
renderer::BorderRadius,
subscription, window,
},
iced_style::{application, button::StyleSheet, svg},
theme::{Button, Svg},
widget::{button, horizontal_rule, icon, list_column, toggler},
Element, Theme,
};
use cosmic::{
iced_style,
widget::segmented_button::{
self,
cosmic::{
horizontal_segmented_selection, horizontal_view_switcher, vertical_segmented_selection,
vertical_view_switcher,
},
},
};
use cosmic_dbus_networkmanager::access_point;
use futures::channel::mpsc::UnboundedSender;
use crate::{
@ -30,7 +46,29 @@ pub fn run() -> cosmic::iced::Result {
CosmicNetworkApplet::run(helper.window_settings())
}
#[derive(Clone, Default)]
enum NewConnectionState {
EnterPassword {
access_point: AccessPoint,
password: String,
},
Waiting(AccessPoint),
Failure(AccessPoint),
}
impl Into<AccessPoint> for NewConnectionState {
fn into(self) -> AccessPoint {
match self {
NewConnectionState::EnterPassword {
access_point,
password,
} => access_point,
NewConnectionState::Waiting(access_point) => access_point,
NewConnectionState::Failure(access_point) => access_point,
}
}
}
#[derive(Default)]
struct CosmicNetworkApplet {
icon_name: String,
theme: Theme,
@ -43,6 +81,8 @@ struct CosmicNetworkApplet {
wireless_access_points: Vec<AccessPoint>,
active_conns: Vec<ActiveConnectionInfo>,
nm_sender: Option<UnboundedSender<NetworkManagerRequest>>,
show_visible_networks: bool,
new_connection: Option<NewConnectionState>,
}
impl CosmicNetworkApplet {
@ -72,10 +112,14 @@ enum Message {
TogglePopup,
ToggleAirplaneMode(bool),
ToggleWiFi(bool),
ToggleVisibleNetworks,
Errored(String),
Ignore,
NetworkManagerEvent(NetworkManagerEvent),
SelectWirelessAccessPoint(String),
SelectWirelessAccessPoint(AccessPoint),
CancelNewConnection,
Password(String),
SubmitPassword,
}
impl Application for CosmicNetworkApplet {
@ -109,13 +153,18 @@ impl Application for CosmicNetworkApplet {
let new_id = window::Id::new(self.id_ctr);
self.popup.replace(new_id);
let popup_settings = self.applet_helper.get_popup_settings(
let mut popup_settings = self.applet_helper.get_popup_settings(
window::Id::new(0),
new_id,
None,
None,
None,
);
popup_settings.positioner.size_limits = Limits::NONE
.min_height(1)
.min_width(1)
.max_height(600)
.max_width(600);
return get_popup(popup_settings);
}
}
@ -166,20 +215,65 @@ impl Application for CosmicNetworkApplet {
if success {
self.wireless_access_points = wireless_access_points;
self.active_conns = active_conns;
self.wifi = wifi_enabled;
self.update_icon_name();
}
}
},
Message::SelectWirelessAccessPoint(ssid) => {
if let Some(tx) = self.nm_sender.as_ref() {
let _ = tx.unbounded_send(NetworkManagerRequest::SelectAccessPoint(ssid));
Message::SelectWirelessAccessPoint(access_point) => {
// if let Some(tx) = self.nm_sender.as_ref() {
// let _ = tx.unbounded_send(NetworkManagerRequest::SelectAccessPoint(
// access_point.ssid.clone(),
// ));
// }
self.new_connection
.replace(NewConnectionState::EnterPassword {
access_point,
password: String::new(),
});
}
Message::ToggleVisibleNetworks => {
self.new_connection.take();
self.show_visible_networks = !self.show_visible_networks;
}
Message::Password(entered_pw) => {
dbg!(&entered_pw);
match &mut self.new_connection {
Some(NewConnectionState::EnterPassword { password, .. }) => {
*password = entered_pw;
}
_ => {}
}
}
Message::SubmitPassword => {
// TODO setup connection
match self.new_connection.take() {
Some(new_connection) => {
self.new_connection
.replace(NewConnectionState::Failure(new_connection.into()));
}
None => {}
}
}
Message::CancelNewConnection => {
self.new_connection.take();
}
}
Command::none()
}
fn view(&self, id: SurfaceIdWrapper) -> Element<Message> {
let button_style = Button::Custom {
active: |t| iced_style::button::Appearance {
border_radius: BorderRadius::from(0.0),
..t.active(&Button::Text)
},
hover: |t| iced_style::button::Appearance {
border_radius: BorderRadius::from(0.0),
..t.hovered(&Button::Text)
},
};
match id {
SurfaceIdWrapper::LayerSurface(_) => unimplemented!(),
SurfaceIdWrapper::Window(_) => self
@ -188,39 +282,23 @@ impl Application for CosmicNetworkApplet {
.on_press(Message::TogglePopup)
.into(),
SurfaceIdWrapper::Popup(_) => {
let name = text(fl!("network")).size(18);
let icon = icon(&*self.icon_name, 24)
.style(Svg::Custom(|theme| svg::Appearance {
color: Some(theme.palette().text),
}))
.width(Length::Units(24))
.height(Length::Units(24));
let mut list_col = list_column();
let mut list_col = column![];
for conn in &self.active_conns {
let el = match conn {
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
let mut ipv4 = column![];
let mut ipv6 = column![];
for addr in ip_addresses {
match addr {
std::net::IpAddr::V4(a) => {
ipv4 = ipv4.push(text(format!(
"{}: {}",
fl!("ipv4"),
a.to_string()
)));
}
std::net::IpAddr::V6(a) => {
ipv6 = ipv6.push(text(format!(
"{}: {}",
fl!("ipv6"),
a.to_string()
)));
ipv4 = ipv4.push(
text(format!("{}: {}", fl!("ipv4"), a.to_string()))
.size(12),
);
}
std::net::IpAddr::V6(a) => {}
}
}
column![text(name), ipv4, ipv6].spacing(4)
column![text(name), ipv4].spacing(4)
}
ActiveConnectionInfo::Wired {
name,
@ -229,23 +307,15 @@ impl Application for CosmicNetworkApplet {
ip_addresses,
} => {
let mut ipv4 = column![];
let mut ipv6 = column![];
for addr in ip_addresses {
match addr {
std::net::IpAddr::V4(a) => {
ipv4 = ipv4.push(text(format!(
"{}: {}",
fl!("ipv4"),
a.to_string()
)));
}
std::net::IpAddr::V6(a) => {
ipv6 = ipv6.push(text(format!(
"{}: {}",
fl!("ipv6"),
a.to_string()
)));
ipv4 = ipv4.push(
text(format!("{}: {}", fl!("ipv4"), a.to_string()))
.size(12),
);
}
std::net::IpAddr::V6(a) => {}
}
}
column![
@ -255,27 +325,51 @@ impl Application for CosmicNetworkApplet {
]
.spacing(16),
ipv4,
ipv6,
text(format!("{}: {hw_address}", fl!("mac"))),
]
.spacing(4)
}
ActiveConnectionInfo::WiFi {
name, hw_address, ..
} => column![row![
text(name),
text(format!("{}: {hw_address}", fl!("mac")))
]
.spacing(12)]
.spacing(4),
name, ip_addresses, ..
} => {
let mut ipv4 = column![];
for addr in ip_addresses {
match addr {
std::net::IpAddr::V4(a) => {
ipv4 = ipv4.push(
text(format!("{}: {}", fl!("ipv4"), a.to_string()))
.size(12),
);
}
std::net::IpAddr::V6(a) => {}
}
}
column![button(Button::Secondary)
.custom(vec![
icon("network-wireless-symbolic", 24)
.style(Svg::Custom(|theme| svg::Appearance {
color: Some(theme.palette().text),
}))
.width(Length::Units(24))
.height(Length::Units(24))
.into(),
column![text(name).size(14), ipv4,].into(),
text(format!("{}", fl!("connected")))
.size(14)
.width(Length::Fill)
.height(Length::Units(24))
.horizontal_alignment(Horizontal::Right)
.vertical_alignment(Vertical::Center)
.into()
])
.padding([8, 24])
.style(button_style.clone())
.on_press(Message::Ignore)]
}
};
list_col = list_col.add(el);
list_col = list_col.push(el);
}
let mut content = column![
row![icon, name].spacing(8).width(Length::Fill),
list_col,
horizontal_rule(1),
container(
toggler(fl!("airplane-mode"), self.airplane_mode, |m| {
Message::ToggleAirplaneMode(m)
@ -289,36 +383,178 @@ impl Application for CosmicNetworkApplet {
.width(Length::Fill)
)
.padding([0, 12]),
horizontal_rule(1),
list_col,
]
.align_items(Alignment::Center)
.spacing(8)
.padding(8);
if self.wifi {
let mut list_col = list_column();
for ap in &self.wireless_access_points {
let button = self
.active_conns
.iter()
.find_map(|conn| match conn {
ActiveConnectionInfo::WiFi { name, .. } if name == &ap.ssid => {
Some(
button(Button::Primary)
.text(&ap.ssid)
.on_press(Message::Ignore)
.width(Length::Fill),
)
}
_ => None,
})
.unwrap_or_else(|| {
button(Button::Text)
.text(&ap.ssid)
.on_press(Message::SelectWirelessAccessPoint(ap.ssid.clone()))
.width(Length::Fill)
});
list_col = list_col.add(button);
.padding([8, 0]);
let dropdown_icon = if self.show_visible_networks {
"go-down-symbolic"
} else {
"go-next-symbolic"
};
let available_connections_btn = button(Button::Secondary)
.custom(
vec![
text(fl!("visible-wireless-networks"))
.size(14)
.width(Length::Fill)
.height(Length::Units(24))
.vertical_alignment(Vertical::Center)
.into(),
container(
icon(dropdown_icon, 14)
.style(Svg::Custom(|theme| svg::Appearance {
color: Some(theme.palette().text),
}))
.width(Length::Units(14))
.height(Length::Units(14)),
)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.width(Length::Units(24))
.height(Length::Units(24))
.into(),
]
.into(),
)
.padding([8, 24])
.style(button_style.clone())
.on_press(Message::ToggleVisibleNetworks);
content = content.push(available_connections_btn);
if self.show_visible_networks {
if let Some(new_conn_state) = self.new_connection.as_ref() {
match new_conn_state {
NewConnectionState::EnterPassword {
access_point,
password,
} => {
let id = row![
icon("network-wireless-symbolic", 24)
.style(Svg::Custom(|theme| svg::Appearance {
color: Some(theme.palette().text),
}))
.width(Length::Units(24))
.height(Length::Units(24)),
text(&access_point.ssid).size(14),
]
.align_items(Alignment::Center)
.width(Length::Fill)
.padding([0, 24])
.spacing(12);
content = content.push(id);
let col = column![
text(fl!("enter-password")),
text_input("", password, Message::Password)
.on_submit(Message::SubmitPassword)
.password(),
container(text(fl!("router-wps-button"))).padding(8),
row![
button(Button::Secondary)
.custom(vec![container(text(fl!("cancel")))
.padding([0, 24])
.into()])
.on_press(Message::CancelNewConnection),
button(Button::Secondary).custom(vec![container(text(
fl!("connect")
))
.padding([0, 24])
.into()])
]
.spacing(24)
]
.spacing(8)
.padding([0, 48])
.align_items(Alignment::Center);
content = content.push(col);
}
NewConnectionState::Waiting(access_point) => {
let connecting = row![
icon("network-wireless-symbolic", 24)
.style(Svg::Custom(|theme| svg::Appearance {
color: Some(theme.palette().text),
}))
.width(Length::Units(24))
.height(Length::Units(24)),
text(format!("{}", fl!("connecting")))
.size(14)
.width(Length::Fill)
.height(Length::Units(24))
.horizontal_alignment(Horizontal::Right)
.vertical_alignment(Vertical::Center)
];
content = content.push(connecting);
}
NewConnectionState::Failure(access_point) => {
let id = row![
icon("network-wireless-symbolic", 24)
.style(Svg::Custom(|theme| svg::Appearance {
color: Some(theme.palette().text),
}))
.width(Length::Units(24))
.height(Length::Units(24)),
text(&access_point.ssid).size(14),
]
.align_items(Alignment::Center)
.width(Length::Fill)
.padding([0, 24])
.spacing(8);
content = content.push(id);
let col = column![
text(fl!("unable-to-connect")),
text(fl!("check-wifi-connection")),
row![
button(Button::Secondary)
.custom(vec![container(text("Cancel"))
.padding([0, 24])
.into()])
.on_press(Message::CancelNewConnection),
button(Button::Secondary)
.custom(vec![container(text("Connect"))
.padding([0, 24])
.into()])
.on_press(Message::SelectWirelessAccessPoint(
access_point.clone()
))
]
.spacing(24)
]
.spacing(16)
.padding([0, 48])
.align_items(Alignment::Center);
content = content.push(col);
}
}
} else if self.wifi {
let mut list_col = column![];
for ap in &self.wireless_access_points {
if self.active_conns.iter().any(|a| ap.ssid == a.name()) {
continue;
}
let button = button(button_style)
.custom(vec![row![
icon("network-wireless-symbolic", 16)
.style(Svg::Custom(|theme| svg::Appearance {
color: Some(theme.palette().text),
}))
.width(Length::Units(16))
.height(Length::Units(16)),
text(&ap.ssid)
.size(14)
.height(Length::Units(24))
.vertical_alignment(Vertical::Center)
]
.align_items(Alignment::Center)
.spacing(12)
.into()])
.on_press(Message::SelectWirelessAccessPoint(ap.clone()))
.width(Length::Fill)
.padding([8, 24]);
list_col = list_col.push(button);
}
content = content.push(scrollable(list_col).height(Length::Units(300)));
}
content = content.push(scrollable(list_col).height(Length::Units(300)));
}
self.applet_helper.popup_container(content).into()
}

View file

@ -39,6 +39,25 @@ pub async fn active_connections(
continue;
}
for device in connection.devices().await.unwrap_or_default() {
let mut ip_addresses = Vec::new();
for address_data in connection
.ip4_config()
.await?
.address_data()
.await
.unwrap_or_default()
{
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in connection
.ip6_config()
.await?
.address_data()
.await
.unwrap_or_default()
{
ip_addresses.push(IpAddr::V6(address_data.address));
}
match device
.downcast_to_device()
.await
@ -46,25 +65,6 @@ pub async fn active_connections(
.and_then(|inner| inner)
{
Some(SpecificDevice::Wired(wired_device)) => {
let mut ip_addresses = Vec::new();
for address_data in device
.ip4_config()
.await?
.address_data()
.await
.unwrap_or_default()
{
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in device
.ip6_config()
.await?
.address_data()
.await
.unwrap_or_default()
{
ip_addresses.push(IpAddr::V6(address_data.address));
}
info.push(ActiveConnectionInfo::Wired {
name: connection.id().await?,
hw_address: wired_device.hw_address().await?,
@ -76,6 +76,7 @@ pub async fn active_connections(
if let Ok(access_point) = wireless_device.active_access_point().await {
info.push(ActiveConnectionInfo::WiFi {
name: String::from_utf8_lossy(&access_point.ssid().await?).into_owned(),
ip_addresses,
hw_address: wireless_device.hw_address().await?,
flags: access_point.flags().await?,
rsn_flags: access_point.rsn_flags().await?,
@ -84,25 +85,6 @@ pub async fn active_connections(
}
}
Some(SpecificDevice::WireGuard(_)) => {
let mut ip_addresses = Vec::new();
for address_data in connection
.ip4_config()
.await?
.address_data()
.await
.unwrap_or_default()
{
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in connection
.ip6_config()
.await?
.address_data()
.await
.unwrap_or_default()
{
ip_addresses.push(IpAddr::V6(address_data.address));
}
info.push(ActiveConnectionInfo::Vpn {
name: connection.id().await?,
ip_addresses,
@ -135,6 +117,7 @@ pub enum ActiveConnectionInfo {
},
WiFi {
name: String,
ip_addresses: Vec<IpAddr>,
hw_address: String,
flags: ApFlags,
rsn_flags: ApSecurityFlags,
@ -145,3 +128,13 @@ pub enum ActiveConnectionInfo {
ip_addresses: Vec<IpAddr>,
},
}
impl ActiveConnectionInfo {
pub fn name(&self) -> String {
match &self {
ActiveConnectionInfo::Wired { name, .. } => name.clone(),
ActiveConnectionInfo::WiFi { name, .. } => name.clone(),
ActiveConnectionInfo::Vpn { name, .. } => name.clone(),
}
}
}