Skip to content

Commit 8dc3542

Browse files
committed
Align brightness reporting with cosmic-osd
1 parent 351f138 commit 8dc3542

File tree

1 file changed

+27
-6
lines changed
  • cosmic-applet-battery/src

1 file changed

+27
-6
lines changed

cosmic-applet-battery/src/app.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,19 @@ impl CosmicBatteryApplet {
136136
}
137137

138138
fn screen_brightness_percent(&self) -> Option<f64> {
139-
Some(
140-
(self.screen_brightness? as f64 / self.max_screen_brightness?.max(1) as f64)
141-
.clamp(0.01, 1.0),
142-
)
139+
let raw = self.screen_brightness? as i64;
140+
let max = self.max_screen_brightness?.max(1) as i64;
141+
if max <= 20 {
142+
// Matching brightness calculation logic from cosmic-osd and cosmic-settings-daemon
143+
let mut k = (raw * 20 + max / 2) / max;
144+
if k < 0 { k = 0; }
145+
if k > 20 { k = 20; }
146+
let p = if k == 0 { 1 } else { 5 * k };
147+
Some((p as f64) / 100.0)
148+
} else {
149+
let p = ((raw * 100 + max / 2) / max).clamp(1, 100) as f64;
150+
Some(p / 100.0)
151+
}
143152
}
144153

145154
fn update_display(&mut self) {
@@ -242,8 +251,20 @@ impl cosmic::Application for CosmicBatteryApplet {
242251
return cosmic::task::message(Message::SetKbdBrightnessDebounced);
243252
}
244253
}
254+
// Matching brightness calculation logic from cosmic-osd and cosmic-settings-daemon
245255
Message::SetScreenBrightness(brightness) => {
246-
self.screen_brightness = Some(brightness);
256+
let snapped = if let Some(max) = self.max_screen_brightness {
257+
if max > 0 && max <= 20 {
258+
// Coarse: map raw→k by round, then back to raw setpoint round(k*max/20)
259+
let k = ((brightness as i64 * 20 + (max as i64)/2) / (max as i64)).clamp(0, 20);
260+
(((k * (max as i64)) + 10) / 20) as i32
261+
} else {
262+
brightness
263+
}
264+
} else {
265+
brightness
266+
};
267+
self.screen_brightness = Some(snapped);
247268
if !self.dragging_screen_brightness {
248269
self.dragging_screen_brightness = true;
249270
self.update_display();
@@ -652,7 +673,7 @@ impl cosmic::Application for CosmicBatteryApplet {
652673
.size(24)
653674
.symbolic(true),
654675
slider(
655-
1..=max_screen_brightness,
676+
0..=max_screen_brightness,
656677
screen_brightness,
657678
Message::SetScreenBrightness
658679
)

0 commit comments

Comments
 (0)