Skip to content

Truly independent web browser

UI/Qt: Move popups along with their browser window on macOS

Andreas Kling3fc83daddd4a
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index b382791fac0..6c3e15a3b05 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -291,6 +291,7 @@ public:
#if defined(AK_OS_MACOS)
setQuitOnLastWindowClosed(false);
install_appkit_event_capture();
+ install_appkit_popup_attachment();
#endif
installEventFilter(&m_plain_text_event_filter);
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index dc6368838a1..e5286714eb2 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -44,6 +44,8 @@ void offset_appkit_window_controls(QWidget&, int x_offset, int y_offset);
// to their offset right away, before the next frame is drawn.
void reapply_appkit_window_control_offsets(QWidget&);
void install_appkit_event_capture();
+// Popups, such as popovers and menus, move along with the window they hang from, and cannot be moved on their own.
+void install_appkit_popup_attachment();
// What AppKit says of a flick's momentum at the scroll event a wheel event came from, which Qt does not pass on.
enum class AppKitScrollMomentumTransition {
// The fingers lifted, and the momentum of the flick they made is about to begin.
diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mm
index 14abe8698e2..2dbe7d2db35 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -461,6 +461,66 @@ void install_appkit_event_capture()
}
}
+static NSWindow* appkit_window_for(QWidget& widget)
+{
+ if (QGuiApplication::platformName() != "cocoa")
+ return nil;
+ auto* view = reinterpret_cast<NSView*>(widget.winId());
+ return view ? view.window : nil;
+}
+
+// A popup is a window of its own, placed by what it hangs from when it opens. Dragging that window by its title bar
+// never reaches the popup as a press outside it, so the popup would stay open where it was while the window moved away.
+// The popup's window is a child window of the window it hangs from while it is shown, so AppKit moves it along. On
+// other platforms, starting to move a window is a press outside the popup, which closes it.
+class LadybirdAppKitPopupAttachmentFilter final : public QObject {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ virtual bool eventFilter(QObject* watched, QEvent* event) override
+ {
+ if (event->type() != QEvent::Show && event->type() != QEvent::Hide)
+ return false;
+ if (!watched->isWidgetType())
+ return false;
+ auto& popup = static_cast<QWidget&>(*watched);
+ if (popup.windowType() != Qt::Popup || !popup.parentWidget())
+ return false;
+
+ auto* popup_window = appkit_window_for(popup);
+ if (!popup_window)
+ return false;
+
+ if (event->type() == QEvent::Hide) {
+ if (popup_window.parentWindow)
+ [popup_window.parentWindow removeChildWindow:popup_window];
+ return false;
+ }
+
+ // NB: A window can be dragged by its content with a gesture, which would take a popup away from what it hangs
+ // from.
+ popup_window.movable = NO;
+ auto* parent_window = appkit_window_for(*popup.parentWidget()->window());
+ if (!parent_window || parent_window == popup_window || popup_window.parentWindow == parent_window)
+ return false;
+ if (popup_window.parentWindow)
+ [popup_window.parentWindow removeChildWindow:popup_window];
+ [parent_window addChildWindow:popup_window ordered:NSWindowAbove];
+ return false;
+ }
+};
+
+void install_appkit_popup_attachment()
+{
+ static bool installed = false;
+ if (installed)
+ return;
+ if (auto* application = QCoreApplication::instance()) {
+ application->installEventFilter(new LadybirdAppKitPopupAttachmentFilter);
+ installed = true;
+ }
+}
+
static NSScreen* appkit_screen_of_window(QWidget& widget)
{
NSScreen* screen = nil;