Skip to content

Truly independent web browser

UI/Qt: Keep the macOS window controls in place on title changes

Andreas Kling777d672c5666
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 2ed18782b98..15c411d7c80 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -1119,6 +1119,9 @@ void BrowserWindow::exit_fullscreen()
bool BrowserWindow::event(QEvent* event)
{
#if defined(AK_OS_MACOS)
+ // NB: Qt sends this right after it sets the native title, which also puts the controls back in their default place.
+ if (event->type() == QEvent::WindowTitleChange)
+ reapply_appkit_window_control_offsets(*this);
if (event->type() == QEvent::PlatformSurface) {
auto* platform_surface_event = static_cast<QPlatformSurfaceEvent*>(event);
if (platform_surface_event->surfaceEventType() == QPlatformSurfaceEvent::SurfaceCreated) {
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index fe90a1a58ef..4caa2bc14c8 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -29,6 +29,9 @@ void make_appkit_window_resizable(QWidget&);
void keep_appkit_window_visible_while_inactive(QWidget&);
void set_appkit_window_content_aspect_ratio(QWidget&, Gfx::IntSize);
void offset_appkit_window_controls(QWidget&, int x_offset, int y_offset);
+// AppKit puts the window controls back in their default place when the window title changes; this moves them back
+// to their offset right away, before the next frame is drawn.
+void reapply_appkit_window_control_offsets(QWidget&);
void install_appkit_event_capture();
void make_appkit_window_first_responder(QWidget&);
bool start_appkit_window_drag(QWidget&);
diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mm
index 998a1f7c153..d7a45f4384f 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -409,6 +409,8 @@ void set_appkit_window_content_aspect_ratio(QWidget& widget, Gfx::IntSize aspect
view.window.contentAspectRatio = NSMakeSize(aspect_ratio.width(), aspect_ratio.height());
}
+static char s_window_control_offset_observer_key;
+
void offset_appkit_window_controls(QWidget& widget, int x_offset, int y_offset)
{
auto* view = reinterpret_cast<NSView*>(widget.winId());
@@ -419,12 +421,21 @@ void offset_appkit_window_controls(QWidget& widget, int x_offset, int y_offset)
if (!window)
return;
- static char observer_key;
auto* observer = [[LadybirdWindowControlOffsetObserver alloc] initWithWindow:window xOffset:x_offset yOffset:y_offset];
- objc_setAssociatedObject(window, &observer_key, observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ objc_setAssociatedObject(window, &s_window_control_offset_observer_key, observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[observer release];
}
+void reapply_appkit_window_control_offsets(QWidget& widget)
+{
+ auto* view = reinterpret_cast<NSView*>(widget.internalWinId());
+ if (!view || !view.window)
+ return;
+
+ LadybirdWindowControlOffsetObserver* observer = objc_getAssociatedObject(view.window, &s_window_control_offset_observer_key);
+ [observer applyWindowControlOffsets];
+}
+
bool start_appkit_window_drag(QWidget& widget)
{
auto* window_widget = widget.window();