[haiku-commits] Change in haiku[master]: BTextView: Fix scrolling in text view

  • From: Gerrit <review@xxxxxxxxxxxxxxxxxxx>
  • To: waddlesplash <waddlesplash@xxxxxxxxx>, haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 17 Jan 2021 20:34:15 +0000

From John Scipione <jscipione@xxxxxxxxx>:

John Scipione has uploaded this change for review. ( 
https://review.haiku-os.org/c/haiku/+/3642 ;)


Change subject: BTextView: Fix scrolling in text view
......................................................................

BTextView: Fix scrolling in text view

Prevent scrolling out of bounds horizontally. Algorithm to keep
cursor in text view bounds was copied from _PerformAutoScrolling()
(mouse scrolling method) and was fixed to account for the left
margin which is not always 0.

Fix left and top margin scrolling not always 0 in
_PerformAutoScrolling() as well, this fixes a similar bug that
was present when scrolling the text with the mouse instead.

Remove extraSpace variable, no longer needed.

Replace xDiff and yDiff float variables with a single scrollTo BPoint
in ScrollToOffset(). This was also copied from _PerformAutoScrolling().

Remove a floorf() call, we know that we are dealing with integers.

Fixes #16642, #16476
---
M src/kits/interface/TextView.cpp
1 file changed, 34 insertions(+), 20 deletions(-)



  git pull ssh://git.haiku-os.org:22/haiku refs/changes/42/3642/1

diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp
index babfc7a..08e7aa1 100644
--- a/src/kits/interface/TextView.cpp
+++ b/src/kits/interface/TextView.cpp
@@ -2164,30 +2164,42 @@
 {
        BRect bounds = Bounds();
        float lineHeight = 0.0;
-       float xDiff = 0.0;
-       float yDiff = 0.0;
        BPoint point = PointAt(offset, &lineHeight);
+       BPoint scrollBy(B_ORIGIN);

        // horizontal
-       float extraSpace = ceilf(bounds.IntegerWidth() / 2);
        if (point.x < bounds.left)
-               xDiff = point.x - bounds.right + extraSpace;
+               scrollBy.x = point.x - bounds.right;
        else if (point.x > bounds.right)
-               xDiff = point.x - bounds.left - extraSpace;
+               scrollBy.x = point.x - bounds.left;

-       // vertical
-       if (point.y < bounds.top)
-               yDiff = point.y - bounds.top;
-       else if (point.y + lineHeight > bounds.bottom
-               && point.y - lineHeight > bounds.top) {
-               yDiff = point.y + lineHeight - bounds.bottom;
+       // prevent from scrolling out of view
+       if (scrollBy.x != 0.0) {
+               float rightMax = fTextRect.right + fLayoutData->rightInset;
+               if (bounds.right + scrollBy.x > rightMax)
+                       scrollBy.x = rightMax - bounds.right;
+               float leftMin = fTextRect.left - fLayoutData->leftInset;
+               if (bounds.left + scrollBy.x < leftMin)
+                       scrollBy.x = leftMin - bounds.left;
        }

-       // prevent negative scroll offset in y
-       if (bounds.top + yDiff < 0.0)
-               yDiff = -bounds.top;
+       if (CountLines() > 1) {
+               // scroll in Y only if multiple lines!

-       ScrollBy(xDiff, yDiff);
+               // vertical
+               if (point.y < bounds.top)
+                       scrollBy.y = point.y - bounds.top;
+               else if (point.y + lineHeight > bounds.bottom
+                       && point.y - lineHeight > bounds.top) {
+                       scrollBy.y = point.y + lineHeight - bounds.bottom;
+               }
+
+               // prevent negative scroll offset in y
+               if (bounds.top + scrollBy.y < 0.0)
+                       scrollBy.y = -bounds.top;
+       }
+
+       ScrollBy(scrollBy.x, scrollBy.y);
 }


@@ -4937,11 +4949,12 @@

        // prevent from scrolling out of view
        if (scrollBy.x != 0.0) {
-               float rightMax = floorf(fTextRect.right + 
fLayoutData->rightInset);
+               float rightMax = fTextRect.right + fLayoutData->rightInset;
                if (bounds.right + scrollBy.x > rightMax)
                        scrollBy.x = rightMax - bounds.right;
-               if (bounds.left + scrollBy.x < 0)
-                       scrollBy.x = -bounds.left;
+               float leftMin = fTextRect.left - fLayoutData->leftInset;
+               if (bounds.left + scrollBy.x < leftMin)
+                       scrollBy.x = leftMin - bounds.left;
        }

        if (CountLines() > 1) {
@@ -4957,8 +4970,9 @@
                                + fLayoutData->bottomInset);
                        if (bounds.bottom + scrollBy.y > bottomMax)
                                scrollBy.y = bottomMax - bounds.bottom;
-                       if (bounds.top + scrollBy.y < 0)
-                               scrollBy.y = -bounds.top;
+                       float topMin = fTextRect.top - fLayoutData->topInset;
+                       if (bounds.top + scrollBy.y < topMin)
+                               scrollBy.y = topMin - bounds.top;
                }
        }


--
To view, visit https://review.haiku-os.org/c/haiku/+/3642
To unsubscribe, or for help writing mail filters, visit 
https://review.haiku-os.org/settings

Gerrit-Project: haiku
Gerrit-Branch: master
Gerrit-Change-Id: I2d32d6039944d2dc3218ce4de71f2966cc98c866
Gerrit-Change-Number: 3642
Gerrit-PatchSet: 1
Gerrit-Owner: John Scipione <jscipione@xxxxxxxxx>
Gerrit-MessageType: newchange

Other related posts:

  • » [haiku-commits] Change in haiku[master]: BTextView: Fix scrolling in text view - Gerrit