Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions cosmic-applet-battery/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,19 @@ impl CosmicBatteryApplet {
}

fn screen_brightness_percent(&self) -> Option<f64> {
Some(
(self.screen_brightness? as f64 / self.max_screen_brightness?.max(1) as f64)
.clamp(0.01, 1.0),
)
let raw = self.screen_brightness? as i64;
let max = self.max_screen_brightness?.max(1) as i64;
if max <= 20 {
// Matching brightness calculation logic from cosmic-osd and cosmic-settings-daemon
let mut k = (raw * 20 + max / 2) / max;
if k < 0 { k = 0; }
if k > 20 { k = 20; }
let p = if k == 0 { 1 } else { 5 * k };
Some((p as f64) / 100.0)
} else {
let p = ((raw * 100 + max / 2) / max).clamp(1, 100) as f64;
Some(p / 100.0)
}
}

fn update_display(&mut self) {
Expand Down Expand Up @@ -242,8 +251,20 @@ impl cosmic::Application for CosmicBatteryApplet {
return cosmic::task::message(Message::SetKbdBrightnessDebounced);
}
}
// Matching brightness calculation logic from cosmic-osd and cosmic-settings-daemon
Message::SetScreenBrightness(brightness) => {
self.screen_brightness = Some(brightness);
let snapped = if let Some(max) = self.max_screen_brightness {
if max > 0 && max <= 20 {
// Coarse: map raw→k by round, then back to raw setpoint round(k*max/20)
let k = ((brightness as i64 * 20 + (max as i64)/2) / (max as i64)).clamp(0, 20);
(((k * (max as i64)) + 10) / 20) as i32
} else {
brightness
}
} else {
brightness
};
self.screen_brightness = Some(snapped);
if !self.dragging_screen_brightness {
self.dragging_screen_brightness = true;
self.update_display();
Expand Down Expand Up @@ -652,7 +673,7 @@ impl cosmic::Application for CosmicBatteryApplet {
.size(24)
.symbolic(true),
slider(
1..=max_screen_brightness,
0..=max_screen_brightness,
screen_brightness,
Message::SetScreenBrightness
)
Expand Down
Loading