Change windows messages api to hook instead of emitter

This commit is contained in:
Omri Litov 2015-10-27 14:00:08 +02:00
parent edbebf84b9
commit e355532d27
4 changed files with 73 additions and 9 deletions

View file

@ -190,8 +190,10 @@ void Window::OnExecuteWindowsCommand(const std::string& command_name) {
} }
#if defined(OS_WIN) #if defined(OS_WIN)
void Window::OnWindowMessage(UINT message, LPARAM l_param, WPARAM w_param) { void Window::OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {
Emit("message", message, l_param, w_param); if (IsWindowMessageHooked(message)) {
messages_callback_map_[message].Run(w_param, l_param);
}
} }
#endif #endif
@ -498,6 +500,29 @@ bool Window::IsMenuBarVisible() {
return 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) #if defined(OS_MACOSX)
void Window::ShowDefinitionForSelection() { void Window::ShowDefinitionForSelection() {
window_->ShowDefinitionForSelection(); window_->ShowDefinitionForSelection();
@ -596,6 +621,12 @@ void Window::BuildPrototype(v8::Isolate* isolate,
&Window::SetVisibleOnAllWorkspaces) &Window::SetVisibleOnAllWorkspaces)
.SetMethod("isVisibleOnAllWorkspaces", .SetMethod("isVisibleOnAllWorkspaces",
&Window::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) #if defined(OS_MACOSX)
.SetMethod("showDefinitionForSelection", .SetMethod("showDefinitionForSelection",
&Window::ShowDefinitionForSelection) &Window::ShowDefinitionForSelection)

View file

@ -5,6 +5,7 @@
#ifndef ATOM_BROWSER_API_ATOM_API_WINDOW_H_ #ifndef ATOM_BROWSER_API_ATOM_API_WINDOW_H_
#define ATOM_BROWSER_API_ATOM_API_WINDOW_H_ #define ATOM_BROWSER_API_ATOM_API_WINDOW_H_
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
@ -76,8 +77,8 @@ class Window : public mate::TrackableObject<Window>,
void OnExecuteWindowsCommand(const std::string& command_name) override; void OnExecuteWindowsCommand(const std::string& command_name) override;
#if defined(OS_WIN) #if defined(OS_WIN)
void OnWindowMessage(unsigned int message, LPARAM l_param, void OnWindowMessage(UINT message, WPARAM w_param,
WPARAM w_param) override; LPARAM l_param) override;
#endif #endif
// mate::Wrappable: // mate::Wrappable:
@ -148,6 +149,18 @@ class Window : public mate::TrackableObject<Window>,
bool IsMenuBarVisible(); bool IsMenuBarVisible();
void SetAspectRatio(double aspect_ratio, mate::Arguments* args); 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) #if defined(OS_MACOSX)
void ShowDefinitionForSelection(); void ShowDefinitionForSelection();
#endif #endif

View file

@ -61,7 +61,7 @@ class NativeWindowObserver {
// Called when window message received // Called when window message received
#if defined(OS_WIN) #if defined(OS_WIN)
virtual void OnWindowMessage(UINT message, LPARAM l_param, WPARAM w_param) {} virtual void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {}
#endif #endif
// Called when renderer is hung. // Called when renderer is hung.

View file

@ -232,10 +232,6 @@ Emitted when the window enters full screen state triggered by html api.
Emitted when the window leaves full screen state triggered by html api. Emitted when the window leaves full screen state triggered by html api.
### Event: 'message' _WINDOWS_
Emitted when the window receives a message from the operating system.
### Event: 'app-command': ### Event: 'app-command':
Emitted when an [App Command](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646275(v=vs.85).aspx) Emitted when an [App Command](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646275(v=vs.85).aspx)
@ -556,6 +552,30 @@ Enters or leaves the kiosk mode.
Returns whether the window is in 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_ ### `win.setRepresentedFilename(filename)` _OS X_
* `filename` String * `filename` String