Skip to content

Commit adfc0f5

Browse files
fix: macOS modal focus (electron#24353)
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent 6852bfb commit adfc0f5

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
@@ -255,29 +255,29 @@ void BrowserWindow::OnWindowClosed() {
255255

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

264259
TopLevelWindow::OnWindowBlur();
265260
}
266261

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

278270
TopLevelWindow::OnWindowFocus();
279271
}
280272

273+
void BrowserWindow::OnWindowIsKeyChanged(bool is_key) {
274+
#if defined(OS_MACOSX)
275+
auto* rwhv = web_contents()->GetRenderWidgetHostView();
276+
if (rwhv)
277+
rwhv->SetActive(is_key);
278+
#endif
279+
}
280+
281281
void BrowserWindow::OnWindowResize() {
282282
#if defined(OS_MACOSX)
283283
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
@@ -438,6 +438,11 @@ void NativeWindow::NotifyWindowFocus() {
438438
observer.OnWindowFocus();
439439
}
440440

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

shell/browser/native_window.h

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ class NativeWindow : public base::SupportsUserData,
260260
void NotifyWindowBlur();
261261
void NotifyWindowFocus();
262262
void NotifyWindowShow();
263+
void NotifyWindowIsKeyChanged(bool is_key);
263264
void NotifyWindowHide();
264265
void NotifyWindowMaximize();
265266
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)