From fe2f3258b254e2f4a8662bb010cc94656cdc3520 Mon Sep 17 00:00:00 2001
From: lcoursodon <liam.coursodon@savoirfairelinux.com>
Date: Mon, 3 Jul 2023 10:44:40 -0400
Subject: [PATCH] Popup : Close all popups by pressing escape

Using a key OnPressed instead of Shortcut
Making all popups closable with escape (focus true and a close policy)

GitLab: #1234
Change-Id: I1a612834c439aea94a59e91cb915b18b4ef5cf84
---
 src/app/commoncomponents/BaseModalDialog.qml        |  1 +
 src/app/commoncomponents/EmojiReactionPopup.qml     |  1 +
 src/app/commoncomponents/MarkdownPopup.qml          |  3 +++
 src/app/commoncomponents/MessageOptionsPopup.qml    |  3 +++
 src/app/mainview/MainView.qml                       | 13 ++++++-------
 .../mainview/components/AccountComboBoxPopup.qml    |  3 +++
 src/app/mainview/components/ChatViewFooter.qml      |  5 +----
 src/app/mainview/components/RecordBox.qml           |  6 +++---
 src/app/webengine/emojipicker/EmojiPicker.qml       |  3 +++
 src/app/webengine/map/StopSharingPositionPopup.qml  |  1 +
 src/app/wizardview/components/NoUsernamePopup.qml   |  1 +
 11 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/src/app/commoncomponents/BaseModalDialog.qml b/src/app/commoncomponents/BaseModalDialog.qml
index 0fdc3ff71..2ee3de52a 100644
--- a/src/app/commoncomponents/BaseModalDialog.qml
+++ b/src/app/commoncomponents/BaseModalDialog.qml
@@ -47,6 +47,7 @@ Popup {
 
     // A popup is invisible until opened.
     visible: false
+    focus: true
     closePolicy: autoClose ? (Popup.CloseOnEscape | Popup.CloseOnPressOutside) : Popup.NoAutoClose
 
     Rectangle {
diff --git a/src/app/commoncomponents/EmojiReactionPopup.qml b/src/app/commoncomponents/EmojiReactionPopup.qml
index cc2dfbb3f..2358870a2 100644
--- a/src/app/commoncomponents/EmojiReactionPopup.qml
+++ b/src/app/commoncomponents/EmojiReactionPopup.qml
@@ -42,6 +42,7 @@ Popup {
     padding: 0
 
     visible: false
+    focus: true
     closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
 
     Rectangle {
diff --git a/src/app/commoncomponents/MarkdownPopup.qml b/src/app/commoncomponents/MarkdownPopup.qml
index 0423571e5..67c727371 100644
--- a/src/app/commoncomponents/MarkdownPopup.qml
+++ b/src/app/commoncomponents/MarkdownPopup.qml
@@ -30,6 +30,9 @@ Popup {
     padding: 0
     property list<Action> menuTypoActionsSecond
 
+    focus: true
+    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
+
     contentItem: ListView {
         id: listViewTypoSecond
 
diff --git a/src/app/commoncomponents/MessageOptionsPopup.qml b/src/app/commoncomponents/MessageOptionsPopup.qml
index 9a32d41a5..e359bc196 100644
--- a/src/app/commoncomponents/MessageOptionsPopup.qml
+++ b/src/app/commoncomponents/MessageOptionsPopup.qml
@@ -49,6 +49,9 @@ Popup {
     property bool closeWithoutAnimation: false
     property var emojiPicker
 
+    focus: true
+    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
+
     function xPositionProvider(width) {
         // Use the width at function scope to retrigger property evaluation.
         const listViewWidth = listView.width
diff --git a/src/app/mainview/MainView.qml b/src/app/mainview/MainView.qml
index 6735e2b09..cd7c5a211 100644
--- a/src/app/mainview/MainView.qml
+++ b/src/app/mainview/MainView.qml
@@ -149,13 +149,12 @@ Rectangle {
         onActivated: layoutManager.toggleWindowFullScreen()
     }
 
-    Shortcut {
-        sequence: "Escape"
-        context: Qt.ApplicationShortcut
-        onActivated: {
-            MessagesAdapter.replyToId = ""
-            MessagesAdapter.editId = ""
-            layoutManager.popFullScreenItem()
+    Keys.onPressed: function (keyEvent) {
+        if (keyEvent.key === Qt.Key_Escape) {
+            MessagesAdapter.replyToId = "";
+            MessagesAdapter.editId = "";
+            layoutManager.popFullScreenItem();
+            keyEvent.accepted = true;
         }
     }
 
diff --git a/src/app/mainview/components/AccountComboBoxPopup.qml b/src/app/mainview/components/AccountComboBoxPopup.qml
index 1328e59ed..c9dac291e 100644
--- a/src/app/mainview/components/AccountComboBoxPopup.qml
+++ b/src/app/mainview/components/AccountComboBoxPopup.qml
@@ -40,6 +40,9 @@ Popup {
         color: JamiTheme.transparentColor
     }
 
+    focus: true
+    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
+
     contentItem: ColumnLayout {
         spacing: 0
 
diff --git a/src/app/mainview/components/ChatViewFooter.qml b/src/app/mainview/components/ChatViewFooter.qml
index a15ef68a5..c37ac3a99 100644
--- a/src/app/mainview/components/ChatViewFooter.qml
+++ b/src/app/mainview/components/ChatViewFooter.qml
@@ -220,10 +220,7 @@ Rectangle {
 
             Keys.onShortcutOverride: function (keyEvent) {
                 if (keyEvent.key === Qt.Key_Escape) {
-                    if (emojiPicker != null && emojiPicker.opened) {
-                        emojiPicker.closeEmojiPicker();
-                    }
-                    else if (recordBox != null && recordBox.opened) {
+                    if (recordBox != null && recordBox.opened) {
                         recordBox.closeRecorder();
                     }
                     else if (PositionManager.isMapActive(CurrentAccount.id)){
diff --git a/src/app/mainview/components/RecordBox.qml b/src/app/mainview/components/RecordBox.qml
index 1e1aca668..c7cb03600 100644
--- a/src/app/mainview/components/RecordBox.qml
+++ b/src/app/mainview/components/RecordBox.qml
@@ -51,6 +51,9 @@ Popup {
 
     signal validatePhoto(string photo)
 
+    modal: true
+    closePolicy: Popup.CloseOnPressOutsideParent
+
     function openRecorder(vid) {
         isVideo = vid;
         updateState(RecordBox.States.INIT);
@@ -121,9 +124,6 @@ Popup {
         time.text = min + ":" + sec;
     }
 
-    modal: true
-    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
-
     onActiveFocusChanged: {
         if (visible) {
             closeRecorder();
diff --git a/src/app/webengine/emojipicker/EmojiPicker.qml b/src/app/webengine/emojipicker/EmojiPicker.qml
index c268e3606..e3f3cf6d7 100644
--- a/src/app/webengine/emojipicker/EmojiPicker.qml
+++ b/src/app/webengine/emojipicker/EmojiPicker.qml
@@ -32,6 +32,9 @@ Popup {
     required property ListView listView
     signal emojiIsPicked(string content)
 
+    focus: true
+    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
+
     // Close the picker when attached to a listView that receives height/scroll
     // property changes.
     property real listViewHeight: listView ? listView.height : 0
diff --git a/src/app/webengine/map/StopSharingPositionPopup.qml b/src/app/webengine/map/StopSharingPositionPopup.qml
index 81c9e205d..153d81efc 100644
--- a/src/app/webengine/map/StopSharingPositionPopup.qml
+++ b/src/app/webengine/map/StopSharingPositionPopup.qml
@@ -42,6 +42,7 @@ Popup {
     padding: 0
 
     visible: false
+    focus: true
     closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
     Rectangle {
         id: container
diff --git a/src/app/wizardview/components/NoUsernamePopup.qml b/src/app/wizardview/components/NoUsernamePopup.qml
index 3fa1bf261..4bf79a2c7 100644
--- a/src/app/wizardview/components/NoUsernamePopup.qml
+++ b/src/app/wizardview/components/NoUsernamePopup.qml
@@ -42,6 +42,7 @@ Popup {
     padding: 0
 
     visible: false
+    focus: true
     closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
 
     Rectangle {
-- 
GitLab