Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: macOS modal focus behavior #24354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions shell/browser/api/electron_api_browser_window.cc
Expand Up @@ -256,29 +256,29 @@ void BrowserWindow::OnWindowClosed() {

void BrowserWindow::OnWindowBlur() {
web_contents()->StoreFocus();
#if defined(OS_MACOSX)
auto* rwhv = web_contents()->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(false);
#endif

TopLevelWindow::OnWindowBlur();
}

void BrowserWindow::OnWindowFocus() {
web_contents()->RestoreFocus();
#if defined(OS_MACOSX)
auto* rwhv = web_contents()->GetRenderWidgetHostView();
if (rwhv)
rwhv->SetActive(true);
#else

#if !defined(OS_MACOSX)
if (!api_web_contents_->IsDevToolsOpened())
web_contents()->Focus();
#endif

TopLevelWindow::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() {
#if defined(OS_MACOSX)
if (!draggable_regions_.empty())
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_browser_window.h
Expand Up @@ -61,6 +61,7 @@ class BrowserWindow : public TopLevelWindow,
// NativeWindowObserver:
void RequestPreferredWidth(int* width) override;
void OnCloseButtonClicked(bool* prevent_default) override;
void OnWindowIsKeyChanged(bool is_key) override;

// TopLevelWindow:
void OnWindowClosed() override;
Expand Down
5 changes: 5 additions & 0 deletions shell/browser/native_window.cc
Expand Up @@ -439,6 +439,11 @@ void NativeWindow::NotifyWindowFocus() {
observer.OnWindowFocus();
}

void NativeWindow::NotifyWindowIsKeyChanged(bool is_key) {
for (NativeWindowObserver& observer : observers_)
observer.OnWindowIsKeyChanged(is_key);
}

void NativeWindow::NotifyWindowShow() {
for (NativeWindowObserver& observer : observers_)
observer.OnWindowShow();
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window.h
Expand Up @@ -259,6 +259,7 @@ class NativeWindow : public base::SupportsUserData,
void NotifyWindowBlur();
void NotifyWindowFocus();
void NotifyWindowShow();
void NotifyWindowIsKeyChanged(bool is_key);
void NotifyWindowHide();
void NotifyWindowMaximize();
void NotifyWindowUnmaximize();
Expand Down
3 changes: 3 additions & 0 deletions shell/browser/native_window_observer.h
Expand Up @@ -57,6 +57,9 @@ class NativeWindowObserver : public base::CheckedObserver {
// Called when window gains focus.
virtual void OnWindowFocus() {}

// Called when window gained or lost key window status.
virtual void OnWindowIsKeyChanged(bool is_key) {}

// Called when window is shown.
virtual void OnWindowShow() {}

Expand Down
15 changes: 15 additions & 0 deletions shell/browser/ui/cocoa/electron_ns_window_delegate.mm
Expand Up @@ -97,6 +97,21 @@ - (void)windowDidResignMain:(NSNotification*)notification {
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 newSize = frameSize;
double aspectRatio = shell_->GetAspectRatio();
Expand Down