Author: rwhitcomb
Date: Thu Feb  8 23:15:57 2018
New Revision: 1823608

URL: http://svn.apache.org/viewvc?rev=1823608&view=rev
Log:
PIVOT-891:  Fix the left/right word selection logic in TerraTextInputSkin.
Instead of the word-shift logic just setting the new selection, we need to
adjust the selection, so that if a new left-word selection reverses a previous
right selection we might need to just shorten the selection, depending. And
correspondingly for right-word selection.

Modified:
    
pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java

Modified: 
pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java?rev=1823608&r1=1823607&r2=1823608&view=diff
==============================================================================
--- 
pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java
 (original)
+++ 
pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java
 Thu Feb  8 23:15:57 2018
@@ -1359,29 +1359,41 @@ public class TerraTextInputSkin extends
             consumed = true;
         } else if (keyCode == Keyboard.KeyCode.LEFT) {
             if (Keyboard.isPressed(wordNavigationModifier)) {
-                selectDirection = null;
-                // Move the caret to the start of the next word to the left
-                if (start > 0) {
+                int wordStart = (selectDirection == SelectDirection.RIGHT) ? 
start + length : start;
+                // Find the start of the next word to the left
+                if (wordStart > 0) {
                     // Skip over any space immediately to the left
-                    int index = start;
-                    while (index > 0 && 
Character.isWhitespace(textInput.getCharacterAt(index - 1))) {
-                        index--;
+                    while (wordStart > 0 && 
Character.isWhitespace(textInput.getCharacterAt(wordStart - 1))) {
+                        wordStart--;
                     }
 
                     // Skip over any word-letters to the left
-                    while (index > 0
-                        && 
!Character.isWhitespace(textInput.getCharacterAt(index - 1))) {
-                        index--;
+                    while (wordStart > 0
+                        && 
!Character.isWhitespace(textInput.getCharacterAt(wordStart - 1))) {
+                        wordStart--;
                     }
 
                     if (isShiftPressed) {
-                        length += start - index;
-                        selectDirection = SelectDirection.LEFT;
+                        if (wordStart >= start) {
+                            // We've just reduced the previous right 
selection, so leave the anchor alone
+                            length = wordStart - start;
+                            wordStart = start;
+                        } else {
+                            if (selectDirection == SelectDirection.RIGHT) {
+                                // We've "crossed over" the start, so reverse 
direction
+                                length = start - wordStart;
+                            } else {
+                                // Just increase the selection in the same 
direction
+                                length += start - wordStart;
+                            }
+                            selectDirection = SelectDirection.LEFT;
+                        }
                     } else {
                         length = 0;
+                        selectDirection = null;
                     }
 
-                    start = index;
+                    start = wordStart;
                 }
             } else if (isShiftPressed) {
                 // If the previous direction was LEFT, then increase the 
selection
@@ -1405,7 +1417,7 @@ public class TerraTextInputSkin extends
                                 if (--length == 0) {
                                     if (start > 0) {
                                         start--;
-                                         length++;
+                                        length++;
                                         selectDirection = SelectDirection.LEFT;
                                     }
                                 }
@@ -1439,29 +1451,41 @@ public class TerraTextInputSkin extends
             }
         } else if (keyCode == Keyboard.KeyCode.RIGHT) {
             if (Keyboard.isPressed(wordNavigationModifier)) {
-                selectDirection = null;
-                // Move the caret to the start of the next word to the right
-                if (start < textInput.getCharacterCount()) {
-                    int index = start + length;
-
+                int wordStart = (selectDirection == SelectDirection.LEFT) ? 
start : start + length;
+                // Find the start of the next word to the right
+                if (wordStart < textInput.getCharacterCount()) {
                     // Skip over any space immediately to the right
-                    while (index < textInput.getCharacterCount()
-                        && 
Character.isWhitespace(textInput.getCharacterAt(index))) {
-                        index++;
+                    while (wordStart < textInput.getCharacterCount()
+                        && 
Character.isWhitespace(textInput.getCharacterAt(wordStart))) {
+                        wordStart++;
                     }
 
                     // Skip over any word-letters to the right
-                    while (index < textInput.getCharacterCount()
-                        && 
!Character.isWhitespace(textInput.getCharacterAt(index))) {
-                        index++;
+                    while (wordStart < textInput.getCharacterCount()
+                        && 
!Character.isWhitespace(textInput.getCharacterAt(wordStart))) {
+                        wordStart++;
                     }
 
                     if (isShiftPressed) {
-                        length = index - start;
-                        selectDirection = SelectDirection.RIGHT;
+                        if (wordStart <= start + length) {
+                            // We've just reduced the previous left selection, 
so leave the anchor alone
+                            length -= wordStart - start;
+                            start = wordStart;
+                        } else {
+                            if (selectDirection == SelectDirection.LEFT) {
+                                // We've "crossed over" the start, so reverse 
direction
+                                start += length;
+                                length = wordStart - start;
+                            } else {
+                                // Just increase the selection in the same 
direction
+                                length = wordStart - start;
+                            }
+                            selectDirection = SelectDirection.RIGHT;
+                        }
                     } else {
-                        start = index;
+                        start = wordStart;
                         length = 0;
+                        selectDirection = null;
                     }
                 }
             } else if (isShiftPressed) {


Reply via email to