Daniel Kurka has submitted this change and it was merged.

Change subject: Move arrow key functions from Tree to KeyCodes
......................................................................


Move arrow key functions from Tree to KeyCodes

remove handling for old Webkit keycodes (version < 525)
as well.

Update a couple of widgets to use the new utility functions

fixes issue 8247

Change-Id: I811520d330c181958dd67cbfbe30c46becebbe43
---
M user/src/com/google/gwt/event/dom/client/KeyCodes.java
M user/src/com/google/gwt/user/cellview/client/CellBrowser.java
M user/src/com/google/gwt/user/cellview/client/CellTree.java
M user/src/com/google/gwt/user/client/ui/MenuBar.java
M user/src/com/google/gwt/user/client/ui/Tree.java
5 files changed, 60 insertions(+), 82 deletions(-)

Approvals:
  Leeroy Jenkins: Verified
  Thomas Broyer: Looks good to me, approved
  Goktug Gokdogan: Looks good to me, but someone else must approve



diff --git a/user/src/com/google/gwt/event/dom/client/KeyCodes.java b/user/src/com/google/gwt/event/dom/client/KeyCodes.java
index 699510e..e6881ef 100644
--- a/user/src/com/google/gwt/event/dom/client/KeyCodes.java
+++ b/user/src/com/google/gwt/event/dom/client/KeyCodes.java
@@ -436,6 +436,52 @@
    */
   public static final int KEY_WIN_IME = 229;

+  /**
+   * Determines if a key code is an arrow key.
+   */
+  public static boolean isArrowKey(int code) {
+    switch (code) {
+      case KEY_DOWN:
+      case KEY_RIGHT:
+      case KEY_UP:
+      case KEY_LEFT:
+        return true;
+      default:
+        return false;
+    }
+  }
+
+  /**
+   * Update arrow keys for left and right based on current locale.
+   *
+   * <p>Note: this method is used internally by built-in GWT widgets but
+   *   could be renamed/refactored without notice.
+   * <p>This method simplifies RTL handling in your code:
+   * <code><pre>
+   * int keyCode = KeyCodes.maybeSwapArrowKeysForRtl(event.getKeyCode(),
+   *   LocaleInfo.getCurrentLocale().isRTL());
+   * switch (keyCode) {
+   *   case KeyCodes.KEY_LEFT:
+   *     ... // start of the line, no special RTL handling
+   *     break;
+   *   case KeyCodes.KEY_RIGHT:
+   *     ... // end of the line, no special RTL handling
+   *     break;
+   *   ...
+   *   }
+   * </pre></code>
+   */
+  public static int maybeSwapArrowKeysForRtl(int code, boolean isRtl) {
+    if (isRtl) {
+      if (code == KEY_RIGHT) {
+        code = KEY_LEFT;
+      } else if (code == KEY_LEFT) {
+        code = KEY_RIGHT;
+      }
+    }
+    return code;
+  }
+
   // This class should never be instantiated
   private KeyCodes() {
   }
diff --git a/user/src/com/google/gwt/user/cellview/client/CellBrowser.java b/user/src/com/google/gwt/user/cellview/client/CellBrowser.java
index 34e5c7b..4d1c7d3 100644
--- a/user/src/com/google/gwt/user/cellview/client/CellBrowser.java
+++ b/user/src/com/google/gwt/user/cellview/client/CellBrowser.java
@@ -270,20 +270,14 @@
       String eventType = event.getType();
if (BrowserEvents.KEYDOWN.equals(eventType) && !isKeyboardNavigationSuppressed()) {
         int keyCode = event.getKeyCode();
+        boolean isRtl = LocaleInfo.getCurrentLocale().isRTL();
+        keyCode = KeyCodes.maybeSwapArrowKeysForRtl(keyCode, isRtl);
         switch (keyCode) {
           case KeyCodes.KEY_LEFT:
-            if (LocaleInfo.getCurrentLocale().isRTL()) {
-              keyboardNavigateDeep();
-            } else {
-              keyboardNavigateShallow();
-            }
+            keyboardNavigateShallow();
             return;
           case KeyCodes.KEY_RIGHT:
-            if (LocaleInfo.getCurrentLocale().isRTL()) {
-              keyboardNavigateShallow();
-            } else {
-              keyboardNavigateDeep();
-            }
+            keyboardNavigateDeep();
             return;
         }
       }
diff --git a/user/src/com/google/gwt/user/cellview/client/CellTree.java b/user/src/com/google/gwt/user/cellview/client/CellTree.java
index 4847ee6..69c871a 100644
--- a/user/src/com/google/gwt/user/cellview/client/CellTree.java
+++ b/user/src/com/google/gwt/user/cellview/client/CellTree.java
@@ -1025,6 +1025,8 @@
     int parentChildCount = (parent == null) ? 0 : parent.getChildCount();
     int index = keyboardSelectedNode.getIndex();
     int childCount = keyboardSelectedNode.getChildCount();
+    boolean isRtl = LocaleInfo.getCurrentLocale().isRTL();
+    keyCode = KeyCodes.maybeSwapArrowKeysForRtl(keyCode, isRtl);

     switch (keyCode) {
       case KeyCodes.KEY_DOWN:
@@ -1069,18 +1071,10 @@
         }
         break;
       case KeyCodes.KEY_RIGHT:
-        if (LocaleInfo.getCurrentLocale().isRTL()) {
-          keyboardNavigateShallow();
-        } else {
-          keyboardNavigateDeep();
-        }
+        keyboardNavigateDeep();
         break;
       case KeyCodes.KEY_LEFT:
-        if (LocaleInfo.getCurrentLocale().isRTL()) {
-          keyboardNavigateDeep();
-        } else {
-          keyboardNavigateShallow();
-        }
+        keyboardNavigateShallow();
         break;
     }
   }
diff --git a/user/src/com/google/gwt/user/client/ui/MenuBar.java b/user/src/com/google/gwt/user/client/ui/MenuBar.java
index 0cb43d4..a66ab8c 100644
--- a/user/src/com/google/gwt/user/client/ui/MenuBar.java
+++ b/user/src/com/google/gwt/user/client/ui/MenuBar.java
@@ -626,21 +626,15 @@

       case Event.ONKEYDOWN: {
         int keyCode = DOM.eventGetKeyCode(event);
+        boolean isRtl = LocaleInfo.getCurrentLocale().isRTL();
+        keyCode = KeyCodes.maybeSwapArrowKeysForRtl(keyCode, isRtl);
         switch (keyCode) {
           case KeyCodes.KEY_LEFT:
-            if (LocaleInfo.getCurrentLocale().isRTL()) {
-              moveToNextItem();
-            } else {
-              moveToPrevItem();
-            }
+            moveToPrevItem();
             eatEvent(event);
             break;
           case KeyCodes.KEY_RIGHT:
-            if (LocaleInfo.getCurrentLocale().isRTL()) {
-              moveToPrevItem();
-            } else {
-              moveToNextItem();
-            }
+            moveToNextItem();
             eatEvent(event);
             break;
           case KeyCodes.KEY_UP:
diff --git a/user/src/com/google/gwt/user/client/ui/Tree.java b/user/src/com/google/gwt/user/client/ui/Tree.java
index 5d062a5..20c37bd 100644
--- a/user/src/com/google/gwt/user/client/ui/Tree.java
+++ b/user/src/com/google/gwt/user/client/ui/Tree.java
@@ -164,11 +164,6 @@
     }
   }

-  private static final int OTHER_KEY_DOWN = 63233;
-  private static final int OTHER_KEY_LEFT = 63234;
-  private static final int OTHER_KEY_RIGHT = 63235;
-  private static final int OTHER_KEY_UP = 63232;
-
   static native boolean shouldTreeDelegateFocusToElement(Element elem) /*-{
     var name = elem.nodeName;
     return ((name == "SELECT") ||
@@ -178,51 +173,6 @@
         (name == "BUTTON") ||
         (name == "LABEL"));
   }-*/;
-
-  private static boolean isArrowKey(int code) {
-    switch (code) {
-      case OTHER_KEY_DOWN:
-      case OTHER_KEY_RIGHT:
-      case OTHER_KEY_UP:
-      case OTHER_KEY_LEFT:
-      case KeyCodes.KEY_DOWN:
-      case KeyCodes.KEY_RIGHT:
-      case KeyCodes.KEY_UP:
-      case KeyCodes.KEY_LEFT:
-        return true;
-      default:
-        return false;
-    }
-  }
-
-  /**
-   * Normalized key codes. Also switches KEY_RIGHT and KEY_LEFT in RTL
-   * languages.
-   */
-  private static int standardizeKeycode(int code) {
-    switch (code) {
-      case OTHER_KEY_DOWN:
-        code = KeyCodes.KEY_DOWN;
-        break;
-      case OTHER_KEY_RIGHT:
-        code = KeyCodes.KEY_RIGHT;
-        break;
-      case OTHER_KEY_UP:
-        code = KeyCodes.KEY_UP;
-        break;
-      case OTHER_KEY_LEFT:
-        code = KeyCodes.KEY_LEFT;
-        break;
-    }
-    if (LocaleInfo.getCurrentLocale().isRTL()) {
-      if (code == KeyCodes.KEY_RIGHT) {
-        code = KeyCodes.KEY_LEFT;
-      } else if (code == KeyCodes.KEY_LEFT) {
-        code = KeyCodes.KEY_RIGHT;
-      }
-    }
-    return code;
-  }

   /**
    * Map of TreeItem.widget -> TreeItem.
@@ -734,7 +684,7 @@
     switch (eventType) {
       case Event.ONKEYDOWN:
       case Event.ONKEYUP: {
-        if (isArrowKey(DOM.eventGetKeyCode(event))) {
+        if (KeyCodes.isArrowKey(DOM.eventGetKeyCode(event))) {
           DOM.eventCancelBubble(event, true);
           DOM.eventPreventDefault(event);
           return;
@@ -1178,7 +1128,7 @@
     if (isKeyboardNavigationEnabled(curSelection)) {
       int code = DOM.eventGetKeyCode(event);

-      switch (standardizeKeycode(code)) {
+ switch (KeyCodes.maybeSwapArrowKeysForRtl(code, LocaleInfo.getCurrentLocale().isRTL())) {
         case KeyCodes.KEY_UP: {
           moveSelectionUp(curSelection);
           break;

--
To view, visit https://gwt-review.googlesource.com/3662
To unsubscribe, visit https://gwt-review.googlesource.com/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I811520d330c181958dd67cbfbe30c46becebbe43
Gerrit-PatchSet: 8
Gerrit-Project: gwt
Gerrit-Branch: master
Gerrit-Owner: Daniel Kurka <[email protected]>
Gerrit-Reviewer: Daniel Kurka <[email protected]>
Gerrit-Reviewer: Goktug Gokdogan <[email protected]>
Gerrit-Reviewer: Leeroy Jenkins <[email protected]>
Gerrit-Reviewer: Matthew Dempsky <[email protected]>
Gerrit-Reviewer: Thomas Broyer <[email protected]>

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
--- You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to