Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Unified Diff: ui/events/keycodes/dom/dom_key.h

Issue 1284433002: Revise ui::DomKey to unify character and non-character codes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/events/keycodes/dom/dom_key.h
diff --git a/ui/events/keycodes/dom/dom_key.h b/ui/events/keycodes/dom/dom_key.h
index 25ea09209b522e649fae8c8f5fe6e6a3438974d8..87c1a86b27f3e4fa7cefcfda0ce5ee80727e1f9b 100644
--- a/ui/events/keycodes/dom/dom_key.h
+++ b/ui/events/keycodes/dom/dom_key.h
@@ -5,13 +5,112 @@
#ifndef UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_
#define UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_
+#include <stdint.h>
+
namespace ui {
kpschoedel 2015/08/07 20:42:12 Main change here.
+// Integer representation of UI Events KeyboardEvent.key value.
+//
+// The semantics follow the web string form[1]: the value is either a
+// Unicode character or one of a defined set of additional values[2].
+// In this integer form, a printable character is represented by
+// the Unicode code point directly, and a non-Unicode key is represented
+// by a named constant in the DomKey:: scope. This allows DomKey to be
+// used as a proper superset of a Unicode character type.
+//
+// There is one notable difference from the UI Events string key: for
+// the 'Dead' key, this type provides a whole range of values that also
+// encode the associated combining character. This allows the interpretation
+// of any keystroke to be carried as a single integer value.
Wez 2015/08/13 22:31:48 Why aren't dead-keys encoded using the correspondi
kpschoedel 2015/08/14 01:20:07 The main reason is that they're actually two disti
Wez 2015/08/18 22:34:32 Oh, interesting; I wonder what Chrome does with th
+//
+// DomKey::NONE is a sentinel used to indicate an error or undefined value.
+// It is not the same as Unicode code point 0 (ASCII NUL) or the valid DOM
+// key 'Unidentified'.
+//
+// References:
+// [1] http://www.w3.org/TR/uievents/#widl-KeyboardEvent-key
+// [2] http://www.w3.org/TR/DOM-Level-3-Events-key/
+//
+class DomKey {
+ public:
+ using Base = int32_t;
dtapuska 2015/08/13 14:47:26 Any specific reason we'd use int32_t here? As oppo
kpschoedel 2015/08/13 15:46:06 In isolation I'd use uint32_t, but Blink currently
Wez 2015/08/13 22:31:48 Agreed; the Unicode range only extends to 0x10ffff
+
+ private:
+ // Integer representation of DomKey. This is arranged so that Unicode values
+ // are directly usable as DomKey values, and DomKey values are either Unicode
+ // code points or outside the range of Unicode code points.
+ //
+ // 31 24 16 8 0
+ // | | | | | | | | |
+ // | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
+ // | z | t | v |
+ //
+ // - z is reserved and 0 for any valid DomKey.
+ // - t distinguish the type of DomKey:
+ // 0 for a Unicode code point;
+ // 1 for a dead key;
+ // 2 for any other symbolic DomKey.
+ // - v is a value whose interpretation depends on t:
+ // - for a Unicode value, it is the code point;
+ // - for a dead key, the code point of the associated combining character;
Wez 2015/08/13 22:31:48 See above re using the Unicode combining character
+ // - for others, an arbitrary distinct value.
+ enum { VALUE_BITS = 21, TYPE_BITS = 2 };
+ enum Type : Base {
+ VALUE_MASK = (1L << VALUE_BITS) - 1,
+ TYPE_MASK = ((1L << TYPE_BITS) - 1) << VALUE_BITS,
+ TYPE_UNICODE = (0 << VALUE_BITS),
+ TYPE_DEAD = (1 << VALUE_BITS),
+ TYPE_NON_UNICODE = (2 << VALUE_BITS),
+ VALID_MASK = VALUE_MASK | TYPE_MASK,
+ };
+
+ public:
+ enum InvalidKey : Base { NONE = -1 };
+#define DOM_KEY_MAP_DECLARATION enum Key : Base
Wez 2015/08/13 22:31:48 nit: Introduce this macro block with a comment to
kpschoedel 2015/08/17 20:16:14 Done.
+#define DOM_KEY_UNI(key, id, value) id = (TYPE_UNICODE | (value))
+#define DOM_KEY_MAP_BEGIN FIRST_NON_UNICODE = TYPE_NON_UNICODE,
#define DOM_KEY_MAP(key, id) id
-#define DOM_KEY_MAP_DECLARATION enum class DomKey
+#define DOM_KEY_MAP_END LAST_NON_UNICODE
#include "ui/events/keycodes/dom/dom_key_data.inc"
-#undef DOM_KEY_MAP
#undef DOM_KEY_MAP_DECLARATION
+#undef DOM_KEY_MAP_BEGIN
+#undef DOM_KEY_MAP
+#undef DOM_KEY_MAP_END
+#undef DOM_KEY_UNI
+
+ // Create a DomKey, with the undefined-value sentinal DomKey::NONE.
Wez 2015/08/13 22:31:48 nit: sentinel
kpschoedel 2015/08/17 20:16:14 Ouch. I can spell, but my fingers can't.
+ DomKey() : value_(NONE) {}
+
+ // Create a DomKey from an integer value. This is not |explicit|, so that
+ // Unicode characters and DomKey:: constants can be used directly.
+ DomKey(Base value) : value_(value) {}
dtapuska 2015/08/13 14:47:26 I do find this sneaky; in that character -> DomKey
kpschoedel 2015/08/13 15:46:06 Will add static assertions as necessary for the fi
+
+ // Obtain the integer representation of the DomKey.
+ operator Base() const { return value_; }
+
+ // True if the value is a valid DomKey (which excludes DomKey::NONE and
+ // integers not following the DomKey format).
+ bool IsValid() const { return (value_ & ~(TYPE_MASK | VALUE_MASK)) == 0; }
+
+ // True if the value is a Unicode code point.
+ bool IsUnicode() const { return (value_ & TYPE_MASK) == TYPE_UNICODE; }
+
+ // True if the value is a dead key.
+ bool IsDead() const { return (value_ & TYPE_MASK) == TYPE_DEAD; }
+
+ // Returns the Unicode code point for either a Unicode key or a dead key.
+ // (The return value for other keys is meaningless and not comparable to
+ // a DomKey:: constant.)
+ int32_t GetCodePoint() const { return value_ & VALUE_MASK; }
+
+ // Returns a dead-key DomKey for the given combining character.
+ static DomKey Dead(int32_t combining_character) {
+ return TYPE_DEAD | combining_character;
+ }
+
+ private:
+ Base value_;
+};
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698