diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 62994c9c93d4..45b57f5307be 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -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) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 8a8ff266a883..870f8e134216 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -5,6 +5,7 @@ #ifndef ATOM_BROWSER_API_ATOM_API_WINDOW_H_ #define ATOM_BROWSER_API_ATOM_API_WINDOW_H_ +#include #include #include @@ -75,6 +76,11 @@ class Window : public mate::TrackableObject, 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, bool IsMenuBarVisible(); void SetAspectRatio(double aspect_ratio, mate::Arguments* args); +#if defined(OS_WIN) + typedef base::Callback MessageCallback; + typedef std::map 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 diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index c19bcdf467f3..e3abed473e7d 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -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 NativeWindow::DraggableRegionsToSkRegion( const std::vector& regions) { scoped_ptr sk_region(new SkRegion); diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 37a59ff8a9e4..e32b94811823 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -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); } diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index 33ab1ecb6b3d..54004a300d94 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -11,6 +11,10 @@ #include "ui/base/window_open_disposition.h" #include "url/gurl.h" +#if defined(OS_WIN) +#include +#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() {} diff --git a/atom/browser/native_window_views_win.cc b/atom/browser/native_window_views_win.cc index 6092a2242ac0..d49683acb3cf 100644 --- a/atom/browser/native_window_views_win.cc +++ b/atom/browser/native_window_views_win.cc @@ -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. diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index aa46590c746b..d7bf5be374d4 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -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