OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "core/events/InputEvent.h" | 5 #include "core/events/InputEvent.h" |
6 | 6 |
7 #include "core/events/EventDispatcher.h" | 7 #include "core/events/EventDispatcher.h" |
8 #include "public/platform/WebEditingCommandType.h" | |
8 | 9 |
9 namespace blink { | 10 namespace blink { |
10 | 11 |
12 namespace { | |
13 | |
14 const struct { | |
15 InputEvent::InputType inputType; | |
16 const char* stringName; | |
17 } kInputTypeStringNameMap[] = { | |
18 {InputEvent::InputType::None, ""}, | |
19 {InputEvent::InputType::InsertText, "insertText"}, | |
20 {InputEvent::InputType::ReplaceContent, "replaceContent"}, | |
21 {InputEvent::InputType::DeleteContent, "deleteContent"}, | |
22 {InputEvent::InputType::DeleteComposedCharacter, "deleteComposedCharacter"}, | |
23 {InputEvent::InputType::Undo, "undo"}, | |
24 {InputEvent::InputType::Redo, "redo"}, | |
25 }; | |
26 | |
27 static_assert(arraysize(kInputTypeStringNameMap) == static_cast<size_t>(InputEve nt::InputType::NumberOfInputTypes), | |
28 "must handle all InputEvent::InputType"); | |
29 | |
30 String convertInputTypeToString(InputEvent::InputType inputType) | |
31 { | |
32 const auto& it = std::begin(kInputTypeStringNameMap) + static_cast<size_t>(i nputType); | |
33 if (it >= std::begin(kInputTypeStringNameMap) && it < std::end(kInputTypeStr ingNameMap)) | |
34 return AtomicString(it->stringName); | |
35 return emptyString(); | |
36 } | |
37 | |
38 InputEvent::InputType convertStringToInputType(const String& stringName) | |
39 { | |
40 // TODO(chongz): Use binary search if the map goes larger. | |
41 for (const auto& entry : kInputTypeStringNameMap) { | |
42 if (stringName == entry.stringName) | |
43 return entry.inputType; | |
44 } | |
45 return InputEvent::InputType::None; | |
46 } | |
47 | |
48 } // anonymous namespace | |
49 | |
11 InputEvent::InputEvent() | 50 InputEvent::InputEvent() |
12 { | 51 { |
13 } | 52 } |
14 | 53 |
15 InputEvent::InputEvent(const AtomicString& type, const InputEventInit& initializ er) | 54 InputEvent::InputEvent(const AtomicString& type, const InputEventInit& initializ er) |
16 : UIEvent(type, initializer) | 55 : UIEvent(type, initializer) |
17 { | 56 { |
57 // TODO(ojan): We should find a way to prevent conversion like String->enum- >String just in order to use initializer. | |
58 // See InputEvent::createBeforeInput() for the first conversion. | |
59 if (initializer.hasInputType()) | |
60 m_inputType = convertStringToInputType(initializer.inputType()); | |
61 if (initializer.hasData()) | |
62 m_data = initializer.data(); | |
63 } | |
64 | |
65 /* static */ | |
66 InputEvent* InputEvent::createBeforeInput(InputType inputType, const String& dat a, EventCancelable cancelable) | |
67 { | |
68 InputEventInit inputEventInit; | |
69 | |
70 inputEventInit.setBubbles(true); | |
71 inputEventInit.setCancelable(static_cast<bool>(cancelable)); | |
ojan
2016/04/19 21:45:59
Typically we avoid casting unless really necessary
| |
72 // TODO(ojan): We should find a way to prevent conversion like String->enum- >String just in order to use initializer. | |
73 // See InputEvent::InputEvent() for the second conversion. | |
74 inputEventInit.setInputType(convertInputTypeToString(inputType)); | |
75 inputEventInit.setData(data); | |
76 | |
77 return InputEvent::create(EventTypeNames::beforeinput, inputEventInit); | |
78 } | |
79 | |
80 String InputEvent::inputType() const | |
81 { | |
82 return convertInputTypeToString(m_inputType); | |
18 } | 83 } |
19 | 84 |
20 bool InputEvent::isInputEvent() const | 85 bool InputEvent::isInputEvent() const |
21 { | 86 { |
22 return true; | 87 return true; |
23 } | 88 } |
24 | 89 |
25 DEFINE_TRACE(InputEvent) | 90 DEFINE_TRACE(InputEvent) |
26 { | 91 { |
27 UIEvent::trace(visitor); | 92 UIEvent::trace(visitor); |
28 } | 93 } |
29 | 94 |
30 } // namespace blink | 95 } // namespace blink |
OLD | NEW |