Keep image selector preview centered

This commit is contained in:
Ryan Hughes
2026-05-11 16:03:31 -04:00
parent 3aaedd88b1
commit 314a7f75c0
+20 -39
View File
@@ -60,8 +60,6 @@ ShellRoot {
if (index === selectedIndex && immediate !== true) return if (index === selectedIndex && immediate !== true) return
selectedIndex = index selectedIndex = index
list.currentIndex = index
list.centerSelected(immediate === true)
} }
function applySelected() { function applySelected() {
@@ -90,7 +88,7 @@ ShellRoot {
root.select(root.selectedImageIndex(), true) root.select(root.selectedImageIndex(), true)
root.imagesLoaded = true root.imagesLoaded = true
root.opened = true root.opened = true
list.forceActiveFocus() carousel.forceActiveFocus()
} }
function openSelector(nextImageDirs, nextImageRows, nextSelectedImage, nextSelectionFile, nextDoneFile, nextColorsFile, nextColorsRaw, nextShowLabels) { function openSelector(nextImageDirs, nextImageRows, nextSelectedImage, nextSelectionFile, nextDoneFile, nextColorsFile, nextColorsRaw, nextShowLabels) {
@@ -105,7 +103,6 @@ ShellRoot {
loadColors(nextColorsRaw) loadColors(nextColorsRaw)
imageModel.clear() imageModel.clear()
selectedIndex = 0 selectedIndex = 0
list.currentIndex = 0
imagesLoaded = false imagesLoaded = false
opened = false opened = false
if (imageRows) { if (imageRows) {
@@ -116,13 +113,7 @@ ShellRoot {
} }
} }
ListModel { ListModel { id: imageModel }
id: imageModel
onCountChanged: {
if (count > 0 && list.currentIndex < 0)
root.select(0)
}
}
function addImage(path, thumbnailPath) { function addImage(path, thumbnailPath) {
if (!path) return if (!path) return
@@ -242,37 +233,19 @@ ShellRoot {
MouseArea { anchors.fill: parent; onClicked: {} } MouseArea { anchors.fill: parent; onClicked: {} }
ListView { Item {
id: list id: carousel
anchors.top: parent.top anchors.top: parent.top
anchors.topMargin: 30 anchors.topMargin: 30
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
anchors.bottomMargin: root.showLabels ? 74 : 30 anchors.bottomMargin: root.showLabels ? 74 : 30
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
width: root.expandedWidth + 13 * (root.sliceWidth + root.sliceSpacing) width: root.expandedWidth + 13 * (root.sliceWidth + root.sliceSpacing)
orientation: ListView.Horizontal
model: imageModel
spacing: root.sliceSpacing
clip: false clip: false
focus: true focus: true
currentIndex: 0
preferredHighlightBegin: (width - root.expandedWidth) / 2
preferredHighlightEnd: (width + root.expandedWidth) / 2
highlightRangeMode: ListView.NoHighlightRange
highlightMoveDuration: 120
highlight: Item {}
header: Item { width: (list.width - root.expandedWidth) / 2; height: 1 }
footer: Item { width: (list.width - root.expandedWidth) / 2; height: 1 }
function centerSelected(immediate) { readonly property real itemStep: root.sliceWidth + root.sliceSpacing
var center = function() { readonly property real previewX: (width - root.expandedWidth) / 2
list.contentX = Math.max(0, root.selectedIndex * (root.sliceWidth + root.sliceSpacing))
}
center()
if (immediate)
Qt.callLater(center)
}
Keys.priority: Keys.BeforeItem Keys.priority: Keys.BeforeItem
Keys.onPressed: function(event) { Keys.onPressed: function(event) {
@@ -293,6 +266,9 @@ ShellRoot {
Component.onCompleted: forceActiveFocus() Component.onCompleted: forceActiveFocus()
Repeater {
model: imageModel
delegate: Item { delegate: Item {
id: item id: item
required property int index required property int index
@@ -300,11 +276,15 @@ ShellRoot {
required property string fileName required property string fileName
required property string thumbnailPath required property string thumbnailPath
readonly property bool selected: index === root.selectedIndex readonly property int relativeIndex: index - root.selectedIndex
readonly property bool selected: relativeIndex === 0
readonly property bool nearby: Math.abs(relativeIndex) <= 16
visible: nearby
x: selected ? carousel.previewX : (relativeIndex < 0 ? carousel.previewX + relativeIndex * carousel.itemStep : carousel.previewX + root.expandedWidth + root.sliceSpacing + (relativeIndex - 1) * carousel.itemStep)
width: selected ? root.expandedWidth : root.sliceWidth width: selected ? root.expandedWidth : root.sliceWidth
height: list.height height: carousel.height
z: selected ? 100 : 50 - Math.min(Math.abs(index - root.selectedIndex), 40) z: selected ? 100 : 50 - Math.min(Math.abs(relativeIndex), 40)
readonly property real skAbs: Math.abs(root.skewOffset) readonly property real skAbs: Math.abs(root.skewOffset)
readonly property real topLeft: root.skewOffset >= 0 ? skAbs : 0 readonly property real topLeft: root.skewOffset >= 0 ? skAbs : 0
@@ -367,7 +347,7 @@ ShellRoot {
Image { Image {
id: image id: image
anchors.fill: parent anchors.fill: parent
source: root.fileUrl(item.thumbnailPath) source: item.nearby ? root.fileUrl(item.thumbnailPath) : ""
fillMode: Image.PreserveAspectCrop fillMode: Image.PreserveAspectCrop
asynchronous: false asynchronous: false
cache: true cache: true
@@ -404,12 +384,13 @@ ShellRoot {
} }
} }
} }
}
Text { Text {
visible: root.showLabels visible: root.showLabels
anchors.top: list.bottom anchors.top: carousel.bottom
anchors.topMargin: 16 anchors.topMargin: 16
anchors.horizontalCenter: list.horizontalCenter anchors.horizontalCenter: carousel.horizontalCenter
width: root.expandedWidth width: root.expandedWidth
text: root.currentLabel() text: root.currentLabel()
color: root.foreground color: root.foreground