Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMickael Istria2020-12-10 14:32:47 +0000
committerMickael Istria2020-12-10 15:04:08 +0000
commite0cdeb9f02df25e419adcab3555771a5670c70b3 (patch)
tree733b099dfc4f6c5b2982363c9facf6f5ee5c09e1
parent62873ac24d92f573108ea676aaf2559a7deaec93 (diff)
downloadeclipse.platform.swt-e0cdeb9f02df25e419adcab3555771a5670c70b3.tar.gz
eclipse.platform.swt-e0cdeb9f02df25e419adcab3555771a5670c70b3.tar.xz
eclipse.platform.swt-e0cdeb9f02df25e419adcab3555771a5670c70b3.zip
Bug 569550 - Rendering vert.indent keeps caret position unchanged
When adding some vertical spacing, try to 1. keep the current visible line with caret unmoved, and, 2. if caret not visible, move either line above or below the spacing, picking the least amount of lines moved. Change-Id: I97a48293fd92d219706bb20268c6964e198bae3c Signed-off-by: Mickael Istria <mistria@redhat.com>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java37
-rw-r--r--tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java19
2 files changed, 44 insertions, 12 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
index 1acc2fcd32..231568ac04 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
@@ -9443,21 +9443,34 @@ public void setLineVerticalIndent(int lineIndex, int verticalLineIndent) {
if (lineIndex < 0 || lineIndex >= content.getLineCount()) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
- if (verticalLineIndent == renderer.getLineVerticalIndent(lineIndex)) {
- return;
- }
- int oldBottom = getLinePixel(lineIndex + 1);
- if (oldBottom <= getClientArea().height) {
- verticalScrollOffset = -1;
+ int previousVerticalIndent = renderer.getLineVerticalIndent(lineIndex);
+ if (verticalLineIndent == previousVerticalIndent) {
+ return;
}
+ int initialTopPixel = getTopPixel();
+ int verticalIndentDiff = verticalLineIndent - previousVerticalIndent;
renderer.setLineVerticalIndent(lineIndex, verticalLineIndent);
- hasVerticalIndent = verticalLineIndent != 0 || renderer.hasVerticalIndent();
+ this.hasVerticalIndent = verticalLineIndent != 0 || renderer.hasVerticalIndent();
resetCache(lineIndex, 1);
- int newBottom = getLinePixel(lineIndex + 1);
- redrawLines(lineIndex, 1, oldBottom != newBottom);
- int caretLine = getCaretLine();
- if (lineIndex <= caretLine && caretLine < lineIndex + 1) {
- setCaretLocation();
+ if (lineIndex < getPartialTopIndex()) {
+ setTopPixel(initialTopPixel + verticalIndentDiff);
+ } else if (lineIndex > getPartialBottomIndex()) {
+ // adjust vertical scollbar
+ } else {
+ if (getCaretLine() >= getPartialTopIndex() && getCaretLine() <= getPartialBottomIndex()) { // caret line with caret mustn't move
+ if (getCaretLine() < lineIndex) {
+ redrawLines(lineIndex, getPartialBottomIndex(), true);
+ } else {
+ setTopPixel(initialTopPixel + verticalIndentDiff);
+ }
+ } else { // move as few lines as possible
+ if (lineIndex - getTopIndex() < getBottomIndex() - lineIndex) {
+ setTopPixel(initialTopPixel + verticalIndentDiff);
+ } else {
+ // redraw below
+ redrawLines(lineIndex, getPartialBottomIndex() - lineIndex + 1, true);
+ }
+ }
}
}
diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java
index 5f795fc9d1..302663e825 100644
--- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java
+++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java
@@ -4925,6 +4925,25 @@ public void test_verticalIndent_changeRelativeBounds() {
}
@Test
+public void test_verticalIndent_keepsCurrentCaretAndLinePosition() throws InterruptedException {
+ String _50lines = IntStream.range(1, 50).mapToObj(Integer::toString).collect(Collectors.joining("\n"));
+ text.setText(_50lines);
+ text.setSize(500, 200);
+ int line = 30;
+ int offset = text.getOffsetAtLine(line);
+ text.setSelection(offset);
+ text.showSelection();
+ Point caretLocation = text.getCaret().getLocation();
+ Point offsetLocation = text.getLocationAtOffset(offset);
+ text.setLineVerticalIndent(line, 34);
+ assertEquals(caretLocation, text.getCaret().getLocation());
+ assertEquals(offsetLocation, text.getLocationAtOffset(offset));
+ text.setLineVerticalIndent(line, 0);
+ assertEquals(caretLocation, text.getCaret().getLocation());
+ assertEquals(offsetLocation, text.getLocationAtOffset(offset));
+}
+
+@Test
public void test_notFixedLineHeightDoesntChangeLinePixelIfUnnecessary() {
String _50lines = IntStream.range(1, 50).mapToObj(Integer::toString).collect(Collectors.joining("\n"));
text.setText(_50lines);

Back to the top