Browse Source

Merge pull request #4288 from MaxWhere/master

Adding cursor-changed event to webContents
Cheng Zhao 9 years ago
parent
commit
e450d1586e

+ 16 - 0
atom/browser/api/atom_api_web_contents.cc

@@ -27,6 +27,7 @@
 #include "atom/common/native_mate_converters/image_converter.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
 #include "atom/common/native_mate_converters/value_converter.h"
+#include "atom/common/mouse_util.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "brightray/browser/inspectable_web_contents.h"
@@ -619,6 +620,8 @@ bool WebContents::OnMessageReceived(const IPC::Message& message) {
     IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
     IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync,
                                     OnRendererMessageSync)
+    IPC_MESSAGE_HANDLER_CODE(ViewHostMsg_SetCursor, OnCursorChange,
+      handled = false)
     IPC_MESSAGE_UNHANDLED(handled = false)
   IPC_END_MESSAGE_MAP()
 
@@ -1039,6 +1042,19 @@ void WebContents::EndFrameSubscription() {
     view->EndFrameSubscription();
 }
 
+void WebContents::OnCursorChange(const content::WebCursor& cursor) {
+  content::WebCursor::CursorInfo info;
+  cursor.GetCursorInfo(&info);
+
+  if (cursor.IsCustom()) {
+    Emit("cursor-changed", CursorTypeToString(info),
+      gfx::Image::CreateFrom1xBitmap(info.custom_image),
+      info.image_scale_factor);
+  } else {
+    Emit("cursor-changed", CursorTypeToString(info));
+  }
+}
+
 void WebContents::SetSize(const SetSizeParams& params) {
   if (guest_delegate_)
     guest_delegate_->SetSize(params);

+ 4 - 0
atom/browser/api/atom_api_web_contents.h

@@ -14,6 +14,7 @@
 #include "atom/browser/common_web_contents_delegate.h"
 #include "content/public/browser/web_contents_observer.h"
 #include "content/public/common/favicon_url.h"
+#include "content/common/cursors/webcursor.h"
 #include "native_mate/handle.h"
 #include "ui/gfx/image/image.h"
 
@@ -255,6 +256,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
     return ++request_id_;
   }
 
+  // Called when we receive a CursorChange message from chromium.
+  void OnCursorChange(const content::WebCursor& cursor);
+
   // Called when received a message from renderer.
   void OnRendererMessage(const base::string16& channel,
                          const base::ListValue& args);

+ 1 - 1
atom/browser/ui/accelerator_util.cc

@@ -9,7 +9,7 @@
 #include <string>
 #include <vector>
 
-#include "atom/common/keyboad_util.h"
+#include "atom/common/keyboard_util.h"
 #include "base/stl_util.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_split.h"

+ 1 - 1
atom/common/keyboad_util.cc → atom/common/keyboard_util.cc

@@ -3,7 +3,7 @@
 // found in the LICENSE file.
 
 #include <string>
-#include "atom/common/keyboad_util.h"
+#include "atom/common/keyboard_util.h"
 
 namespace atom {
 

+ 3 - 3
atom/common/keyboad_util.h → atom/common/keyboard_util.h

@@ -2,8 +2,8 @@
 // Use of this source code is governed by the MIT license that can be
 // found in the LICENSE file.
 
-#ifndef ATOM_COMMON_KEYBOAD_UTIL_H_
-#define ATOM_COMMON_KEYBOAD_UTIL_H_
+#ifndef ATOM_COMMON_KEYBOARD_UTIL_H_
+#define ATOM_COMMON_KEYBOARD_UTIL_H_
 
 #include <string>
 #include "ui/events/keycodes/keyboard_codes.h"
@@ -20,4 +20,4 @@ ui::KeyboardCode KeyboardCodeFromKeyIdentifier(const std::string& chr);
 
 }  // namespace atom
 
-#endif  // ATOM_COMMON_KEYBOAD_UTIL_H_
+#endif  // ATOM_COMMON_KEYBOARD_UTIL_H_

+ 62 - 0
atom/common/mouse_util.cc

@@ -0,0 +1,62 @@
+// Copyright (c) 2015 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include <string>
+#include "atom/common/mouse_util.h"
+
+using Cursor = blink::WebCursorInfo::Type;
+
+namespace atom {
+
+std::string CursorTypeToString(const content::WebCursor::CursorInfo& info) {
+  switch (info.type) {
+    case Cursor::TypePointer: return "default";
+    case Cursor::TypeCross: return "crosshair";
+    case Cursor::TypeHand: return "pointer";
+    case Cursor::TypeIBeam: return "text";
+    case Cursor::TypeWait: return "wait";
+    case Cursor::TypeHelp: return "help";
+    case Cursor::TypeEastResize: return "e-resize";
+    case Cursor::TypeNorthResize: return "n-resize";
+    case Cursor::TypeNorthEastResize: return "ne-resize";
+    case Cursor::TypeNorthWestResize: return "nw-resize";
+    case Cursor::TypeSouthResize: return "s-resize";
+    case Cursor::TypeSouthEastResize: return "se-resize";
+    case Cursor::TypeSouthWestResize: return "sw-resize";
+    case Cursor::TypeWestResize: return "w-resize";
+    case Cursor::TypeNorthSouthResize: return "ns-resize";
+    case Cursor::TypeEastWestResize: return "ew-resize";
+    case Cursor::TypeNorthEastSouthWestResize: return "nesw-resize";
+    case Cursor::TypeNorthWestSouthEastResize: return "nwse-resize";
+    case Cursor::TypeColumnResize: return "col-resize";
+    case Cursor::TypeRowResize: return "row-resize";
+    case Cursor::TypeMiddlePanning: return "m-panning";
+    case Cursor::TypeEastPanning: return "e-panning";
+    case Cursor::TypeNorthPanning: return "n-panning";
+    case Cursor::TypeNorthEastPanning: return "ne-panning";
+    case Cursor::TypeNorthWestPanning: return "nw-panning";
+    case Cursor::TypeSouthPanning: return "s-panning";
+    case Cursor::TypeSouthEastPanning: return "se-panning";
+    case Cursor::TypeSouthWestPanning: return "sw-panning";
+    case Cursor::TypeWestPanning: return "w-panning";
+    case Cursor::TypeMove: return "move";
+    case Cursor::TypeVerticalText: return "vertical-text";
+    case Cursor::TypeCell: return "cell";
+    case Cursor::TypeContextMenu: return "context-menu";
+    case Cursor::TypeAlias: return "alias";
+    case Cursor::TypeProgress: return "progress";
+    case Cursor::TypeNoDrop: return "nodrop";
+    case Cursor::TypeCopy: return "copy";
+    case Cursor::TypeNone: return "none";
+    case Cursor::TypeNotAllowed: return "not-allowed";
+    case Cursor::TypeZoomIn: return "zoom-in";
+    case Cursor::TypeZoomOut: return "zoom-out";
+    case Cursor::TypeGrab: return "grab";
+    case Cursor::TypeGrabbing: return "grabbing";
+    case Cursor::TypeCustom: return "custom";
+    default: return "default";
+  }
+}
+
+}  // namespace atom

+ 36 - 0
atom/common/mouse_util.h

@@ -0,0 +1,36 @@
+// Copyright (c) 2015 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_COMMON_MOUSE_UTIL_H_
+#define ATOM_COMMON_MOUSE_UTIL_H_
+
+#include <string>
+#include "content/common/cursors/webcursor.h"
+#include "ipc/ipc_message_macros.h"
+
+// IPC macros similar to the already existing ones in the chromium source.
+// We need these to listen to the cursor change IPC message while still
+// letting chromium handle the actual cursor change by setting handled = false.
+#define IPC_MESSAGE_HANDLER_CODE(msg_class, member_func, code)                 \
+  IPC_MESSAGE_FORWARD_CODE(msg_class, this,                                    \
+    _IpcMessageHandlerClass::member_func, code)
+
+#define IPC_MESSAGE_FORWARD_CODE(msg_class, obj, member_func, code)            \
+    case msg_class::ID: {                                                      \
+        TRACK_RUN_IN_THIS_SCOPED_REGION(member_func);                          \
+        if (!msg_class::Dispatch(&ipc_message__, obj, this, param__,           \
+                                 &member_func))                                \
+          ipc_message__.set_dispatch_error();                                  \
+        code;                                                                  \
+      }                                                                        \
+      break;
+
+namespace atom {
+
+// Returns the cursor's type as a string.
+std::string CursorTypeToString(const content::WebCursor::CursorInfo& info);
+
+}  // namespace atom
+
+#endif  // ATOM_COMMON_MOUSE_UTIL_H_

+ 1 - 1
atom/common/native_mate_converters/blink_converter.cc

@@ -7,7 +7,7 @@
 #include <string>
 #include <vector>
 
-#include "atom/common/keyboad_util.h"
+#include "atom/common/keyboard_util.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "content/public/browser/native_web_keyboard_event.h"

+ 22 - 0
docs/api/web-contents.md

@@ -281,6 +281,28 @@ Emitted when a page's theme color changes. This is usually due to encountering a
 <meta name='theme-color' content='#ff0000'>
 ```
 
+### Event: 'cursor-changed'
+
+Returns:
+
+* `event` Event
+* `type` String
+* `image` NativeImage (optional)
+* `scale` Float (optional)
+
+Emitted when the cursor's type changes. The `type` parameter can be `default`,
+`crosshair`, `pointer`, `text`, `wait`, `help`, `e-resize`, `n-resize`,
+`ne-resize`, `nw-resize`, `s-resize`, `se-resize`, `sw-resize`, `w-resize`,
+`ns-resize`, `ew-resize`, `nesw-resize`, `nwse-resize`, `col-resize`,
+`row-resize`, `m-panning`, `e-panning`, `n-panning`, `ne-panning`, `nw-panning`,
+`s-panning`, `se-panning`, `sw-panning`, `w-panning`, `move`, `vertical-text`,
+`cell`, `context-menu`, `alias`, `progress`, `nodrop`, `copy`, `none`,
+`not-allowed`, `zoom-in`, `zoom-out`, `grab`, `grabbing`, `custom`.
+
+If the `type` parameter is `custom`, the `image` parameter will hold the custom
+cursor image in a `NativeImage`, and the `scale` will hold scaling information
+for the image.
+
 ## Instance Methods
 
 The `webContents` object has the following instance methods:

+ 4 - 2
filenames.gypi

@@ -317,8 +317,10 @@
       'atom/common/google_api_key.h',
       'atom/common/id_weak_map.cc',
       'atom/common/id_weak_map.h',
-      'atom/common/keyboad_util.cc',
-      'atom/common/keyboad_util.h',
+      'atom/common/keyboard_util.cc',
+      'atom/common/keyboard_util.h',
+      'atom/common/mouse_util.cc',
+      'atom/common/mouse_util.h',
       'atom/common/linux/application_info.cc',
       'atom/common/native_mate_converters/accelerator_converter.cc',
       'atom/common/native_mate_converters/accelerator_converter.h',