[vlc-devel] [PATCH 09/22] qml: restore the focus on the last album when navigating back to the album view

Pierre Lamot pierre at videolabs.io
Fri Feb 14 11:23:44 CET 2020


---
 .../gui/qt/medialibrary/qml/MusicAlbums.qml   | 48 ++++++++++++-------
 .../medialibrary/qml/MusicAlbumsDisplay.qml   |  4 ++
 2 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/modules/gui/qt/medialibrary/qml/MusicAlbums.qml b/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
index 9f45d6bb75..4375e406ab 100644
--- a/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
@@ -38,19 +38,40 @@ Widgets.NavigableFocusScope {
 
     property alias model: delegateModel.model
     property alias parentId: delegateModel.parentId
-    property var currentIndex: view.currentItem.currentIndex
+    readonly property var currentIndex: view.currentItem.currentIndex
+    //the index to "go to" when the view is loaded
+    property var initialIndex: 0
 
 
     navigationCancel: function() {
-        if (view.currentItem.currentIndex <= 0)
+        if (view.currentItem.currentIndex <= 0) {
             defaultNavigationCancel()
-        else
+        } else {
             view.currentItem.currentIndex = 0;
+            view.currentItem.positionViewAtIndex(0, ItemView.Contain)
+        }
     }
 
     property Component header: Item{}
     readonly property var headerItem: view.currentItem ? view.currentItem.headerItem : undefined
 
+    onInitialIndexChanged:  resetFocus()
+    onModelChanged: resetFocus()
+    onParentIdChanged: resetFocus()
+
+    function resetFocus() {
+        if (delegateModel.items.count === 0) {
+            return
+        }
+        var initialIndex = root.initialIndex
+        if (initialIndex >= delegateModel.items.count)
+            initialIndex = 0
+        delegateModel.selectNone()
+        delegateModel.items.get(initialIndex).inSelected = true
+        view.currentItem.currentIndex = initialIndex
+        view.currentItem.positionViewAtIndex(initialIndex, ItemView.Contain)
+    }
+
     Util.SelectableDelegateModel {
         id: delegateModel
         property alias parentId: albumModelId.parentId
@@ -88,6 +109,12 @@ Widgets.NavigableFocusScope {
             }
         }
 
+        onCountChanged: {
+            if (delegateModel.items.count > 0 && delegateModel.selectedGroup.count === 0) {
+                root.resetFocus()
+            }
+        }
+
         function actionAtIndex(index) {
             if (delegateModel.selectedGroup.count > 1) {
                 var list = []
@@ -100,21 +127,6 @@ Widgets.NavigableFocusScope {
         }
     }
 
-    /*
-     *define the intial position/selection
-     * This is done on activeFocus rather than Component.onCompleted because delegateModel.
-     * selectedGroup update itself after this event
-     */
-    onActiveFocusChanged: {
-        if (activeFocus && delegateModel.items.count > 0 && delegateModel.selectedGroup.count === 0) {
-            var initialIndex = 0
-            if (view.currentItem.currentIndex !== -1)
-                initialIndex = view.currentItem.currentIndex
-            delegateModel.items.get(initialIndex).inSelected = true
-            view.currentItem.currentIndex = initialIndex
-        }
-    }
-
     Component {
         id: gridComponent
 
diff --git a/modules/gui/qt/medialibrary/qml/MusicAlbumsDisplay.qml b/modules/gui/qt/medialibrary/qml/MusicAlbumsDisplay.qml
index f4653ca079..c126566bc3 100644
--- a/modules/gui/qt/medialibrary/qml/MusicAlbumsDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicAlbumsDisplay.qml
@@ -30,4 +30,8 @@ MusicAlbums {
         text: i18n.qtr("Albums")
         width: root.width
     }
+
+    onCurrentIndexChanged: {
+        history.update(["mc","music", "albums", {"initialIndex": currentIndex}])
+    }
 }
-- 
2.17.1



More information about the vlc-devel mailing list