Skip to content

Commit ce479f1

Browse files
committed
chore: open network settings when the button is pressed
1 parent 13f4757 commit ce479f1

File tree

1 file changed

+39
-2
lines changed
  • cosmic-applet-network/src

1 file changed

+39
-2
lines changed

cosmic-applet-network/src/app.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use cosmic::app::Command;
2+
use cosmic::applet::token::subscription::{
3+
activation_token_subscription, TokenRequest, TokenUpdate,
4+
};
25
use cosmic::applet::{menu_button, menu_control_padding, padded_control};
6+
use cosmic::cctk::sctk::reexports::calloop;
37
use cosmic::iced_widget::Row;
48
use cosmic::{
59
iced::{
@@ -93,6 +97,7 @@ struct CosmicNetworkApplet {
9397
conn: Option<Connection>,
9498
timeline: Timeline,
9599
toggle_wifi_ctr: u128,
100+
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
96101
}
97102

98103
fn wifi_icon(strength: u8) -> &'static str {
@@ -181,6 +186,8 @@ pub(crate) enum Message {
181186
Password(String),
182187
SubmitPassword,
183188
Frame(Instant),
189+
Token(TokenUpdate),
190+
OpenSettings,
184191
// Errored(String),
185192
}
186193

@@ -195,6 +202,7 @@ impl cosmic::Application for CosmicNetworkApplet {
195202
Self {
196203
core,
197204
icon_name: "network-offline-symbolic".to_string(),
205+
token_tx: None,
198206
..Default::default()
199207
},
200208
Command::none(),
@@ -392,6 +400,32 @@ impl cosmic::Application for CosmicNetworkApplet {
392400
self.popup = None;
393401
}
394402
}
403+
Message::OpenSettings => {
404+
let exec = "cosmic-settings network".to_string();
405+
if let Some(tx) = self.token_tx.as_ref() {
406+
let _ = tx.send(TokenRequest {
407+
app_id: Self::APP_ID.to_string(),
408+
exec,
409+
});
410+
}
411+
}
412+
Message::Token(u) => match u {
413+
TokenUpdate::Init(tx) => {
414+
self.token_tx = Some(tx);
415+
}
416+
TokenUpdate::Finished => {
417+
self.token_tx = None;
418+
}
419+
TokenUpdate::ActivationToken { token, .. } => {
420+
let mut cmd = std::process::Command::new("cosmic-settings");
421+
cmd.arg("network");
422+
if let Some(token) = token {
423+
cmd.env("XDG_ACTIVATION_TOKEN", &token);
424+
cmd.env("DESKTOP_STARTUP_ID", &token);
425+
}
426+
cosmic::process::spawn(cmd);
427+
}
428+
},
395429
}
396430
Command::none()
397431
}
@@ -772,7 +806,8 @@ impl cosmic::Application for CosmicNetworkApplet {
772806
}
773807
}
774808
content = content.push(padded_control(divider::horizontal::default()));
775-
content = content.push(menu_button(text(fl!("settings")).size(14)));
809+
content = content
810+
.push(menu_button(text(fl!("settings")).size(14)).on_press(Message::OpenSettings));
776811
self.core
777812
.applet
778813
.popup_container(content.padding([8, 0, 8, 0]))
@@ -785,12 +820,14 @@ impl cosmic::Application for CosmicNetworkApplet {
785820
.timeline
786821
.as_subscription()
787822
.map(|(_, now)| Message::Frame(now));
823+
let token_sub = activation_token_subscription(0).map(Message::Token);
788824

789825
if let Some(conn) = self.conn.as_ref() {
790826
let has_popup = self.popup.is_some();
791827
Subscription::batch(vec![
792828
timeline,
793829
network_sub,
830+
token_sub,
794831
active_conns_subscription(self.toggle_wifi_ctr, conn.clone())
795832
.map(Message::NetworkManagerEvent),
796833
devices_subscription(self.toggle_wifi_ctr, has_popup, conn.clone())
@@ -799,7 +836,7 @@ impl cosmic::Application for CosmicNetworkApplet {
799836
.map(Message::NetworkManagerEvent),
800837
])
801838
} else {
802-
Subscription::batch(vec![timeline, network_sub])
839+
Subscription::batch(vec![timeline, network_sub, token_sub])
803840
}
804841
}
805842

0 commit comments

Comments
 (0)