Skip to content

Commit b7b7b95

Browse files
committed
chore: open notification settings when the button is pressed
1 parent ce479f1 commit b7b7b95

File tree

1 file changed

+36
-8
lines changed
  • cosmic-applet-notifications/src

1 file changed

+36
-8
lines changed

cosmic-applet-notifications/src/main.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
mod localize;
22
mod subscriptions;
3-
3+
use cosmic::applet::token::subscription::{
4+
activation_token_subscription, TokenRequest, TokenUpdate,
5+
};
46
use cosmic::applet::{menu_button, menu_control_padding, padded_control};
7+
use cosmic::cctk::sctk::reexports::calloop;
58
use cosmic::cosmic_config::{config_subscription, Config, CosmicConfigEntry};
69
use cosmic::iced::wayland::popup::{destroy_popup, get_popup};
710
use cosmic::iced::Limits;
@@ -23,7 +26,6 @@ use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline};
2326
use std::borrow::Cow;
2427
use std::collections::HashMap;
2528
use std::path::PathBuf;
26-
use std::process;
2729
use tokio::sync::mpsc::Sender;
2830
use tracing::info;
2931

@@ -52,6 +54,7 @@ struct Notifications {
5254
timeline: Timeline,
5355
dbus_sender: Option<Sender<subscriptions::dbus::Input>>,
5456
cards: Vec<(id::Cards, Vec<Notification>, bool, String, String, String)>,
57+
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
5558
}
5659

5760
impl Notifications {
@@ -84,14 +87,15 @@ enum Message {
8487
TogglePopup,
8588
CloseRequested(window::Id),
8689
DoNotDisturb(chain::Toggler, bool),
87-
Settings,
8890
Frame(Instant),
8991
NotificationEvent(Notification),
9092
Config(NotificationsConfig),
9193
DbusEvent(subscriptions::dbus::Output),
9294
Dismissed(u32),
9395
ClearAll(String),
9496
CardsToggled(String, bool),
97+
Token(TokenUpdate),
98+
OpenSettings,
9599
}
96100

97101
impl cosmic::Application for Notifications {
@@ -167,6 +171,7 @@ impl cosmic::Application for Notifications {
167171
.map(|(_, now)| Message::Frame(now)),
168172
subscriptions::dbus::proxy().map(Message::DbusEvent),
169173
subscriptions::notifications::notifications().map(Message::NotificationEvent),
174+
activation_token_subscription(0).map(Message::Token),
170175
])
171176
}
172177

@@ -210,9 +215,6 @@ impl cosmic::Application for Notifications {
210215
}
211216
}
212217
}
213-
Message::Settings => {
214-
let _ = process::Command::new("cosmic-settings notifications").spawn();
215-
}
216218
Message::NotificationEvent(n) => {
217219
if let Some(c) = self
218220
.cards
@@ -311,6 +313,32 @@ impl cosmic::Application for Notifications {
311313
self.popup = None;
312314
}
313315
}
316+
Message::OpenSettings => {
317+
let exec = "cosmic-settings notifications".to_string();
318+
if let Some(tx) = self.token_tx.as_ref() {
319+
let _ = tx.send(TokenRequest {
320+
app_id: Self::APP_ID.to_string(),
321+
exec,
322+
});
323+
}
324+
}
325+
Message::Token(u) => match u {
326+
TokenUpdate::Init(tx) => {
327+
self.token_tx = Some(tx);
328+
}
329+
TokenUpdate::Finished => {
330+
self.token_tx = None;
331+
}
332+
TokenUpdate::ActivationToken { token, .. } => {
333+
let mut cmd = std::process::Command::new("cosmic-settings");
334+
cmd.arg("notifications");
335+
if let Some(token) = token {
336+
cmd.env("XDG_ACTIVATION_TOKEN", &token);
337+
cmd.env("DESKTOP_STARTUP_ID", &token);
338+
}
339+
cosmic::process::spawn(cmd);
340+
}
341+
},
314342
};
315343
self.update_icon();
316344
Command::none()
@@ -335,8 +363,8 @@ impl cosmic::Application for Notifications {
335363
.text_size(14)
336364
.width(Length::Fill)]);
337365

338-
let settings =
339-
menu_button(text(fl!("notification-settings")).size(14)).on_press(Message::Settings);
366+
let settings = menu_button(text(fl!("notification-settings")).size(14))
367+
.on_press(Message::OpenSettings);
340368

341369
let notifications = if self.cards.is_empty() {
342370
row![container(

0 commit comments

Comments
 (0)