[vlc-commits] [Git][videolan/vlc][master] qml: layout in Grid View such that view top row is preserved
    Steve Lhomme (@robUx4) 
    gitlab at videolan.org
       
    Mon Jul 29 07:05:47 UTC 2024
    
    
  
Steve Lhomme pushed to branch master at VideoLAN / VLC
Commits:
4f774de3 by Prince Gupta at 2024-07-29T06:43:31+00:00
qml: layout in Grid View such that view top row is preserved
- - - - -
1 changed file:
- modules/gui/qt/widgets/qml/ExpandGridView.qml
Changes:
=====================================
modules/gui/qt/widgets/qml/ExpandGridView.qml
=====================================
@@ -120,6 +120,10 @@ FocusScope {
     property var _currentRange: [0,0]
     property var _delayedChildrenMap: ({})
 
+    property int _anchoredIdx: -1
+    property real _anchoredIdxFraction: 1
+    property real _anchoredIdxUpdate: 0
+
     // Aliases
 
     property alias contentHeight: flickable.contentHeight
@@ -174,15 +178,14 @@ FocusScope {
             flickable.layout(true)
     }
 
-    onHeightChanged: flickable.layout(false)
-
-    onContentWidthChanged: flickable.layout(true)
-    onContentHeightChanged: flickable.layout(true)
+    onHeightChanged: _anchoredLayout(false)
+    onContentWidthChanged: _anchoredLayout(true)
+    onContentHeightChanged: _anchoredLayout(true)
 
     // NOTE: Update on contentLeftMargin since we depend on this for item placements.
-    onContentLeftMarginChanged: flickable.layout(true)
+    onContentLeftMarginChanged: _anchoredLayout(true)
 
-    onDisplayMarginEndChanged: flickable.layout(false)
+    onDisplayMarginEndChanged: _anchoredLayout(false)
 
     onModelChanged: _onModelCountChanged()
 
@@ -351,6 +354,36 @@ FocusScope {
 
     // Functions
 
+    // layouts such that views indexes are preserved during a resize
+    function _anchoredLayout(forceLayout) {
+        if (_currentRange[0] === _currentRange[1])
+            return
+
+        // fix anchor for some duration, so that index remains
+        // same during whole resize operation
+        const dirty = (Date.now() - _anchoredIdxUpdate) > VLCStyle.duration_veryLong
+
+        if (dirty || (_anchoredIdx < 0) || (_anchoredIdx >= _count)) {
+            _anchoredIdx = _currentRange?.[0] ?? 0
+            const item = _getItem(_anchoredIdx)
+            _anchoredIdxFraction = (item.y - flickable.contentY) / cellHeight
+        }
+
+        // reset timer for each call, this allows preserving anchor for extened duration
+        _anchoredIdxUpdate = Date.now()
+
+        let y = 0 // by default show full view including header
+        if (_anchoredIdx !== 0)
+            y = getItemPos(_anchoredIdx)[1] + (cellHeight * _anchoredIdxFraction)
+
+        const previousContentY = flickable.contentY
+        const maxY = Math.max(0, flickable.contentHeight - height)
+
+        flickable.contentY = Helpers.clamp(y, 0, maxY)
+        if (forceLayout || (previousContentY === flickable.contentY))
+            flickable.layout(forceLayout)
+    }
+
     // NOTE: This function is useful to set the currentItem without losing the visual focus.
     function setCurrentItem(index) {
         if (currentIndex === index)
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/4f774de3500856147388635a72e9ecc867e0d2cc
-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/4f774de3500856147388635a72e9ecc867e0d2cc
You're receiving this email because of your account on code.videolan.org.
VideoLAN code repository instance
    
    
More information about the vlc-commits
mailing list