diff --git a/applets/cosmic-applet-battery/src/app.rs b/applets/cosmic-applet-battery/src/app.rs index 92e3a4d3..ae36c178 100644 --- a/applets/cosmic-applet-battery/src/app.rs +++ b/applets/cosmic-applet-battery/src/app.rs @@ -1,3 +1,12 @@ +use crate::backlight::{ + screen_backlight_subscription, ScreenBacklightRequest, ScreenBacklightUpdate, +}; +use crate::config; +use crate::fl; +use crate::upower_device::{device_subscription, DeviceDbusEvent}; +use crate::upower_kbdbacklight::{ + kbd_backlight_subscription, KeyboardBacklightRequest, KeyboardBacklightUpdate, +}; use cosmic::applet::{get_popup_settings, icon_button, popup_container}; use cosmic::iced::alignment::Horizontal; use cosmic::iced::{ @@ -17,14 +26,9 @@ use iced_sctk::application::SurfaceIdWrapper; use iced_sctk::command::platform_specific::wayland::window::SctkWindowSettings; use iced_sctk::commands::popup::{destroy_popup, get_popup}; use iced_sctk::settings::InitialSurface; -use iced_sctk::{Color}; +use iced_sctk::Color; use std::time::Duration; use tokio::sync::mpsc::UnboundedSender; -use crate::backlight::{ScreenBacklightRequest, screen_backlight_subscription, ScreenBacklightUpdate}; -use crate::config; -use crate::upower_device::{device_subscription, DeviceDbusEvent}; -use crate::upower_kbdbacklight::{KeyboardBacklightRequest, kbd_backlight_subscription, KeyboardBacklightUpdate}; -use crate::fl; // XXX improve // TODO: time to empty varies? needs averaging? @@ -185,22 +189,21 @@ impl Application for CosmicBatteryApplet { } Message::UpdateKbdBrightness(b) => { self.kbd_brightness = b; - }, - Message::Ignore => {}, + } + Message::Ignore => {} Message::InitKbdBacklight(tx, brightness) => { let _ = tx.send(KeyboardBacklightRequest::Get); self.kbd_sender = Some(tx); self.kbd_brightness = brightness; - }, + } Message::InitScreenBacklight(tx, brightness) => { let _ = tx.send(ScreenBacklightRequest::Get); self.screen_sender = Some(tx); self.screen_brightness = brightness; - - }, + } Message::UpdateScreenBrightness(b) => { self.screen_brightness = b; - }, + } } Command::none() } @@ -218,66 +221,92 @@ impl Application for CosmicBatteryApplet { .into(), SurfaceIdWrapper::Popup(_) => { let name = text(fl!("battery")).size(18); - let description = text(if "battery-full-charging-symbolic" == self.icon_name || "battery-full-charged-symbolic" == self.icon_name { - format!("{}%", self.battery_percent) - } else { - format!( - "{} {} ({:.0}%)", - format_duration(self.time_remaining), - fl!("until-empty"), - self.battery_percent - ) - }) + let description = text( + if "battery-full-charging-symbolic" == self.icon_name + || "battery-full-charged-symbolic" == self.icon_name + { + format!("{}%", self.battery_percent) + } else { + format!( + "{} {} ({:.0}%)", + format_duration(self.time_remaining), + fl!("until-empty"), + self.battery_percent + ) + }, + ) .size(12); popup_container( column![ row![ - icon(&self.icon_name, 24) - .style(Svg::Custom(|theme| { - svg::Appearance { - fill: Some(theme.palette().text), - } - })) - .width(Length::Units(24)) - .height(Length::Units(24)), - column![name, description] - ] - .spacing(8) - .align_items(Alignment::Center), + icon(&self.icon_name, 24) + .style(Svg::Custom(|theme| { + svg::Appearance { + fill: Some(theme.palette().text), + } + })) + .width(Length::Units(24)) + .height(Length::Units(24)), + column![name, description] + ] + .spacing(8) + .align_items(Alignment::Center), separator!(1), // text{"Limit Battery Charging"}, - widget::Toggler::new(self.charging_limit, fl!("max-charge"), |_| Message::SetChargingLimit(!self.charging_limit)), + widget::Toggler::new(self.charging_limit, fl!("max-charge"), |_| { + Message::SetChargingLimit(!self.charging_limit) + }), separator!(1), - row![icon("display-brightness-symbolic", 24) - .style( - Svg::Custom(|theme| { - svg::Appearance { - fill: Some(theme.palette().text), - } - })) - .width(Length::Units(24)) - .height(Length::Units(24)), - widget::slider(0..=100, (self.screen_brightness * 100.0) as i32, Message::SetScreenBrightness), - text(format!("{:.0}%", self.screen_brightness * 100.0)).width(Length::Units(40)).horizontal_alignment(Horizontal::Right) - ].spacing(12), + row![ + icon("display-brightness-symbolic", 24) + .style(Svg::Custom(|theme| { + svg::Appearance { + fill: Some(theme.palette().text), + } + })) + .width(Length::Units(24)) + .height(Length::Units(24)), + widget::slider( + 0..=100, + (self.screen_brightness * 100.0) as i32, + Message::SetScreenBrightness + ), + text(format!("{:.0}%", self.screen_brightness * 100.0)) + .width(Length::Units(40)) + .horizontal_alignment(Horizontal::Right) + ] + .spacing(12), row![ icon("keyboard-brightness-symbolic", 24) - .style(Svg::Custom(|theme| { - svg::Appearance { - fill: Some(theme.palette().text), - } - })) - .width(Length::Units(24)) - .height(Length::Units(24)), - widget::slider(0..=100, (self.kbd_brightness * 100.0) as i32, Message::SetKbdBrightness), - text(format!("{:.0}%", self.kbd_brightness * 100.0)).width(Length::Units(40)).horizontal_alignment(Horizontal::Right) - ].spacing(12), - button(text(fl!("power-settings")).horizontal_alignment(Horizontal::Center).width(Length::Fill).style(theme::Text::Custom(|theme| { - let cosmic = theme.cosmic(); - iced_style::text::Appearance { - color: Some(cosmic.accent.on.into()) - } - }))).width(Length::Fill) + .style(Svg::Custom(|theme| { + svg::Appearance { + fill: Some(theme.palette().text), + } + })) + .width(Length::Units(24)) + .height(Length::Units(24)), + widget::slider( + 0..=100, + (self.kbd_brightness * 100.0) as i32, + Message::SetKbdBrightness + ), + text(format!("{:.0}%", self.kbd_brightness * 100.0)) + .width(Length::Units(40)) + .horizontal_alignment(Horizontal::Right) + ] + .spacing(12), + button( + text(fl!("power-settings")) + .horizontal_alignment(Horizontal::Center) + .width(Length::Fill) + .style(theme::Text::Custom(|theme| { + let cosmic = theme.cosmic(); + iced_style::text::Appearance { + color: Some(cosmic.accent.on.into()), + } + })) + ) + .width(Length::Fill) ] .spacing(4) .padding(8), @@ -307,9 +336,8 @@ impl Application for CosmicBatteryApplet { screen_backlight_subscription(0).map(|(_, event)| match event { ScreenBacklightUpdate::Update(b) => Message::UpdateScreenBrightness(b), ScreenBacklightUpdate::Init(tx, b) => Message::InitScreenBacklight(tx, b), - }) + }), ]) - } fn theme(&self) -> Theme { diff --git a/applets/cosmic-applet-battery/src/main.rs b/applets/cosmic-applet-battery/src/main.rs index 004be95a..98a8f9ca 100644 --- a/applets/cosmic-applet-battery/src/main.rs +++ b/applets/cosmic-applet-battery/src/main.rs @@ -1,7 +1,7 @@ #[rustfmt::skip] mod backlight; -mod config; mod app; +mod config; mod localize; mod power_daemon; mod upower; diff --git a/applets/cosmic-applet-battery/src/upower_kbdbacklight.rs b/applets/cosmic-applet-battery/src/upower_kbdbacklight.rs index ead97f17..e89c910b 100644 --- a/applets/cosmic-applet-battery/src/upower_kbdbacklight.rs +++ b/applets/cosmic-applet-battery/src/upower_kbdbacklight.rs @@ -5,9 +5,9 @@ use cosmic::iced; use iced::subscription; -use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel}; -use zbus::dbus_proxy; use std::{fmt::Debug, hash::Hash}; +use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; +use zbus::dbus_proxy; #[dbus_proxy( default_service = "org.freedesktop.UPower", interface = "org.freedesktop.UPower.KbdBacklight", @@ -41,11 +41,17 @@ pub fn kbd_backlight_subscription, UnboundedReceiver), + Waiting( + KbdBacklightProxy<'static>, + UnboundedReceiver, + ), Finished, } -async fn start_listening(id: I, state: State) -> (Option<(I, KeyboardBacklightUpdate)>, State) { +async fn start_listening( + id: I, + state: State, +) -> (Option<(I, KeyboardBacklightUpdate)>, State) { match state { State::Ready => { let conn = match zbus::Connection::system().await { @@ -61,38 +67,37 @@ async fn start_listening(id: I, state: State) -> (Option<(I, KeyboardBa return ( Some(( id, - KeyboardBacklightUpdate::Init(tx, kbd_proxy.get_brightness().await.unwrap_or_default() as f64) + KeyboardBacklightUpdate::Init( + tx, + kbd_proxy.get_brightness().await.unwrap_or_default() as f64, + ), )), State::Waiting(kbd_proxy, rx), ); + } + State::Waiting(proxy, mut rx) => match rx.recv().await { + Some(req) => match req { + KeyboardBacklightRequest::Get => ( + Some(( + id, + KeyboardBacklightUpdate::Update( + proxy.get_brightness().await.unwrap_or_default() as f64, + ), + )), + State::Waiting(proxy, rx), + ), + KeyboardBacklightRequest::Set(value) => { + if let Ok(max_brightness) = proxy.get_max_brightness().await { + let value = value.clamp(0., 1.) * (max_brightness as f64); + let value = value.round() as i32; + let _ = proxy.set_brightness(value).await; + } - } - State::Waiting(proxy, mut rx) => { - match rx.recv().await { - Some(req) => match req { - KeyboardBacklightRequest::Get => ( - Some(( - id, - KeyboardBacklightUpdate::Update(proxy.get_brightness().await.unwrap_or_default() as f64) - )), - State::Waiting(proxy, rx), - ), - KeyboardBacklightRequest::Set(value) => { - if let Ok(max_brightness) = proxy.get_max_brightness().await { - let value = value.clamp(0., 1.) * (max_brightness as f64); - let value = value.round() as i32; - let _ = proxy.set_brightness(value).await; - } - - ( - None, - State::Waiting(proxy, rx), - ) - }, - }, - None => (None, State::Finished), - } - } + (None, State::Waiting(proxy, rx)) + } + }, + None => (None, State::Finished), + }, State::Finished => iced::futures::future::pending().await, } } @@ -100,7 +105,7 @@ async fn start_listening(id: I, state: State) -> (Option<(I, KeyboardBa #[derive(Debug, Clone)] pub enum KeyboardBacklightUpdate { Update(f64), - Init(UnboundedSender, f64) + Init(UnboundedSender, f64), } #[derive(Debug, Clone)]