Do not manually convert Object to WebInputEvent

This commit is contained in:
Cheng Zhao 2015-09-18 14:09:31 +08:00
parent 5a599cb6ff
commit c550546ff1
11 changed files with 90 additions and 396 deletions

View file

@ -9,16 +9,18 @@
#include "atom/browser/browser.h"
#include "atom/browser/native_window.h"
#include "atom/common/node_includes.h"
#include "atom/common/event_types.h"
#include "atom/common/native_mate_converters/blink_converter.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/options_switches.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/render_process_host.h"
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/gfx/geometry/rect.h"
#if defined(OS_WIN)
@ -462,14 +464,6 @@ void Window::CapturePage(mate::Arguments* args) {
rect, base::Bind(&OnCapturePageDone, args->isolate(), callback));
}
void Window::BeginFrameSubscription() {
window_->SetFrameSubscription(true);
}
void Window::EndFrameSubscription() {
window_->SetFrameSubscription(false);
}
void Window::SetProgressBar(double progress) {
window_->SetProgressBar(progress);
}
@ -546,125 +540,39 @@ bool Window::IsVisibleOnAllWorkspaces() {
return window_->IsVisibleOnAllWorkspaces();
}
void Window::SendKeyboardEvent(v8::Isolate* isolate, const mate::Dictionary& data){
auto type = blink::WebInputEvent::Type::Char;
int modifiers = 0;
int keycode = 0;
int native = 0;
std::string type_str = "";
mate::Dictionary modifier_list = mate::Dictionary::CreateEmpty(isolate);
data.Get(switches::kEventType, &type_str);
data.Get(switches::kModifiers, &modifier_list);
data.Get(switches::kKeyCode, &keycode);
data.Get(switches::kNativeKeyCode, &native);
if(type_str.compare(event_types::kKeyDown) == 0){
type = blink::WebInputEvent::Type::KeyDown;
}else if(type_str.compare(event_types::kKeyUp) == 0){
type = blink::WebInputEvent::Type::KeyUp;
}else if(type_str.compare(event_types::kChar) == 0){
type = blink::WebInputEvent::Type::Char;
}
std::map<std::string, bool> modifier_types;
modifier_types[event_types::kModifierIsKeyPad] = false;
modifier_types[event_types::kModifierIsAutoRepeat] = false;
modifier_types[event_types::kModifierIsLeft] = false;
modifier_types[event_types::kModifierIsRight] = false;
modifier_types[event_types::kModifierShiftKey] = false;
modifier_types[event_types::kModifierControlKey] = false;
modifier_types[event_types::kModifierAltKey] = false;
modifier_types[event_types::kModifierMetaKey] = false;
modifier_types[event_types::kModifierCapsLockOn] = false;
modifier_types[event_types::kModifierNumLockOn] = false;
for(std::map<std::string, bool>::iterator it = modifier_types.begin(); it != modifier_types.end(); ++it){
modifier_list.Get(it->first,&(it->second));
if(it->second) modifiers = modifiers & event_types::modifierStrToWebModifier(it->first);
}
window_->SendKeyboardEvent(type, modifiers, keycode, native);
}
void Window::SendMouseEvent(v8::Isolate* isolate, const mate::Dictionary& data){
int x, y, movementX, movementY, clickCount;
std::string type_str = "";
std::string button_str = "";
mate::Dictionary modifier_list = mate::Dictionary::CreateEmpty(isolate);
blink::WebInputEvent::Type type = blink::WebInputEvent::Type::MouseMove;
blink::WebMouseEvent::Button button = blink::WebMouseEvent::Button::ButtonNone;
int modifiers = 0;
data.Get(switches::kEventType, &type_str);
data.Get(switches::kMouseEventButton, &button_str);
data.Get(switches::kModifiers, &modifier_list);
if(type_str.compare(event_types::kMouseDown) == 0){
type = blink::WebInputEvent::Type::MouseDown;
}else if(type_str.compare(event_types::kMouseUp) == 0){
type = blink::WebInputEvent::Type::MouseUp;
}else if(type_str.compare(event_types::kMouseMove) == 0){
type = blink::WebInputEvent::Type::MouseMove;
}else if(type_str.compare(event_types::kMouseEnter) == 0){
type = blink::WebInputEvent::Type::MouseEnter;
}else if(type_str.compare(event_types::kMouseLeave) == 0){
type = blink::WebInputEvent::Type::MouseLeave;
}else if(type_str.compare(event_types::kContextMenu) == 0){
type = blink::WebInputEvent::Type::ContextMenu;
}else if(type_str.compare(event_types::kMouseWheel) == 0){
type = blink::WebInputEvent::Type::MouseWheel;
}
std::map<std::string, bool> modifier_types;
modifier_types[event_types::kMouseLeftButton] = false;
modifier_types[event_types::kMouseRightButton] = false;
modifier_types[event_types::kMouseMiddleButton] = false;
modifier_types[event_types::kModifierLeftButtonDown] = false;
modifier_types[event_types::kModifierMiddleButtonDown] = false;
modifier_types[event_types::kModifierRightButtonDown] = false;
modifier_types[event_types::kModifierShiftKey] = false;
modifier_types[event_types::kModifierControlKey] = false;
modifier_types[event_types::kModifierAltKey] = false;
modifier_types[event_types::kModifierMetaKey] = false;
modifier_types[event_types::kModifierCapsLockOn] = false;
modifier_types[event_types::kModifierNumLockOn] = false;
for(std::map<std::string, bool>::iterator it = modifier_types.begin(); it != modifier_types.end(); ++it){
modifier_list.Get(it->first,&(it->second));
if(it->second) modifiers = modifiers & event_types::modifierStrToWebModifier(it->first);
}
if(type == blink::WebInputEvent::Type::MouseWheel){
bool precise = true;
x = 0;
y = 0;
data.Get(switches::kX, &x);
data.Get(switches::kY, &y);
data.Get(switches::kMouseWheelPrecise, &precise);
window_->SendMouseWheelEvent(modifiers, x, y, precise);
}else{
if (data.Get(switches::kX, &x) && data.Get(switches::kY, &y)) {
if(!data.Get(switches::kMovementX, &movementX)){
movementX = 0;
}
if(!data.Get(switches::kMovementY, &movementY)){
movementY = 0;
}
if(!data.Get(switches::kClickCount, &clickCount)){
clickCount = 0;
}
window_->SendMouseEvent(type, modifiers, button, x, y, movementX, movementY, clickCount);
void Window::SendInputEvent(v8::Isolate* isolate,
v8::Local<v8::Value> input_event) {
int type = mate::GetWebInputEventType(isolate, input_event);
if (blink::WebInputEvent::isMouseEventType(type)) {
blink::WebMouseEvent mouse_event;
if (mate::ConvertFromV8(isolate, input_event, &mouse_event)) {
window_->SendInputEvent(mouse_event);
return;
}
} else if (blink::WebInputEvent::isKeyboardEventType(type)) {
content::NativeWebKeyboardEvent keyboard_event;;
if (mate::ConvertFromV8(isolate, input_event, &keyboard_event)) {
window_->SendInputEvent(keyboard_event);
return;
}
} else if (type == blink::WebInputEvent::MouseWheel) {
blink::WebMouseWheelEvent mouse_wheel_event;
if (mate::ConvertFromV8(isolate, input_event, &mouse_wheel_event)) {
window_->SendInputEvent(mouse_wheel_event);
return;
}
}
isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
isolate, "Invalid event type")));
}
void Window::BeginFrameSubscription() {
window_->SetFrameSubscription(true);
}
void Window::EndFrameSubscription() {
window_->SetFrameSubscription(false);
}
int32_t Window::ID() const {
@ -751,8 +659,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
&Window::SetVisibleOnAllWorkspaces)
.SetMethod("isVisibleOnAllWorkspaces",
&Window::IsVisibleOnAllWorkspaces)
.SetMethod("sendMouseEvent", &Window::SendMouseEvent)
.SetMethod("sendKeyboardEvent", &Window::SendKeyboardEvent)
.SetMethod("sendInputEvent", &Window::SendInputEvent)
.SetMethod("beginFrameSubscription", &Window::BeginFrameSubscription)
.SetMethod("endFrameSubscription", &Window::EndFrameSubscription)
#if defined(OS_MACOSX)

View file

@ -142,8 +142,7 @@ class Window : public mate::TrackableObject<Window>,
bool IsMenuBarVisible();
void SetAspectRatio(double aspect_ratio, mate::Arguments* args);
void SendKeyboardEvent(v8::Isolate* isolate, const mate::Dictionary& data);
void SendMouseEvent(v8::Isolate* isolate, const mate::Dictionary& data);
void SendInputEvent(v8::Isolate* isolate, v8::Local<v8::Value> input_event);
void BeginFrameSubscription();
void EndFrameSubscription();

View file

@ -8,10 +8,6 @@
#include <utility>
#include <vector>
#include "content/public/browser/render_widget_host_view_frame_subscriber.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "media/base/video_frame.h"
#include "media/base/yuv_convert.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
@ -43,7 +39,6 @@
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/screen.h"
#include "ui/gl/gpu_switching_manager.h"
#include "ui/events/event.h"
using content::NavigationEntry;
using content::RenderWidgetHostView;
@ -312,6 +307,38 @@ void NativeWindow::SetFrameSubscription(bool isOffscreen) {
}
}
void NativeWindow::SendInputEvent(const blink::WebMouseEvent& mouse_event) {
const auto view = web_contents()->GetRenderWidgetHostView();
if (!view)
return;
const auto host = view->GetRenderWidgetHost();
if (!host)
return;
host->ForwardMouseEvent(mouse_event);
}
void NativeWindow::SendInputEvent(
const blink::WebMouseWheelEvent& mouse_wheel_event) {
const auto view = web_contents()->GetRenderWidgetHostView();
if (!view)
return;
const auto host = view->GetRenderWidgetHost();
if (!host)
return;
host->ForwardWheelEvent(mouse_wheel_event);
}
void NativeWindow::SendInputEvent(
const content::NativeWebKeyboardEvent& keyboard_event) {
const auto view = web_contents()->GetRenderWidgetHostView();
if (!view)
return;
const auto host = view->GetRenderWidgetHost();
if (!host)
return;
host->ForwardKeyboardEvent(keyboard_event);
}
void NativeWindow::RequestToClosePage() {
bool prevent_default = false;
FOR_EACH_OBSERVER(NativeWindowObserver,
@ -454,65 +481,6 @@ void NativeWindow::DevToolsClosed() {
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnDevToolsClosed());
}
void NativeWindow::SendKeyboardEvent(blink::WebInputEvent::Type type, int modifiers, int keycode, int nativeKeycode){
auto keyb_event = new content::NativeWebKeyboardEvent;
keyb_event->nativeKeyCode = nativeKeycode;
keyb_event->windowsKeyCode = keycode;
keyb_event->setKeyIdentifierFromWindowsKeyCode();
keyb_event->type = type;
keyb_event->modifiers = modifiers;
keyb_event->isSystemKey = false;
keyb_event->timeStampSeconds = base::Time::Now().ToDoubleT();
keyb_event->skip_in_browser = false;
if (type == blink::WebInputEvent::Char || type == blink::WebInputEvent::RawKeyDown) {
keyb_event->text[0] = keycode;
keyb_event->unmodifiedText[0] = keycode;
}
const auto view = web_contents()->GetRenderWidgetHostView();
const auto host = view ? view->GetRenderWidgetHost() : nullptr;
host->ForwardKeyboardEvent(*keyb_event);
}
void NativeWindow::SendMouseEvent(blink::WebInputEvent::Type type, int modifiers, blink::WebMouseEvent::Button button, int x, int y, int movementX, int movementY, int clickCount){
auto mouse_event = new blink::WebMouseEvent();
mouse_event->x = x;
mouse_event->y = y;
mouse_event->windowX = x;
mouse_event->windowY = y;
mouse_event->clickCount = clickCount;
mouse_event->type = type;
mouse_event->modifiers = modifiers;
mouse_event->button = button;
mouse_event->timeStampSeconds = base::Time::Now().ToDoubleT();
const auto view = web_contents()->GetRenderWidgetHostView();
const auto host = view ? view->GetRenderWidgetHost() : nullptr;
host->ForwardMouseEvent(*mouse_event);
}
void NativeWindow::SendMouseWheelEvent(int modifiers, int x, int y, bool precise){
auto wheel_event = new blink::WebMouseWheelEvent();
wheel_event->type = blink::WebInputEvent::MouseWheel;
wheel_event->deltaX = x;
wheel_event->deltaY = y;
if(x) wheel_event->wheelTicksX = x > 0.0f ? 1.0f : -1.0f;
if(y) wheel_event->wheelTicksY = y > 0.0f ? 1.0f : -1.0f;
wheel_event->modifiers = modifiers;
wheel_event->hasPreciseScrollingDeltas = precise;
wheel_event->canScroll = true;
const auto view = web_contents()->GetRenderWidgetHostView();
const auto host = view ? view->GetRenderWidgetHost() : nullptr;
host->ForwardWheelEvent(*wheel_event);
}
void NativeWindow::RenderViewCreated(
content::RenderViewHost* render_view_host) {
if (!transparent_)

View file

@ -11,8 +11,6 @@
#include "media/base/video_frame.h"
#include "content/public/browser/render_widget_host_view_frame_subscriber.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "atom/browser/native_window_observer.h"
#include "atom/browser/ui/accelerator_util.h"
#include "base/cancelable_callback.h"
@ -28,6 +26,11 @@
class SkRegion;
namespace blink {
class WebMouseEvent;
class WebMouseWheelEvent;
}
namespace brightray {
class InspectableWebContents;
}
@ -173,6 +176,14 @@ class NativeWindow : public content::WebContentsObserver,
gfx::Size GetAspectRatioExtraSize();
void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
// Subscribe to the frame updates.
void SetFrameSubscription(bool isOffscreen);
// Send the WebInputEvent to the page.
void SendInputEvent(const blink::WebMouseEvent& mouse_event);
void SendInputEvent(const blink::WebMouseWheelEvent& mouse_wheel_event);
void SendInputEvent(const content::NativeWebKeyboardEvent& keyboard_event);
base::WeakPtr<NativeWindow> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
@ -229,12 +240,6 @@ class NativeWindow : public content::WebContentsObserver,
void OnFrameReceived(bool result, scoped_refptr<media::VideoFrame> frame);
void SendKeyboardEvent(blink::WebInputEvent::Type type, int modifiers, int keycode, int nativeKeycode);
void SendMouseEvent(blink::WebInputEvent::Type type, int modifiers, blink::WebMouseEvent::Button button, int x, int y, int movementX, int movementY, int clickCount);
void SendMouseWheelEvent(int modifiers, int x, int y, bool clickCount);
void SetFrameSubscription(bool isOffscreen);
protected:
NativeWindow(brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options);

View file

@ -1,118 +0,0 @@
// Copyright (c) 2013 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/common/event_types.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
namespace atom {
namespace event_types {
const char kMouseDown[] = "down";
const char kMouseUp[] = "up";
const char kMouseMove[] = "move";
const char kMouseEnter[] = "enter";
const char kMouseLeave[] = "leave";
const char kContextMenu[] = "context-menu";
const char kMouseWheel[] = "wheel";
const char kKeyDown[] = "down";
const char kKeyUp[] = "up";
const char kChar[] = "char";
const char kMouseLeftButton[] = "left";
const char kMouseRightButton[] = "right";
const char kMouseMiddleButton[] = "middle";
const char kModifierLeftButtonDown[] = "left-button-down";
const char kModifierMiddleButtonDown[] = "middle-button-down";
const char kModifierRightButtonDown[] = "right-button-down";
const char kModifierShiftKey[] = "shift";
const char kModifierControlKey[] = "control";
const char kModifierAltKey[] = "alt";
const char kModifierMetaKey[] = "meta";
const char kModifierCapsLockOn[] = "caps-lock";
const char kModifierNumLockOn[] = "num-lock";
const char kModifierIsKeyPad[] = "keypad";
const char kModifierIsAutoRepeat[] = "auto-repeat";
const char kModifierIsLeft[] = "left";
const char kModifierIsRight[] = "right";
int modifierStrToWebModifier(std::string modifier){
if(modifier.compare(event_types::kModifierLeftButtonDown) == 0){
return blink::WebInputEvent::Modifiers::LeftButtonDown;
}else if(modifier.compare(event_types::kModifierMiddleButtonDown) == 0){
return blink::WebInputEvent::Modifiers::MiddleButtonDown;
}else if(modifier.compare(event_types::kModifierRightButtonDown) == 0){
return blink::WebInputEvent::Modifiers::RightButtonDown;
}else if(modifier.compare(event_types::kMouseLeftButton) == 0){
return blink::WebInputEvent::Modifiers::LeftButtonDown;
}else if(modifier.compare(event_types::kMouseRightButton) == 0){
return blink::WebInputEvent::Modifiers::RightButtonDown;
}else if(modifier.compare(event_types::kMouseMiddleButton) == 0){
return blink::WebInputEvent::Modifiers::MiddleButtonDown;
}else if(modifier.compare(event_types::kModifierIsKeyPad) == 0){
return blink::WebInputEvent::Modifiers::IsKeyPad;
}else if(modifier.compare(event_types::kModifierIsAutoRepeat) == 0){
return blink::WebInputEvent::Modifiers::IsAutoRepeat;
}else if(modifier.compare(event_types::kModifierIsLeft) == 0){
return blink::WebInputEvent::Modifiers::IsLeft;
}else if(modifier.compare(event_types::kModifierIsRight) == 0){
return blink::WebInputEvent::Modifiers::IsRight;
}else if(modifier.compare(event_types::kModifierShiftKey) == 0){
return blink::WebInputEvent::Modifiers::ShiftKey;
}else if(modifier.compare(event_types::kModifierControlKey) == 0){
return blink::WebInputEvent::Modifiers::ControlKey;
}else if(modifier.compare(event_types::kModifierAltKey) == 0){
return blink::WebInputEvent::Modifiers::AltKey;
}else if(modifier.compare(event_types::kModifierMetaKey) == 0){
return blink::WebInputEvent::Modifiers::MetaKey;
}else if(modifier.compare(event_types::kModifierCapsLockOn) == 0){
return blink::WebInputEvent::Modifiers::CapsLockOn;
}else if(modifier.compare(event_types::kModifierNumLockOn) == 0){
return blink::WebInputEvent::Modifiers::NumLockOn;
}else{
return 0;
}
}
} // namespace event_types
} // namespace atom

View file

@ -1,52 +0,0 @@
// Copyright (c) 2013 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_EVENT_TYPES_H_
#define ATOM_COMMON_EVENT_TYPES_H_
#include "third_party/WebKit/public/web/WebInputEvent.h"
namespace atom {
namespace event_types {
extern const char kMouseDown[];
extern const char kMouseUp[];
extern const char kMouseMove[];
extern const char kMouseEnter[];
extern const char kMouseLeave[];
extern const char kContextMenu[];
extern const char kMouseWheel[];
extern const char kKeyDown[];
extern const char kKeyUp[];
extern const char kChar[];
extern const char kMouseLeftButton[];
extern const char kMouseRightButton[];
extern const char kMouseMiddleButton[];
extern const char kModifierLeftButtonDown[];
extern const char kModifierMiddleButtonDown[];
extern const char kModifierRightButtonDown[];
extern const char kModifierShiftKey[];
extern const char kModifierControlKey[];
extern const char kModifierAltKey[];
extern const char kModifierMetaKey[];
extern const char kModifierCapsLockOn[];
extern const char kModifierNumLockOn[];
extern const char kModifierIsKeyPad[];
extern const char kModifierIsAutoRepeat[];
extern const char kModifierIsLeft[];
extern const char kModifierIsRight[];
int modifierStrToWebModifier(std::string modifier);
} // namespace event_types
} // namespace atom
#endif // ATOM_COMMON_EVENT_TYPES_H_

View file

@ -112,6 +112,13 @@ struct Converter<blink::WebInputEvent::Modifiers> {
}
};
int GetWebInputEventType(v8::Isolate* isolate, v8::Local<v8::Value> val) {
int type = -1;
mate::Dictionary dict;
ConvertFromV8(isolate, val, &dict) && dict.Get("type", &type);
return type;
}
bool Converter<blink::WebInputEvent>::FromV8(
v8::Isolate* isolate, v8::Local<v8::Value> val,
blink::WebInputEvent* out) {

View file

@ -24,6 +24,8 @@ struct NativeWebKeyboardEvent;
namespace mate {
int GetWebInputEventType(v8::Isolate* isolate, v8::Local<v8::Value> val);
template<>
struct Converter<blink::WebInputEvent> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,

View file

@ -116,17 +116,6 @@ const char kRegisterStandardSchemes[] = "register-standard-schemes";
// The browser process app model ID
const char kAppUserModelId[] = "app-user-model-id";
const char kModifiers[] = "modifiers";
const char kKeyCode[] = "code";
const char kNativeKeyCode[] = "native";
const char kMovementX[] = "movement-x";
const char kMovementY[] = "movement-y";
const char kClickCount[] = "click-count";
const char kEventType[] = "type";
const char kMouseEventButton[] = "button";
const char kMouseWheelPrecise[] = "precise";
} // namespace switches
} // namespace atom

View file

@ -62,17 +62,6 @@ extern const char kRegisterStandardSchemes[];
extern const char kAppUserModelId[];
extern const char kModifiers[];
extern const char kKeyCode[];
extern const char kNativeKeyCode[];
extern const char kMovementX[];
extern const char kMovementY[];
extern const char kClickCount[];
extern const char kEventType[];
extern const char kMouseEventButton[];
extern const char kMouseWheelPrecise[];
} // namespace switches
} // namespace atom

View file

@ -313,8 +313,6 @@
'atom/common/node_includes.h',
'atom/common/options_switches.cc',
'atom/common/options_switches.h',
'atom/common/event_types.cc',
'atom/common/event_types.h',
'atom/common/platform_util.h',
'atom/common/platform_util_linux.cc',
'atom/common/platform_util_mac.mm',