Skip to content

Commit 3c19aee

Browse files
fix: reposition traffic lights on theme change (electron#22534)
1 parent b3e1134 commit 3c19aee

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

shell/browser/native_window_mac.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "base/mac/scoped_nsobject.h"
1616
#include "shell/browser/native_window.h"
17+
#include "ui/native_theme/native_theme_observer.h"
1718
#include "ui/views/controls/native/native_view_host.h"
1819

1920
@class ElectronNSWindow;
@@ -26,7 +27,7 @@ namespace electron {
2627

2728
class RootViewMac;
2829

29-
class NativeWindowMac : public NativeWindow {
30+
class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
3031
public:
3132
NativeWindowMac(const gin_helper::Dictionary& options, NativeWindow* parent);
3233
~NativeWindowMac() override;
@@ -152,6 +153,7 @@ class NativeWindowMac : public NativeWindow {
152153
// Custom traffic light positioning
153154
void RepositionTrafficLights();
154155
void SetExitingFullScreen(bool flag);
156+
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
155157

156158
enum class TitleBarStyle {
157159
NORMAL,

shell/browser/native_window_mac.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
#include "base/mac/scoped_cftyperef.h"
1717
#include "base/numerics/ranges.h"
1818
#include "base/strings/sys_string_conversions.h"
19+
#include "base/task/post_task.h"
1920
#include "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
2021
#include "content/public/browser/browser_accessibility_state.h"
22+
#include "content/public/browser/browser_task_traits.h"
23+
#include "content/public/browser/browser_thread.h"
2124
#include "content/public/browser/desktop_media_id.h"
2225
#include "shell/browser/native_browser_view_mac.h"
2326
#include "shell/browser/ui/cocoa/electron_native_widget_mac.h"
@@ -324,6 +327,8 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
324327
NativeWindowMac::NativeWindowMac(const gin_helper::Dictionary& options,
325328
NativeWindow* parent)
326329
: NativeWindow(options, parent), root_view_(new RootViewMac(this)) {
330+
ui::NativeTheme::GetInstanceForNativeUi()->AddObserver(this);
331+
327332
int width = 800, height = 600;
328333
options.Get(options::kWidth, &width);
329334
options.Get(options::kHeight, &height);
@@ -509,6 +514,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
509514
}
510515

511516
NativeWindowMac::~NativeWindowMac() {
517+
ui::NativeTheme::GetInstanceForNativeUi()->RemoveObserver(this);
512518
if (wheel_event_monitor_)
513519
[NSEvent removeMonitor:wheel_event_monitor_];
514520
}
@@ -700,6 +706,12 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
700706
exiting_fullscreen_ = flag;
701707
}
702708

709+
void NativeWindowMac::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
710+
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
711+
base::BindOnce(&NativeWindowMac::RepositionTrafficLights,
712+
base::Unretained(this)));
713+
}
714+
703715
bool NativeWindowMac::IsEnabled() {
704716
return [window_ attachedSheet] == nil;
705717
}

0 commit comments

Comments
 (0)