Merge pull request #6023 from electron/ignore-focus

Add focusable option
This commit is contained in:
Cheng Zhao 2016-06-13 09:17:55 +00:00 committed by GitHub
commit 2dc453b4be
11 changed files with 36 additions and 21 deletions

View file

@ -529,8 +529,8 @@ void Window::SetIgnoreMouseEvents(bool ignore) {
return window_->SetIgnoreMouseEvents(ignore); return window_->SetIgnoreMouseEvents(ignore);
} }
void Window::SetIgnoreFocus(bool ignore) { void Window::SetFocusable(bool focusable) {
return window_->SetIgnoreFocus(ignore); return window_->SetFocusable(focusable);
} }
void Window::CapturePage(mate::Arguments* args) { void Window::CapturePage(mate::Arguments* args) {
@ -736,7 +736,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setDocumentEdited", &Window::SetDocumentEdited) .SetMethod("setDocumentEdited", &Window::SetDocumentEdited)
.SetMethod("isDocumentEdited", &Window::IsDocumentEdited) .SetMethod("isDocumentEdited", &Window::IsDocumentEdited)
.SetMethod("setIgnoreMouseEvents", &Window::SetIgnoreMouseEvents) .SetMethod("setIgnoreMouseEvents", &Window::SetIgnoreMouseEvents)
.SetMethod("setIgnoreFocus", &Window::SetIgnoreFocus) .SetMethod("setFocusable", &Window::SetFocusable)
.SetMethod("focusOnWebView", &Window::FocusOnWebView) .SetMethod("focusOnWebView", &Window::FocusOnWebView)
.SetMethod("blurWebView", &Window::BlurWebView) .SetMethod("blurWebView", &Window::BlurWebView)
.SetMethod("isWebViewFocused", &Window::IsWebViewFocused) .SetMethod("isWebViewFocused", &Window::IsWebViewFocused)

View file

@ -146,7 +146,7 @@ class Window : public mate::TrackableObject<Window>,
void SetDocumentEdited(bool edited); void SetDocumentEdited(bool edited);
bool IsDocumentEdited(); bool IsDocumentEdited();
void SetIgnoreMouseEvents(bool ignore); void SetIgnoreMouseEvents(bool ignore);
void SetIgnoreFocus(bool ignore); void SetFocusable(bool focusable);
void CapturePage(mate::Arguments* args); void CapturePage(mate::Arguments* args);
void SetProgressBar(double progress); void SetProgressBar(double progress);
void SetOverlayIcon(const gfx::Image& overlay, void SetOverlayIcon(const gfx::Image& overlay,

View file

@ -153,7 +153,7 @@ void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
SetFullScreen(true); SetFullScreen(true);
} }
bool skip; bool skip;
if (options.Get(options::kSkipTaskbar, &skip) && skip) { if (options.Get(options::kSkipTaskbar, &skip)) {
SetSkipTaskbar(skip); SetSkipTaskbar(skip);
} }
bool kiosk; bool kiosk;
@ -282,6 +282,9 @@ bool NativeWindow::IsDocumentEdited() {
return false; return false;
} }
void NativeWindow::SetFocusable(bool focusable) {
}
void NativeWindow::SetMenu(ui::MenuModel* menu) { void NativeWindow::SetMenu(ui::MenuModel* menu) {
} }

View file

@ -155,7 +155,7 @@ class NativeWindow : public base::SupportsUserData,
virtual void SetDocumentEdited(bool edited); virtual void SetDocumentEdited(bool edited);
virtual bool IsDocumentEdited(); virtual bool IsDocumentEdited();
virtual void SetIgnoreMouseEvents(bool ignore) = 0; virtual void SetIgnoreMouseEvents(bool ignore) = 0;
virtual void SetIgnoreFocus(bool ignore) = 0; virtual void SetFocusable(bool focusable);
virtual void SetMenu(ui::MenuModel* menu); virtual void SetMenu(ui::MenuModel* menu);
virtual bool HasModalDialog(); virtual bool HasModalDialog();
virtual gfx::NativeWindow GetNativeWindow() = 0; virtual gfx::NativeWindow GetNativeWindow() = 0;

View file

@ -77,7 +77,6 @@ class NativeWindowMac : public NativeWindow {
void SetDocumentEdited(bool edited) override; void SetDocumentEdited(bool edited) override;
bool IsDocumentEdited() override; bool IsDocumentEdited() override;
void SetIgnoreMouseEvents(bool ignore) override; void SetIgnoreMouseEvents(bool ignore) override;
void SetIgnoreFocus(bool ignore) override;
bool HasModalDialog() override; bool HasModalDialog() override;
gfx::NativeWindow GetNativeWindow() override; gfx::NativeWindow GetNativeWindow() override;
gfx::AcceleratedWidget GetAcceleratedWidget() override; gfx::AcceleratedWidget GetAcceleratedWidget() override;

View file

@ -490,6 +490,10 @@ NativeWindowMac::NativeWindowMac(
NSWindowCollectionBehaviorIgnoresCycle)]; NSWindowCollectionBehaviorIgnoresCycle)];
} }
bool focusable;
if (options.Get(options::kFocusable, &focusable) && !focusable)
[window_ setDisableKeyOrMainWindow:YES];
// Remove non-transparent corners, see http://git.io/vfonD. // Remove non-transparent corners, see http://git.io/vfonD.
if (!has_frame()) if (!has_frame())
[window_ setOpaque:NO]; [window_ setOpaque:NO];
@ -883,10 +887,6 @@ void NativeWindowMac::SetIgnoreMouseEvents(bool ignore) {
[window_ setIgnoresMouseEvents:ignore]; [window_ setIgnoresMouseEvents:ignore];
} }
void NativeWindowMac::SetIgnoreFocus(bool ignore) {
[window_ setDisableKeyOrMainWindow:ignore];
}
bool NativeWindowMac::HasModalDialog() { bool NativeWindowMac::HasModalDialog() {
return [window_ attachedSheet] != nil; return [window_ attachedSheet] != nil;
} }

View file

@ -181,6 +181,10 @@ NativeWindowViews::NativeWindowViews(
if (transparent() && !has_frame()) if (transparent() && !has_frame())
params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE; params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE;
bool focusable;
if (options.Get(options::kFocusable, &focusable) && !focusable)
params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
#if defined(OS_WIN) #if defined(OS_WIN)
params.native_widget = params.native_widget =
new views::DesktopNativeWidgetAura(window_.get()); new views::DesktopNativeWidgetAura(window_.get());
@ -692,14 +696,16 @@ void NativeWindowViews::SetIgnoreMouseEvents(bool ignore) {
#endif #endif
} }
void NativeWindowViews::SetIgnoreFocus(bool ignore) { void NativeWindowViews::SetFocusable(bool focusable) {
#if defined(OS_WIN) #if defined(OS_WIN)
LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE); LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
if (ignore) if (focusable)
ex_style |= WS_EX_NOACTIVATE;
else
ex_style &= ~WS_EX_NOACTIVATE; ex_style &= ~WS_EX_NOACTIVATE;
else
ex_style |= WS_EX_NOACTIVATE;
::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style); ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
SetSkipTaskbar(!focusable);
Focus(false);
#endif #endif
} }

View file

@ -92,7 +92,7 @@ class NativeWindowViews : public NativeWindow,
void SetHasShadow(bool has_shadow) override; void SetHasShadow(bool has_shadow) override;
bool HasShadow() override; bool HasShadow() override;
void SetIgnoreMouseEvents(bool ignore) override; void SetIgnoreMouseEvents(bool ignore) override;
void SetIgnoreFocus(bool ignore) override; void SetFocusable(bool focusable) override;
void SetMenu(ui::MenuModel* menu_model) override; void SetMenu(ui::MenuModel* menu_model) override;
gfx::NativeWindow GetNativeWindow() override; gfx::NativeWindow GetNativeWindow() override;
void SetOverlayIcon(const gfx::Image& overlay, void SetOverlayIcon(const gfx::Image& overlay,

View file

@ -75,6 +75,9 @@ const char kBackgroundColor[] = "backgroundColor";
// Whether the window should have a shadow. // Whether the window should have a shadow.
const char kHasShadow[] = "hasShadow"; const char kHasShadow[] = "hasShadow";
// Whether the window can be activated.
const char kFocusable[] = "focusable";
// The WebPreferences. // The WebPreferences.
const char kWebPreferences[] = "webPreferences"; const char kWebPreferences[] = "webPreferences";

View file

@ -44,6 +44,7 @@ extern const char kDisableAutoHideCursor[];
extern const char kStandardWindow[]; extern const char kStandardWindow[];
extern const char kBackgroundColor[]; extern const char kBackgroundColor[];
extern const char kHasShadow[]; extern const char kHasShadow[];
extern const char kFocusable[];
extern const char kWebPreferences[]; extern const char kWebPreferences[];
// WebPreferences. // WebPreferences.

View file

@ -54,6 +54,11 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
implemented on Linux. Default is `true`. implemented on Linux. Default is `true`.
* `closable` Boolean - Whether window is closable. This is not implemented * `closable` Boolean - Whether window is closable. This is not implemented
on Linux. Default is `true`. on Linux. Default is `true`.
* `focusable` Boolean - Whether the window can be focused. Default is
`true`. On Windows setting `focusable: false` also implies setting
`skipTaskbar: true`. On Linux setting `focusable: false` makes the window
stop interacting with wm, so the window will always stay on top in all
workspaces.
* `alwaysOnTop` Boolean - Whether the window should always stay on top of * `alwaysOnTop` Boolean - Whether the window should always stay on top of
other windows. Default is `false`. other windows. Default is `false`.
* `fullscreen` Boolean - Whether the window should show in fullscreen. When * `fullscreen` Boolean - Whether the window should show in fullscreen. When
@ -953,12 +958,10 @@ 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 this window, but if this window has focus, it will still receive keyboard
events. events.
### `win.setIgnoreFocus(ignore)` _OS X_ _Windows_ ### `win.setFocusable(focusable)` _Windows_
* `ignore` Boolean * `focusable` Boolean
Makes the window ignore focus. Changes whether the window can be focused.
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 [blink-feature-string]: https://cs.chromium.org/chromium/src/third_party/WebKit/Source/platform/RuntimeEnabledFeatures.in