refactor: Vec::with_capacity where possible
This commit is contained in:
parent
cbbd350fc6
commit
a423aaa27a
2 changed files with 46 additions and 84 deletions
|
|
@ -7,7 +7,7 @@ use cosmic::{
|
||||||
popup::{destroy_popup, get_popup},
|
popup::{destroy_popup, get_popup},
|
||||||
SurfaceIdWrapper,
|
SurfaceIdWrapper,
|
||||||
},
|
},
|
||||||
widget::{column, container, row, scrollable, text, text_input},
|
widget::{column, container, row, scrollable, text, text_input, Column},
|
||||||
Alignment, Application, Color, Command, Length, Subscription,
|
Alignment, Application, Color, Command, Length, Subscription,
|
||||||
},
|
},
|
||||||
iced_native::{
|
iced_native::{
|
||||||
|
|
@ -328,20 +328,16 @@ impl Application for CosmicNetworkApplet {
|
||||||
for conn in &self.nm_state.active_conns {
|
for conn in &self.nm_state.active_conns {
|
||||||
match conn {
|
match conn {
|
||||||
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
|
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
|
||||||
let mut ipv4 = column![];
|
let mut ipv4 = Vec::with_capacity(ip_addresses.len());
|
||||||
for addr in ip_addresses {
|
for addr in ip_addresses {
|
||||||
match addr {
|
ipv4.push(
|
||||||
std::net::IpAddr::V4(a) => {
|
text(format!("{}: {}", fl!("ipv4"), addr.to_string()))
|
||||||
ipv4 = ipv4.push(
|
.size(12)
|
||||||
text(format!("{}: {}", fl!("ipv4"), a.to_string()))
|
.into(),
|
||||||
.size(12),
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
std::net::IpAddr::V6(_) => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
vpn_ethernet_col =
|
vpn_ethernet_col = vpn_ethernet_col
|
||||||
vpn_ethernet_col.push(column![text(name), ipv4].spacing(4));
|
.push(column![text(name), Column::with_children(ipv4)].spacing(4));
|
||||||
}
|
}
|
||||||
ActiveConnectionInfo::Wired {
|
ActiveConnectionInfo::Wired {
|
||||||
name,
|
name,
|
||||||
|
|
@ -349,17 +345,13 @@ impl Application for CosmicNetworkApplet {
|
||||||
speed,
|
speed,
|
||||||
ip_addresses,
|
ip_addresses,
|
||||||
} => {
|
} => {
|
||||||
let mut ipv4 = column![];
|
let mut ipv4 = Vec::with_capacity(ip_addresses.len());
|
||||||
for addr in ip_addresses {
|
for addr in ip_addresses {
|
||||||
match addr {
|
ipv4.push(
|
||||||
std::net::IpAddr::V4(a) => {
|
text(format!("{}: {}", fl!("ipv4"), addr.to_string()))
|
||||||
ipv4 = ipv4.push(
|
.size(12)
|
||||||
text(format!("{}: {}", fl!("ipv4"), a.to_string()))
|
.into(),
|
||||||
.size(12),
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
std::net::IpAddr::V6(a) => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
vpn_ethernet_col = vpn_ethernet_col.push(
|
vpn_ethernet_col = vpn_ethernet_col.push(
|
||||||
column![
|
column![
|
||||||
|
|
@ -368,7 +360,7 @@ impl Application for CosmicNetworkApplet {
|
||||||
text(format!("{speed} {}", fl!("megabits-per-second")))
|
text(format!("{speed} {}", fl!("megabits-per-second")))
|
||||||
]
|
]
|
||||||
.spacing(16),
|
.spacing(16),
|
||||||
ipv4,
|
Column::with_children(ipv4),
|
||||||
]
|
]
|
||||||
.spacing(4),
|
.spacing(4),
|
||||||
);
|
);
|
||||||
|
|
@ -376,17 +368,13 @@ impl Application for CosmicNetworkApplet {
|
||||||
ActiveConnectionInfo::WiFi {
|
ActiveConnectionInfo::WiFi {
|
||||||
name, ip_addresses, ..
|
name, ip_addresses, ..
|
||||||
} => {
|
} => {
|
||||||
let mut ipv4 = column![];
|
let mut ipv4 = Vec::with_capacity(ip_addresses.len());
|
||||||
for addr in ip_addresses {
|
for addr in ip_addresses {
|
||||||
match addr {
|
ipv4.push(
|
||||||
std::net::IpAddr::V4(a) => {
|
text(format!("{}: {}", fl!("ipv4"), addr.to_string()))
|
||||||
ipv4 = ipv4.push(
|
.size(12)
|
||||||
text(format!("{}: {}", fl!("ipv4"), a.to_string()))
|
.into(),
|
||||||
.size(12),
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
std::net::IpAddr::V6(_) => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
known_wifi = known_wifi.push(column![button(Button::Secondary)
|
known_wifi = known_wifi.push(column![button(Button::Secondary)
|
||||||
.custom(vec![
|
.custom(vec![
|
||||||
|
|
@ -397,7 +385,8 @@ impl Application for CosmicNetworkApplet {
|
||||||
.width(Length::Units(24))
|
.width(Length::Units(24))
|
||||||
.height(Length::Units(24))
|
.height(Length::Units(24))
|
||||||
.into(),
|
.into(),
|
||||||
column![text(name).size(14), ipv4,].into(),
|
column![text(name).size(14), Column::with_children(ipv4)]
|
||||||
|
.into(),
|
||||||
text(format!("{}", fl!("connected")))
|
text(format!("{}", fl!("connected")))
|
||||||
.size(14)
|
.size(14)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
|
|
@ -615,7 +604,8 @@ impl Application for CosmicNetworkApplet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if self.nm_state.wifi_enabled {
|
} else if self.nm_state.wifi_enabled {
|
||||||
let mut list_col = column![];
|
let mut list_col =
|
||||||
|
Vec::with_capacity(self.nm_state.wireless_access_points.len());
|
||||||
for ap in &self.nm_state.wireless_access_points {
|
for ap in &self.nm_state.wireless_access_points {
|
||||||
if self
|
if self
|
||||||
.nm_state
|
.nm_state
|
||||||
|
|
@ -644,9 +634,11 @@ impl Application for CosmicNetworkApplet {
|
||||||
.on_press(Message::SelectWirelessAccessPoint(ap.clone()))
|
.on_press(Message::SelectWirelessAccessPoint(ap.clone()))
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.padding([8, 24]);
|
.padding([8, 24]);
|
||||||
list_col = list_col.push(button);
|
list_col.push(button.into());
|
||||||
}
|
}
|
||||||
content = content.push(scrollable(list_col).height(Length::Units(300)));
|
content = content.push(
|
||||||
|
scrollable(Column::with_children(list_col)).height(Length::Units(300)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.applet_helper.popup_container(content).into()
|
self.applet_helper.popup_container(content).into()
|
||||||
|
|
|
||||||
|
|
@ -5,59 +5,29 @@ use cosmic_dbus_networkmanager::{
|
||||||
device::SpecificDevice,
|
device::SpecificDevice,
|
||||||
interface::enums::{ApFlags, ApSecurityFlags},
|
interface::enums::{ApFlags, ApSecurityFlags},
|
||||||
};
|
};
|
||||||
use std::net::IpAddr;
|
use std::net::Ipv4Addr;
|
||||||
|
|
||||||
pub async fn active_connections(
|
pub async fn active_connections(
|
||||||
active_connections: Vec<ActiveConnection<'_>>,
|
active_connections: Vec<ActiveConnection<'_>>,
|
||||||
) -> zbus::Result<Vec<ActiveConnectionInfo>> {
|
) -> zbus::Result<Vec<ActiveConnectionInfo>> {
|
||||||
let mut info = Vec::<ActiveConnectionInfo>::with_capacity(active_connections.len());
|
let mut info = Vec::<ActiveConnectionInfo>::with_capacity(active_connections.len());
|
||||||
for connection in active_connections {
|
for connection in active_connections {
|
||||||
|
let ipv4 = connection
|
||||||
|
.ip4_config()
|
||||||
|
.await?
|
||||||
|
.address_data()
|
||||||
|
.await
|
||||||
|
.unwrap_or_default();
|
||||||
|
let addresses: Vec<_> = ipv4.iter().map(|d| d.address).collect();
|
||||||
|
|
||||||
if connection.vpn().await.unwrap_or_default() {
|
if connection.vpn().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));
|
|
||||||
}
|
|
||||||
info.push(ActiveConnectionInfo::Vpn {
|
info.push(ActiveConnectionInfo::Vpn {
|
||||||
name: connection.id().await?,
|
name: connection.id().await?,
|
||||||
ip_addresses,
|
ip_addresses: addresses.clone(),
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for device in connection.devices().await.unwrap_or_default() {
|
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
|
match device
|
||||||
.downcast_to_device()
|
.downcast_to_device()
|
||||||
.await
|
.await
|
||||||
|
|
@ -69,14 +39,14 @@ pub async fn active_connections(
|
||||||
name: connection.id().await?,
|
name: connection.id().await?,
|
||||||
hw_address: wired_device.hw_address().await?,
|
hw_address: wired_device.hw_address().await?,
|
||||||
speed: wired_device.speed().await?,
|
speed: wired_device.speed().await?,
|
||||||
ip_addresses,
|
ip_addresses: addresses.clone(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Some(SpecificDevice::Wireless(wireless_device)) => {
|
Some(SpecificDevice::Wireless(wireless_device)) => {
|
||||||
if let Ok(access_point) = wireless_device.active_access_point().await {
|
if let Ok(access_point) = wireless_device.active_access_point().await {
|
||||||
info.push(ActiveConnectionInfo::WiFi {
|
info.push(ActiveConnectionInfo::WiFi {
|
||||||
name: String::from_utf8_lossy(&access_point.ssid().await?).into_owned(),
|
name: String::from_utf8_lossy(&access_point.ssid().await?).into_owned(),
|
||||||
ip_addresses,
|
ip_addresses: addresses.clone(),
|
||||||
hw_address: wireless_device.hw_address().await?,
|
hw_address: wireless_device.hw_address().await?,
|
||||||
flags: access_point.flags().await?,
|
flags: access_point.flags().await?,
|
||||||
rsn_flags: access_point.rsn_flags().await?,
|
rsn_flags: access_point.rsn_flags().await?,
|
||||||
|
|
@ -87,7 +57,7 @@ pub async fn active_connections(
|
||||||
Some(SpecificDevice::WireGuard(_)) => {
|
Some(SpecificDevice::WireGuard(_)) => {
|
||||||
info.push(ActiveConnectionInfo::Vpn {
|
info.push(ActiveConnectionInfo::Vpn {
|
||||||
name: connection.id().await?,
|
name: connection.id().await?,
|
||||||
ip_addresses,
|
ip_addresses: addresses.clone(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
@ -113,11 +83,11 @@ pub enum ActiveConnectionInfo {
|
||||||
name: String,
|
name: String,
|
||||||
hw_address: String,
|
hw_address: String,
|
||||||
speed: u32,
|
speed: u32,
|
||||||
ip_addresses: Vec<IpAddr>,
|
ip_addresses: Vec<Ipv4Addr>,
|
||||||
},
|
},
|
||||||
WiFi {
|
WiFi {
|
||||||
name: String,
|
name: String,
|
||||||
ip_addresses: Vec<IpAddr>,
|
ip_addresses: Vec<Ipv4Addr>,
|
||||||
hw_address: String,
|
hw_address: String,
|
||||||
flags: ApFlags,
|
flags: ApFlags,
|
||||||
rsn_flags: ApSecurityFlags,
|
rsn_flags: ApSecurityFlags,
|
||||||
|
|
@ -125,7 +95,7 @@ pub enum ActiveConnectionInfo {
|
||||||
},
|
},
|
||||||
Vpn {
|
Vpn {
|
||||||
name: String,
|
name: String,
|
||||||
ip_addresses: Vec<IpAddr>,
|
ip_addresses: Vec<Ipv4Addr>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue