battery: Make screen brightness Option; hide if none
This commit is contained in:
parent
61a9102fce
commit
3beda52c1c
1 changed files with 50 additions and 38 deletions
|
|
@ -82,8 +82,8 @@ struct CosmicBatteryApplet {
|
|||
time_remaining: Duration,
|
||||
max_kbd_brightness: Option<i32>,
|
||||
kbd_brightness: Option<i32>,
|
||||
max_screen_brightness: i32,
|
||||
screen_brightness: i32,
|
||||
max_screen_brightness: Option<i32>,
|
||||
screen_brightness: Option<i32>,
|
||||
popup: Option<window::Id>,
|
||||
settings_daemon_sender: Option<UnboundedSender<settings_daemon::Request>>,
|
||||
kbd_sender: Option<UnboundedSender<KeyboardBacklightRequest>>,
|
||||
|
|
@ -124,14 +124,16 @@ impl CosmicBatteryApplet {
|
|||
format!("cosmic-applet-battery-level-{battery_percent}-{limited}{charging}symbolic",);
|
||||
}
|
||||
|
||||
fn screen_brightness_percent(&self) -> f64 {
|
||||
(self.screen_brightness as f64 / self.max_screen_brightness.max(1) as f64).clamp(0.01, 1.0)
|
||||
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),
|
||||
)
|
||||
}
|
||||
|
||||
fn update_display(&mut self) {
|
||||
let percent = self.screen_brightness_percent();
|
||||
|
||||
let screen_brightness = if percent < 0.011 {
|
||||
let screen_brightness = if let Some(percent) = self.screen_brightness_percent() {
|
||||
if percent < 0.011 {
|
||||
"off"
|
||||
} else if percent < 0.333 {
|
||||
"low"
|
||||
|
|
@ -140,6 +142,9 @@ impl CosmicBatteryApplet {
|
|||
} else {
|
||||
"high"
|
||||
}
|
||||
} else {
|
||||
"off"
|
||||
}
|
||||
.to_string();
|
||||
|
||||
self.display_icon_name =
|
||||
|
|
@ -224,12 +229,10 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
}
|
||||
}
|
||||
Message::SetScreenBrightness(brightness) => {
|
||||
self.screen_brightness = brightness;
|
||||
self.screen_brightness = Some(brightness);
|
||||
self.update_display();
|
||||
if let Some(tx) = &self.settings_daemon_sender {
|
||||
let _ = tx.send(settings_daemon::Request::SetDisplayBrightness(
|
||||
self.screen_brightness,
|
||||
));
|
||||
let _ = tx.send(settings_daemon::Request::SetDisplayBrightness(brightness));
|
||||
}
|
||||
}
|
||||
Message::SetChargingLimit(chain, enable) => {
|
||||
|
|
@ -375,11 +378,10 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
self.settings_daemon_sender = Some(tx);
|
||||
}
|
||||
settings_daemon::Event::MaxDisplayBrightness(max_brightness) => {
|
||||
// XXX option
|
||||
self.max_screen_brightness = max_brightness;
|
||||
self.max_screen_brightness = Some(max_brightness);
|
||||
}
|
||||
settings_daemon::Event::DisplayBrightness(brightness) => {
|
||||
self.screen_brightness = brightness;
|
||||
self.screen_brightness = Some(brightness);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
@ -528,17 +530,25 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
)
|
||||
.into(),
|
||||
padded_control(divider::horizontal::default()).into(),
|
||||
];
|
||||
|
||||
if let Some(max_screen_brightness) = self.max_screen_brightness {
|
||||
if let Some(screen_brightness) = self.max_screen_brightness {
|
||||
content.push(
|
||||
padded_control(
|
||||
row![
|
||||
icon::from_name(self.display_icon_name.as_str())
|
||||
.size(24)
|
||||
.symbolic(true),
|
||||
slider(
|
||||
1..=self.max_screen_brightness,
|
||||
self.screen_brightness,
|
||||
1..=max_screen_brightness,
|
||||
screen_brightness,
|
||||
Message::SetScreenBrightness
|
||||
),
|
||||
text(format!("{:.0}%", self.screen_brightness_percent() * 100.))
|
||||
text(format!(
|
||||
"{:.0}%",
|
||||
self.screen_brightness_percent().unwrap_or(0.) * 100.
|
||||
))
|
||||
.size(16)
|
||||
.width(Length::Fixed(40.0))
|
||||
.horizontal_alignment(Horizontal::Right)
|
||||
|
|
@ -546,7 +556,9 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
.spacing(12),
|
||||
)
|
||||
.into(),
|
||||
];
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(max_kbd_brightness) = self.max_kbd_brightness {
|
||||
if let Some(kbd_brightness) = self.kbd_brightness {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue