battery: Show keyboard backlight only when supported
This commit is contained in:
parent
d5aa2f3252
commit
a18d64add7
3 changed files with 45 additions and 56 deletions
|
|
@ -77,7 +77,7 @@ struct CosmicBatteryApplet {
|
|||
on_battery: bool,
|
||||
gpus: HashMap<PathBuf, GPUData>,
|
||||
time_remaining: Duration,
|
||||
kbd_brightness: f64,
|
||||
kbd_brightness: Option<f64>,
|
||||
screen_brightness: f64,
|
||||
popup: Option<window::Id>,
|
||||
screen_sender: Option<UnboundedSender<ScreenBacklightRequest>>,
|
||||
|
|
@ -154,9 +154,9 @@ enum Message {
|
|||
SetKbdBrightness(i32),
|
||||
SetScreenBrightness(i32),
|
||||
SetChargingLimit(chain::Toggler, bool),
|
||||
UpdateKbdBrightness(f64),
|
||||
UpdateKbdBrightness(Option<f64>),
|
||||
UpdateScreenBrightness(f64),
|
||||
InitKbdBacklight(UnboundedSender<KeyboardBacklightRequest>, f64),
|
||||
InitKbdBacklight(UnboundedSender<KeyboardBacklightRequest>),
|
||||
InitScreenBacklight(UnboundedSender<ScreenBacklightRequest>, f64),
|
||||
GpuOn(PathBuf, String, Option<Vec<Entry>>),
|
||||
GpuOff(PathBuf),
|
||||
|
|
@ -211,9 +211,10 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
match message {
|
||||
Message::Frame(now) => self.timeline.now(now),
|
||||
Message::SetKbdBrightness(brightness) => {
|
||||
self.kbd_brightness = (brightness as f64 / 100.0).clamp(0., 1.);
|
||||
let brightness = (brightness as f64 / 100.0).clamp(0., 1.);
|
||||
self.kbd_brightness = Some(brightness);
|
||||
if let Some(tx) = &self.kbd_sender {
|
||||
let _ = tx.send(KeyboardBacklightRequest::Set(self.kbd_brightness));
|
||||
let _ = tx.send(KeyboardBacklightRequest::Set(brightness));
|
||||
}
|
||||
}
|
||||
Message::SetScreenBrightness(brightness) => {
|
||||
|
|
@ -272,10 +273,8 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
Message::UpdateKbdBrightness(b) => {
|
||||
self.kbd_brightness = b;
|
||||
}
|
||||
Message::InitKbdBacklight(tx, brightness) => {
|
||||
let _ = tx.send(KeyboardBacklightRequest::Get);
|
||||
Message::InitKbdBacklight(tx) => {
|
||||
self.kbd_sender = Some(tx);
|
||||
self.kbd_brightness = brightness;
|
||||
}
|
||||
Message::InitScreenBacklight(tx, brightness) => {
|
||||
let _ = tx.send(ScreenBacklightRequest::Get);
|
||||
|
|
@ -523,27 +522,33 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
.spacing(12),
|
||||
)
|
||||
.into(),
|
||||
padded_control(
|
||||
row![
|
||||
icon::from_name("keyboard-brightness-symbolic")
|
||||
.size(24)
|
||||
.symbolic(true),
|
||||
slider(
|
||||
0..=100,
|
||||
(self.kbd_brightness * 100.0) as i32,
|
||||
Message::SetKbdBrightness
|
||||
),
|
||||
text(format!("{:.0}%", self.kbd_brightness * 100.0))
|
||||
.size(16)
|
||||
.width(Length::Fixed(40.0))
|
||||
.horizontal_alignment(Horizontal::Right)
|
||||
]
|
||||
.spacing(12),
|
||||
)
|
||||
.into(),
|
||||
padded_control(divider::horizontal::default()).into(),
|
||||
];
|
||||
|
||||
if let Some(kbd_brightness) = self.kbd_brightness {
|
||||
content.push(
|
||||
padded_control(
|
||||
row![
|
||||
icon::from_name("keyboard-brightness-symbolic")
|
||||
.size(24)
|
||||
.symbolic(true),
|
||||
slider(
|
||||
0..=100,
|
||||
(kbd_brightness * 100.0) as i32,
|
||||
Message::SetKbdBrightness
|
||||
),
|
||||
text(format!("{:.0}%", kbd_brightness * 100.0))
|
||||
.size(16)
|
||||
.width(Length::Fixed(40.0))
|
||||
.horizontal_alignment(Horizontal::Right)
|
||||
]
|
||||
.spacing(12),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
content.push(padded_control(divider::horizontal::default()).into());
|
||||
|
||||
if !self.gpus.is_empty() {
|
||||
content.push(
|
||||
padded_control(
|
||||
|
|
@ -670,8 +675,8 @@ impl cosmic::Application for CosmicBatteryApplet {
|
|||
},
|
||||
),
|
||||
kbd_backlight_subscription(0).map(|event| match event {
|
||||
KeyboardBacklightUpdate::Update(b) => Message::UpdateKbdBrightness(b),
|
||||
KeyboardBacklightUpdate::Init(tx, b) => Message::InitKbdBacklight(tx, b),
|
||||
KeyboardBacklightUpdate::Brightness(b) => Message::UpdateKbdBrightness(b),
|
||||
KeyboardBacklightUpdate::Sender(tx) => Message::InitKbdBacklight(tx),
|
||||
}),
|
||||
screen_backlight_subscription(0).map(|e| match e {
|
||||
ScreenBacklightUpdate::Update(b) => Message::UpdateScreenBrightness(b),
|
||||
|
|
|
|||
|
|
@ -160,22 +160,3 @@ pub enum ScreenBacklightRequest {
|
|||
Get,
|
||||
Set(f64),
|
||||
}
|
||||
|
||||
/*
|
||||
// TODO: Cache device, max_brightness, etc.
|
||||
async fn set_display_brightness(brightness: f64) -> io::Result<()> {
|
||||
if let Some(backlight) = backlight()? {
|
||||
if let Some(max_brightness) = backlight.max_brightness() {
|
||||
let value = brightness.clamp(0., 1.) * (max_brightness as f64);
|
||||
let value = value.round() as u32;
|
||||
let connection = zbus::Connection::system().await?;
|
||||
if let Ok(session) = LogindSessionProxy::builder(&connection).build().await {
|
||||
backlight.set_brightness(&session, value).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
*/
|
||||
|
||||
// TODO: keyboard backlight
|
||||
|
|
|
|||
|
|
@ -54,6 +54,10 @@ pub enum State {
|
|||
Finished,
|
||||
}
|
||||
|
||||
async fn get_brightness(kbd_proxy: &KbdBacklightProxy<'_>) -> zbus::Result<f64> {
|
||||
Ok(kbd_proxy.get_brightness().await? as f64 / kbd_proxy.get_max_brightness().await? as f64)
|
||||
}
|
||||
|
||||
async fn start_listening(
|
||||
state: State,
|
||||
output: &mut futures::channel::mpsc::Sender<KeyboardBacklightUpdate>,
|
||||
|
|
@ -70,18 +74,17 @@ async fn start_listening(
|
|||
};
|
||||
let (tx, rx) = unbounded_channel();
|
||||
|
||||
let b = kbd_proxy.get_brightness().await.unwrap_or_default() as f64
|
||||
/ kbd_proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
||||
_ = output.send(KeyboardBacklightUpdate::Init(tx, b)).await;
|
||||
let b = get_brightness(&kbd_proxy).await.ok();
|
||||
_ = output.send(KeyboardBacklightUpdate::Sender(tx)).await;
|
||||
_ = output.send(KeyboardBacklightUpdate::Brightness(b)).await;
|
||||
|
||||
State::Waiting(kbd_proxy, rx)
|
||||
}
|
||||
State::Waiting(proxy, mut rx) => match rx.recv().await {
|
||||
Some(req) => match req {
|
||||
KeyboardBacklightRequest::Get => {
|
||||
let b = proxy.get_brightness().await.unwrap_or_default() as f64
|
||||
/ proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
||||
_ = output.send(KeyboardBacklightUpdate::Update(b)).await;
|
||||
let b = get_brightness(&proxy).await.ok();
|
||||
_ = output.send(KeyboardBacklightUpdate::Brightness(b)).await;
|
||||
State::Waiting(proxy, rx)
|
||||
}
|
||||
KeyboardBacklightRequest::Set(value) => {
|
||||
|
|
@ -102,8 +105,8 @@ async fn start_listening(
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum KeyboardBacklightUpdate {
|
||||
Update(f64),
|
||||
Init(UnboundedSender<KeyboardBacklightRequest>, f64),
|
||||
Sender(UnboundedSender<KeyboardBacklightRequest>),
|
||||
Brightness(Option<f64>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue