battery: Use max_kbd_brightness as slider range

This makes sure we can listen to brightness subscription and set the
brightness without conflicts or weird workarounds.
This commit is contained in:
Ian Douglas Scott 2024-07-01 13:35:13 -07:00 committed by Ian Douglas Scott
parent 00e34f4eba
commit 0ef2393288
2 changed files with 46 additions and 40 deletions

16
Cargo.lock generated
View file

@ -1324,7 +1324,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-settings-subscriptions" name = "cosmic-settings-subscriptions"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/cosmic-settings-subscriptions#3a4e58559149fd5bf4d1d2c93229b2920ce03695" source = "git+https://github.com/pop-os/cosmic-settings-subscriptions#21cc79434fa24f1fbd6dc4e8d394aa51d5ee12d9"
dependencies = [ dependencies = [
"futures", "futures",
"iced_futures", "iced_futures",
@ -1511,7 +1511,7 @@ version = "0.19.0"
source = "git+https://github.com/gfx-rs/wgpu?rev=20fda69#20fda698341efbdc870b8027d6d49f5bf3f36109" source = "git+https://github.com/gfx-rs/wgpu?rev=20fda69#20fda698341efbdc870b8027d6d49f5bf3f36109"
dependencies = [ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"libloading 0.7.4", "libloading 0.8.3",
"winapi", "winapi",
] ]
@ -1774,7 +1774,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412" checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412"
dependencies = [ dependencies = [
"libloading 0.7.4", "libloading 0.8.3",
] ]
[[package]] [[package]]
@ -2608,7 +2608,7 @@ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"com", "com",
"libc", "libc",
"libloading 0.7.4", "libloading 0.8.3",
"thiserror", "thiserror",
"widestring", "widestring",
"winapi", "winapi",
@ -6476,7 +6476,7 @@ dependencies = [
"js-sys", "js-sys",
"log", "log",
"naga", "naga",
"parking_lot 0.11.2", "parking_lot 0.12.3",
"profiling", "profiling",
"raw-window-handle", "raw-window-handle",
"smallvec", "smallvec",
@ -6503,7 +6503,7 @@ dependencies = [
"log", "log",
"naga", "naga",
"once_cell", "once_cell",
"parking_lot 0.11.2", "parking_lot 0.12.3",
"profiling", "profiling",
"raw-window-handle", "raw-window-handle",
"rustc-hash", "rustc-hash",
@ -6537,13 +6537,13 @@ dependencies = [
"js-sys", "js-sys",
"khronos-egl", "khronos-egl",
"libc", "libc",
"libloading 0.7.4", "libloading 0.8.3",
"log", "log",
"metal", "metal",
"naga", "naga",
"objc", "objc",
"once_cell", "once_cell",
"parking_lot 0.11.2", "parking_lot 0.12.3",
"profiling", "profiling",
"range-alloc", "range-alloc",
"raw-window-handle", "raw-window-handle",

View file

@ -80,7 +80,8 @@ struct CosmicBatteryApplet {
on_battery: bool, on_battery: bool,
gpus: HashMap<PathBuf, GPUData>, gpus: HashMap<PathBuf, GPUData>,
time_remaining: Duration, time_remaining: Duration,
kbd_brightness: Option<f64>, max_kbd_brightness: Option<i32>,
kbd_brightness: Option<i32>,
max_screen_brightness: i32, max_screen_brightness: i32,
screen_brightness: i32, screen_brightness: i32,
popup: Option<window::Id>, popup: Option<window::Id>,
@ -163,8 +164,7 @@ enum Message {
SetKbdBrightness(i32), SetKbdBrightness(i32),
SetScreenBrightness(i32), SetScreenBrightness(i32),
SetChargingLimit(chain::Toggler, bool), SetChargingLimit(chain::Toggler, bool),
UpdateKbdBrightness(Option<f64>), KeyboardBacklight(KeyboardBacklightUpdate),
InitKbdBacklight(UnboundedSender<KeyboardBacklightRequest>),
GpuOn(PathBuf, String, Option<Vec<Entry>>), GpuOn(PathBuf, String, Option<Vec<Entry>>),
GpuOff(PathBuf), GpuOff(PathBuf),
ToggleGpuApps(PathBuf), ToggleGpuApps(PathBuf),
@ -222,7 +222,6 @@ impl cosmic::Application for CosmicBatteryApplet {
match message { match message {
Message::Frame(now) => self.timeline.now(now), Message::Frame(now) => self.timeline.now(now),
Message::SetKbdBrightness(brightness) => { Message::SetKbdBrightness(brightness) => {
let brightness = (brightness as f64 / 100.0).clamp(0., 1.);
self.kbd_brightness = Some(brightness); self.kbd_brightness = Some(brightness);
if let Some(tx) = &self.kbd_sender { if let Some(tx) = &self.kbd_sender {
let _ = tx.send(KeyboardBacklightRequest::Set(brightness)); let _ = tx.send(KeyboardBacklightRequest::Set(brightness));
@ -282,12 +281,17 @@ impl cosmic::Application for CosmicBatteryApplet {
self.update_battery(percent, on_battery); self.update_battery(percent, on_battery);
self.time_remaining = Duration::from_secs(time_to_empty as u64); self.time_remaining = Duration::from_secs(time_to_empty as u64);
} }
Message::UpdateKbdBrightness(b) => { Message::KeyboardBacklight(event) => match event {
self.kbd_brightness = b; KeyboardBacklightUpdate::Sender(tx) => {
} self.kbd_sender = Some(tx);
Message::InitKbdBacklight(tx) => { }
self.kbd_sender = Some(tx); KeyboardBacklightUpdate::MaxBrightness(max_brightness) => {
} self.max_kbd_brightness = Some(max_brightness);
}
KeyboardBacklightUpdate::Brightness(brightness) => {
self.kbd_brightness = Some(brightness);
}
},
Message::InitProfile(tx, profile) => { Message::InitProfile(tx, profile) => {
self.power_profile_sender.replace(tx); self.power_profile_sender.replace(tx);
self.power_profile = profile; self.power_profile = profile;
@ -543,27 +547,32 @@ impl cosmic::Application for CosmicBatteryApplet {
.into(), .into(),
]; ];
if let Some(kbd_brightness) = self.kbd_brightness { if let Some(max_kbd_brightness) = self.max_kbd_brightness {
content.push( if let Some(kbd_brightness) = self.kbd_brightness {
padded_control( content.push(
row![ padded_control(
icon::from_name("keyboard-brightness-symbolic") row![
.size(24) icon::from_name("keyboard-brightness-symbolic")
.symbolic(true), .size(24)
slider( .symbolic(true),
0..=100, slider(
(kbd_brightness * 100.0) as i32, 0..=max_kbd_brightness,
Message::SetKbdBrightness kbd_brightness,
), Message::SetKbdBrightness
text(format!("{:.0}%", kbd_brightness * 100.0)) ),
text(format!(
"{:.0}%",
100. * kbd_brightness as f64 / max_kbd_brightness as f64
))
.size(16) .size(16)
.width(Length::Fixed(40.0)) .width(Length::Fixed(40.0))
.horizontal_alignment(Horizontal::Right) .horizontal_alignment(Horizontal::Right)
] ]
.spacing(12), .spacing(12),
) )
.into(), .into(),
); );
}
} }
content.push(padded_control(divider::horizontal::default()).into()); content.push(padded_control(divider::horizontal::default()).into());
@ -684,10 +693,7 @@ impl cosmic::Application for CosmicBatteryApplet {
time_to_empty, time_to_empty,
}, },
), ),
kbd_backlight_subscription(0).map(|event| match event { kbd_backlight_subscription(0).map(Message::KeyboardBacklight),
KeyboardBacklightUpdate::Brightness(b) => Message::UpdateKbdBrightness(b),
KeyboardBacklightUpdate::Sender(tx) => Message::InitKbdBacklight(tx),
}),
power_profile_subscription(0).map(|event| match event { power_profile_subscription(0).map(|event| match event {
PowerProfileUpdate::Update { profile } => Message::Profile(profile), PowerProfileUpdate::Update { profile } => Message::Profile(profile),
PowerProfileUpdate::Init(tx, p) => Message::InitProfile(p, tx), PowerProfileUpdate::Init(tx, p) => Message::InitProfile(p, tx),