feat: add BrowserWindow.isFocusable() (#28642)

This commit is contained in:
Shelley Vohr 2021-04-21 12:32:19 +02:00 committed by GitHub
parent 69f3e330e7
commit 11199d8824
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 52 additions and 0 deletions

View file

@ -757,6 +757,10 @@ A `Boolean` property that determines whether the window is in simple (pre-Lion)
A `Boolean` property that determines whether the window is in fullscreen mode. A `Boolean` property that determines whether the window is in fullscreen mode.
#### `win.focusable` _Windows_ _macOS_
A `Boolean` property that determines whether the window is focusable.
#### `win.visibleOnAllWorkspaces` #### `win.visibleOnAllWorkspaces`
A `Boolean` property that determines whether the window is visible on all workspaces. A `Boolean` property that determines whether the window is visible on all workspaces.
@ -1675,6 +1679,10 @@ Changes whether the window can be focused.
On macOS it does not remove the focus from the window. On macOS it does not remove the focus from the window.
#### `win.isFocusable()` _macOS_ _Windows_
Returns whether the window can be focused.
#### `win.setParentWindow(parent)` #### `win.setParentWindow(parent)`
* `parent` BrowserWindow | null * `parent` BrowserWindow | null

View file

@ -37,6 +37,11 @@ Object.defineProperty(BaseWindow.prototype, 'simpleFullScreen', {
set: function (simple) { this.setSimpleFullScreen(simple); } set: function (simple) { this.setSimpleFullScreen(simple); }
}); });
Object.defineProperty(BaseWindow.prototype, 'focusable', {
get: function () { return this.isFocusable(); },
set: function (focusable) { this.setFocusable(focusable); }
});
Object.defineProperty(BaseWindow.prototype, 'kiosk', { Object.defineProperty(BaseWindow.prototype, 'kiosk', {
get: function () { return this.isKiosk(); }, get: function () { return this.isKiosk(); },
set: function (kiosk) { this.setKiosk(kiosk); } set: function (kiosk) { this.setKiosk(kiosk); }

View file

@ -696,6 +696,10 @@ void BaseWindow::SetFocusable(bool focusable) {
return window_->SetFocusable(focusable); return window_->SetFocusable(focusable);
} }
bool BaseWindow::IsFocusable() {
return window_->IsFocusable();
}
void BaseWindow::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) { void BaseWindow::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) {
auto context = isolate->GetCurrentContext(); auto context = isolate->GetCurrentContext();
gin::Handle<Menu> menu; gin::Handle<Menu> menu;
@ -1243,6 +1247,7 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setIgnoreMouseEvents", &BaseWindow::SetIgnoreMouseEvents) .SetMethod("setIgnoreMouseEvents", &BaseWindow::SetIgnoreMouseEvents)
.SetMethod("setContentProtection", &BaseWindow::SetContentProtection) .SetMethod("setContentProtection", &BaseWindow::SetContentProtection)
.SetMethod("setFocusable", &BaseWindow::SetFocusable) .SetMethod("setFocusable", &BaseWindow::SetFocusable)
.SetMethod("isFocusable", &BaseWindow::IsFocusable)
.SetMethod("setMenu", &BaseWindow::SetMenu) .SetMethod("setMenu", &BaseWindow::SetMenu)
.SetMethod("removeMenu", &BaseWindow::RemoveMenu) .SetMethod("removeMenu", &BaseWindow::RemoveMenu)
.SetMethod("setParentWindow", &BaseWindow::SetParentWindow) .SetMethod("setParentWindow", &BaseWindow::SetParentWindow)

View file

@ -171,6 +171,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
void SetIgnoreMouseEvents(bool ignore, gin_helper::Arguments* args); void SetIgnoreMouseEvents(bool ignore, gin_helper::Arguments* args);
void SetContentProtection(bool enable); void SetContentProtection(bool enable);
void SetFocusable(bool focusable); void SetFocusable(bool focusable);
bool IsFocusable();
void SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> menu); void SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> menu);
void RemoveMenu(); void RemoveMenu();
void SetParentWindow(v8::Local<v8::Value> value, gin_helper::Arguments* args); void SetParentWindow(v8::Local<v8::Value> value, gin_helper::Arguments* args);

View file

@ -328,6 +328,10 @@ bool NativeWindow::IsDocumentEdited() {
void NativeWindow::SetFocusable(bool focusable) {} void NativeWindow::SetFocusable(bool focusable) {}
bool NativeWindow::IsFocusable() {
return false;
}
void NativeWindow::SetMenu(ElectronMenuModel* menu) {} void NativeWindow::SetMenu(ElectronMenuModel* menu) {}
void NativeWindow::SetParentWindow(NativeWindow* parent) { void NativeWindow::SetParentWindow(NativeWindow* parent) {

View file

@ -162,6 +162,7 @@ class NativeWindow : public base::SupportsUserData,
virtual void SetIgnoreMouseEvents(bool ignore, bool forward) = 0; virtual void SetIgnoreMouseEvents(bool ignore, bool forward) = 0;
virtual void SetContentProtection(bool enable) = 0; virtual void SetContentProtection(bool enable) = 0;
virtual void SetFocusable(bool focusable); virtual void SetFocusable(bool focusable);
virtual bool IsFocusable();
virtual void SetMenu(ElectronMenuModel* menu); virtual void SetMenu(ElectronMenuModel* menu);
virtual void SetParentWindow(NativeWindow* parent); virtual void SetParentWindow(NativeWindow* parent);
virtual void AddBrowserView(NativeBrowserView* browser_view) = 0; virtual void AddBrowserView(NativeBrowserView* browser_view) = 0;

View file

@ -104,6 +104,7 @@ class NativeWindowMac : public NativeWindow,
void SetIgnoreMouseEvents(bool ignore, bool forward) override; void SetIgnoreMouseEvents(bool ignore, bool forward) override;
void SetContentProtection(bool enable) override; void SetContentProtection(bool enable) override;
void SetFocusable(bool focusable) override; void SetFocusable(bool focusable) override;
bool IsFocusable() override;
void AddBrowserView(NativeBrowserView* browser_view) override; void AddBrowserView(NativeBrowserView* browser_view) override;
void RemoveBrowserView(NativeBrowserView* browser_view) override; void RemoveBrowserView(NativeBrowserView* browser_view) override;
void SetTopBrowserView(NativeBrowserView* browser_view) override; void SetTopBrowserView(NativeBrowserView* browser_view) override;

View file

@ -1080,6 +1080,10 @@ void NativeWindowMac::SetFocusable(bool focusable) {
[window_ setDisableKeyOrMainWindow:!focusable]; [window_ setDisableKeyOrMainWindow:!focusable];
} }
bool NativeWindowMac::IsFocusable() {
return ![window_ disableKeyOrMainWindow];
}
void NativeWindowMac::AddBrowserView(NativeBrowserView* view) { void NativeWindowMac::AddBrowserView(NativeBrowserView* view) {
[CATransaction begin]; [CATransaction begin];
[CATransaction setDisableActions:YES]; [CATransaction setDisableActions:YES];

View file

@ -1087,6 +1087,17 @@ void NativeWindowViews::SetFocusable(bool focusable) {
#endif #endif
} }
bool NativeWindowViews::IsFocusable() {
bool can_activate = widget()->widget_delegate()->CanActivate();
#if defined(OS_WIN)
LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
bool no_activate = ex_style & WS_EX_NOACTIVATE;
return !no_activate && can_activate;
#else
return can_activate;
#endif
}
void NativeWindowViews::SetMenu(ElectronMenuModel* menu_model) { void NativeWindowViews::SetMenu(ElectronMenuModel* menu_model) {
#if defined(USE_X11) #if defined(USE_X11)
if (!features::IsUsingOzonePlatform()) { if (!features::IsUsingOzonePlatform()) {

View file

@ -112,6 +112,7 @@ class NativeWindowViews : public NativeWindow,
void SetIgnoreMouseEvents(bool ignore, bool forward) override; void SetIgnoreMouseEvents(bool ignore, bool forward) override;
void SetContentProtection(bool enable) override; void SetContentProtection(bool enable) override;
void SetFocusable(bool focusable) override; void SetFocusable(bool focusable) override;
bool IsFocusable() override;
void SetMenu(ElectronMenuModel* menu_model) override; void SetMenu(ElectronMenuModel* menu_model) override;
void AddBrowserView(NativeBrowserView* browser_view) override; void AddBrowserView(NativeBrowserView* browser_view) override;
void RemoveBrowserView(NativeBrowserView* browser_view) override; void RemoveBrowserView(NativeBrowserView* browser_view) override;

View file

@ -838,6 +838,17 @@ describe('BrowserWindow module', () => {
await closeWindow(w2, { assertNotWindows: false }); await closeWindow(w2, { assertNotWindows: false });
}); });
}); });
describe('BrowserWindow.isFocusable()', () => {
it('correctly returns whether a window is focusable', async () => {
const w2 = new BrowserWindow({ focusable: false });
expect(w2.isFocusable()).to.be.false();
w2.setFocusable(true);
expect(w2.isFocusable()).to.be.true();
await closeWindow(w2, { assertNotWindows: false });
});
});
}); });
describe('sizing', () => { describe('sizing', () => {