Merge pull request #3231 from omrilitov/master

Hooking windows messages
This commit is contained in:
Cheng Zhao 2015-10-27 20:48:19 +08:00
commit 142f380376
7 changed files with 102 additions and 0 deletions

View file

@ -189,6 +189,14 @@ void Window::OnExecuteWindowsCommand(const std::string& command_name) {
Emit("app-command", command_name);
}
#if defined(OS_WIN)
void Window::OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {
if (IsWindowMessageHooked(message)) {
messages_callback_map_[message].Run(w_param, l_param);
}
}
#endif
// static
mate::Wrappable* Window::New(v8::Isolate* isolate,
const mate::Dictionary& options) {
@ -492,6 +500,29 @@ bool Window::IsMenuBarVisible() {
return window_->IsMenuBarVisible();
}
#if defined(OS_WIN)
bool Window::HookWindowMessage(UINT message,
const MessageCallback& callback) {
messages_callback_map_[message] = callback;
return true;
}
void Window::UnhookWindowMessage(UINT message) {
if (!ContainsKey(messages_callback_map_, message))
return;
messages_callback_map_.erase(message);
}
bool Window::IsWindowMessageHooked(UINT message) {
return ContainsKey(messages_callback_map_, message);
}
void Window::UnhookAllWindowMessages() {
messages_callback_map_.clear();
}
#endif
#if defined(OS_MACOSX)
void Window::ShowDefinitionForSelection() {
window_->ShowDefinitionForSelection();
@ -590,6 +621,12 @@ void Window::BuildPrototype(v8::Isolate* isolate,
&Window::SetVisibleOnAllWorkspaces)
.SetMethod("isVisibleOnAllWorkspaces",
&Window::IsVisibleOnAllWorkspaces)
#if defined(OS_WIN)
.SetMethod("hookWindowMessage", &Window::HookWindowMessage)
.SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked)
.SetMethod("unhookWindowMessage", &Window::UnhookWindowMessage)
.SetMethod("unhookAllWindowMessages", &Window::UnhookAllWindowMessages)
#endif
#if defined(OS_MACOSX)
.SetMethod("showDefinitionForSelection",
&Window::ShowDefinitionForSelection)

View file

@ -5,6 +5,7 @@
#ifndef ATOM_BROWSER_API_ATOM_API_WINDOW_H_
#define ATOM_BROWSER_API_ATOM_API_WINDOW_H_
#include <map>
#include <string>
#include <vector>
@ -75,6 +76,11 @@ class Window : public mate::TrackableObject<Window>,
void OnRendererResponsive() override;
void OnExecuteWindowsCommand(const std::string& command_name) override;
#if defined(OS_WIN)
void OnWindowMessage(UINT message, WPARAM w_param,
LPARAM l_param) override;
#endif
// mate::Wrappable:
bool IsDestroyed() const override;
@ -143,6 +149,18 @@ class Window : public mate::TrackableObject<Window>,
bool IsMenuBarVisible();
void SetAspectRatio(double aspect_ratio, mate::Arguments* args);
#if defined(OS_WIN)
typedef base::Callback<void(WPARAM, LPARAM)> MessageCallback;
typedef std::map<UINT, MessageCallback> MessageCallbackMap;
MessageCallbackMap messages_callback_map_;
bool HookWindowMessage(UINT message,
const MessageCallback& callback);
bool IsWindowMessageHooked(UINT message);
void UnhookWindowMessage(UINT message);
void UnhookAllWindowMessages();
#endif
#if defined(OS_MACOSX)
void ShowDefinitionForSelection();
#endif

View file

@ -464,6 +464,14 @@ void NativeWindow::NotifyWindowExecuteWindowsCommand(
OnExecuteWindowsCommand(command));
}
#if defined(OS_WIN)
void NativeWindow::NotifyWindowMessage(UINT message, WPARAM w_param,
LPARAM l_param) {
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
OnWindowMessage(message, w_param, l_param));
}
#endif
scoped_ptr<SkRegion> NativeWindow::DraggableRegionsToSkRegion(
const std::vector<DraggableRegion>& regions) {
scoped_ptr<SkRegion> sk_region(new SkRegion);

View file

@ -210,6 +210,10 @@ class NativeWindow : public base::SupportsUserData,
void NotifyWindowLeaveHtmlFullScreen();
void NotifyWindowExecuteWindowsCommand(const std::string& command);
#if defined(OS_WIN)
void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
#endif
void AddObserver(NativeWindowObserver* obs) {
observers_.AddObserver(obs);
}

View file

@ -11,6 +11,10 @@
#include "ui/base/window_open_disposition.h"
#include "url/gurl.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
namespace atom {
class NativeWindowObserver {
@ -55,6 +59,11 @@ class NativeWindowObserver {
virtual void OnWindowEnterHtmlFullScreen() {}
virtual void OnWindowLeaveHtmlFullScreen() {}
// Called when window message received
#if defined(OS_WIN)
virtual void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {}
#endif
// Called when renderer is hung.
virtual void OnRendererUnresponsive() {}

View file

@ -80,6 +80,8 @@ bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
bool NativeWindowViews::PreHandleMSG(
UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) {
NotifyWindowMessage(message, w_param, l_param);
switch (message) {
case WM_COMMAND:
// Handle thumbar button click message.

View file

@ -552,6 +552,30 @@ Enters or leaves the kiosk mode.
Returns whether the window is in kiosk mode.
### `win.hookWindowMessage(message, callback)` _WINDOWS_
* `message` Integer
* `callback` Function
Hooks a windows message. The `callback` is called when
the message is received in the WndProc.
### `win.isWindowMessageHooked(message)` _WINDOWS_
* `message` Integer
Returns `true` or `false` depending on whether the message is hooked.
### `win.unhookWindowMessage(message)` _WINDOWS_
* `message` Integer
Unhook the window message.
### `win.unhookAllWindowMessages()` _WINDOWS_
Unhooks all of the window messages.
### `win.setRepresentedFilename(filename)` _OS X_
* `filename` String