From 7842040d9d4ef7488518f1b51fc767836c4b9e70 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 13 Dec 2016 15:52:37 -0800 Subject: [PATCH] Add ToV8 converter for NativeWebKeyboardEvent --- atom/browser/api/atom_api_web_contents.cc | 24 ++++--------------- .../native_mate_converters/blink_converter.cc | 21 ++++++++++++++++ .../native_mate_converters/blink_converter.h | 2 ++ 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index c306ba695fca..2ee287912488 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -493,27 +493,11 @@ bool WebContents::PreHandleKeyboardEvent( content::WebContents* source, const content::NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) { - const char* type = - event.type == blink::WebInputEvent::Type::RawKeyDown ? "keyDown" : - event.type == blink::WebInputEvent::Type::KeyUp ? "keyUp" : - nullptr; - DCHECK(type); - if (!type) { + if (event.type == blink::WebInputEvent::Type::RawKeyDown + || event.type == blink::WebInputEvent::Type::KeyUp) + return Emit("before-input-event", event); + else return false; - } - - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate()); - dict.Set("type", type); - dict.Set("key", ui::KeycodeConverter::DomKeyToKeyString(event.domKey)); - - using Modifiers = blink::WebInputEvent::Modifiers; - dict.Set("isAutoRepeat", (event.modifiers & Modifiers::IsAutoRepeat) != 0); - dict.Set("shift", (event.modifiers & Modifiers::ShiftKey) != 0); - dict.Set("control", (event.modifiers & Modifiers::ControlKey) != 0); - dict.Set("alt", (event.modifiers & Modifiers::AltKey) != 0); - dict.Set("meta", (event.modifiers & Modifiers::MetaKey) != 0); - - return Emit("before-input-event", dict); } void WebContents::EnterFullscreenModeForTab(content::WebContents* source, diff --git a/atom/common/native_mate_converters/blink_converter.cc b/atom/common/native_mate_converters/blink_converter.cc index 07971e78dd43..950b4f5f0a91 100644 --- a/atom/common/native_mate_converters/blink_converter.cc +++ b/atom/common/native_mate_converters/blink_converter.cc @@ -18,6 +18,7 @@ #include "third_party/WebKit/public/web/WebFindOptions.h" #include "third_party/WebKit/public/web/WebInputEvent.h" #include "ui/base/clipboard/clipboard.h" +#include "ui/events/keycodes/dom/keycode_converter.h" #include "ui/events/keycodes/keyboard_code_conversion.h" namespace { @@ -215,6 +216,26 @@ bool Converter::FromV8( return true; } +v8::Local Converter::ToV8( + v8::Isolate* isolate, const content::NativeWebKeyboardEvent& in) { + mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + + if (in.type == blink::WebInputEvent::Type::RawKeyDown) + dict.Set("type", "keyDown"); + else if (in.type == blink::WebInputEvent::Type::KeyUp) + dict.Set("type", "keyUp"); + dict.Set("key", ui::KeycodeConverter::DomKeyToKeyString(in.domKey)); + + using Modifiers = blink::WebInputEvent::Modifiers; + dict.Set("isAutoRepeat", (in.modifiers & Modifiers::IsAutoRepeat) != 0); + dict.Set("shift", (in.modifiers & Modifiers::ShiftKey) != 0); + dict.Set("control", (in.modifiers & Modifiers::ControlKey) != 0); + dict.Set("alt", (in.modifiers & Modifiers::AltKey) != 0); + dict.Set("meta", (in.modifiers & Modifiers::MetaKey) != 0); + + return dict.GetHandle(); +} + bool Converter::FromV8( v8::Isolate* isolate, v8::Local val, blink::WebMouseEvent* out) { mate::Dictionary dict; diff --git a/atom/common/native_mate_converters/blink_converter.h b/atom/common/native_mate_converters/blink_converter.h index 78275ab62ec9..34156f313e56 100644 --- a/atom/common/native_mate_converters/blink_converter.h +++ b/atom/common/native_mate_converters/blink_converter.h @@ -45,6 +45,8 @@ template<> struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Local val, content::NativeWebKeyboardEvent* out); + static v8::Local ToV8(v8::Isolate* isolate, + const content::NativeWebKeyboardEvent& in); }; template<>