From 1e8f1fe76993e567b588bfcfbe0779ab21fbaf20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 27 Oct 2023 13:45:07 +0200 Subject: [PATCH] utils: Add message dialog to confirm leaving a room --- po/POTFILES.in | 3 ++- src/utils/message_dialog.rs | 51 +++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 1 + 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/utils/message_dialog.rs diff --git a/po/POTFILES.in b/po/POTFILES.in index d6082123..61acb991 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -108,6 +108,7 @@ src/session/view/sidebar/room_row.rs src/session/view/sidebar/row.rs src/shortcuts.ui src/user_facing_error.rs -src/utils/media.rs src/utils/matrix.rs +src/utils/media.rs +src/utils/message_dialog.rs src/window.rs diff --git a/src/utils/message_dialog.rs b/src/utils/message_dialog.rs new file mode 100644 index 00000000..3af97871 --- /dev/null +++ b/src/utils/message_dialog.rs @@ -0,0 +1,51 @@ +//! Common message dialogs. + +use adw::prelude::*; +use gettextrs::gettext; + +use crate::session::model::{Room, RoomType}; + +/// Show a dialog to confirm leaving a room. +/// +/// This supports both leaving a joined room and rejecting an invite. +pub async fn confirm_leave_room(room: &Room, transient_for: >k::Window) -> bool { + let (heading, body, response) = if room.category() == RoomType::Invited { + // We are rejecting an invite. + let heading = gettext("Decline Invite?"); + let body = if room.is_join_rule_public() { + gettext("Do you really want to decline this invite? You can join this room on you own later.") + } else { + gettext( + "Do you really want to decline this invite? You won’t be able to join this room without it.", + ) + }; + let response = gettext("Decline"); + + (heading, body, response) + } else { + // We are leaving a room that was joined. + let heading = gettext("Leave Room?"); + let body = if room.is_join_rule_public() { + gettext("Do you really want to leave this room? You can come back later.") + } else { + gettext( + "Do you really want to leave this room? You won’t be able to come back without an invitation.", + ) + }; + let response = gettext("Leave"); + + (heading, body, response) + }; + + // Ask for confirmation. + let confirm_dialog = adw::MessageDialog::builder() + .transient_for(transient_for) + .default_response("cancel") + .heading(heading) + .body(body) + .build(); + confirm_dialog.add_responses(&[("cancel", &gettext("Cancel")), ("leave", &response)]); + confirm_dialog.set_response_appearance("leave", adw::ResponseAppearance::Destructive); + + confirm_dialog.choose_future().await == "leave" +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index f357b675..6eb78806 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -4,6 +4,7 @@ mod expression_list_model; pub mod macros; pub mod matrix; pub mod media; +pub mod message_dialog; pub mod notifications; pub mod sourceview; pub mod template_callbacks;