Skip to content

Commit

Permalink
In GNOME 46 (Wayland), notifications are immediately dismissed withou…
Browse files Browse the repository at this point in the history
…t user interaction if `on_close` is called on the `NotificationHandle` (related issue: hoodie/notify-rust#218).  This adds a noop `on_close` to ensure that doesn't happen, but it prevents Halloy from closing until all notifications are dismissed.
  • Loading branch information
andymandias committed May 17, 2024
1 parent 317a1b8 commit 047015c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
55 changes: 41 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub enum Message {
Tick(Instant),
Version(Option<String>),
CloseModal,
Notification(Result<(), notify_rust::error::Error>),
}

impl Application for Halloy {
Expand Down Expand Up @@ -346,13 +347,20 @@ impl Application for Halloy {
// Intial is sent when first trying to connect
dashboard.broadcast_connecting(&server, &self.config, sent_time);
} else {
dashboard.broadcast_disconnected(&server, error, &self.config, sent_time);

let notification = &self.config.notifications.disconnected;

if notification.enabled {
notification::show("Disconnected", &server, notification.sound());
return Command::perform(
notification::show(
"Disconnected".to_string(),
server.to_string(),
notification.sound().map(|sound| sound.to_string()),
),
Message::Notification,
);
};

dashboard.broadcast_disconnected(&server, error, &self.config, sent_time);
}

Command::none()
Expand All @@ -370,21 +378,35 @@ impl Application for Halloy {
};

if is_initial {
dashboard.broadcast_connected(&server, &self.config, sent_time);

let notification = &self.config.notifications.connected;

if notification.enabled {
notification::show("Connected", &server, notification.sound());
return Command::perform(
notification::show(
"Connected".to_string(),
server.to_string(),
notification.sound().map(|sound| sound.to_string()),
),
Message::Notification,
);
}

dashboard.broadcast_connected(&server, &self.config, sent_time);
} else {
dashboard.broadcast_reconnected(&server, &self.config, sent_time);

let notification = &self.config.notifications.reconnected;

if notification.enabled {
notification::show("Reconnected", &server, notification.sound());
return Command::perform(
notification::show(
"Reconnected".to_string(),
server.to_string(),
notification.sound().map(|sound| sound.to_string()),
),
Message::Notification,
);
}

dashboard.broadcast_reconnected(&server, &self.config, sent_time);
}

Command::none()
Expand Down Expand Up @@ -528,12 +550,16 @@ impl Application for Halloy {
match notification {
data::client::Notification::Highlight(user, channel) => {
let notification = &self.config.notifications.highlight;

if notification.enabled {
notification::show(
"Highlight",
format!("{} highlighted you in {}", user.nickname(), channel),
notification.sound(),
);
commands.push(Command::perform(
notification::show(
"Highlight".to_string(),
format!("{} highlighted you in {}", user.nickname(), channel),
notification.sound().map(|sound| sound.to_string()),
),
Message::Notification,
));
}
}
}
Expand Down Expand Up @@ -711,6 +737,7 @@ impl Application for Halloy {

Command::none()
}
Message::Notification(_) => Command::none(),
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ pub fn prepare() {
#[cfg(not(target_os = "macos"))]
pub fn prepare() {}

pub fn show(title: &str, body: impl ToString, sound: Option<&str>) {
pub async fn show(
title: String,
body: String,
sound: Option<String>,
) -> Result<(), notify_rust::error::Error> {
let mut notification = notify_rust::Notification::new();

notification.summary(title);
notification.body(&body.to_string());
notification.summary(&title);
notification.body(&body);

if let Some(sound) = sound {
notification.sound_name(sound);
notification.sound_name(&sound);
}

#[cfg(target_os = "linux")]
Expand All @@ -31,5 +35,14 @@ pub fn show(title: &str, body: impl ToString, sound: Option<&str>) {
notification.app_id(data::environment::APPLICATION_ID);
}

let _ = notification.show();
#[cfg(target_os = "linux")]
{
notification.show_async().await?.on_close(|_| ());
}
#[cfg(not(target_os = "linux"))]
{
notification.show_async().await?;
}

Ok(())
}
18 changes: 14 additions & 4 deletions src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum Message {
FileTransfer(file_transfer::task::Update),
SendFileSelected(Server, Nick, Option<PathBuf>),
CloseContextMenu(bool),
Notification(Result<(), notify_rust::error::Error>),
}

#[derive(Debug)]
Expand Down Expand Up @@ -635,6 +636,7 @@ impl Dashboard {
}
}
}
Message::Notification(_) => (),
}

(Command::none(), None)
Expand Down Expand Up @@ -1374,10 +1376,18 @@ impl Dashboard {
let notification = &config.notifications.file_transfer_request;

if notification.enabled {
let text = format!("File Transfer Request: {}", request.from);

notification::show(text.as_str(), server, notification.sound());
};
return Some(Command::batch(vec![
Command::perform(
notification::show(
format!("File Transfer Request: {}", request.from),
server.to_string(),
notification.sound().map(|sound| sound.to_string()),
),
Message::Notification,
),
self.handle_file_transfer_event(server, event),
]));
}

return Some(self.handle_file_transfer_event(server, event));
}
Expand Down

0 comments on commit 047015c

Please sign in to comment.