Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_ | 5 #ifndef UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_ |
| 6 #define UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_ | 6 #define UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | |
| 9 | |
| 8 namespace ui { | 10 namespace ui { |
| 9 | 11 |
|
kpschoedel
2015/08/07 20:42:12
Main change here.
| |
| 12 // Integer representation of UI Events KeyboardEvent.key value. | |
| 13 // | |
| 14 // The semantics follow the web string form[1]: the value is either a | |
| 15 // Unicode character or one of a defined set of additional values[2]. | |
| 16 // In this integer form, a printable character is represented by | |
| 17 // the Unicode code point directly, and a non-Unicode key is represented | |
| 18 // by a named constant in the DomKey:: scope. This allows DomKey to be | |
| 19 // used as a proper superset of a Unicode character type. | |
| 20 // | |
| 21 // There is one notable difference from the UI Events string key: for | |
| 22 // the 'Dead' key, this type provides a whole range of values that also | |
| 23 // encode the associated combining character. This allows the interpretation | |
| 24 // 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
| |
| 25 // | |
| 26 // DomKey::NONE is a sentinel used to indicate an error or undefined value. | |
| 27 // It is not the same as Unicode code point 0 (ASCII NUL) or the valid DOM | |
| 28 // key 'Unidentified'. | |
| 29 // | |
| 30 // References: | |
| 31 // [1] http://www.w3.org/TR/uievents/#widl-KeyboardEvent-key | |
| 32 // [2] http://www.w3.org/TR/DOM-Level-3-Events-key/ | |
| 33 // | |
| 34 class DomKey { | |
| 35 public: | |
| 36 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
| |
| 37 | |
| 38 private: | |
| 39 // Integer representation of DomKey. This is arranged so that Unicode values | |
| 40 // are directly usable as DomKey values, and DomKey values are either Unicode | |
| 41 // code points or outside the range of Unicode code points. | |
| 42 // | |
| 43 // 31 24 16 8 0 | |
| 44 // | | | | | | | | | | |
| 45 // | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| 46 // | z | t | v | | |
| 47 // | |
| 48 // - z is reserved and 0 for any valid DomKey. | |
| 49 // - t distinguish the type of DomKey: | |
| 50 // 0 for a Unicode code point; | |
| 51 // 1 for a dead key; | |
| 52 // 2 for any other symbolic DomKey. | |
| 53 // - v is a value whose interpretation depends on t: | |
| 54 // - for a Unicode value, it is the code point; | |
| 55 // - 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
| |
| 56 // - for others, an arbitrary distinct value. | |
| 57 enum { VALUE_BITS = 21, TYPE_BITS = 2 }; | |
| 58 enum Type : Base { | |
| 59 VALUE_MASK = (1L << VALUE_BITS) - 1, | |
| 60 TYPE_MASK = ((1L << TYPE_BITS) - 1) << VALUE_BITS, | |
| 61 TYPE_UNICODE = (0 << VALUE_BITS), | |
| 62 TYPE_DEAD = (1 << VALUE_BITS), | |
| 63 TYPE_NON_UNICODE = (2 << VALUE_BITS), | |
| 64 VALID_MASK = VALUE_MASK | TYPE_MASK, | |
| 65 }; | |
| 66 | |
| 67 public: | |
| 68 enum InvalidKey : Base { NONE = -1 }; | |
| 69 #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.
| |
| 70 #define DOM_KEY_UNI(key, id, value) id = (TYPE_UNICODE | (value)) | |
| 71 #define DOM_KEY_MAP_BEGIN FIRST_NON_UNICODE = TYPE_NON_UNICODE, | |
| 10 #define DOM_KEY_MAP(key, id) id | 72 #define DOM_KEY_MAP(key, id) id |
| 11 #define DOM_KEY_MAP_DECLARATION enum class DomKey | 73 #define DOM_KEY_MAP_END LAST_NON_UNICODE |
| 12 #include "ui/events/keycodes/dom/dom_key_data.inc" | 74 #include "ui/events/keycodes/dom/dom_key_data.inc" |
| 75 #undef DOM_KEY_MAP_DECLARATION | |
| 76 #undef DOM_KEY_MAP_BEGIN | |
| 13 #undef DOM_KEY_MAP | 77 #undef DOM_KEY_MAP |
| 14 #undef DOM_KEY_MAP_DECLARATION | 78 #undef DOM_KEY_MAP_END |
| 79 #undef DOM_KEY_UNI | |
| 80 | |
| 81 // 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.
| |
| 82 DomKey() : value_(NONE) {} | |
| 83 | |
| 84 // Create a DomKey from an integer value. This is not |explicit|, so that | |
| 85 // Unicode characters and DomKey:: constants can be used directly. | |
| 86 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
| |
| 87 | |
| 88 // Obtain the integer representation of the DomKey. | |
| 89 operator Base() const { return value_; } | |
| 90 | |
| 91 // True if the value is a valid DomKey (which excludes DomKey::NONE and | |
| 92 // integers not following the DomKey format). | |
| 93 bool IsValid() const { return (value_ & ~(TYPE_MASK | VALUE_MASK)) == 0; } | |
| 94 | |
| 95 // True if the value is a Unicode code point. | |
| 96 bool IsUnicode() const { return (value_ & TYPE_MASK) == TYPE_UNICODE; } | |
| 97 | |
| 98 // True if the value is a dead key. | |
| 99 bool IsDead() const { return (value_ & TYPE_MASK) == TYPE_DEAD; } | |
| 100 | |
| 101 // Returns the Unicode code point for either a Unicode key or a dead key. | |
| 102 // (The return value for other keys is meaningless and not comparable to | |
| 103 // a DomKey:: constant.) | |
| 104 int32_t GetCodePoint() const { return value_ & VALUE_MASK; } | |
| 105 | |
| 106 // Returns a dead-key DomKey for the given combining character. | |
| 107 static DomKey Dead(int32_t combining_character) { | |
| 108 return TYPE_DEAD | combining_character; | |
| 109 } | |
| 110 | |
| 111 private: | |
| 112 Base value_; | |
| 113 }; | |
| 15 | 114 |
| 16 } // namespace ui | 115 } // namespace ui |
| 17 | 116 |
| 18 #endif // UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_ | 117 #endif // UI_EVENTS_KEYCODES_DOM3_DOM_KEY_H_ |
| OLD | NEW |