From 7a1d1ee0ea60db533f31a0f1854f8b92281902ef Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Sun, 12 Jun 2016 20:20:25 +0200 Subject: [PATCH] Add BrowserWindow.prototype.setIgnoreFocus --- atom/browser/api/atom_api_window.cc | 5 +++++ atom/browser/api/atom_api_window.h | 1 + atom/browser/native_window.h | 1 + atom/browser/native_window_mac.h | 1 + atom/browser/native_window_mac.mm | 4 ++++ atom/browser/native_window_views.cc | 11 +++++++++++ atom/browser/native_window_views.h | 1 + docs/api/browser-window.md | 8 ++++++++ 8 files changed, 32 insertions(+) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index c95a43340418..0ce3e0e77a2a 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -529,6 +529,10 @@ void Window::SetIgnoreMouseEvents(bool ignore) { return window_->SetIgnoreMouseEvents(ignore); } +void Window::SetIgnoreFocus(bool ignore) { + return window_->SetIgnoreFocus(ignore); +} + void Window::CapturePage(mate::Arguments* args) { gfx::Rect rect; base::Callback callback; @@ -732,6 +736,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("setDocumentEdited", &Window::SetDocumentEdited) .SetMethod("isDocumentEdited", &Window::IsDocumentEdited) .SetMethod("setIgnoreMouseEvents", &Window::SetIgnoreMouseEvents) + .SetMethod("setIgnoreFocus", &Window::SetIgnoreFocus) .SetMethod("focusOnWebView", &Window::FocusOnWebView) .SetMethod("blurWebView", &Window::BlurWebView) .SetMethod("isWebViewFocused", &Window::IsWebViewFocused) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 23ab162261ff..e84e850a1966 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -146,6 +146,7 @@ class Window : public mate::TrackableObject, void SetDocumentEdited(bool edited); bool IsDocumentEdited(); void SetIgnoreMouseEvents(bool ignore); + void SetIgnoreFocus(bool ignore); void CapturePage(mate::Arguments* args); void SetProgressBar(double progress); void SetOverlayIcon(const gfx::Image& overlay, diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index c7d099fcadf2..a321bc41543d 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -155,6 +155,7 @@ class NativeWindow : public base::SupportsUserData, virtual void SetDocumentEdited(bool edited); virtual bool IsDocumentEdited(); virtual void SetIgnoreMouseEvents(bool ignore) = 0; + virtual void SetIgnoreFocus(bool ignore) = 0; virtual void SetMenu(ui::MenuModel* menu); virtual bool HasModalDialog(); virtual gfx::NativeWindow GetNativeWindow() = 0; diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 27857239e8c8..2cbdbc851dab 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -77,6 +77,7 @@ class NativeWindowMac : public NativeWindow { void SetDocumentEdited(bool edited) override; bool IsDocumentEdited() override; void SetIgnoreMouseEvents(bool ignore) override; + void SetIgnoreFocus(bool ignore) override; bool HasModalDialog() override; gfx::NativeWindow GetNativeWindow() override; gfx::AcceleratedWidget GetAcceleratedWidget() override; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 8de4b109373a..de02033b6ff8 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -883,6 +883,10 @@ void NativeWindowMac::SetIgnoreMouseEvents(bool ignore) { [window_ setIgnoresMouseEvents:ignore]; } +void NativeWindowMac::SetIgnoreFocus(bool ignore) { + [window_ setDisableKeyOrMainWindow:ignore]; +} + bool NativeWindowMac::HasModalDialog() { return [window_ attachedSheet] != nil; } diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index 29c4187a124c..845e08a07de0 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -692,6 +692,17 @@ void NativeWindowViews::SetIgnoreMouseEvents(bool ignore) { #endif } +void NativeWindowViews::SetIgnoreFocus(bool ignore) { +#if defined(OS_WIN) + LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE); + if (ignore) + ex_style |= WS_EX_NOACTIVATE; + else + ex_style &= ~WS_EX_NOACTIVATE; + ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style); +#endif +} + void NativeWindowViews::SetMenu(ui::MenuModel* menu_model) { if (menu_model == nullptr) { // Remove accelerators diff --git a/atom/browser/native_window_views.h b/atom/browser/native_window_views.h index 6a26c14e0084..826b3fce7617 100644 --- a/atom/browser/native_window_views.h +++ b/atom/browser/native_window_views.h @@ -92,6 +92,7 @@ class NativeWindowViews : public NativeWindow, void SetHasShadow(bool has_shadow) override; bool HasShadow() override; void SetIgnoreMouseEvents(bool ignore) override; + void SetIgnoreFocus(bool ignore) override; void SetMenu(ui::MenuModel* menu_model) override; gfx::NativeWindow GetNativeWindow() override; void SetOverlayIcon(const gfx::Image& overlay, diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 8b850b5c4388..975eb96bdd8d 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -942,4 +942,12 @@ All mouse events happened in this window will be passed to the window below this window, but if this window has focus, it will still receive keyboard events. +### `win.setIgnoreFocus(ignore)` _OS X_ _Windows_ + +* `ignore` Boolean + +Makes the window ignore focus. + +On OS X this prevents the window from becoming key and main window. + [blink-feature-string]: https://cs.chromium.org/chromium/src/third_party/WebKit/Source/platform/RuntimeEnabledFeatures.in