Skip to content

Truly independent web browser

UI/Qt: Limit the number of visible rows in select popups

sideshowbarker2ca39729826f
diff --git a/Tests/UI/Qt/TestSelectDropdown.cpp b/Tests/UI/Qt/TestSelectDropdown.cpp
index 2edf18e7fbe..7da1ac8eeba 100644
--- a/Tests/UI/Qt/TestSelectDropdown.cpp
+++ b/Tests/UI/Qt/TestSelectDropdown.cpp
@@ -9,6 +9,7 @@
#include <QApplication>
#include <QKeyEvent>
+#include <QListWidget>
#include <QTimer>
#include <QWidget>
@@ -195,3 +196,71 @@ TEST_CASE(closing_without_reporting_reports_nothing)
EXPECT_EQ(report.calls, 1);
EXPECT(!report.selected_item_id.has_value());
}
+
+TEST_CASE(long_dropdown_limits_its_height_and_keeps_the_last_option_reachable)
+{
+ QWidget web_view;
+ web_view.resize(800, 600);
+ web_view.show();
+ Ladybird::SelectDropdown dropdown(&web_view);
+ Report report;
+ record_reports(dropdown, report);
+
+ int maximum_height = 0;
+ open_and_interact(dropdown, three_options(), [&] {
+ int row_height = dropdown.actionGeometry(dropdown.actions().first()).height();
+ maximum_height = dropdown.height() + 17 * row_height;
+ dropdown.close();
+ });
+ report = {};
+ for (auto count : { 25u, 250u }) {
+ Vector<Web::HTML::SelectItem> items;
+ for (u32 id = 1; id <= count; ++id)
+ items.append(Web::HTML::SelectItemOption { .id = id, .label = Utf16String::formatted("Option {}", id) });
+
+ open_and_interact(dropdown, items, [&] {
+ EXPECT(dropdown.height() <= maximum_height);
+ auto* target = QApplication::focusWidget();
+ if (!target || !dropdown.isAncestorOf(target))
+ target = &dropdown;
+ send_key(*target, Qt::Key_End);
+ if (auto* view = dropdown.findChild<QListWidget*>())
+ EXPECT(view->viewport()->rect().contains(view->visualItemRect(view->currentItem()).center()));
+ send_key(*target, Qt::Key_Return);
+ if (dropdown.isVisible())
+ dropdown.close();
+ });
+ EXPECT(report.selected_item_id == count);
+ }
+ EXPECT_EQ(report.calls, 2);
+}
+
+TEST_CASE(canceling_a_long_dropdown_preserves_the_selected_option_and_reports_no_choice)
+{
+ QWidget web_view;
+ web_view.show();
+ Ladybird::SelectDropdown dropdown(&web_view);
+ Report report;
+ record_reports(dropdown, report);
+
+ Vector<Web::HTML::SelectItem> items;
+ for (u32 id = 0; id < 40; ++id)
+ items.append(Web::HTML::SelectItemOption { .id = id, .selected = id == 39, .label = Utf16String::formatted("Option {}", id) });
+
+ open_and_interact(dropdown, items, [&] {
+ auto* list = dropdown.findChild<QListWidget*>();
+ if (!list) {
+ FAIL("Expected a scrollable option list");
+ dropdown.close();
+ return;
+ }
+ EXPECT_EQ(list->currentRow(), 39);
+ EXPECT(list->viewport()->rect().contains(list->visualItemRect(list->currentItem()).center()));
+ send_key(*list, Qt::Key_Escape);
+ EXPECT(!dropdown.isVisible());
+ if (dropdown.isVisible())
+ dropdown.close();
+ });
+ EXPECT_EQ(report.calls, 1);
+ EXPECT(!report.selected_item_id.has_value());
+}
diff --git a/UI/Qt/SelectDropdown.cpp b/UI/Qt/SelectDropdown.cpp
index e10b906e969..16effe1df77 100644
--- a/UI/Qt/SelectDropdown.cpp
+++ b/UI/Qt/SelectDropdown.cpp
@@ -9,10 +9,32 @@
#include <UI/Qt/StringUtils.h>
#include <QAction>
+#include <QGuiApplication>
+#include <QKeyEvent>
+#include <QListWidget>
+#include <QScreen>
+#include <QScrollBar>
#include <QVariant>
+#include <QWidgetAction>
namespace Ladybird {
+class SelectOptionList final : public QListWidget {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+protected:
+ void keyPressEvent(QKeyEvent* event) override
+ {
+ if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
+ emit itemActivated(currentItem());
+ event->accept();
+ return;
+ }
+ QListWidget::keyPressEvent(event);
+ }
+};
+
SelectDropdown::SelectDropdown(QWidget* parent)
: QMenu("Select Dropdown", parent)
{
@@ -59,21 +81,65 @@ void SelectDropdown::open(QPoint const& global_position, int minimum_width, Vect
addSeparator();
}
- // NB: exec() returns the action the user chose, or null when the menu went away without a choice — and that covers
- // every way a QMenu can close: Escape, a click elsewhere, the window deactivating. The aboutToHide signal can't
- // tell those apart from a choice; Qt emits it with the highlighted action still current, before triggered()
- // would fire, so a cancel with an item highlighted looks just like a choice there.
- auto* chosen_action = exec(global_position);
+ constexpr int maximum_visible_rows = 20;
+ Optional<u32> chosen_item_id;
+ QObject selection_context;
+ auto const menu_actions = actions();
+ if (menu_actions.size() > maximum_visible_rows) {
+ auto* list = new SelectOptionList;
+ list->setFont(font());
+ list->setFrameShape(QFrame::NoFrame);
+ list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ list->setSelectionMode(QAbstractItemView::SingleSelection);
+ int row_height = 0;
+ for (auto* action : menu_actions) {
+ auto* item = new QListWidgetItem(action->text(), list);
+ item->setData(Qt::UserRole, action->data());
+ auto size = actionGeometry(action).size();
+ item->setSizeHint(size);
+ row_height = qMax(row_height, size.height());
+ if (action->isChecked()) {
+ item->setIcon(style()->standardIcon(QStyle::SP_DialogApplyButton, nullptr, list));
+ list->setCurrentItem(item);
+ }
+ if (action->isSeparator()) {
+ auto* line = new QFrame;
+ line->setFrameShape(QFrame::HLine);
+ list->setItemWidget(item, line);
+ }
+ if (!action->isEnabled() || action->isSeparator())
+ item->setFlags(Qt::NoItemFlags);
+ }
+ clear();
+ auto* screen = QGuiApplication::screenAt(global_position);
+ if (!screen)
+ screen = this->screen();
+ auto available = screen->availableGeometry();
+ int const width = qMax(minimum_width, list->sizeHintForColumn(0) + list->verticalScrollBar()->sizeHint().width());
+ auto panel_width = style()->pixelMetric(QStyle::PM_MenuPanelWidth, nullptr, this);
+ list->setFixedSize(QSize(width, maximum_visible_rows * row_height).boundedTo(available.size() - QSize(2 * panel_width, 2 * panel_width)));
+ auto* list_action = new QWidgetAction(this);
+ list_action->setDefaultWidget(list);
+ addAction(list_action);
+ auto choose = [&](QListWidgetItem* item) {
+ if (item && item->flags().testFlag(Qt::ItemIsEnabled) && item->data(Qt::UserRole).isValid()) {
+ chosen_item_id = item->data(Qt::UserRole).toUInt();
+ close();
+ }
+ };
+ connect(list, &QListWidget::itemClicked, &selection_context, choose);
+ connect(list, &QListWidget::itemActivated, &selection_context, choose);
+ list->setFocus();
+ }
+
+ // A highlighted action alone does not indicate a choice: Escape and an outside click also dismiss the menu.
+ if (auto* action = exec(global_position); action && action->data().isValid())
+ chosen_item_id = action->data().toUInt();
if (exchange(m_suppress_close_report, false))
return;
- if (!on_closed)
- return;
-
- if (chosen_action)
- on_closed(chosen_action->data().value<uint>());
- else
- on_closed({});
+ if (on_closed)
+ on_closed(chosen_item_id);
}
void SelectDropdown::close_without_reporting()