X Tutup
Skip to content

Commit fdc2e2b

Browse files
authored
fix: recalibrate simpleFullscreen when display metrics change (electron#28150)
* fix: recalibrate simpleFullscreen when display metrics change * Address review feedback * fix: compilation issues * Address feedback from review
1 parent b045d42 commit fdc2e2b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

shell/browser/native_window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class NativeWindow : public base::SupportsUserData,
207207
virtual void SetTrafficLightPosition(base::Optional<gfx::Point> position) = 0;
208208
virtual base::Optional<gfx::Point> GetTrafficLightPosition() const = 0;
209209
virtual void RedrawTrafficLights() = 0;
210+
virtual void UpdateFrame() = 0;
210211
#endif
211212

212213
// Touchbar API

shell/browser/native_window_mac.h

Lines changed: 9 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/display/display_observer.h"
1718
#include "ui/native_theme/native_theme_observer.h"
1819
#include "ui/views/controls/native/native_view_host.h"
1920

@@ -27,7 +28,9 @@ namespace electron {
2728

2829
class RootViewMac;
2930

30-
class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
31+
class NativeWindowMac : public NativeWindow,
32+
public ui::NativeThemeObserver,
33+
public display::DisplayObserver {
3134
public:
3235
NativeWindowMac(const gin_helper::Dictionary& options, NativeWindow* parent);
3336
~NativeWindowMac() override;
@@ -124,6 +127,7 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
124127
void SetTrafficLightPosition(base::Optional<gfx::Point> position) override;
125128
base::Optional<gfx::Point> GetTrafficLightPosition() const override;
126129
void RedrawTrafficLights() override;
130+
void UpdateFrame() override;
127131
void SetTouchBar(
128132
std::vector<gin_helper::PersistentDictionary> items) override;
129133
void RefreshTouchBarItem(const std::string& item_id) override;
@@ -188,6 +192,10 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
188192
// ui::NativeThemeObserver:
189193
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
190194

195+
// display::DisplayObserver:
196+
void OnDisplayMetricsChanged(const display::Display& display,
197+
uint32_t changed_metrics) override;
198+
191199
private:
192200
// Add custom layers to the content view.
193201
void AddContentViewLayers();

shell/browser/native_window_mac.mm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "shell/common/process_util.h"
4040
#include "skia/ext/skia_utils_mac.h"
4141
#include "third_party/webrtc/modules/desktop_capture/mac/window_list_utils.h"
42+
#include "ui/display/screen.h"
4243
#include "ui/gfx/skia_util.h"
4344
#include "ui/gl/gpu_switching_manager.h"
4445
#include "ui/views/background.h"
@@ -258,6 +259,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
258259
NativeWindow* parent)
259260
: NativeWindow(options, parent), root_view_(new RootViewMac(this)) {
260261
ui::NativeTheme::GetInstanceForNativeUi()->AddObserver(this);
262+
display::Screen::GetScreen()->AddObserver(this);
261263

262264
int width = 800, height = 600;
263265
options.Get(options::kWidth, &width);
@@ -882,6 +884,17 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
882884
[window setExcludedFromWindowsMenu:excluded];
883885
}
884886

887+
void NativeWindowMac::OnDisplayMetricsChanged(const display::Display& display,
888+
uint32_t changed_metrics) {
889+
// We only want to force screen recalibration if we're in simpleFullscreen
890+
// mode.
891+
if (!is_simple_fullscreen_)
892+
return;
893+
894+
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
895+
base::BindOnce(&NativeWindow::UpdateFrame, GetWeakPtr()));
896+
}
897+
885898
void NativeWindowMac::SetSimpleFullScreen(bool simple_fullscreen) {
886899
NSWindow* window = GetNativeWindow().GetNativeNSWindow();
887900

@@ -1396,6 +1409,13 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
13961409
[buttons_view_ setNeedsDisplayForButtons];
13971410
}
13981411

1412+
// In simpleFullScreen mode, update the frame for new bounds.
1413+
void NativeWindowMac::UpdateFrame() {
1414+
NSWindow* window = GetNativeWindow().GetNativeNSWindow();
1415+
NSRect fullscreenFrame = [window.screen frame];
1416+
[window setFrame:fullscreenFrame display:YES animate:YES];
1417+
}
1418+
13991419
void NativeWindowMac::SetTouchBar(
14001420
std::vector<gin_helper::PersistentDictionary> items) {
14011421
if (@available(macOS 10.12.2, *)) {
@@ -1551,6 +1571,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
15511571
void NativeWindowMac::Cleanup() {
15521572
DCHECK(!IsClosed());
15531573
ui::NativeTheme::GetInstanceForNativeUi()->RemoveObserver(this);
1574+
display::Screen::GetScreen()->RemoveObserver(this);
15541575
[NSEvent removeMonitor:wheel_event_monitor_];
15551576
}
15561577

0 commit comments

Comments
 (0)
X Tutup