Init Animations: animate toggler widgets (#96)

This commit is contained in:
Brock 2023-06-07 13:43:49 -06:00 committed by GitHub
parent 8119b29aca
commit 3ec06bef80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 251 additions and 153 deletions

View file

@ -13,11 +13,13 @@ use cosmic::{
},
iced_style::{application, button::StyleSheet},
theme::{Button, Svg},
widget::{button, divider, icon, toggler},
widget::{button, divider, icon},
Element, Theme,
};
use cosmic_applet::CosmicAppletHelper;
use cosmic_dbus_networkmanager::interface::enums::{ActiveConnectionState, DeviceState};
use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline};
use futures::channel::mpsc::UnboundedSender;
use zbus::Connection;
@ -76,6 +78,9 @@ impl Into<AccessPoint> for NewConnectionState {
}
}
static WIFI: Lazy<id::Toggler> = Lazy::new(id::Toggler::unique);
static AIRPLANE_MODE: Lazy<id::Toggler> = Lazy::new(id::Toggler::unique);
#[derive(Default)]
struct CosmicNetworkApplet {
icon_name: String,
@ -89,6 +94,7 @@ struct CosmicNetworkApplet {
show_visible_networks: bool,
new_connection: Option<NewConnectionState>,
conn: Option<Connection>,
timeline: Timeline,
}
impl CosmicNetworkApplet {
@ -112,6 +118,29 @@ impl CosmicNetworkApplet {
})
.to_string()
}
fn update_togglers(&mut self, state: &NetworkManagerState) {
let timeline = &mut self.timeline;
if state.wifi_enabled != self.nm_state.wifi_enabled {
let chain = if state.wifi_enabled {
chain::Toggler::on(WIFI.clone(), 1.)
} else {
chain::Toggler::off(WIFI.clone(), 1.)
};
timeline.set_chain(chain);
};
if state.airplane_mode != self.nm_state.airplane_mode {
let chain = if state.airplane_mode {
chain::Toggler::on(AIRPLANE_MODE.clone(), 1.)
} else {
chain::Toggler::off(AIRPLANE_MODE.clone(), 1.)
};
timeline.set_chain(chain);
};
timeline.start();
}
}
#[derive(Debug, Clone)]
@ -122,13 +151,14 @@ pub(crate) enum Message {
ToggleAirplaneMode(bool),
ToggleWiFi(bool),
ToggleVisibleNetworks,
Errored(String),
Ignore,
NetworkManagerEvent(NetworkManagerEvent),
SelectWirelessAccessPoint(AccessPoint),
CancelNewConnection,
Password(String),
SubmitPassword,
Frame(Instant),
// Errored(String),
}
impl Application for CosmicNetworkApplet {
@ -153,6 +183,7 @@ impl Application for CosmicNetworkApplet {
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::Frame(now) => self.timeline.now(now),
Message::TogglePopup => {
if let Some(p) = self.popup.take() {
self.show_visible_networks = false;
@ -179,20 +210,14 @@ impl Application for CosmicNetworkApplet {
return get_popup(popup_settings);
}
}
Message::Errored(_) => todo!(),
// Message::Errored(_) => todo!(),
Message::Ignore => {}
Message::ToggleAirplaneMode(enabled) => {
self.nm_state.airplane_mode = enabled;
if let Some(tx) = self.nm_sender.as_mut() {
let _ = tx.unbounded_send(NetworkManagerRequest::SetAirplaneMode(enabled));
}
}
Message::ToggleWiFi(enabled) => {
if !enabled {
self.nm_state.clear();
}
self.nm_state.wifi_enabled = enabled;
if let Some(tx) = self.nm_sender.as_mut() {
let _ = tx.unbounded_send(NetworkManagerRequest::SetWiFi(enabled));
}
@ -204,6 +229,7 @@ impl Application for CosmicNetworkApplet {
state,
} => {
self.nm_sender.replace(sender);
self.update_togglers(&state);
self.nm_state = state;
self.update_icon_name();
self.conn = Some(conn);
@ -236,7 +262,9 @@ impl Application for CosmicNetworkApplet {
self.new_connection.take();
}
}
_ => {}
_ => {
self.update_togglers(&state);
}
}
} else {
match req {
@ -500,18 +528,28 @@ impl Application for CosmicNetworkApplet {
let mut content = column![
vpn_ethernet_col,
container(
toggler(fl!("airplane-mode"), self.nm_state.airplane_mode, |m| {
Message::ToggleAirplaneMode(m)
})
anim!(
//toggler
AIRPLANE_MODE,
&self.timeline,
fl!("airplane-mode"),
self.nm_state.airplane_mode,
|_chain, enable| { Message::ToggleAirplaneMode(enable) },
)
.text_size(14)
.width(Length::Fill)
)
.padding([0, 12]),
divider::horizontal::light(),
container(
toggler(fl!("wifi"), self.nm_state.wifi_enabled, |m| {
Message::ToggleWiFi(m)
})
anim!(
//toggler
WIFI,
&self.timeline,
fl!("wifi"),
self.nm_state.wifi_enabled,
|_chain, enable| { Message::ToggleWiFi(enable) },
)
.text_size(14)
.width(Length::Fill)
)
@ -683,9 +721,11 @@ impl Application for CosmicNetworkApplet {
fn subscription(&self) -> Subscription<Message> {
let network_sub =
network_manager_subscription(0).map(|e| Message::NetworkManagerEvent(e.1));
let timeline = self.timeline.as_subscription().map(Message::Frame);
if let Some(conn) = self.conn.as_ref() {
Subscription::batch(vec![
timeline,
network_sub,
active_conns_subscription(0, conn.clone())
.map(|e| Message::NetworkManagerEvent(e.1)),
@ -694,7 +734,7 @@ impl Application for CosmicNetworkApplet {
.map(|e| Message::NetworkManagerEvent(e.1)),
])
} else {
network_sub
Subscription::batch(vec![timeline, network_sub])
}
}

View file

@ -1,10 +1,10 @@
// SPDX-License-Identifier: MPL-2.0-only
use cosmic_time::once_cell::sync::Lazy;
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DefaultLocalizer, LanguageLoader, Localizer,
};
use once_cell::sync::Lazy;
use rust_embed::RustEmbed;
#[derive(RustEmbed)]

View file

@ -1,11 +1,11 @@
use super::{NetworkManagerEvent, NetworkManagerState};
use cosmic::iced::{self, subscription};
use cosmic_dbus_networkmanager::nm::NetworkManager;
use futures::StreamExt;
use log::error;
use std::fmt::Debug;
use std::hash::Hash;
use zbus::Connection;
use futures::StreamExt;
pub fn devices_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
@ -50,10 +50,7 @@ async fn start_listening<I: Copy + Debug>(
let new_state = NetworkManagerState::new(&conn).await.unwrap_or_default();
(
Some((
id,
NetworkManagerEvent::WirelessAccessPoints(new_state),
)),
Some((id, NetworkManagerEvent::WirelessAccessPoints(new_state))),
State::Continue(conn),
)
}

View file

@ -15,7 +15,7 @@ use cosmic_dbus_networkmanager::{
};
use futures::{
channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
FutureExt, StreamExt,
StreamExt,
};
use tokio::{process::Command, time::timeout};
use zbus::{
@ -141,7 +141,7 @@ async fn start_listening<I: Copy + Debug>(
Some(NetworkManagerRequest::SetWiFi(enabled)) => {
let success = network_manager.set_wireless_enabled(enabled).await.is_ok();
let response = NetworkManagerEvent::RequestResponse {
req: NetworkManagerRequest::SetAirplaneMode(enabled),
req: NetworkManagerRequest::SetWiFi(enabled),
success,
state: NetworkManagerState::new(&conn).await.unwrap_or_default(),
};
@ -624,6 +624,7 @@ impl NetworkManagerState {
Ok(_self)
}
#[allow(dead_code)]
pub fn clear(&mut self) {
self.active_conns = Vec::new();
self.known_access_points = Vec::new();