fix: macOS modal focus (#24286)

This commit is contained in:
Shelley Vohr 2020-06-29 13:15:28 -07:00 committed by GitHub
parent d9d07c65b2
commit af4876296c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 10 deletions

View file

@ -284,22 +284,14 @@ void BrowserWindow::OnWindowClosed() {
void BrowserWindow::OnWindowBlur() { void BrowserWindow::OnWindowBlur() {
web_contents()->StoreFocus(); web_contents()->StoreFocus();
#if defined(OS_MACOSX)
auto* rwhv = web_contents()->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(false);
#endif
BaseWindow::OnWindowBlur(); BaseWindow::OnWindowBlur();
} }
void BrowserWindow::OnWindowFocus() { void BrowserWindow::OnWindowFocus() {
web_contents()->RestoreFocus(); web_contents()->RestoreFocus();
#if defined(OS_MACOSX)
auto* rwhv = web_contents()->GetRenderWidgetHostView(); #if !defined(OS_MACOSX)
if (rwhv)
rwhv->SetActive(true);
#else
if (!api_web_contents_->IsDevToolsOpened()) if (!api_web_contents_->IsDevToolsOpened())
web_contents()->Focus(); web_contents()->Focus();
#endif #endif
@ -307,6 +299,14 @@ void BrowserWindow::OnWindowFocus() {
BaseWindow::OnWindowFocus(); BaseWindow::OnWindowFocus();
} }
void BrowserWindow::OnWindowIsKeyChanged(bool is_key) {
#if defined(OS_MACOSX)
auto* rwhv = web_contents()->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(is_key);
#endif
}
void BrowserWindow::OnWindowResize() { void BrowserWindow::OnWindowResize() {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
if (!draggable_regions_.empty()) if (!draggable_regions_.empty())

View file

@ -66,6 +66,7 @@ class BrowserWindow : public BaseWindow,
// NativeWindowObserver: // NativeWindowObserver:
void RequestPreferredWidth(int* width) override; void RequestPreferredWidth(int* width) override;
void OnCloseButtonClicked(bool* prevent_default) override; void OnCloseButtonClicked(bool* prevent_default) override;
void OnWindowIsKeyChanged(bool is_key) override;
// BaseWindow: // BaseWindow:
void OnWindowClosed() override; void OnWindowClosed() override;

View file

@ -439,6 +439,11 @@ void NativeWindow::NotifyWindowFocus() {
observer.OnWindowFocus(); observer.OnWindowFocus();
} }
void NativeWindow::NotifyWindowIsKeyChanged(bool is_key) {
for (NativeWindowObserver& observer : observers_)
observer.OnWindowIsKeyChanged(is_key);
}
void NativeWindow::NotifyWindowShow() { void NativeWindow::NotifyWindowShow() {
for (NativeWindowObserver& observer : observers_) for (NativeWindowObserver& observer : observers_)
observer.OnWindowShow(); observer.OnWindowShow();

View file

@ -257,6 +257,7 @@ class NativeWindow : public base::SupportsUserData,
void NotifyWindowBlur(); void NotifyWindowBlur();
void NotifyWindowFocus(); void NotifyWindowFocus();
void NotifyWindowShow(); void NotifyWindowShow();
void NotifyWindowIsKeyChanged(bool is_key);
void NotifyWindowHide(); void NotifyWindowHide();
void NotifyWindowMaximize(); void NotifyWindowMaximize();
void NotifyWindowUnmaximize(); void NotifyWindowUnmaximize();

View file

@ -57,6 +57,9 @@ class NativeWindowObserver : public base::CheckedObserver {
// Called when window gains focus. // Called when window gains focus.
virtual void OnWindowFocus() {} virtual void OnWindowFocus() {}
// Called when window gained or lost key window status.
virtual void OnWindowIsKeyChanged(bool is_key) {}
// Called when window is shown. // Called when window is shown.
virtual void OnWindowShow() {} virtual void OnWindowShow() {}

View file

@ -97,6 +97,21 @@ using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle;
shell_->NotifyWindowBlur(); shell_->NotifyWindowBlur();
} }
- (void)windowDidBecomeKey:(NSNotification*)notification {
shell_->NotifyWindowIsKeyChanged(true);
}
- (void)windowDidResignKey:(NSNotification*)notification {
// If our app is still active and we're still the key window, ignore this
// message, since it just means that a menu extra (on the "system status bar")
// was activated; we'll get another |-windowDidResignKey| if we ever really
// lose key window status.
if ([NSApp isActive] && ([NSApp keyWindow] == [notification object]))
return;
shell_->NotifyWindowIsKeyChanged(false);
}
- (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize { - (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize {
NSSize newSize = frameSize; NSSize newSize = frameSize;
double aspectRatio = shell_->GetAspectRatio(); double aspectRatio = shell_->GetAspectRatio();