Skip to content

Commit 67002fd

Browse files
fix: macOS modal focus (#24354)
Co-authored-by: Shelley Vohr <[email protected]>
1 parent 348b2fb commit 67002fd

6 files changed

+35
-10
lines changed

shell/browser/api/electron_api_browser_window.cc

+10-10
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,29 @@ void BrowserWindow::OnWindowClosed() {
256256

257257
void BrowserWindow::OnWindowBlur() {
258258
web_contents()->StoreFocus();
259-
#if defined(OS_MACOSX)
260-
auto* rwhv = web_contents()->GetRenderWidgetHostView();
261-
if (rwhv)
262-
rwhv->SetActive(false);
263-
#endif
264259

265260
TopLevelWindow::OnWindowBlur();
266261
}
267262

268263
void BrowserWindow::OnWindowFocus() {
269264
web_contents()->RestoreFocus();
270-
#if defined(OS_MACOSX)
271-
auto* rwhv = web_contents()->GetRenderWidgetHostView();
272-
if (rwhv)
273-
rwhv->SetActive(true);
274-
#else
265+
266+
#if !defined(OS_MACOSX)
275267
if (!api_web_contents_->IsDevToolsOpened())
276268
web_contents()->Focus();
277269
#endif
278270

279271
TopLevelWindow::OnWindowFocus();
280272
}
281273

274+
void BrowserWindow::OnWindowIsKeyChanged(bool is_key) {
275+
#if defined(OS_MACOSX)
276+
auto* rwhv = web_contents()->GetRenderWidgetHostView();
277+
if (rwhv)
278+
rwhv->SetActive(is_key);
279+
#endif
280+
}
281+
282282
void BrowserWindow::OnWindowResize() {
283283
#if defined(OS_MACOSX)
284284
if (!draggable_regions_.empty())

shell/browser/api/electron_api_browser_window.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class BrowserWindow : public TopLevelWindow,
6161
// NativeWindowObserver:
6262
void RequestPreferredWidth(int* width) override;
6363
void OnCloseButtonClicked(bool* prevent_default) override;
64+
void OnWindowIsKeyChanged(bool is_key) override;
6465

6566
// TopLevelWindow:
6667
void OnWindowClosed() override;

shell/browser/native_window.cc

+5
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ void NativeWindow::NotifyWindowFocus() {
439439
observer.OnWindowFocus();
440440
}
441441

442+
void NativeWindow::NotifyWindowIsKeyChanged(bool is_key) {
443+
for (NativeWindowObserver& observer : observers_)
444+
observer.OnWindowIsKeyChanged(is_key);
445+
}
446+
442447
void NativeWindow::NotifyWindowShow() {
443448
for (NativeWindowObserver& observer : observers_)
444449
observer.OnWindowShow();

shell/browser/native_window.h

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ class NativeWindow : public base::SupportsUserData,
259259
void NotifyWindowBlur();
260260
void NotifyWindowFocus();
261261
void NotifyWindowShow();
262+
void NotifyWindowIsKeyChanged(bool is_key);
262263
void NotifyWindowHide();
263264
void NotifyWindowMaximize();
264265
void NotifyWindowUnmaximize();

shell/browser/native_window_observer.h

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class NativeWindowObserver : public base::CheckedObserver {
5757
// Called when window gains focus.
5858
virtual void OnWindowFocus() {}
5959

60+
// Called when window gained or lost key window status.
61+
virtual void OnWindowIsKeyChanged(bool is_key) {}
62+
6063
// Called when window is shown.
6164
virtual void OnWindowShow() {}
6265

shell/browser/ui/cocoa/electron_ns_window_delegate.mm

+15
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ - (void)windowDidResignMain:(NSNotification*)notification {
9797
shell_->NotifyWindowBlur();
9898
}
9999

100+
- (void)windowDidBecomeKey:(NSNotification*)notification {
101+
shell_->NotifyWindowIsKeyChanged(true);
102+
}
103+
104+
- (void)windowDidResignKey:(NSNotification*)notification {
105+
// If our app is still active and we're still the key window, ignore this
106+
// message, since it just means that a menu extra (on the "system status bar")
107+
// was activated; we'll get another |-windowDidResignKey| if we ever really
108+
// lose key window status.
109+
if ([NSApp isActive] && ([NSApp keyWindow] == [notification object]))
110+
return;
111+
112+
shell_->NotifyWindowIsKeyChanged(false);
113+
}
114+
100115
- (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize {
101116
NSSize newSize = frameSize;
102117
double aspectRatio = shell_->GetAspectRatio();

0 commit comments

Comments
 (0)