feat(StatusScrollView): add ensureVisible function to Utils (#868)

Replace local ensureVisible with ensureVisible function from Utils

Required for https://github.com/status-im/status-desktop/issues/7093
This commit is contained in:
PavelS 2022-08-31 11:19:20 +03:00 committed by Michał Cieślak
parent e754af13d3
commit 0717f155e8
4 changed files with 26 additions and 25 deletions

View File

@ -7,6 +7,7 @@ import StatusQ.Components 0.1
import StatusQ.Controls 0.1
import StatusQ.Core 0.1
import StatusQ.Core.Theme 0.1
import StatusQ.Core.Utils 0.1
/*!
\qmltype StatusBaseInput
@ -339,17 +340,6 @@ Item {
}
clip: true
function ensureVisible(r) {
if (contentX >= r.x)
contentX = r.x
else if (contentX + width <= r.x + r.width)
contentX = r.x + r.width - width
if (contentY >= r.y)
contentY = r.y
else if (contentY + height <= r.y + r.height)
contentY = r.y + r.height - height
}
TextEdit {
id: edit
property string previousText: text
@ -374,7 +364,7 @@ Item {
edit.keyEvent = event.key
root.keyPressed(event);
}
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
onCursorRectangleChanged: Utils.ensureVisible(flick, cursorRectangle)
onActiveFocusChanged: if (root.pristine) root.pristine = false
onTextChanged: {
if (previousText === text) {

View File

@ -57,18 +57,7 @@ Item {
contentHeight: edit.paintedHeight
boundsBehavior: Flickable.StopAtBounds
QC.ScrollBar.vertical: QC.ScrollBar { interactive: multiline; enabled: multiline }
function ensureVisible(r) {
if (width-contentX >= r.x) {
contentX = 0;
}
else if (contentX+width <= r.x+r.width) {
contentX = r.x+r.width-width;
}
if (contentY >= r.y)
contentY = r.y;
else if (contentY+height <= r.y+r.height)
contentY = r.y+r.height-height;
}
TextEdit {
id: edit
width: flick.width
@ -83,7 +72,7 @@ Item {
font.family: Theme.palette.baseFont.name
color: Theme.palette.directColor1
textFormat: Text.RichText
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
onCursorRectangleChanged: Utils.ensureVisible(flick, cursorRectangle)
wrapMode: statusSelectableText.multiline ? Text.WrapAtWordBoundaryOrAnywhere : TextEdit.NoWrap
Keys.forwardTo: [statusSelectableText]

View File

@ -2,6 +2,7 @@ import QtQuick 2.14
import QtQuick.Controls 2.14
import StatusQ.Controls 0.1
import StatusQ.Core.Utils 0.1
/*!
\qmltype StatusScrollView
@ -63,4 +64,8 @@ Flickable {
policy: ScrollBar.AsNeeded
visible: resolveVisibility(policy, root.height, root.contentHeight)
}
function ensureVisible(rect) {
Utils.ensureVisible(this, rect)
}
}

View File

@ -244,6 +244,23 @@ QtObject {
function elideText(text, leftCharsCount, rightCharsCount = leftCharsCount) {
return text.substr(0, leftCharsCount) + "..." + text.substr(text.length - rightCharsCount)
}
function ensureVisible(flickable, rect) {
const rectRight = rect.x + rect.width
const rectBottom = rect.y + rect.height
const flickableRight = flickable.contentX + flickable.width
const flickableBottom = flickable.contentY + flickable.height
if (flickable.contentX >= rect.x)
flickable.contentX = rect.x
else if (flickableRight <= rectRight)
flickable.contentX = rectRight - flickable.width
if (flickable.contentY >= rect.y)
flickable.contentY = rect.y
else if (flickableBottom <= rectBottom)
flickable.contentY = rectBottom - flickable.height
}
}