Skip to content

Truly independent web browser

UI/Qt: Skip the macOS slide-back animation when dragging tabs

Timothy Flynnbc4dfc0d5397
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index 6a3325a74f9..25f6a5c210f 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -12,12 +12,10 @@
#include <LibGfx/Forward.h>
#include <LibWebView/Forward.h>
-class QWidget;
-namespace Gfx {
-
-class Color;
+#include <Qt>
-}
+class QDrag;
+class QWidget;
namespace Ladybird {
@@ -36,6 +34,7 @@ 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&);
+Qt::DropAction execute_appkit_tab_drag(QWidget&, QDrag&);
void show_appkit_dictionary_lookup(QWidget&, WebView::DictionaryLookup const&, Gfx::IntPoint);
Gfx::Color appkit_web_inactive_selection_color();
Gfx::Color appkit_web_inactive_selection_text_color();
diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mm
index 098b5d71aaa..ff69e1a30df 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -6,11 +6,14 @@
#include <UI/Qt/MacWindow.h>
+#include <AK/ScopeGuard.h>
#include <LibGfx/Color.h>
#include <LibGfx/Point.h>
#include <LibGfx/Size.h>
#include <QAbstractNativeEventFilter>
#include <QCoreApplication>
+#include <QDrag>
+#include <QGuiApplication>
#include <QMouseEvent>
#include <QPointer>
#include <QWidget>
@@ -486,6 +489,43 @@ bool start_appkit_window_drag(QWidget& widget)
return true;
}
+Qt::DropAction execute_appkit_tab_drag(QWidget& widget, QDrag& drag)
+{
+ if (QGuiApplication::platformName() != "cocoa")
+ return drag.exec(Qt::MoveAction, Qt::MoveAction);
+
+ auto* view = reinterpret_cast<NSView*>(widget.window()->winId());
+ auto* window = view.window;
+ if (!window)
+ return drag.exec(Qt::MoveAction, Qt::MoveAction);
+
+ // Qt's Cocoa backend calls this synchronous AppKit API with slideBack:YES. Dropping outside a tab strip is
+ // rejected by AppKit, so exec() waits for the slide-back animation before we can create the detached window.
+ // Override only the source window's class, and restore it as soon as this tab drag finishes.
+ auto window_class = object_getClass(window);
+ auto* selector = @selector(dragImage:at:offset:event:pasteboard:source:slideBack:);
+ auto* method = class_getInstanceMethod(window_class, selector);
+ if (!method)
+ return drag.exec(Qt::MoveAction, Qt::MoveAction);
+
+ // Add an override if the method is inherited, so we don't replace NSWindow's implementation globally.
+ class_addMethod(window_class, selector, method_getImplementation(method), method_getTypeEncoding(method));
+ method = class_getInstanceMethod(window_class, selector);
+
+ auto original_implementation = method_getImplementation(method);
+ auto original_drag_image = reinterpret_cast<void (*)(id, SEL, NSImage*, NSPoint, NSSize, NSEvent*, NSPasteboard*, id, BOOL)>(original_implementation);
+ auto implementation = imp_implementationWithBlock(^(NSWindow* receiver, NSImage* image, NSPoint location, NSSize offset, NSEvent* event, NSPasteboard* pasteboard, id source, BOOL slide_back) {
+ original_drag_image(receiver, selector, image, location, offset, event, pasteboard, source, receiver == window ? NO : slide_back);
+ });
+ method_setImplementation(method, implementation);
+ ScopeGuard restore_implementation = [&]() {
+ method_setImplementation(method, original_implementation);
+ imp_removeBlock(implementation);
+ };
+
+ return drag.exec(Qt::MoveAction, Qt::MoveAction);
+}
+
void show_appkit_dictionary_lookup(QWidget& widget, WebView::DictionaryLookup const& lookup, Gfx::IntPoint position)
{
auto* view = reinterpret_cast<NSView*>(widget.winId());
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index 9634a95ea3a..52f340a1747 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -1347,7 +1347,11 @@ void TabBar::start_tab_drag(int index)
QApplication::instance()->installEventFilter(&cancellation_filter);
});
+#if defined(AK_OS_MACOS)
+ auto action = execute_appkit_tab_drag(*this, *drag);
+#else
auto action = drag->exec(Qt::MoveAction, Qt::MoveAction);
+#endif
QApplication::instance()->removeEventFilter(&cancellation_filter);
static auto const is_wayland = QGuiApplication::platformName().startsWith("wayland");