Skip to content

Commit

Permalink
fix(battery): fix brightness slider lag
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 authored and mmstick committed Dec 4, 2024
1 parent f486b3b commit 104a608
Showing 1 changed file with 74 additions and 7 deletions.
81 changes: 74 additions & 7 deletions cosmic-applet-battery/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct CosmicBatteryApplet {
timeline: Timeline,
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
zbus_connection: Option<zbus::Connection>,
dragging_screen_brightness: bool,
dragging_kbd_brightness: bool,
}

impl CosmicBatteryApplet {
Expand Down Expand Up @@ -165,7 +167,11 @@ enum Message {
TogglePopup,
CloseRequested(window::Id),
SetKbdBrightness(i32),
ReleaseKbdBrightness,
SetScreenBrightness(i32),
SetKbdBrightnessDebounced,
SetScreenBrightnessDebounced,
ReleaseScreenBrightness,
InitChargingLimit(bool),
SetChargingLimit(chain::Toggler, bool),
KeyboardBacklight(KeyboardBacklightUpdate),
Expand Down Expand Up @@ -232,15 +238,65 @@ impl cosmic::Application for CosmicBatteryApplet {
Message::Frame(now) => self.timeline.now(now),
Message::SetKbdBrightness(brightness) => {
self.kbd_brightness = Some(brightness);
if let Some(tx) = &self.kbd_sender {
let _ = tx.send(KeyboardBacklightRequest::Set(brightness));

if !self.dragging_kbd_brightness {
self.dragging_kbd_brightness = true;
return cosmic::task::message(Message::SetKbdBrightnessDebounced);
}
}
Message::SetScreenBrightness(brightness) => {
self.screen_brightness = Some(brightness);
if !self.dragging_screen_brightness {
self.dragging_screen_brightness = true;
self.update_display();
return cosmic::task::message(Message::SetScreenBrightnessDebounced);
}
}
Message::SetKbdBrightnessDebounced => {
if !self.dragging_kbd_brightness {
return Task::none();
}
if let Some(tx) = &self.kbd_sender {
if let Some(b) = self.kbd_brightness {
let _ = tx.send(KeyboardBacklightRequest::Set(b));
}
}
return cosmic::iced::Task::perform(
tokio::time::sleep(Duration::from_millis(200)),
|_| cosmic::app::Message::App(Message::SetKbdBrightnessDebounced),
);
}
Message::SetScreenBrightnessDebounced => {
if !self.dragging_screen_brightness {
return Task::none();
}

if let Some(tx) = &self.settings_daemon_sender {
if let Some(b) = self.screen_brightness {
let _ = tx.send(settings_daemon::Request::SetDisplayBrightness(b));
}
}
return cosmic::iced::Task::perform(
tokio::time::sleep(Duration::from_millis(200)),
|_| cosmic::app::Message::App(Message::SetScreenBrightnessDebounced),
);
}
Message::ReleaseKbdBrightness => {
self.dragging_kbd_brightness = false;
if let Some(tx) = &self.kbd_sender {
if let Some(b) = self.kbd_brightness {
let _ = tx.send(KeyboardBacklightRequest::Set(b));
}
}
}
Message::ReleaseScreenBrightness => {
self.dragging_screen_brightness = false;

self.update_display();
if let Some(tx) = &self.settings_daemon_sender {
let _ = tx.send(settings_daemon::Request::SetDisplayBrightness(brightness));
if let Some(b) = self.screen_brightness {
let _ = tx.send(settings_daemon::Request::SetDisplayBrightness(b));
}
}
}
Message::InitChargingLimit(enable) => {
Expand All @@ -260,6 +316,9 @@ impl cosmic::Application for CosmicBatteryApplet {
tracing::error!("{}", why);
}
Message::TogglePopup => {
self.dragging_kbd_brightness = false;
self.dragging_screen_brightness = false;

if let Some(p) = self.popup.take() {
return destroy_popup(p);
} else {
Expand Down Expand Up @@ -310,7 +369,9 @@ impl cosmic::Application for CosmicBatteryApplet {
self.max_kbd_brightness = Some(max_brightness);
}
KeyboardBacklightUpdate::Brightness(brightness) => {
self.kbd_brightness = Some(brightness);
if !self.dragging_kbd_brightness {
self.kbd_brightness = Some(brightness);
}
}
},
Message::InitProfile(tx, profile) => {
Expand All @@ -329,6 +390,8 @@ impl cosmic::Application for CosmicBatteryApplet {
}
}
Message::CloseRequested(id) => {
self.dragging_kbd_brightness = false;
self.dragging_screen_brightness = false;
if Some(id) == self.popup {
self.popup = None;
}
Expand Down Expand Up @@ -398,7 +461,9 @@ impl cosmic::Application for CosmicBatteryApplet {
self.max_screen_brightness = Some(max_brightness);
}
settings_daemon::Event::DisplayBrightness(brightness) => {
self.screen_brightness = Some(brightness);
if !self.dragging_screen_brightness {
self.screen_brightness = Some(brightness);
}
}
},
}
Expand Down Expand Up @@ -570,7 +635,8 @@ impl cosmic::Application for CosmicBatteryApplet {
1..=max_screen_brightness,
screen_brightness,
Message::SetScreenBrightness
),
)
.on_release(Message::ReleaseScreenBrightness),
text(format!(
"{:.0}%",
self.screen_brightness_percent().unwrap_or(0.) * 100.
Expand Down Expand Up @@ -598,7 +664,8 @@ impl cosmic::Application for CosmicBatteryApplet {
0..=max_kbd_brightness,
kbd_brightness,
Message::SetKbdBrightness
),
)
.on_release(Message::ReleaseKbdBrightness),
text(format!(
"{:.0}%",
100. * kbd_brightness as f64 / max_kbd_brightness as f64
Expand Down

0 comments on commit 104a608

Please sign in to comment.