Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
feat(StatusScrollView): add ensureVisible function to Utils (#868)
Browse files Browse the repository at this point in the history
Replace local ensureVisible with ensureVisible function from Utils

Required for status-im/status-desktop#7093
  • Loading branch information
faust4exe committed Aug 31, 2022
1 parent 8646daa commit 4d73c25
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
14 changes: 2 additions & 12 deletions src/StatusQ/Controls/StatusBaseInput.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
15 changes: 2 additions & 13 deletions src/StatusQ/Controls/StatusSelectableText.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions src/StatusQ/Core/StatusScrollView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,4 +64,8 @@ Flickable {
policy: ScrollBar.AsNeeded
visible: resolveVisibility(policy, root.height, root.contentHeight)
}

function ensureVisible(rect) {
Utils.ensureVisible(this, rect)
}
}
17 changes: 17 additions & 0 deletions src/StatusQ/Core/Utils/Utils.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}


0 comments on commit 4d73c25

Please sign in to comment.