Skip to content

Truly independent web browser

UI/Qt: Move the task manager window by dragging its title bar

Andreas Klingd65ecafb968a
diff --git a/Tests/UI/Qt/TestProcessManagerWindow.cpp b/Tests/UI/Qt/TestProcessManagerWindow.cpp
index 2548701b5bc..4aa4a37e20e 100644
--- a/Tests/UI/Qt/TestProcessManagerWindow.cpp
+++ b/Tests/UI/Qt/TestProcessManagerWindow.cpp
@@ -19,6 +19,7 @@
#include <QContextMenuEvent>
#include <QLineEdit>
#include <QMenu>
+#include <QMouseEvent>
#include <QPalette>
#include <QPushButton>
#include <QTimer>
@@ -284,6 +285,38 @@ TEST_CASE(helper_processes_are_listed_with_the_site_they_work_for)
}
#endif
+#if defined(AK_OS_MACOS)
+TEST_CASE(pressing_the_title_bar_moves_the_window)
+{
+ Core::EventLoop event_loop;
+ Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(MUST(String::from_utf8(TEST_RESOURCE_ROOT ""sv))));
+ WebView::ProcessManager manager;
+ Ladybird::ProcessManagerWindow window(manager);
+ size_t drag_count = 0;
+ window.on_title_bar_drag = [&] {
+ ++drag_count;
+ return true;
+ };
+ window.show();
+ auto* title_bar = window.findChild<QWidget*>("LadybirdTaskManagerTitleBar");
+ auto* title = window.findChild<QWidget*>("LadybirdTaskManagerTitle");
+ auto* search = window.findChild<QLineEdit*>();
+ auto press = [](QWidget& widget, Qt::MouseButton button) {
+ QPointF position(2, 2);
+ QMouseEvent event(QEvent::MouseButtonPress, position, widget.mapToGlobal(position), button, button, Qt::NoModifier);
+ QApplication::sendEvent(&widget, &event);
+ };
+
+ press(*title_bar, Qt::LeftButton);
+ EXPECT_EQ(drag_count, 1u);
+ press(*title, Qt::LeftButton);
+ EXPECT_EQ(drag_count, 2u);
+ press(*title_bar, Qt::RightButton);
+ press(*search, Qt::LeftButton);
+ EXPECT_EQ(drag_count, 2u);
+}
+#endif
+
TEST_CASE(site_labels_handle_urls_without_hosts)
{
EXPECT_EQ(Ladybird::task_manager_site_label(URL::Parser::basic_parse("https://google.com/search?q=test"sv).value()), QString("google.com"));
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 82dc96de482..8a29845525e 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -584,6 +584,9 @@ void Application::show_process_manager()
hide_appkit_window_title(*window);
offset_appkit_window_controls(*window, 3, 10);
};
+ m_process_manager_window->on_title_bar_drag = [window = m_process_manager_window.ptr()] {
+ return start_appkit_window_drag(*window);
+ };
#endif
}
m_process_manager_window->show();
diff --git a/UI/Qt/ProcessManagerWindow.cpp b/UI/Qt/ProcessManagerWindow.cpp
index c1f77e01079..ae5734a03a9 100644
--- a/UI/Qt/ProcessManagerWindow.cpp
+++ b/UI/Qt/ProcessManagerWindow.cpp
@@ -28,6 +28,7 @@
#include <QLocale>
#include <QMenu>
#include <QMessageBox>
+#include <QMouseEvent>
#include <QPainter>
#include <QPushButton>
#include <QScrollBar>
@@ -39,6 +40,7 @@
#include <QTimer>
#include <QTreeWidget>
#include <QVBoxLayout>
+#include <QWindow>
#include <errno.h>
@@ -290,6 +292,42 @@ bool sorts_before(QTreeWidgetItem const* a, QTreeWidgetItem const* b, int column
return order == Qt::AscendingOrder ? less(a, b) : less(b, a);
}
+// A window whose content fills its title bar gets no window moving from the platform there, so dragging the title
+// bar's empty parts moves the window, and double-clicking them zooms it, as in the browser window.
+class TitleBarMouseFilter final : public QObject {
+public:
+ TitleBarMouseFilter(QObject* parent, Function<bool()> start_window_move)
+ : QObject(parent)
+ , m_start_window_move(move(start_window_move))
+ {
+ }
+
+private:
+ virtual bool eventFilter(QObject* object, QEvent* event) override
+ {
+ if (event->type() != QEvent::MouseButtonPress && event->type() != QEvent::MouseButtonDblClick)
+ return false;
+ if (static_cast<QMouseEvent*>(event)->button() != Qt::LeftButton)
+ return false;
+ auto* window = static_cast<QWidget*>(object)->window();
+ if (window->isFullScreen())
+ return false;
+ if (event->type() == QEvent::MouseButtonDblClick) {
+ if (window->isMaximized())
+ window->showNormal();
+ else
+ window->showMaximized();
+ return true;
+ }
+ if (m_start_window_move())
+ return true;
+ auto* handle = window->windowHandle();
+ return handle && handle->startSystemMove();
+ }
+
+ Function<bool()> m_start_window_move;
+};
+
}
ProcessManagerWindow::ProcessManagerWindow(WebView::ProcessManager& process_manager)
@@ -334,6 +372,9 @@ ProcessManagerWindow::ProcessManagerWindow(WebView::ProcessManager& process_mana
m_search->setFixedSize(220, 28);
title_bar_layout->addWidget(m_search);
layout->addWidget(m_title_bar);
+#if defined(AK_OS_MACOS)
+ m_title_bar->installEventFilter(new TitleBarMouseFilter(m_title_bar, [this] { return on_title_bar_drag && on_title_bar_drag(); }));
+#endif
auto* list = new QWidget(content);
auto* list_layout = new QVBoxLayout(list);
diff --git a/UI/Qt/ProcessManagerWindow.h b/UI/Qt/ProcessManagerWindow.h
index 201a88b7349..9cd58a17aa3 100644
--- a/UI/Qt/ProcessManagerWindow.h
+++ b/UI/Qt/ProcessManagerWindow.h
@@ -47,6 +47,9 @@ public:
// Called as the window is shown, and as its title changes, so the platform can hide the title and place the window
// controls in the window's title bar.
Function<void()> on_title_bar_change;
+ // Called as the empty part of the title bar is pressed, where the window's content covers the platform's own title
+ // bar, so the platform can start moving the window. Returns whether it did.
+ Function<bool()> on_title_bar_drag;
private:
virtual bool event(QEvent*) override;