Skip to content

Commit 13f4757

Browse files
committed
chore: open bluetooth settings when button is pressed
1 parent afff014 commit 13f4757

File tree

1 file changed

+40
-2
lines changed
  • cosmic-applet-bluetooth/src

1 file changed

+40
-2
lines changed

cosmic-applet-bluetooth/src/app.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
use crate::bluetooth::{BluerDeviceStatus, BluerRequest, BluerState};
2+
use cosmic::applet::token::subscription::{
3+
activation_token_subscription, TokenRequest, TokenUpdate,
4+
};
5+
use cosmic::cctk::sctk::reexports::calloop;
6+
27
use cosmic::applet::{menu_button, padded_control};
38
use cosmic::Command;
49
use cosmic::{
@@ -39,6 +44,7 @@ struct CosmicBluetoothApplet {
3944
// UI state
4045
show_visible_devices: bool,
4146
request_confirmation: Option<(BluerDevice, String, Sender<bool>)>,
47+
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
4248
}
4349

4450
impl CosmicBluetoothApplet {
@@ -62,6 +68,8 @@ enum Message {
6268
Request(BluerRequest),
6369
Cancel,
6470
Confirm,
71+
Token(TokenUpdate),
72+
OpenSettings,
6573
}
6674

6775
impl cosmic::Application for CosmicBluetoothApplet {
@@ -78,6 +86,7 @@ impl cosmic::Application for CosmicBluetoothApplet {
7886
Self {
7987
core,
8088
icon_name: "bluetooth-symbolic".to_string(),
89+
token_tx: None,
8190
..Default::default()
8291
},
8392
Command::none(),
@@ -284,6 +293,32 @@ impl cosmic::Application for CosmicBluetoothApplet {
284293
self.popup = None;
285294
}
286295
}
296+
Message::OpenSettings => {
297+
let exec = "cosmic-settings bluetooth".to_string();
298+
if let Some(tx) = self.token_tx.as_ref() {
299+
let _ = tx.send(TokenRequest {
300+
app_id: Self::APP_ID.to_string(),
301+
exec,
302+
});
303+
};
304+
}
305+
Message::Token(u) => match u {
306+
TokenUpdate::Init(tx) => {
307+
self.token_tx = Some(tx);
308+
}
309+
TokenUpdate::Finished => {
310+
self.token_tx = None;
311+
}
312+
TokenUpdate::ActivationToken { token, .. } => {
313+
let mut cmd = std::process::Command::new("cosmic-settings");
314+
cmd.arg("bluetooth");
315+
if let Some(token) = token {
316+
cmd.env("XDG_ACTIVATION_TOKEN", &token);
317+
cmd.env("DESKTOP_STARTUP_ID", &token);
318+
}
319+
cosmic::process::spawn(cmd);
320+
}
321+
},
287322
}
288323
self.update_icon();
289324
Command::none()
@@ -492,13 +527,16 @@ impl cosmic::Application for CosmicBluetoothApplet {
492527
content = content.push(padded_control(divider::horizontal::default()));
493528
content = content.push(
494529
menu_button(text(fl!("settings")).size(14).width(Length::Fill))
495-
.on_press(Message::Ignore),
530+
.on_press(Message::OpenSettings),
496531
);
497532
self.core.applet.popup_container(content).into()
498533
}
499534

500535
fn subscription(&self) -> Subscription<Message> {
501-
bluetooth_subscription(0).map(Message::BluetoothEvent)
536+
Subscription::batch(vec![
537+
activation_token_subscription(0).map(Message::Token),
538+
bluetooth_subscription(0).map(Message::BluetoothEvent),
539+
])
502540
}
503541

504542
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {

0 commit comments

Comments
 (0)