Verified Commit 48ffa11c authored by ivan tkachenko's avatar ivan tkachenko
Browse files

applets/batterymonitor: Fix scroll positioning when navigating through the list

This is a rip-off of ListView::positionViewAtIndex method, except it
should've been provided for any Flickable and arbitrary items/bounds.
parent 6b51785b
Pipeline #232089 passed with stage
in 6 minutes and 12 seconds
......@@ -70,11 +70,16 @@ PlasmaExtras.Representation {
// HACK: workaround for https://bugreports.qt.io/browse/QTBUG-83890
PlasmaComponents3.ScrollBar.horizontal.policy: PlasmaComponents3.ScrollBar.AlwaysOff
function scrollToY(yPos) {
function positionViewAtItem(item) {
if (!PlasmaComponents3.ScrollBar.vertical.visible) {
return;
}
PlasmaComponents3.ScrollBar.vertical.position = Math.min(1, Math.max(0, yPos / batteryList.implicitHeight));
const rect = batteryList.mapFromItem(item, 0, 0, item.width, item.height);
if (rect.y < scrollView.contentItem.contentY) {
scrollView.contentItem.contentY = rect.y;
} else if (rect.y + rect.height > scrollView.contentItem.contentY + scrollView.height) {
scrollView.contentItem.contentY = rect.y + rect.height - scrollView.height;
}
}
Column {
......@@ -121,7 +126,7 @@ PlasmaExtras.Representation {
stepSize: batterymonitor.maximumScreenBrightness/100
onMoved: batterymonitor.screenBrightness = value
onActiveFocusChanged: if (activeFocus) scrollView.scrollToY(y)
onActiveFocusChanged: if (activeFocus) scrollView.positionViewAtItem(this)
// Manually dragging the slider around breaks the binding
Connections {
......@@ -150,7 +155,7 @@ PlasmaExtras.Representation {
KeyNavigation.tab: KeyNavigation.down
onMoved: batterymonitor.keyboardBrightness = value
onActiveFocusChanged: if (activeFocus) scrollView.scrollToY(y)
onActiveFocusChanged: if (activeFocus) scrollView.positionViewAtItem(this)
// Manually dragging the slider around breaks the binding
Connections {
......@@ -178,7 +183,7 @@ PlasmaExtras.Representation {
profileHolds: dialog.profileHolds
onActivateProfileRequested: dialog.activateProfileRequested(profile)
onActiveFocusChanged: if (activeFocus) scrollView.scrollToY(y)
onActiveFocusChanged: if (activeFocus) scrollView.positionViewAtItem(this)
}
Repeater {
......@@ -205,7 +210,7 @@ PlasmaExtras.Representation {
}
}
onActiveFocusChanged: if (activeFocus) scrollView.scrollToY(y)
onActiveFocusChanged: if (activeFocus) scrollView.positionViewAtItem(this)
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment