mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 09:21:57 -05:00
UI/Qt: Assign dropdown handler for select in WebContentView, not Tab
This makes `<select>` elements also work outside of Tab content, for example in the Inspector. Co-authored-by: Tim Flynn <trflynn89@serenityos.org> (cherry picked from commit 421fb6309f6d5ea13676c5788a3d8294631a403b; amended to resolve conflict due to us not yet having LadybirdBrowser/ladybird#910, so this will cause extra conflicts when finally cherry-picking that. But that PR will need lots of conflict resolving anyways.)
This commit is contained in:
parent
86b610e38a
commit
549f5fe374
4 changed files with 53 additions and 48 deletions
|
@ -319,47 +319,6 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St
|
|||
m_find_in_page->update_result_label(current_match_index, total_match_count);
|
||||
};
|
||||
|
||||
m_select_dropdown = new QMenu("Select Dropdown", this);
|
||||
QObject::connect(m_select_dropdown, &QMenu::aboutToHide, this, [this]() {
|
||||
if (!m_select_dropdown->activeAction())
|
||||
view().select_dropdown_closed({});
|
||||
});
|
||||
|
||||
view().on_request_select_dropdown = [this](Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items) {
|
||||
m_select_dropdown->clear();
|
||||
m_select_dropdown->setMinimumWidth(minimum_width / view().device_pixel_ratio());
|
||||
|
||||
auto add_menu_item = [this](Web::HTML::SelectItemOption const& item_option, bool in_option_group) {
|
||||
QAction* action = new QAction(qstring_from_ak_string(in_option_group ? MUST(String::formatted(" {}", item_option.label)) : item_option.label), this);
|
||||
action->setCheckable(true);
|
||||
action->setChecked(item_option.selected);
|
||||
action->setDisabled(item_option.disabled);
|
||||
action->setData(QVariant(static_cast<uint>(item_option.id)));
|
||||
QObject::connect(action, &QAction::triggered, this, &Tab::select_dropdown_action);
|
||||
m_select_dropdown->addAction(action);
|
||||
};
|
||||
|
||||
for (auto const& item : items) {
|
||||
if (item.has<Web::HTML::SelectItemOptionGroup>()) {
|
||||
auto const& item_option_group = item.get<Web::HTML::SelectItemOptionGroup>();
|
||||
QAction* subtitle = new QAction(qstring_from_ak_string(item_option_group.label), this);
|
||||
subtitle->setDisabled(true);
|
||||
m_select_dropdown->addAction(subtitle);
|
||||
|
||||
for (auto const& item_option : item_option_group.items)
|
||||
add_menu_item(item_option, true);
|
||||
}
|
||||
|
||||
if (item.has<Web::HTML::SelectItemOption>())
|
||||
add_menu_item(item.get<Web::HTML::SelectItemOption>(), false);
|
||||
|
||||
if (item.has<Web::HTML::SelectItemSeparator>())
|
||||
m_select_dropdown->addSeparator();
|
||||
}
|
||||
|
||||
m_select_dropdown->exec(view().map_point_to_global_position(content_position));
|
||||
};
|
||||
|
||||
QObject::connect(focus_location_editor_action, &QAction::triggered, this, &Tab::focus_location_editor);
|
||||
|
||||
view().on_received_source = [this](auto const& url, auto const& source) {
|
||||
|
@ -788,12 +747,6 @@ Tab::~Tab()
|
|||
delete m_inspector_widget;
|
||||
}
|
||||
|
||||
void Tab::select_dropdown_action()
|
||||
{
|
||||
QAction* action = qobject_cast<QAction*>(sender());
|
||||
view().select_dropdown_closed(action->data().value<uint>());
|
||||
}
|
||||
|
||||
void Tab::update_reset_zoom_button()
|
||||
{
|
||||
auto zoom_level = view().zoom_level();
|
||||
|
|
|
@ -84,7 +84,6 @@ public:
|
|||
public slots:
|
||||
void focus_location_editor();
|
||||
void location_edit_return_pressed();
|
||||
void select_dropdown_action();
|
||||
|
||||
signals:
|
||||
void title_changed(int id, QString const&);
|
||||
|
|
|
@ -143,10 +143,57 @@ WebContentView::WebContentView(QWidget* window, WebContentOptions const& web_con
|
|||
auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), request_server_client));
|
||||
return worker_client->dup_socket();
|
||||
};
|
||||
|
||||
m_select_dropdown = new QMenu("Select Dropdown", this);
|
||||
QObject::connect(m_select_dropdown, &QMenu::aboutToHide, this, [this]() {
|
||||
if (!m_select_dropdown->activeAction())
|
||||
select_dropdown_closed({});
|
||||
});
|
||||
|
||||
on_request_select_dropdown = [this](Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items) {
|
||||
m_select_dropdown->clear();
|
||||
m_select_dropdown->setMinimumWidth(minimum_width / device_pixel_ratio());
|
||||
|
||||
auto add_menu_item = [this](Web::HTML::SelectItemOption const& item_option, bool in_option_group) {
|
||||
QAction* action = new QAction(qstring_from_ak_string(in_option_group ? MUST(String::formatted(" {}", item_option.label)) : item_option.label), this);
|
||||
action->setCheckable(true);
|
||||
action->setChecked(item_option.selected);
|
||||
action->setDisabled(item_option.disabled);
|
||||
action->setData(QVariant(static_cast<uint>(item_option.id)));
|
||||
QObject::connect(action, &QAction::triggered, this, &WebContentView::select_dropdown_action);
|
||||
m_select_dropdown->addAction(action);
|
||||
};
|
||||
|
||||
for (auto const& item : items) {
|
||||
if (item.has<Web::HTML::SelectItemOptionGroup>()) {
|
||||
auto const& item_option_group = item.get<Web::HTML::SelectItemOptionGroup>();
|
||||
QAction* subtitle = new QAction(qstring_from_ak_string(item_option_group.label), this);
|
||||
subtitle->setDisabled(true);
|
||||
m_select_dropdown->addAction(subtitle);
|
||||
|
||||
for (auto const& item_option : item_option_group.items)
|
||||
add_menu_item(item_option, true);
|
||||
}
|
||||
|
||||
if (item.has<Web::HTML::SelectItemOption>())
|
||||
add_menu_item(item.get<Web::HTML::SelectItemOption>(), false);
|
||||
|
||||
if (item.has<Web::HTML::SelectItemSeparator>())
|
||||
m_select_dropdown->addSeparator();
|
||||
}
|
||||
|
||||
m_select_dropdown->exec(map_point_to_global_position(content_position));
|
||||
};
|
||||
}
|
||||
|
||||
WebContentView::~WebContentView() = default;
|
||||
|
||||
void WebContentView::select_dropdown_action()
|
||||
{
|
||||
QAction* action = qobject_cast<QAction*>(sender());
|
||||
select_dropdown_closed(action->data().value<uint>());
|
||||
}
|
||||
|
||||
static Web::UIEvents::MouseButton get_button_from_qt_mouse_button(Qt::MouseButton button)
|
||||
{
|
||||
if (button == Qt::MouseButton::LeftButton)
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <LibWeb/HTML/ActivateTab.h>
|
||||
#include <LibWebView/ViewImplementation.h>
|
||||
#include <QAbstractScrollArea>
|
||||
#include <QMenu>
|
||||
#include <QTimer>
|
||||
#include <QUrl>
|
||||
|
||||
|
@ -89,6 +90,9 @@ public:
|
|||
|
||||
WebContentOptions const& web_content_options() const { return m_web_content_options; }
|
||||
|
||||
public slots:
|
||||
void select_dropdown_action();
|
||||
|
||||
signals:
|
||||
void urls_dropped(QList<QUrl> const&);
|
||||
|
||||
|
@ -123,6 +127,8 @@ private:
|
|||
|
||||
WebContentOptions m_web_content_options;
|
||||
StringView m_webdriver_content_ipc_path;
|
||||
|
||||
QMenu* m_select_dropdown { nullptr };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue