refactor: applet more like mockup

This commit is contained in:
Ashley Wulber 2023-01-13 18:31:59 -05:00 • committed by Ashley Wulber
parent dc1dc3e27b
commit 899cc0f459
3 changed files with 288 additions and 30 deletions

View file

@ -3,25 +3,29 @@ use crate::backlight::{
};
use crate::config;
use crate::fl;
use crate::power_daemon::{
power_profile_subscription, Power, PowerProfileRequest, PowerProfileUpdate,
};
use crate::upower_device::{device_subscription, DeviceDbusEvent};
use crate::upower_kbdbacklight::{
kbd_backlight_subscription, KeyboardBacklightRequest, KeyboardBacklightUpdate,
};
use cosmic::applet::CosmicAppletHelper;
use cosmic::applet::{CosmicAppletHelper, APPLET_BUTTON_THEME};
use cosmic::iced::alignment::Horizontal;
use cosmic::iced::wayland::popup::{destroy_popup, get_popup};
use cosmic::iced::wayland::SurfaceIdWrapper;
use cosmic::iced::{
executor,
widget::{button, column, row, slider, text},
widget::{column, container, row, slider, text},
window, Alignment, Application, Command, Length, Subscription,
};
use cosmic::iced_native::layout::Limits;
use cosmic::iced_style::application::{self, Appearance};
use cosmic::iced_style::{svg, Color};
use cosmic::theme::{self, Svg};
use cosmic::widget::{horizontal_rule, icon, toggler};
use cosmic::{iced_style, Element, Theme};
use cosmic::theme::Svg;
use cosmic::widget::{button, horizontal_rule, icon, toggler};
use cosmic::{Element, Theme};
use log::error;
use std::time::Duration;
use tokio::sync::mpsc::UnboundedSender;
@ -60,6 +64,8 @@ struct CosmicBatteryApplet {
screen_sender: Option<UnboundedSender<ScreenBacklightRequest>>,
kbd_sender: Option<UnboundedSender<KeyboardBacklightRequest>>,
applet_helper: CosmicAppletHelper,
power_profile: Power,
power_profile_sender: Option<UnboundedSender<PowerProfileRequest>>,
}
#[derive(Debug, Clone)]
@ -80,6 +86,9 @@ enum Message {
InitScreenBacklight(UnboundedSender<ScreenBacklightRequest>, f64),
Errored(String),
Ignore,
InitProfile(UnboundedSender<PowerProfileRequest>, Power),
Profile(Power),
SelectProfile(Power),
}
impl Application for CosmicBatteryApplet {
@ -122,8 +131,8 @@ impl Application for CosmicBatteryApplet {
Message::OpenBatterySettings => {
// TODO Ashley
}
Message::Errored(_) => {
// TODO log errors
Message::Errored(e) => {
error!("{}", e);
}
Message::TogglePopup => {
if let Some(p) = self.popup.take() {
@ -148,10 +157,13 @@ impl Application for CosmicBatteryApplet {
None,
);
popup_settings.positioner.size_limits = Limits::NONE
.max_width(400)
.max_width(372)
.min_width(300)
.min_height(200)
.max_height(1080);
if let Some(tx) = self.power_profile_sender.as_ref() {
let _ = tx.send(PowerProfileRequest::Get);
}
return get_popup(popup_settings);
}
}
@ -181,6 +193,24 @@ impl Application for CosmicBatteryApplet {
Message::UpdateScreenBrightness(b) => {
self.screen_brightness = b;
}
Message::InitProfile(tx, profile) => {
self.power_profile_sender.replace(tx);
self.power_profile = profile;
}
Message::Profile(profile) => {
self.power_profile = profile;
if let Some(tx) = &self.kbd_sender {
let _ = tx.send(KeyboardBacklightRequest::Get);
}
if let Some(tx) = &self.screen_sender {
let _ = tx.send(ScreenBacklightRequest::Get);
}
}
Message::SelectProfile(profile) => {
if let Some(tx) = self.power_profile_sender.as_ref() {
let _ = tx.send(PowerProfileRequest::Set(profile));
}
}
}
Command::none()
}
@ -223,14 +253,80 @@ impl Application for CosmicBatteryApplet {
.height(Length::Units(24)),
column![name, description]
]
.padding([0, 24])
.spacing(8)
.align_items(Alignment::Center),
horizontal_rule(1),
toggler(fl!("max-charge"), self.charging_limit, |_| {
container(horizontal_rule(1))
.width(Length::Fill)
.padding([0, 12]),
button(APPLET_BUTTON_THEME)
.custom(vec![row![
column![
text(fl!("battery")).size(14),
text(fl!("battery-desc")).size(12)
]
.width(Length::Fill),
icon("emblem-ok-symbolic", 12).size(12).style(
match self.power_profile {
Power::Battery => Svg::SymbolicActive,
_ => Svg::Default,
}
),
]
.align_items(Alignment::Center)
.into()])
.padding([8, 24])
.on_press(Message::SelectProfile(Power::Battery))
.width(Length::Fill),
button(APPLET_BUTTON_THEME)
.custom(vec![row![
column![
text(fl!("balanced")).size(14),
text(fl!("balanced-desc")).size(12)
]
.width(Length::Fill),
icon("emblem-ok-symbolic", 12).size(12).style(
match self.power_profile {
Power::Balanced => Svg::SymbolicActive,
_ => Svg::Default,
}
),
]
.align_items(Alignment::Center)
.into()])
.padding([8, 24])
.on_press(Message::SelectProfile(Power::Balanced))
.width(Length::Fill),
button(APPLET_BUTTON_THEME)
.custom(vec![row![
column![
text(fl!("performance")).size(14),
text(fl!("performance-desc")).size(12)
]
.width(Length::Fill),
icon("emblem-ok-symbolic", 12).size(12).style(
match self.power_profile {
Power::Performance => Svg::SymbolicActive,
_ => Svg::Default,
}
),
]
.align_items(Alignment::Center)
.into()])
.padding([8, 24])
.on_press(Message::SelectProfile(Power::Performance))
.width(Length::Fill),
container(horizontal_rule(1))
.width(Length::Fill)
.padding([0, 12]),
container(toggler(fl!("max-charge"), self.charging_limit, |_| {
Message::SetChargingLimit(!self.charging_limit)
})
}))
.padding([0, 24])
.width(Length::Fill),
horizontal_rule(1),
container(horizontal_rule(1))
.width(Length::Fill)
.padding([0, 12]),
row![
icon("display-brightness-symbolic", 24)
.style(Svg::Custom(|theme| {
@ -249,6 +345,7 @@ impl Application for CosmicBatteryApplet {
.width(Length::Units(40))
.horizontal_alignment(Horizontal::Right)
]
.padding([0, 24])
.spacing(12),
row![
icon("keyboard-brightness-symbolic", 24)
@ -268,22 +365,21 @@ impl Application for CosmicBatteryApplet {
.width(Length::Units(40))
.horizontal_alignment(Horizontal::Right)
]
.padding([0, 24])
.spacing(12),
button(
text(fl!("power-settings"))
.horizontal_alignment(Horizontal::Center)
container(horizontal_rule(1))
.width(Length::Fill)
.padding([0, 12]),
button(APPLET_BUTTON_THEME)
.custom(vec![text(fl!("power-settings"))
.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)
.into()])
.on_press(Message::OpenBatterySettings)
.width(Length::Fill)
.padding([8, 24])
]
.spacing(4)
.padding(8),
.spacing(8)
.padding([8, 0]),
)
.into()
}
@ -311,6 +407,11 @@ impl Application for CosmicBatteryApplet {
ScreenBacklightUpdate::Update(b) => Message::UpdateScreenBrightness(b),
ScreenBacklightUpdate::Init(tx, b) => Message::InitScreenBacklight(tx, b),
}),
power_profile_subscription(0).map(|(_, event)| match event {
PowerProfileUpdate::Update { profile } => Message::Profile(profile),
PowerProfileUpdate::Init(tx, p) => Message::InitProfile(p, tx),
PowerProfileUpdate::Error(e) => Message::Errored(e), // TODO: handle error
}),
])
}