chore: open power settings when the batter button is pressed

This commit is contained in:
Ashley Wulber 2023-11-21 17:41:56 -05:00 committed by Ashley Wulber
parent 2dcb76c2d4
commit afff014dd2
3 changed files with 41 additions and 5 deletions

1
Cargo.lock generated
View file

@ -821,6 +821,7 @@ dependencies = [
"pretty_env_logger 0.5.0", "pretty_env_logger 0.5.0",
"rust-embed 6.8.1", "rust-embed 6.8.1",
"tokio", "tokio",
"tracing",
"zbus", "zbus",
] ]

View file

@ -7,6 +7,7 @@ edition = "2021"
once_cell = "1.16.0" once_cell = "1.16.0"
libcosmic.workspace = true libcosmic.workspace = true
cosmic-time.workspace = true cosmic-time.workspace = true
tracing.workspace = true
futures = "0.3" futures = "0.3"
zbus.workspace = true zbus.workspace = true
log = "0.4" log = "0.4"

View file

@ -10,7 +10,11 @@ use crate::upower_device::{device_subscription, DeviceDbusEvent};
use crate::upower_kbdbacklight::{ use crate::upower_kbdbacklight::{
kbd_backlight_subscription, KeyboardBacklightRequest, KeyboardBacklightUpdate, kbd_backlight_subscription, KeyboardBacklightRequest, KeyboardBacklightUpdate,
}; };
use cosmic::applet::token::subscription::{
activation_token_subscription, TokenRequest, TokenUpdate,
};
use cosmic::applet::{menu_button, padded_control}; use cosmic::applet::{menu_button, padded_control};
use cosmic::cctk::sctk::reexports::calloop;
use cosmic::iced::alignment::Horizontal; use cosmic::iced::alignment::Horizontal;
use cosmic::iced::wayland::popup::{destroy_popup, get_popup}; use cosmic::iced::wayland::popup::{destroy_popup, get_popup};
use cosmic::iced::{ use cosmic::iced::{
@ -68,6 +72,7 @@ struct CosmicBatteryApplet {
power_profile: Power, power_profile: Power,
power_profile_sender: Option<UnboundedSender<PowerProfileRequest>>, power_profile_sender: Option<UnboundedSender<PowerProfileRequest>>,
timeline: Timeline, timeline: Timeline,
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
} }
impl CosmicBatteryApplet { impl CosmicBatteryApplet {
@ -138,7 +143,6 @@ enum Message {
SetChargingLimit(chain::Toggler, bool), SetChargingLimit(chain::Toggler, bool),
UpdateKbdBrightness(f64), UpdateKbdBrightness(f64),
UpdateScreenBrightness(f64), UpdateScreenBrightness(f64),
OpenBatterySettings,
InitKbdBacklight(UnboundedSender<KeyboardBacklightRequest>, f64), InitKbdBacklight(UnboundedSender<KeyboardBacklightRequest>, f64),
InitScreenBacklight(UnboundedSender<ScreenBacklightRequest>, f64), InitScreenBacklight(UnboundedSender<ScreenBacklightRequest>, f64),
Errored(String), Errored(String),
@ -146,6 +150,8 @@ enum Message {
Profile(Power), Profile(Power),
SelectProfile(Power), SelectProfile(Power),
Frame(Instant), Frame(Instant),
Token(TokenUpdate),
OpenSettings,
} }
impl cosmic::Application for CosmicBatteryApplet { impl cosmic::Application for CosmicBatteryApplet {
@ -166,6 +172,8 @@ impl cosmic::Application for CosmicBatteryApplet {
core, core,
icon_name: "battery-symbolic".to_string(), icon_name: "battery-symbolic".to_string(),
display_icon_name: "display-brightness-symbolic".to_string(), display_icon_name: "display-brightness-symbolic".to_string(),
token_tx: None,
..Default::default() ..Default::default()
}, },
Command::none(), Command::none(),
@ -202,9 +210,6 @@ impl cosmic::Application for CosmicBatteryApplet {
self.timeline.set_chain(chain).start(); self.timeline.set_chain(chain).start();
self.set_charging_limit(enable); self.set_charging_limit(enable);
} }
Message::OpenBatterySettings => {
// TODO Ashley
}
Message::Errored(e) => { Message::Errored(e) => {
error!("{}", e); error!("{}", e);
} }
@ -288,6 +293,34 @@ impl cosmic::Application for CosmicBatteryApplet {
self.popup = None; self.popup = None;
} }
} }
Message::OpenSettings => {
let exec = "cosmic-settings power".to_string();
if let Some(tx) = self.token_tx.as_ref() {
let _ = tx.send(TokenRequest {
app_id: Self::APP_ID.to_string(),
exec,
});
} else {
tracing::error!("Wayland tx is None");
};
}
Message::Token(u) => match u {
TokenUpdate::Init(tx) => {
self.token_tx = Some(tx);
}
TokenUpdate::Finished => {
self.token_tx = None;
}
TokenUpdate::ActivationToken { token, .. } => {
let mut cmd = std::process::Command::new("cosmic-settings");
cmd.arg("power");
if let Some(token) = token {
cmd.env("XDG_ACTIVATION_TOKEN", &token);
cmd.env("DESKTOP_STARTUP_ID", &token);
}
cosmic::process::spawn(cmd);
}
},
} }
Command::none() Command::none()
} }
@ -436,7 +469,7 @@ impl cosmic::Application for CosmicBatteryApplet {
), ),
padded_control(divider::horizontal::default()), padded_control(divider::horizontal::default()),
menu_button(text(fl!("power-settings")).size(14).width(Length::Fill)) menu_button(text(fl!("power-settings")).size(14).width(Length::Fill))
.on_press(Message::OpenBatterySettings) .on_press(Message::OpenSettings)
] ]
.padding([8, 0]), .padding([8, 0]),
) )
@ -472,6 +505,7 @@ impl cosmic::Application for CosmicBatteryApplet {
self.timeline self.timeline
.as_subscription() .as_subscription()
.map(|(_, now)| Message::Frame(now)), .map(|(_, now)| Message::Frame(now)),
activation_token_subscription(0).map(Message::Token),
]) ])
} }