Skip to content

Truly independent web browser

UI/Qt: Grow the sidebar's address field into a panel with suggestions

Andreas Klingde5b8261fcda
diff --git a/UI/Qt/Autocomplete.cpp b/UI/Qt/Autocomplete.cpp
index 5a33b2d386f..7e2b0bd7c84 100644
--- a/UI/Qt/Autocomplete.cpp
+++ b/UI/Qt/Autocomplete.cpp
@@ -416,28 +416,59 @@ void Autocomplete::create_popup()
m_shadow->hide();
#endif
- m_list_view = new QListView(m_popup);
- m_list_view->setObjectName("LadybirdAutocompleteList");
- m_list_view->setFocusPolicy(Qt::NoFocus);
- m_list_view->setSelectionMode(QAbstractItemView::SingleSelection);
- m_list_view->setMouseTracking(true);
- m_list_view->setFrameShape(QFrame::NoFrame);
- m_list_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- m_list_view->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
- m_list_view->setModel(m_model);
- m_list_view->setItemDelegate(m_delegate);
- update_chrome_style();
+ m_list_view = create_list_view(*m_popup);
auto* layout = new QVBoxLayout(m_popup);
layout->setContentsMargins(POPUP_PADDING, POPUP_PADDING, POPUP_PADDING, POPUP_PADDING);
layout->setSpacing(0);
layout->addWidget(m_list_view);
+ update_chrome_style();
+}
- connect(m_list_view, &QAbstractItemView::clicked, this, [this](QModelIndex const& index) {
+QListView* Autocomplete::create_list_view(QWidget& parent)
+{
+ auto* list_view = new QListView(&parent);
+ list_view->setObjectName("LadybirdAutocompleteList");
+ list_view->setFocusPolicy(Qt::NoFocus);
+ list_view->setSelectionMode(QAbstractItemView::SingleSelection);
+ list_view->setMouseTracking(true);
+ list_view->setFrameShape(QFrame::NoFrame);
+ list_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ list_view->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
+ list_view->setModel(m_model);
+ list_view->setItemDelegate(m_delegate);
+ list_view->installEventFilter(this);
+
+ connect(list_view, &QAbstractItemView::clicked, this, [this](QModelIndex const& index) {
if (!is_selectable_row(index.row()))
return;
emit suggestion_clicked(index.data(SuggestionIndexRole).toInt());
});
+ return list_view;
+}
+
+QListView* Autocomplete::list_view() const
+{
+ return m_host ? m_hosted_list_view.data() : m_list_view;
+}
+
+void Autocomplete::set_host(QWidget* host)
+{
+ if (m_host == host)
+ return;
+
+ auto was_visible = close();
+ delete m_hosted_list_view.data();
+
+ m_host = host;
+ if (host) {
+ m_hosted_list_view = create_list_view(*host);
+ m_hosted_list_view->hide();
+ host->layout()->addWidget(m_hosted_list_view);
+ update_chrome_style();
+ }
+ if (was_visible)
+ show_current_suggestions();
}
Autocomplete::~Autocomplete()
@@ -448,6 +479,7 @@ Autocomplete::~Autocomplete()
delete m_popup;
}
delete m_shadow.data();
+ delete m_hosted_list_view.data();
}
void Autocomplete::update_chrome_style()
@@ -465,6 +497,11 @@ void Autocomplete::update_chrome_style()
m_shadow->update();
cast_card_shadow();
m_list_view->viewport()->update();
+ if (m_hosted_list_view) {
+ m_hosted_list_view->setPalette(palette);
+ m_hosted_list_view->setStyleSheet(ChromeStyle::autocomplete_popup_style_sheet(palette));
+ m_hosted_list_view->viewport()->update();
+ }
m_is_updating_chrome_style = false;
}
@@ -486,12 +523,26 @@ void Autocomplete::show_with_suggestions(Vector<WebView::AutocompleteSuggestion>
create_popup();
m_model->set_suggestions(move(suggestions));
+ show_current_suggestions();
+ set_selected_suggestion(selected_suggestion_index);
+}
+
+void Autocomplete::show_current_suggestions()
+{
if (m_model->rowCount() == 0) {
close();
return;
}
update_chrome_style();
+ // NB: The host may be hidden until it sees the list is open, so the list's own visibility isn't watched.
+ if (m_host) {
+ m_hosted_list_view->setFixedHeight(list_height());
+ m_hosted_list_view->show();
+ set_anchor_popup_open(true);
+ return;
+ }
+
position_popup();
if (!m_popup->isVisible()) {
// The panel fades in from 4 px above its place, unless the system asks for less motion.
@@ -525,13 +576,12 @@ void Autocomplete::show_with_suggestions(Vector<WebView::AutocompleteSuggestion>
if (m_shadow)
m_shadow->raise();
m_popup->raise();
-
- set_selected_suggestion(selected_suggestion_index);
}
void Autocomplete::set_selected_suggestion(Optional<size_t> suggestion_index)
{
- if (!m_popup)
+ auto* list_view = this->list_view();
+ if (!m_popup || !list_view)
return;
int table_row = suggestion_index.has_value()
@@ -539,17 +589,24 @@ void Autocomplete::set_selected_suggestion(Optional<size_t> suggestion_index)
: -1;
if (table_row == -1) {
- m_list_view->setCurrentIndex({});
+ list_view->setCurrentIndex({});
return;
}
auto index = m_model->index(table_row, 0);
- m_list_view->setCurrentIndex(index);
- m_list_view->scrollTo(index);
+ list_view->setCurrentIndex(index);
+ list_view->scrollTo(index);
}
bool Autocomplete::close()
{
+ if (m_host) {
+ if (!m_hosted_list_view || m_hosted_list_view->isHidden())
+ return false;
+ m_hosted_list_view->hide();
+ set_anchor_popup_open(false);
+ return true;
+ }
if (!m_popup || !m_popup->isVisible())
return false;
m_popup->hide();
@@ -570,6 +627,8 @@ void Autocomplete::set_anchor_popup_open(bool open)
bool Autocomplete::is_visible() const
{
+ if (m_host)
+ return m_hosted_list_view && !m_hosted_list_view->isHidden();
return m_popup && m_popup->isVisible();
}
@@ -586,7 +645,7 @@ bool Autocomplete::eventFilter(QObject* watched, QEvent* event)
emit dismissed();
}
- if (is_visible() && (watched == m_anchor || watched == m_anchor->window())
+ if (!m_host && is_visible() && (watched == m_anchor || watched == m_anchor->window())
&& (type == QEvent::Move || type == QEvent::Resize))
position_popup();
@@ -597,7 +656,8 @@ bool Autocomplete::eventFilter(QObject* watched, QEvent* event)
if (event->type() == QEvent::MouseButtonPress && is_visible()) {
auto* mouse_event = static_cast<QMouseEvent*>(event);
auto global = mouse_event->globalPosition().toPoint();
- auto popup_global = QRect(m_popup->mapToGlobal(QPoint(0, 0)), m_popup->size());
+ auto* popup = m_host ? m_host.data() : static_cast<QWidget*>(m_popup);
+ auto popup_global = QRect(popup->mapToGlobal(QPoint(0, 0)), popup->size());
auto anchor_global = QRect(m_anchor->mapToGlobal(QPoint(0, 0)), m_anchor->size());
if (!popup_global.contains(global) && !anchor_global.contains(global)) {
if (close())
@@ -607,26 +667,28 @@ bool Autocomplete::eventFilter(QObject* watched, QEvent* event)
return QObject::eventFilter(watched, event);
}
-void Autocomplete::position_popup()
+int Autocomplete::list_height() const
{
int visible_count = static_cast<int>(min(m_model->visible_suggestion_count(), MAXIMUM_VISIBLE_AUTOCOMPLETE_SUGGESTIONS));
- if (visible_count == 0)
- return;
-
int total_height = 0;
- int seen_suggestions = 0;
- int row_count = m_model->rowCount();
- for (int i = 0; i < row_count; ++i) {
- auto index = m_model->index(i, 0);
+ for (int i = 0; i < visible_count; ++i) {
QStyleOptionViewItem option;
- option.initFrom(m_list_view);
- int h = m_delegate->sizeHint(option, index).height();
- total_height += h;
- ++seen_suggestions;
- if (seen_suggestions >= visible_count)
- break;
+ option.initFrom(list_view());
+ total_height += m_delegate->sizeHint(option, m_model->index(i, 0)).height();
}
+ // The list ends a little above the window's bottom edge.
+ auto* top_window = m_anchor->window();
+ auto list_top = m_anchor->mapTo(top_window, QPoint(0, m_anchor->height() + POPUP_ANCHOR_GAP)).y();
+ int available_height = max(0, top_window->height() - list_top - (2 * POPUP_PADDING));
+ return min(total_height, available_height);
+}
+
+void Autocomplete::position_popup()
+{
+ if (m_model->visible_suggestion_count() == 0)
+ return;
+
auto* top_window = m_anchor->window();
if (!top_window)
return;
@@ -654,8 +716,7 @@ void Autocomplete::position_popup()
int card_width = max(m_anchor->width(), MINIMUM_POPUP_WIDTH);
auto card_position = m_anchor->mapTo(top_window, QPoint(0, m_anchor->height() + POPUP_ANCHOR_GAP + qRound(m_opening_offset)));
int vertical_overhead = 2 * POPUP_PADDING;
- int available_height = max(0, top_window->height() - card_position.y() - vertical_overhead);
- int list_height = min(total_height, available_height);
+ int list_height = this->list_height();
m_list_view->setFixedHeight(list_height);
m_popup->setFixedSize(card_width, list_height + vertical_overhead);
diff --git a/UI/Qt/Autocomplete.h b/UI/Qt/Autocomplete.h
index c476ed278a8..ebeac726486 100644
--- a/UI/Qt/Autocomplete.h
+++ b/UI/Qt/Autocomplete.h
@@ -41,6 +41,9 @@ public:
void update_chrome_style();
void schedule_chrome_style_update();
+ // While a host is set, the suggestions are listed at the end of the host's layout instead of in a popup of their own.
+ void set_host(QWidget* host);
+
void show_with_suggestions(Vector<WebView::AutocompleteSuggestion>, Optional<size_t> selected_suggestion_index);
void set_selected_suggestion(Optional<size_t> suggestion_index);
bool close();
@@ -54,6 +57,10 @@ protected:
private:
void create_popup();
+ QListView* create_list_view(QWidget& parent);
+ QListView* list_view() const;
+ int list_height() const;
+ void show_current_suggestions();
void set_anchor_popup_open(bool);
bool is_visible() const;
void position_popup();
@@ -65,6 +72,8 @@ private:
// The card's shadow, in a window of its own that lets the mouse through. Not used on macOS.
QPointer<QWidget> m_shadow;
QListView* m_list_view { nullptr };
+ QPointer<QWidget> m_host;
+ QPointer<QListView> m_hosted_list_view;
AutocompleteModel* m_model { nullptr };
AutocompleteDelegate* m_delegate { nullptr };
bool m_is_updating_chrome_style { false };
diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cpp
index 7c3f5fe3217..522c1daba8f 100644
--- a/UI/Qt/LocationEdit.cpp
+++ b/UI/Qt/LocationEdit.cpp
@@ -787,7 +787,10 @@ void LocationEdit::update_focus_ring(qreal opacity)
// The ring fades in when the field gets focus, and disappears at once when it loses it.
void LocationEdit::show_focus_ring()
{
- m_is_editing_location = true;
+ if (!m_is_editing_location) {
+ m_is_editing_location = true;
+ emit editing_location_changed();
+ }
update_trailing_action_visibility();
if (!m_focus_ring_animation)
@@ -810,7 +813,10 @@ void LocationEdit::show_focus_ring_at_once()
void LocationEdit::hide_focus_ring()
{
- m_is_editing_location = false;
+ if (m_is_editing_location) {
+ m_is_editing_location = false;
+ emit editing_location_changed();
+ }
update_trailing_action_visibility();
update_completion_title();
@@ -1418,4 +1424,9 @@ QString LocationEdit::current_query() const
return text();
}
+void LocationEdit::set_suggestions_host(QWidget* host)
+{
+ m_autocomplete->set_host(host);
+}
+
}
diff --git a/UI/Qt/LocationEdit.h b/UI/Qt/LocationEdit.h
index 549a08fff8e..530d27858e6 100644
--- a/UI/Qt/LocationEdit.h
+++ b/UI/Qt/LocationEdit.h
@@ -67,6 +67,9 @@ public:
void set_url_is_hidden(bool);
// While idle, a website's address shows only up to its host, as where the field is narrow, in the sidebar.
void set_shows_host_only(bool);
+ // While a host is set, the suggestions are listed in it instead of in a popup below the field.
+ void set_suggestions_host(QWidget*);
+ bool is_editing_location() const { return m_is_editing_location; }
void show_autocomplete();
void park_editing_session();
bool has_parked_editing_session() const { return m_parked_editing_session.has_value(); }
@@ -86,6 +89,7 @@ public:
signals:
void focus_return_requested();
+ void editing_location_changed();
private:
virtual void changeEvent(QEvent* event) override;
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index e2288968b67..54f76f47fd5 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -4093,7 +4093,8 @@ void TabWidget::rebuild_layout()
// NB: Moving the page stack between layouts drops keyboard focus from the page, which leaves the window's keyboard
// shortcuts dead until something is clicked. Give focus back to whatever had it.
QPointer<QWidget> focus_widget = QApplication::focusWidget();
- if (focus_widget && !isAncestorOf(focus_widget))
+ // NB: In the sidebar, the address field floats in a panel of the window's own.
+ if (focus_widget && focus_widget->window() != window())
focus_widget = nullptr;
ScopeGuard restore_focus = [&] {
if (!focus_widget)
diff --git a/UI/Qt/WindowToolbar.cpp b/UI/Qt/WindowToolbar.cpp
index 7daf956921d..e9192d9de81 100644
--- a/UI/Qt/WindowToolbar.cpp
+++ b/UI/Qt/WindowToolbar.cpp
@@ -7,6 +7,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/GenericShorthands.h>
#include <AK/kmalloc.h>
#include <LibWebView/Application.h>
#include <LibWebView/CrashReportOutbox.h>
@@ -20,6 +21,10 @@
#include <UI/Qt/FeedbackPopover.h>
#include <UI/Qt/Icon.h>
#include <UI/Qt/LadybirdButton.h>
+#include <UI/Qt/LocationEdit.h>
+#if defined(AK_OS_MACOS)
+# include <UI/Qt/MacWindow.h>
+#endif
#include <UI/Qt/Menu.h>
#include <UI/Qt/Motion.h>
#include <UI/Qt/PerformanceMonitorWidget.h>
@@ -31,6 +36,7 @@
#include <UI/Qt/WindowToolbar.h>
#include <QApplication>
+#include <QDynamicPropertyChangeEvent>
#include <QFont>
#include <QFontMetrics>
#include <QFrame>
@@ -48,6 +54,7 @@
#include <QToolButton>
#include <QVBoxLayout>
#include <QVariantAnimation>
+#include <QWindow>
namespace Ladybird {
@@ -619,6 +626,72 @@ static constexpr int SIDEBAR_NAVIGATION_ROW_HEIGHT = 32;
static constexpr int SIDEBAR_FIELD_TOP_MARGIN = 4;
static constexpr int SIDEBAR_FIELD_BOTTOM_MARGIN = 8;
static constexpr int SIDEBAR_FOOTER_HEIGHT = 40;
+// While it is edited, the sidebar's address field grows into a panel over the page this wide, with the suggestions
+// below the field. It stays this far from the window's edge.
+static constexpr int FIELD_PANEL_WIDTH = 600;
+static constexpr int FIELD_PANEL_WINDOW_MARGIN = 8;
+static constexpr int FIELD_PANEL_CORNER_RADIUS = 12;
+
+// The sidebar's address field floats over its place in the sidebar, in a panel that is a native child of the window on
+// macOS. So the panel stacks above the native views of the sidebar and the page, and can grow over the page, while the
+// field keeps the keyboard focus.
+class SidebarFieldPanel final : public QWidget {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ explicit SidebarFieldPanel(QWidget& window)
+ : QWidget(&window)
+ {
+ setObjectName("LadybirdSidebarFieldPanel");
+#if defined(AK_OS_MACOS)
+ setAttribute(Qt::WA_DontCreateNativeAncestors);
+ setAttribute(Qt::WA_NativeWindow);
+#endif
+ }
+
+ bool is_expanded() const { return m_expanded; }
+ void set_expanded(bool expanded)
+ {
+ if (m_expanded == expanded)
+ return;
+ m_expanded = expanded;
+ // The grown panel casts the shadow of a popover.
+ if (expanded)
+ m_shadow = &cast_floating_shadow(*this, FIELD_PANEL_CORNER_RADIUS);
+ else
+ delete m_shadow.data();
+ round_corners();
+ update();
+ }
+
+ void round_corners()
+ {
+#if defined(AK_OS_MACOS)
+ if (m_expanded)
+ round_appkit_view_corners(*this, FIELD_PANEL_CORNER_RADIUS, Theme::panel().edge);
+ else
+ round_appkit_view_corners(*this, 0, Qt::transparent);
+#endif
+ }
+
+private:
+ virtual void paintEvent(QPaintEvent*) override
+ {
+ QPainter painter(this);
+ // At rest, the panel is the sidebar under the field.
+ if (!m_expanded) {
+ painter.fillRect(rect(), Theme::chrome(*this).sidebar);
+ return;
+ }
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setPen(QPen(Theme::panel().edge, 1));
+ painter.setBrush(Theme::panel().surface);
+ painter.drawRoundedRect(QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5), FIELD_PANEL_CORNER_RADIUS, FIELD_PANEL_CORNER_RADIUS);
+ }
+
+ bool m_expanded { false };
+ QPointer<QWidget> m_shadow;
+};
// NB: Window controls run to the window's edge. Without them, the address field ends the toolbar, and the room for its
// focus ring takes the place of some of the toolbar's margin.
@@ -1169,7 +1242,15 @@ WindowToolbar::WindowToolbar(WebView::IsPrivate is_private, QWidget* parent)
config_variable_changed(WebView::ConfigVariableID::ShowFeedbackButton);
}
-WindowToolbar::~WindowToolbar() = default;
+// NB: The tabs' address fields are in the field's container, which goes back to the toolbar, so it is torn down with it
+// as it is in a row.
+WindowToolbar::~WindowToolbar()
+{
+ qApp->removeEventFilter(this);
+ if (m_location_edit_container->parentWidget() != m_toolbar)
+ m_location_edit_container->setParent(m_toolbar);
+ delete m_field_panel.data();
+}
void WindowToolbar::lay_out_as_row()
{
@@ -1191,6 +1272,9 @@ void WindowToolbar::lay_out_as_row()
toolbar_layout->addWidget(m_window_controls, 0, Qt::AlignVCenter);
}
+ if (m_field_slot)
+ m_field_slot->hide();
+
// NB: The field reaches the end of the toolbar, leaving room for its focus ring.
m_location_edit_layout->setContentsMargins(TOOLBAR_LOCATION_EDIT_MARGIN, 0, TOOLBAR_LOCATION_FOCUS_RING_ROOM, 0);
// NB: The container draws the line under the toolbar along its bottom edge, below the toolbar's full height.
@@ -1216,9 +1300,18 @@ void WindowToolbar::lay_out_in_sidebar()
// NB: The field's container leaves room for its focus ring all around, which the row's margins make up for.
auto ring_room = TOOLBAR_LOCATION_FOCUS_RING_ROOM;
field_row->setContentsMargins(m_sidebar_inset - ring_room, SIDEBAR_FIELD_TOP_MARGIN - ring_room, m_sidebar_inset - ring_room, SIDEBAR_FIELD_BOTTOM_MARGIN - ring_room);
- field_row->addWidget(m_location_edit_container, 1);
+ if (!m_field_panel)
+ create_field_panel();
+ field_row->addWidget(m_field_slot, 1);
toolbar_layout->addLayout(field_row);
m_location_edit_layout->setContentsMargins(ring_room, ring_room, ring_room, ring_room);
+ static_cast<QBoxLayout*>(m_field_panel->layout())->insertWidget(0, m_location_edit_container);
+ m_location_edit_container->show();
+ m_field_slot->show();
+
+ // NB: The panel follows the slot as the slot or any of its ancestors moves.
+ for (QWidget* widget = m_field_slot; widget; widget = widget->parentWidget())
+ widget->installEventFilter(this);
// NB: What the sidebar has no room for stays hidden, as widgets left out of a layout would show where they last were.
m_sidebar_slot->hide();
@@ -1235,6 +1328,120 @@ void WindowToolbar::lay_out_in_sidebar()
layout()->setContentsMargins(0, 0, 0, 0);
}
+void WindowToolbar::create_field_panel()
+{
+ m_field_panel = new SidebarFieldPanel(*window());
+ m_field_panel->setStyleSheet(styleSheet());
+ m_field_panel->installEventFilter(this);
+ auto* panel_layout = new QVBoxLayout(m_field_panel);
+ panel_layout->setContentsMargins(0, 0, 0, 0);
+ panel_layout->setSpacing(0);
+
+ // The suggestions line up with the field, a little below it.
+ m_suggestions_area = new QWidget(m_field_panel);
+ auto* suggestions_layout = new QVBoxLayout(m_suggestions_area);
+ suggestions_layout->setContentsMargins(TOOLBAR_LOCATION_FOCUS_RING_ROOM, 0, TOOLBAR_LOCATION_FOCUS_RING_ROOM, TOOLBAR_LOCATION_FOCUS_RING_ROOM);
+ suggestions_layout->setSpacing(0);
+ panel_layout->addWidget(m_suggestions_area);
+ m_suggestions_area->hide();
+
+ m_field_slot = new QWidget(m_toolbar);
+ m_field_slot->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);
+ m_field_slot->hide();
+}
+
+// The field grows into the panel while it lists suggestions. Without any, it stays in the sidebar.
+bool WindowToolbar::field_panel_should_expand() const
+{
+ return m_arrangement == ToolbarArrangement::Sidebar && m_location_edit && m_location_edit->is_editing_location() && m_location_edit->isVisible()
+ && m_location_edit->property("autocompleteOpen").toBool();
+}
+
+// NB: The sidebar's native view is raised as the sidebar is laid out. Raising the panel as a widget does nothing while it
+// is already the window's topmost child, so its native view is raised too.
+void WindowToolbar::raise_field_panel()
+{
+ m_field_panel->raise();
+ if (auto* handle = m_field_panel->windowHandle()) {
+ handle->raise();
+ // NB: This lets the panel's shadow follow it.
+ QEvent z_order_change(QEvent::ZOrderChange);
+ QCoreApplication::sendEvent(m_field_panel, &z_order_change);
+ }
+}
+
+void WindowToolbar::update_field_panel()
+{
+ if (!m_field_panel)
+ return;
+
+ auto expanded = field_panel_should_expand();
+ if (expanded != m_field_panel->is_expanded()) {
+ m_field_panel->set_expanded(expanded);
+ m_suggestions_area->setVisible(expanded);
+ // A click outside the grown panel shrinks it back.
+ if (expanded)
+ qApp->installEventFilter(this);
+ else
+ qApp->removeEventFilter(this);
+ }
+
+ if (m_arrangement != ToolbarArrangement::Sidebar) {
+ m_field_panel->hide();
+ return;
+ }
+
+ // NB: The slot keeps the field's place in the sidebar.
+ m_field_slot->setFixedHeight(m_location_edit_container->sizeHint().height());
+ if (!m_field_slot->isVisible()) {
+ m_field_panel->hide();
+ return;
+ }
+
+ auto& window = *m_field_panel->parentWidget();
+ auto position = m_field_slot->mapTo(&window, QPoint());
+ QSize size = m_field_slot->size();
+ if (expanded) {
+ size.setWidth(max(size.width(), min(FIELD_PANEL_WIDTH, window.width() - position.x() - FIELD_PANEL_WINDOW_MARGIN)));
+ size.setHeight(min(m_field_panel->sizeHint().height(), window.height() - position.y()));
+ }
+ m_field_panel->setGeometry(QRect(position, size));
+ m_field_panel->show();
+ raise_field_panel();
+}
+
+bool WindowToolbar::eventFilter(QObject* watched, QEvent* event)
+{
+ if (!m_field_panel)
+ return false;
+
+ // NB: While the panel is grown, this filters the application's events, to see clicks outside of it.
+ auto type = event->type();
+ if (type == QEvent::MouseButtonPress && m_field_panel->is_expanded() && !QApplication::activePopupWidget()) {
+ auto global_position = static_cast<QMouseEvent*>(event)->globalPosition().toPoint();
+ auto panel_rect = QRect(m_field_panel->mapToGlobal(QPoint()), m_field_panel->size());
+ if (!panel_rect.contains(global_position) && m_location_edit && m_location_edit->hasFocus())
+ m_location_edit->clearFocus();
+ return false;
+ }
+
+ if (watched == m_location_edit && type == QEvent::DynamicPropertyChange) {
+ if (static_cast<QDynamicPropertyChangeEvent*>(event)->propertyName() == "autocompleteOpen")
+ update_field_panel();
+ return false;
+ }
+
+ auto* widget = qobject_cast<QWidget*>(watched);
+ if (!widget || !(widget == m_field_panel || widget == m_field_slot || widget->isAncestorOf(m_field_slot)))
+ return false;
+ if (first_is_one_of(type, QEvent::Move, QEvent::Resize, QEvent::Show, QEvent::Hide, QEvent::LayoutRequest))
+ update_field_panel();
+ // NB: The panel stays above the sidebar as the sidebar is raised. Its own change of order comes from raising it.
+ else if (type == QEvent::ZOrderChange && widget != m_field_panel && m_field_panel->isVisible())
+ raise_field_panel();
+ return false;
+}
+
void WindowToolbar::set_arrangement(ToolbarArrangement arrangement, int sidebar_inset)
{
if (m_arrangement == arrangement && m_sidebar_inset == sidebar_inset)
@@ -1255,8 +1462,11 @@ void WindowToolbar::set_arrangement(ToolbarArrangement arrangement, int sidebar_
m_sidebar_footer->show();
}
- if (m_location_edit)
+ if (m_location_edit) {
m_location_edit->set_shows_host_only(arrangement == ToolbarArrangement::Sidebar);
+ m_location_edit->set_suggestions_host(arrangement == ToolbarArrangement::Sidebar ? m_suggestions_area : nullptr);
+ }
+ update_field_panel();
setProperty("inSidebar", arrangement == ToolbarArrangement::Sidebar);
style()->unpolish(this);
@@ -1313,13 +1523,21 @@ void WindowToolbar::show_tab(Tab& tab)
void WindowToolbar::show_location_edit(LocationEdit* location_edit)
{
- if (m_location_edit && m_location_edit != location_edit)
+ if (m_location_edit && m_location_edit != location_edit) {
m_location_edit->hide();
+ m_location_edit->set_suggestions_host(nullptr);
+ m_location_edit->removeEventFilter(this);
+ QObject::disconnect(m_location_edit, &LocationEdit::editing_location_changed, this, nullptr);
+ }
m_location_edit = location_edit;
if (location_edit) {
location_edit->set_shows_host_only(m_arrangement == ToolbarArrangement::Sidebar);
+ location_edit->set_suggestions_host(m_arrangement == ToolbarArrangement::Sidebar ? m_suggestions_area : nullptr);
+ QObject::connect(location_edit, &LocationEdit::editing_location_changed, this, &WindowToolbar::update_field_panel, Qt::UniqueConnection);
+ location_edit->installEventFilter(this);
location_edit->show();
}
+ update_field_panel();
}
// A tab that leaves the window takes its address field along.
@@ -1331,8 +1549,12 @@ void WindowToolbar::release_location_edit(Tab& tab)
auto& location_edit = tab.location_edit();
if (m_location_edit_layout->indexOf(&location_edit) == -1)
return;
- if (m_location_edit == &location_edit)
+ if (m_location_edit == &location_edit) {
+ QObject::disconnect(m_location_edit, &LocationEdit::editing_location_changed, this, nullptr);
+ m_location_edit->removeEventFilter(this);
m_location_edit = nullptr;
+ }
+ location_edit.set_suggestions_host(nullptr);
m_location_edit_layout->removeWidget(&location_edit);
location_edit.hide();
location_edit.setParent(&tab);
@@ -1500,6 +1722,11 @@ void WindowToolbar::update_chrome_style()
setStyleSheet(style_sheet);
// NB: The sidebar's footer lies outside the toolbar while the sidebar holds it, so it takes the style sheet itself.
m_sidebar_footer->setStyleSheet(style_sheet);
+ if (m_field_panel) {
+ m_field_panel->setStyleSheet(style_sheet);
+ m_field_panel->round_corners();
+ m_field_panel->update();
+ }
if (m_downloads_popover)
m_downloads_popover->update_chrome_style(palette());
if (m_feedback_popover)
diff --git a/UI/Qt/WindowToolbar.h b/UI/Qt/WindowToolbar.h
index 8283334be19..5d8d64849aa 100644
--- a/UI/Qt/WindowToolbar.h
+++ b/UI/Qt/WindowToolbar.h
@@ -30,6 +30,7 @@ class LocationEdit;
class PerformanceMonitorWidget;
class Popover;
class ReloadButton;
+class SidebarFieldPanel;
class Tab;
enum class ToolbarArrangement : u8 {
@@ -80,6 +81,7 @@ public:
private:
virtual bool event(QEvent*) override;
+ virtual bool eventFilter(QObject*, QEvent*) override;
virtual void tab_settings_changed() override;
virtual void config_variable_changed(WebView::ConfigVariableID) override;
@@ -101,6 +103,10 @@ private:
void move_popover_to_button(Popover&, QWidget& button);
void lay_out_as_row();
void lay_out_in_sidebar();
+ void create_field_panel();
+ bool field_panel_should_expand() const;
+ void update_field_panel();
+ void raise_field_panel();
WebView::IsPrivate m_is_private { WebView::IsPrivate::No };
QPointer<Tab> m_tab;
@@ -112,6 +118,10 @@ private:
QWidget* m_navigation_buttons { nullptr };
QWidget* m_location_edit_container { nullptr };
QWidget* m_sidebar_footer { nullptr };
+ // In the sidebar, the field's container floats in this panel over the field's slot, and grows while it is edited.
+ QPointer<SidebarFieldPanel> m_field_panel;
+ QWidget* m_field_slot { nullptr };
+ QWidget* m_suggestions_area { nullptr };
bool m_window_controls_visible { false };
int m_sidebar_inset { 0 };
// NB: The fields of the tabs shown so far share this layout, which leaves room around them for the focus ring.