From e6bdb6f37836d382f368e24ba57435b43cbae94c Mon Sep 17 00:00:00 2001
From: Pierre Lamot <pierre@videolabs.io>
Date: Fri, 10 Sep 2021 10:24:18 +0200
Subject: [PATCH] qml: minor refactor in SearchBox

---
 .../qt/maininterface/qml/BannerSources.qml    |  4 +-
 modules/gui/qt/widgets/qml/SearchBox.qml      | 54 +++++++++----------
 2 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/modules/gui/qt/maininterface/qml/BannerSources.qml b/modules/gui/qt/maininterface/qml/BannerSources.qml
index 241d7aee3ce3..eaab88f92359 100644
--- a/modules/gui/qt/maininterface/qml/BannerSources.qml
+++ b/modules/gui/qt/maininterface/qml/BannerSources.qml
@@ -56,8 +56,7 @@ FocusScope {
     }
 
     function search() {
-        if (searchBox.visible)
-            searchBox.expanded = true
+        searchBox.reqExpand()
     }
 
     Binding {
@@ -387,6 +386,7 @@ FocusScope {
                                 visible: root.contentModel !== undefined
                                 enabled: visible
                                 height: VLCStyle.bannerButton_height
+                                width: searchBox.implicitWith
                                 buttonWidth: VLCStyle.bannerButton_width
                             }
 
diff --git a/modules/gui/qt/widgets/qml/SearchBox.qml b/modules/gui/qt/widgets/qml/SearchBox.qml
index 9111cb9809f8..04a7b6802c58 100644
--- a/modules/gui/qt/widgets/qml/SearchBox.qml
+++ b/modules/gui/qt/widgets/qml/SearchBox.qml
@@ -27,52 +27,50 @@ import "qrc:///widgets/" as Widgets
 FocusScope {
     id: root
 
-    width: content.width
+    implicitWidth: content.implicitWidth
 
     property alias buttonWidth: icon.width
+    property alias searchPattern: searchBox.text
 
-    property bool expanded: false
+    property bool _expanded: false
 
-    property alias searchPattern: searchBox.text
+    function reqExpand() {
+        if (visible)
+            _expanded = true
+    }
 
-    onExpandedChanged: {
-        if (expanded) {
-            searchBox.forceActiveFocus()
-            icon.Navigation.rightItem = searchBox
+    on_ExpandedChanged: {
+        if (_expanded) {
+            searchBox.forceActiveFocus(Qt.ShortcutFocusReason)
             animateExpand.start()
-        }
-        else {
-            searchBox.placeholderText = ""
-            searchBox.text = ""
+        } else {
+            searchBox.clear()
             icon.focus = true
             searchBox.focus = false
-            icon.Navigation.rightItem = null
             animateRetract.start()
         }
     }
 
     onActiveFocusChanged: {
-        if (!activeFocus && searchBox.text == "")
-            expanded = false
+        if (!activeFocus && searchBox.text == "") {
+            _expanded = false
+        }
     }
 
     SmoothedAnimation {
         id: animateExpand;
         target: searchBoxRect;
         properties: "width"
-        duration: VLCStyle.ms125
+        duration: VLCStyle.duration_faster
         to: VLCStyle.widthSearchInput
         easing.type: Easing.InSine
-        onStopped: {
-            searchBox.placeholderText = i18n.qtr("filter")
-        }
     }
 
     SmoothedAnimation {
         id: animateRetract;
         target: searchBoxRect;
         properties: "width"
-        duration: VLCStyle.ms125
+        duration: VLCStyle.duration_faster
         to: 0
         easing.type: Easing.OutSine
     }
@@ -92,15 +90,12 @@ FocusScope {
             focus: true
 
             onClicked: {
-                if (searchBox.text == "")
-                    expanded = !expanded
-                else {
-                    searchBox.clear()
-                    expanded = !expanded
-                }
+                searchBox.clear()
+                _expanded = !_expanded
             }
 
             Navigation.parentItem: root
+            Navigation.rightItem: _expanded ? searchBox : nullptr
             Keys.priority: Keys.AfterItem
             Keys.onPressed: Navigation.defaultKeyAction(event)
         }
@@ -126,7 +121,7 @@ FocusScope {
             TextField {
                 id: searchBox
 
-                enabled: root.expanded
+                enabled: root._expanded
 
                 anchors.fill: searchBoxRect
                 anchors.rightMargin: clearButton.visible ? (VLCStyle.margin_xxsmall + clearButton.width) : 0
@@ -139,6 +134,8 @@ FocusScope {
 
                 selectByMouse: true
 
+                placeholderText: i18n.qtr("filter")
+
                 background: Rectangle { color: "transparent" }
 
                 Navigation.parentItem: root
@@ -164,8 +161,7 @@ FocusScope {
                         || event.matches(StandardKey.Back)
                         || event.matches(StandardKey.Cancel))
                     {
-                        text = ""
-                        expanded = false
+                        _expanded = false
                         event.accepted = true
                     }
                     Navigation.defaultKeyReleaseAction(event)
@@ -182,7 +178,7 @@ FocusScope {
                 size: VLCStyle.icon_small
                 iconText: VLCIcons.close
 
-                visible: ( expanded && searchBox.text.length > 0 )
+                visible: ( _expanded && searchBox.text.length > 0 )
                 enabled: visible
 
                 onClicked: {
-- 
GitLab