Skip to content

Truly independent web browser

UI/Qt: Make the address field and its suggestions one surface

Andreas Kling616871b06782
diff --git a/UI/Qt/Autocomplete.cpp b/UI/Qt/Autocomplete.cpp
index 7e2b0bd7c84..9022ec5763d 100644
--- a/UI/Qt/Autocomplete.cpp
+++ b/UI/Qt/Autocomplete.cpp
@@ -8,19 +8,12 @@
#include <AK/AnyOf.h>
#include <AK/GenericShorthands.h>
-#include <AK/Platform.h>
#include <AK/kmalloc.h>
#include <LibWebView/Autocomplete.h>
#include <UI/Qt/Autocomplete.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/Icon.h>
-#if defined(AK_OS_MACOS)
-# include <UI/Qt/MacWindow.h>
-#endif
-#include <UI/Qt/Motion.h>
-#include <UI/Qt/Popover.h>
#include <UI/Qt/StringUtils.h>
-#include <UI/Qt/SystemAppearance.h>
#include <UI/Qt/Theme.h>
#include <QAbstractListModel>
@@ -31,6 +24,7 @@
#include <QFrame>
#include <QIcon>
#include <QKeyEvent>
+#include <QLayout>
#include <QLineEdit>
#include <QListView>
#include <QMouseEvent>
@@ -42,21 +36,13 @@
#include <QStyledItemDelegate>
#include <QTextLayout>
#include <QTimer>
-#include <QVBoxLayout>
-#include <QVariantAnimation>
-#include <QWindow>
namespace Ladybird {
-// The suggestions float in a card a little below the address field, as wide as the field.
-static constexpr int POPUP_ANCHOR_GAP = 4;
-static constexpr int POPUP_PADDING = 6;
-static constexpr int POPUP_CORNER_RADIUS = 12;
-#if !defined(AK_OS_MACOS)
-// The card's shadow is drawn in a window of its own, with this much room around the card.
-// NB: The shadow falls downward, so it needs less room above the card.
-static constexpr QMargins POPUP_SHADOW_MARGINS { 60, 36, 60, 90 };
-#endif
+// The suggestions are listed in the address field's surface, below the field, with this much room around them. The
+// surface also draws the line between the field and the list, which is this tall.
+static constexpr int LIST_PADDING = 6;
+static constexpr int FIELD_DIVIDER_HEIGHT = 1;
static constexpr int ROW_HEIGHT = 34;
static constexpr int ROW_CORNER_RADIUS = 6;
static constexpr int CELL_HORIZONTAL_PADDING = 12;
@@ -66,7 +52,6 @@ static constexpr int CELL_SPACING = 12;
static constexpr int GROUP_SEPARATOR_MARGIN = 5;
static constexpr int GROUP_SEPARATOR_INSET = 8;
static constexpr int GROUP_SEPARATOR_HEIGHT = (2 * GROUP_SEPARATOR_MARGIN) + 1;
-static constexpr int MINIMUM_POPUP_WIDTH = 100;
static constexpr size_t MAXIMUM_VISIBLE_AUTOCOMPLETE_SUGGESTIONS = 8;
@@ -81,26 +66,6 @@ enum AutocompleteRole {
StartsGroupRole,
};
-// The popup window is this card, with transparent corners.
-class AutocompleteCard final : public QFrame {
- AK_ALLOC_WITH_KMALLOC;
-
-public:
- using QFrame::QFrame;
-
-protected:
- virtual void paintEvent(QPaintEvent*) override
- {
- auto opaque_pixels_only = ChromeStyle::shows_opaque_pixels_only(*this);
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing, !opaque_pixels_only);
- auto const& panel = Theme::panel();
- painter.setBrush(panel.surface);
- painter.setPen(QPen(opaque_pixels_only ? ChromeStyle::opaque_panel_edge() : panel.edge, 1));
- painter.drawRoundedRect(QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5), POPUP_CORNER_RADIUS, POPUP_CORNER_RADIUS);
- }
-};
-
static QFont autocomplete_title_font()
{
QFont font = QApplication::font();
@@ -366,65 +331,9 @@ Autocomplete::Autocomplete(QLineEdit* anchor)
{
m_model = new AutocompleteModel(this);
m_delegate = new AutocompleteDelegate(this);
- create_popup();
qApp->installEventFilter(this);
}
-void Autocomplete::create_popup()
-{
- // Use a non-activating top-level widget for its own translucent backing store. Its native window is embedded in the
- // browser window in position_popup(), except on Windows.
- // NB: Without WindowDoesNotAcceptFocus, a click on a suggestion activates the popup's window on Windows, which takes
- // the focus from the location field and closes the popup before the click is released.
- m_popup = new AutocompleteCard(m_anchor->window(), Qt::ToolTip | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::WindowDoesNotAcceptFocus);
- connect(m_popup, &QObject::destroyed, this, [this] {
- // The popup dies with its parent window, which can go away while this object lives on (for
- // example when the tab is moved to another window); it is recreated on the next show.
- m_popup = nullptr;
- m_list_view = nullptr;
- m_opening_animation = nullptr;
- set_anchor_popup_open(false);
- });
- m_popup->setObjectName("LadybirdAutocompletePopup");
- m_popup->setAttribute(Qt::WA_TranslucentBackground);
-#if defined(AK_OS_MACOS)
- // The web content view is a native QRhiWidget on macOS, so the popup must
- // also be native to receive mouse events while overlapping it.
- m_popup->setAttribute(Qt::WA_NativeWindow);
-#endif
- m_popup->setAttribute(Qt::WA_ShowWithoutActivating);
- m_popup->setAttribute(Qt::WA_X11DoNotAcceptFocus);
- m_popup->setFocusPolicy(Qt::NoFocus);
- m_popup->setFrameShape(QFrame::NoFrame);
- m_popup->hide();
-
-#if !defined(AK_OS_MACOS)
- // The card casts the same shadow as popovers. The shadow is a window of its own behind the card, and it lets the
- // mouse through to the page, which a transparent room around the card in the card's window would not.
- // NB: On macOS, Qt sends the mouse movement over a child window to that window even when it is transparent for
- // input, which keeps the page from seeing it. There, the card's own view casts the shadow instead.
- m_shadow = new QWidget(m_anchor->window(), Qt::ToolTip | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::WindowTransparentForInput);
- m_shadow->setAttribute(Qt::WA_TranslucentBackground);
- m_shadow->setAttribute(Qt::WA_ShowWithoutActivating);
- m_shadow->setAttribute(Qt::WA_X11DoNotAcceptFocus);
- m_shadow->setAttribute(Qt::WA_TransparentForMouseEvents);
- auto* shadow_layout = new QVBoxLayout(m_shadow);
- shadow_layout->setContentsMargins(POPUP_SHADOW_MARGINS);
- auto* shadow_caster = new AutocompleteCard(m_shadow);
- cast_popover_shadow(*shadow_caster);
- shadow_layout->addWidget(shadow_caster);
- m_shadow->hide();
-#endif
-
- 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();
-}
-
QListView* Autocomplete::create_list_view(QWidget& parent)
{
auto* list_view = new QListView(&parent);
@@ -447,24 +356,19 @@ QListView* Autocomplete::create_list_view(QWidget& parent)
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();
+ delete m_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);
+ m_list_view = create_list_view(*host);
+ m_list_view->hide();
+ host->layout()->addWidget(m_list_view);
update_chrome_style();
}
if (was_visible)
@@ -474,34 +378,19 @@ void Autocomplete::set_host(QWidget* host)
Autocomplete::~Autocomplete()
{
qApp->removeEventFilter(this);
- if (m_popup) {
- disconnect(m_popup, nullptr, this, nullptr);
- delete m_popup;
- }
- delete m_shadow.data();
- delete m_hosted_list_view.data();
+ delete m_list_view.data();
}
void Autocomplete::update_chrome_style()
{
- if (!m_popup || m_is_updating_chrome_style)
+ if (!m_list_view || m_is_updating_chrome_style)
return;
m_is_updating_chrome_style = true;
auto palette = QApplication::palette();
- m_popup->setPalette(palette);
m_list_view->setPalette(palette);
- m_popup->setStyleSheet(ChromeStyle::autocomplete_popup_style_sheet(palette));
- m_popup->update();
- if (m_shadow)
- m_shadow->update();
- cast_card_shadow();
+ m_list_view->setStyleSheet(ChromeStyle::autocomplete_popup_style_sheet(palette));
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;
}
@@ -519,9 +408,6 @@ void Autocomplete::schedule_chrome_style_update()
void Autocomplete::show_with_suggestions(Vector<WebView::AutocompleteSuggestion> suggestions, Optional<size_t> selected_suggestion_index)
{
- if (!m_popup)
- create_popup();
-
m_model->set_suggestions(move(suggestions));
show_current_suggestions();
set_selected_suggestion(selected_suggestion_index);
@@ -529,59 +415,22 @@ void Autocomplete::show_with_suggestions(Vector<WebView::AutocompleteSuggestion>
void Autocomplete::show_current_suggestions()
{
- if (m_model->rowCount() == 0) {
+ // NB: Only a field with a host, which is the field that is shown, lists suggestions.
+ if (m_model->rowCount() == 0 || !m_list_view) {
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.
- if (!m_opening_animation) {
- m_opening_animation = new QVariantAnimation(m_popup);
- m_opening_animation->setDuration(Motion::FAST);
- m_opening_animation->setEasingCurve(Motion::enter_easing());
- m_opening_animation->setStartValue(0.0);
- m_opening_animation->setEndValue(1.0);
- QObject::connect(m_opening_animation, &QVariantAnimation::valueChanged, m_popup, [this](QVariant const& value) {
- auto progress = value.toReal();
- m_opening_offset = SystemAppearance::prefers_reduced_motion() ? 0 : -4 * (1 - progress);
- set_floating_window_opacity(*m_popup, progress);
- if (m_shadow)
- set_floating_window_opacity(*m_shadow, progress);
- position_popup();
- });
- }
- auto reduce_motion = SystemAppearance::prefers_reduced_motion();
- m_opening_offset = reduce_motion ? 0 : -4;
- set_floating_window_opacity(*m_popup, 0);
- if (m_shadow) {
- set_floating_window_opacity(*m_shadow, 0);
- m_shadow->show();
- }
- m_popup->show();
- position_popup();
- m_opening_animation->stop();
- m_opening_animation->start();
- }
- if (m_shadow)
- m_shadow->raise();
- m_popup->raise();
+ m_list_view->setFixedHeight(list_height());
+ m_list_view->show();
+ set_anchor_popup_open(true);
}
void Autocomplete::set_selected_suggestion(Optional<size_t> suggestion_index)
{
- auto* list_view = this->list_view();
- if (!m_popup || !list_view)
+ if (!m_list_view)
return;
int table_row = suggestion_index.has_value()
@@ -589,29 +438,21 @@ void Autocomplete::set_selected_suggestion(Optional<size_t> suggestion_index)
: -1;
if (table_row == -1) {
- list_view->setCurrentIndex({});
+ m_list_view->setCurrentIndex({});
return;
}
auto index = m_model->index(table_row, 0);
- list_view->setCurrentIndex(index);
- list_view->scrollTo(index);
+ m_list_view->setCurrentIndex(index);
+ m_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())
+ if (!is_visible())
return false;
- m_popup->hide();
- if (m_shadow)
- m_shadow->hide();
+ m_list_view->hide();
+ set_anchor_popup_open(false);
return true;
}
@@ -627,39 +468,27 @@ 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();
+ return m_list_view && !m_list_view->isHidden();
}
bool Autocomplete::eventFilter(QObject* watched, QEvent* event)
{
auto type = event->type();
- if (watched == m_popup && (type == QEvent::Show || type == QEvent::Hide))
- set_anchor_popup_open(type == QEvent::Show);
-
- // Qt still considers the embedded popup a top-level widget. Close it before application shutdown attempts to close
- // its native child window directly.
if ((watched == m_anchor->window() && first_is_one_of(type, QEvent::Hide, QEvent::Resize)) || (watched == qApp && type == QEvent::Quit)) {
if (close())
emit dismissed();
}
- if (!m_host && is_visible() && (watched == m_anchor || watched == m_anchor->window())
- && (type == QEvent::Move || type == QEvent::Resize))
- position_popup();
-
if (type == QEvent::ApplicationPaletteChange || type == QEvent::ThemeChange
- || (type == QEvent::PaletteChange && (watched == m_anchor || watched == m_popup || watched == m_list_view)))
+ || (type == QEvent::PaletteChange && (watched == m_anchor || watched == m_list_view)))
schedule_chrome_style_update();
if (event->type() == QEvent::MouseButtonPress && is_visible()) {
auto* mouse_event = static_cast<QMouseEvent*>(event);
auto global = mouse_event->globalPosition().toPoint();
- auto* popup = m_host ? m_host.data() : static_cast<QWidget*>(m_popup);
- auto popup_global = QRect(popup->mapToGlobal(QPoint(0, 0)), popup->size());
+ auto host_global = QRect(m_host->mapToGlobal(QPoint(0, 0)), m_host->size());
auto anchor_global = QRect(m_anchor->mapToGlobal(QPoint(0, 0)), m_anchor->size());
- if (!popup_global.contains(global) && !anchor_global.contains(global)) {
+ if (!host_global.contains(global) && !anchor_global.contains(global)) {
if (close())
emit dismissed();
}
@@ -673,77 +502,17 @@ int Autocomplete::list_height() const
int total_height = 0;
for (int i = 0; i < visible_count; ++i) {
QStyleOptionViewItem option;
- option.initFrom(list_view());
+ option.initFrom(m_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));
+ auto list_top = m_anchor->mapTo(top_window, QPoint(0, m_anchor->height() + FIELD_DIVIDER_HEIGHT + LIST_PADDING)).y();
+ int available_height = max(0, top_window->height() - list_top - LIST_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;
- for (QWidget* window : { static_cast<QWidget*>(m_shadow.data()), static_cast<QWidget*>(m_popup) }) {
- if (!window)
- continue;
- if (window->parentWidget() != top_window) {
- window->setParent(top_window, window->windowFlags());
- window->setAttribute(Qt::WA_ShowWithoutActivating);
- window->setAttribute(Qt::WA_X11DoNotAcceptFocus);
- }
-
-#if !defined(AK_OS_WINDOWS)
- // Embed the native window so it stacks above native web content without being animated independently of the
- // location field by the window system.
- // NB: Windows keeps a tooltip's native window a popup even when it has a parent, and such a window is activated
- // as it is embedded, taking the keyboard focus from the location field. There, the popup stays a window of
- // its own, which the browser window owns and so keeps above its native web content.
- (void)window->winId();
- window->windowHandle()->setParent(top_window->windowHandle());
-#endif
- }
-
- // The card is as wide as the field and sits a little below it.
- 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 list_height = this->list_height();
-
- m_list_view->setFixedHeight(list_height);
- m_popup->setFixedSize(card_width, list_height + vertical_overhead);
-
- auto card = QRect(card_position, m_popup->size());
-#if defined(AK_OS_WINDOWS)
- card.translate(top_window->mapToGlobal(QPoint(0, 0)));
-#endif
-#if !defined(AK_OS_MACOS)
- m_shadow->setGeometry(card.marginsAdded(POPUP_SHADOW_MARGINS));
-#endif
- m_popup->setGeometry(card);
- cast_card_shadow();
- if (m_shadow)
- m_shadow->raise();
- m_popup->raise();
-}
-
-void Autocomplete::cast_card_shadow()
-{
-#if defined(AK_OS_MACOS)
- // NB: This is the shadow of cast_popover_shadow(), which Core Animation draws outside of the view, where it takes
- // no mouse input.
- cast_appkit_view_shadow(*m_popup, POPUP_CORNER_RADIUS, 15, 14, QColor(0, 0, 0, Theme::is_dark() ? 115 : 41));
-#endif
-}
-
bool Autocomplete::is_selectable_row(int row) const
{
return row >= 0 && row < m_model->rowCount();
diff --git a/UI/Qt/Autocomplete.h b/UI/Qt/Autocomplete.h
index ebeac726486..d96d3e27b1d 100644
--- a/UI/Qt/Autocomplete.h
+++ b/UI/Qt/Autocomplete.h
@@ -16,19 +16,17 @@
#include <QObject>
#include <QPointer>
-class QFrame;
class QLineEdit;
class QListView;
-class QVariantAnimation;
namespace Ladybird {
class AutocompleteModel;
class AutocompleteDelegate;
-// The suggestion popup below the location bar. This is a pure view: LocationEdit feeds it rows and a
-// selection from the WebView::Omnibox model, and it reports clicks and outside-click
-// dismissals back.
+// The suggestions for the address field, listed below the field in its surface, which hosts the list. This is a pure
+// view: LocationEdit feeds it rows and a selection from the WebView::Omnibox model, and it reports clicks and
+// outside-click dismissals back.
class Autocomplete final : public QObject {
Q_OBJECT
@@ -41,7 +39,7 @@ 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.
+ // The suggestions are listed at the end of the host's layout. Without a host, there are none to see.
void set_host(QWidget* host);
void show_with_suggestions(Vector<WebView::AutocompleteSuggestion>, Optional<size_t> selected_suggestion_index);
@@ -56,31 +54,20 @@ protected:
virtual bool eventFilter(QObject* watched, QEvent* event) override;
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();
- void cast_card_shadow();
bool is_selectable_row(int row) const;
QLineEdit* m_anchor { nullptr };
- QFrame* m_popup { nullptr };
- // 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;
+ QPointer<QListView> m_list_view;
AutocompleteModel* m_model { nullptr };
AutocompleteDelegate* m_delegate { nullptr };
bool m_is_updating_chrome_style { false };
bool m_has_pending_chrome_style_update { false };
- // The panel slides down this far into place as it opens.
- qreal m_opening_offset { 0 };
- QVariantAnimation* m_opening_animation { nullptr };
};
}
diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cpp
index 5b8bc558686..ff3087a0b04 100644
--- a/UI/Qt/ChromeStyle.cpp
+++ b/UI/Qt/ChromeStyle.cpp
@@ -526,6 +526,10 @@ QLineEdit#LadybirdLocationEdit:focus {{
border-color: {3};
}}
+QLineEdit#LadybirdLocationEdit[autocompleteOpen="true"] {{
+ border-color: {17};
+}}
+
QLineEdit#LadybirdLocationEdit:disabled {{
color: {5};
border-color: {2};
diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cpp
index 522c1daba8f..0a6b475c336 100644
--- a/UI/Qt/LocationEdit.cpp
+++ b/UI/Qt/LocationEdit.cpp
@@ -23,6 +23,7 @@
#include <QAction>
#include <QApplication>
#include <QClipboard>
+#include <QDynamicPropertyChangeEvent>
#include <QEasingCurve>
#include <QGraphicsEffect>
#include <QGuiApplication>
@@ -572,7 +573,7 @@ void LocationEdit::changeEvent(QEvent* event)
if (!isActiveWindow())
update_focus_ring(0);
else if (m_is_editing_location && hasFocus())
- update_focus_ring(1);
+ update_focus_ring(focus_ring_opacity_while_editing());
}
// NB: Repolishing for the hover look changes our palette to the hovered colors, which needs no new style.
if (event->type() == QEvent::PaletteChange && m_is_updating_field_hover)
@@ -588,6 +589,8 @@ bool LocationEdit::event(QEvent* event)
set_field_hovered(true);
else if (event->type() == QEvent::Leave)
set_field_hovered(false);
+ else if (event->type() == QEvent::DynamicPropertyChange && static_cast<QDynamicPropertyChangeEvent*>(event)->propertyName() == "autocompleteOpen" && m_is_editing_location)
+ show_focus_ring_at_once();
return QLineEdit::event(event);
}
@@ -792,13 +795,23 @@ void LocationEdit::show_focus_ring()
emit editing_location_changed();
}
update_trailing_action_visibility();
+ animate_focus_ring();
+}
+// While the field lists suggestions, it is part of their surface, and the ring goes around the whole surface instead.
+qreal LocationEdit::focus_ring_opacity_while_editing() const
+{
+ return property("autocompleteOpen").toBool() ? 0 : 1;
+}
+
+void LocationEdit::animate_focus_ring()
+{
if (!m_focus_ring_animation)
return;
m_focus_ring_animation->stop();
m_focus_ring_animation->setStartValue(m_focus_ring_opacity);
- m_focus_ring_animation->setEndValue(1.0);
+ m_focus_ring_animation->setEndValue(focus_ring_opacity_while_editing());
m_focus_ring_animation->start();
}
@@ -808,7 +821,7 @@ void LocationEdit::show_focus_ring_at_once()
return;
if (m_focus_ring_animation)
m_focus_ring_animation->stop();
- update_focus_ring(1);
+ update_focus_ring(focus_ring_opacity_while_editing());
}
void LocationEdit::hide_focus_ring()
diff --git a/UI/Qt/LocationEdit.h b/UI/Qt/LocationEdit.h
index 530d27858e6..2b7f0660715 100644
--- a/UI/Qt/LocationEdit.h
+++ b/UI/Qt/LocationEdit.h
@@ -120,6 +120,8 @@ private:
void schedule_chrome_style_update();
void show_focus_ring();
void hide_focus_ring();
+ qreal focus_ring_opacity_while_editing() const;
+ void animate_focus_ring();
void update_trailing_action_visibility();
void slide_site_settings_knobs(qreal travel);
void update_completion_title();
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index 27d151a7c4c..9bb19737424 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -13,9 +13,9 @@
#include <LibGfx/Forward.h>
#include <LibWebView/Forward.h>
+#include <QColor>
#include <Qt>
-class QColor;
class QDrag;
class QMargins;
class QWheelEvent;
@@ -54,6 +54,12 @@ void set_appkit_view_opacity(QWidget&, qreal opacity);
void round_appkit_view_bottom_corners(QWidget&, qreal radius);
// Casts a shadow around a native view. Being outside of the view, the shadow takes no mouse input.
void cast_appkit_view_shadow(QWidget&, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const&);
+// The focus ring around a focused surface: a line right outside its edge, inside a wider, softer ring.
+struct AppKitViewFocusRing {
+ QColor edge_color;
+ QColor ring_color;
+ qreal ring_width { 0 };
+};
// A shadow right below a native view, in a plain view of its own. It is no Qt window, as Qt on macOS takes a child
// window to be under the mouse even when it is transparent for input, and keeps the mouse from what is below it.
class AppKitViewShadow {
@@ -65,13 +71,16 @@ public:
~AppKitViewShadow();
// The shadow is clipped to the given room around the view.
- void follow(QWidget&, QMargins const& room, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const& shadow_color, QColor const& fill_color);
+ // The shadow view also draws the focus ring, if any, as it lies outside the view too.
+ void follow(QWidget&, QMargins const& room, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const& shadow_color, QColor const& fill_color, Optional<AppKitViewFocusRing> const& = {});
void hide();
void set_opacity(qreal);
private:
void* m_view { nullptr };
void* m_caster { nullptr };
+ void* m_focus_edge { nullptr };
+ void* m_focus_ring { nullptr };
};
bool start_appkit_window_drag(QWidget&);
Qt::DropAction execute_appkit_tab_drag(QWidget&, QDrag&);
diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mm
index 3bc5dd64bcc..599501eedd9 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -696,8 +696,17 @@ AppKitViewShadow::AppKitViewShadow()
view.hidden = YES;
auto* caster = [[CALayer alloc] init];
[view.layer addSublayer:caster];
+ auto* focus_edge = [[CALayer alloc] init];
+ auto* focus_ring = [[CALayer alloc] init];
+ for (CALayer* layer in @[ focus_edge, focus_ring ]) {
+ layer.cornerCurve = kCACornerCurveContinuous;
+ layer.hidden = YES;
+ [view.layer addSublayer:layer];
+ }
m_view = view;
m_caster = caster;
+ m_focus_edge = focus_edge;
+ m_focus_ring = focus_ring;
}
AppKitViewShadow::~AppKitViewShadow()
@@ -706,9 +715,11 @@ AppKitViewShadow::~AppKitViewShadow()
[view removeFromSuperview];
[view release];
[static_cast<CALayer*>(m_caster) release];
+ [static_cast<CALayer*>(m_focus_edge) release];
+ [static_cast<CALayer*>(m_focus_ring) release];
}
-void AppKitViewShadow::follow(QWidget& widget, QMargins const& room, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const& shadow_color, QColor const& fill_color)
+void AppKitViewShadow::follow(QWidget& widget, QMargins const& room, qreal corner_radius, qreal blur_radius, qreal y_offset, QColor const& shadow_color, QColor const& fill_color, Optional<AppKitViewFocusRing> const& focus_ring)
{
if (QGuiApplication::platformName() != "cocoa")
return;
@@ -745,6 +756,24 @@ void AppKitViewShadow::follow(QWidget& widget, QMargins const& room, qreal corne
caster.shadowPath = shadow_path;
CGPathRelease(shadow_path);
+ // NB: Both parts of the ring are borders, around the surface that covers what is inside them.
+ auto* focus_edge = static_cast<CALayer*>(m_focus_edge);
+ auto* focus_ring_layer = static_cast<CALayer*>(m_focus_ring);
+ focus_edge.hidden = !focus_ring.has_value();
+ focus_ring_layer.hidden = !focus_ring.has_value();
+ if (focus_ring.has_value()) {
+ auto set_ring_part = [&](CALayer* layer, qreal outset, qreal width, QColor const& color) {
+ layer.frame = CGRectInset(caster_frame, -outset, -outset);
+ layer.cornerRadius = corner_radius + outset;
+ layer.borderWidth = width;
+ auto* border_color = CGColorCreateSRGB(color.redF(), color.greenF(), color.blueF(), color.alphaF());
+ layer.borderColor = border_color;
+ CGColorRelease(border_color);
+ };
+ set_ring_part(focus_edge, 1, 1, focus_ring->edge_color);
+ set_ring_part(focus_ring_layer, 1 + focus_ring->ring_width, focus_ring->ring_width, focus_ring->ring_color);
+ }
+
[CATransaction commit];
}
diff --git a/UI/Qt/Popover.cpp b/UI/Qt/Popover.cpp
index 0418df34a41..0b1668e2bef 100644
--- a/UI/Qt/Popover.cpp
+++ b/UI/Qt/Popover.cpp
@@ -594,10 +594,10 @@ public:
, m_margins(kind == FloatingShadowKind::Sheet
? QMargins(SHEET_SHADOW_MARGIN, 0, SHEET_SHADOW_MARGIN, SHEET_SHADOW_MARGIN)
: POPOVER_SHADOW_MARGINS)
-#if defined(AK_OS_MACOS)
, m_corner_radius(corner_radius)
- , m_tone(tone)
, m_kind(kind)
+#if defined(AK_OS_MACOS)
+ , m_tone(tone)
#endif
{
#if !defined(AK_OS_MACOS)
@@ -620,6 +620,26 @@ public:
#endif
private:
+ // The focus ring's line right outside the surface's edge is inside a softer ring this wide.
+ static constexpr qreal FOCUS_RING_WIDTH = 2;
+
+#if !defined(AK_OS_MACOS)
+ virtual void paintEvent(QPaintEvent*) override
+ {
+ if (m_kind != FloatingShadowKind::FocusedField)
+ return;
+ QPainter painter(this);
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setBrush(Qt::NoBrush);
+ auto surface_rect = QRectF(rect().marginsRemoved(m_margins));
+ auto ring_outset = 1 + (FOCUS_RING_WIDTH / 2);
+ painter.setPen(QPen(Theme::focus_ring(), FOCUS_RING_WIDTH));
+ painter.drawRoundedRect(surface_rect.adjusted(-ring_outset, -ring_outset, ring_outset, ring_outset), m_corner_radius + ring_outset, m_corner_radius + ring_outset);
+ painter.setPen(QPen(Theme::focus_edge(), 1));
+ painter.drawRoundedRect(surface_rect.adjusted(-0.5, -0.5, 0.5, 0.5), m_corner_radius + 0.5, m_corner_radius + 0.5);
+ }
+#endif
+
virtual bool eventFilter(QObject*, QEvent* event) override
{
switch (event->type()) {
@@ -677,6 +697,8 @@ private:
auto fill_color = m_tone == FloatingShadowTone::Dark ? QColor(28, 28, 30) : Theme::panel().surface;
if (m_kind == FloatingShadowKind::Sheet)
m_view_shadow.follow(*m_surface, m_margins, m_corner_radius, 17, 18, QColor(0, 0, 0, is_dark ? 140 : 56), fill_color);
+ else if (m_kind == FloatingShadowKind::FocusedField)
+ m_view_shadow.follow(*m_surface, m_margins, m_corner_radius, 15, 14, QColor(0, 0, 0, is_dark ? 115 : 41), fill_color, AppKitViewFocusRing { Theme::focus_edge(), Theme::focus_ring(), FOCUS_RING_WIDTH });
else
m_view_shadow.follow(*m_surface, m_margins, m_corner_radius, 15, 14, QColor(0, 0, 0, is_dark ? 115 : 41), fill_color);
#else
@@ -691,10 +713,10 @@ private:
QPointer<QWidget> m_surface;
QMargins m_margins;
bool m_is_following { false };
-#if defined(AK_OS_MACOS)
qreal m_corner_radius { 0 };
- FloatingShadowTone m_tone { FloatingShadowTone::FollowTheme };
FloatingShadowKind m_kind { FloatingShadowKind::Popover };
+#if defined(AK_OS_MACOS)
+ FloatingShadowTone m_tone { FloatingShadowTone::FollowTheme };
AppKitViewShadow m_view_shadow;
#endif
};
diff --git a/UI/Qt/Popover.h b/UI/Qt/Popover.h
index 923ca092bbb..c94580d5821 100644
--- a/UI/Qt/Popover.h
+++ b/UI/Qt/Popover.h
@@ -148,6 +148,8 @@ enum class FloatingShadowKind : u8 {
Popover,
// A sheet hangs from the top of the page, and casts the deeper shadow of a dialog below it only.
Sheet,
+ // A focused field that grows into a popover keeps its focus ring around the whole surface.
+ FocusedField,
};
QWidget& cast_floating_shadow(QWidget& surface, qreal corner_radius, FloatingShadowTone = FloatingShadowTone::FollowTheme, FloatingShadowKind = FloatingShadowKind::Popover);
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index d8ac923642e..a64153a096b 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -3725,6 +3725,7 @@ void TabWidget::slide_page_with_toggling_sidebar()
if (!slides_page_with_toggling_sidebar()) {
m_toolbar->set_leading_room(0);
m_toolbar->set_contents_opacity(1);
+ m_toolbar->set_field_clip({});
if (m_toolbar_row_backdrop)
m_toolbar_row_backdrop->hide();
return;
@@ -3789,6 +3790,8 @@ void TabWidget::move_toolbar_with_toggling_sidebar()
// NB: The row's contents stay where they are beside the collapsed sidebar, whose buttons are in the sidebar now.
m_toolbar->set_leading_room(m_toolbar->lead_beside_collapsed_sidebar(*m_ladybird_buttons, COLLAPSED_VERTICAL_TABS_TOGGLE_BUTTON_SIZE));
m_toolbar->set_contents_opacity(clamp(1 - (static_cast<qreal>(opening_time) / TOOLBAR_ROW_FADE_DURATION), 0.0, 1.0));
+ // NB: The field's panel is above the page, which slides up over the row.
+ m_toolbar->set_field_clip(QRect(mapTo(window(), QPoint()), QSize(width(), row_height)));
if (m_toolbar_row_backdrop)
m_toolbar_row_backdrop->hide();
return;
@@ -3806,6 +3809,7 @@ void TabWidget::move_toolbar_with_toggling_sidebar()
footer.setGeometry(side_margin, footer_bottom - footer_height, contents_width, footer_height);
auto fade_in_start = Motion::SLOW - SIDEBAR_CONTENTS_FADE_DURATION;
m_toolbar->set_contents_opacity(clamp(static_cast<qreal>(opening_time - fade_in_start) / SIDEBAR_CONTENTS_FADE_DURATION, 0.0, 1.0));
+ m_toolbar->set_field_clip({});
// The row that the toolbar has left shrinks above the page.
if (!m_toolbar_row_backdrop) {
diff --git a/UI/Qt/WindowToolbar.cpp b/UI/Qt/WindowToolbar.cpp
index 7220f5af67d..3f7cf9ad1de 100644
--- a/UI/Qt/WindowToolbar.cpp
+++ b/UI/Qt/WindowToolbar.cpp
@@ -32,6 +32,7 @@
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/SystemAppearance.h>
#include <UI/Qt/Theme.h>
+#include <UI/Qt/WebContentView.h>
#include <UI/Qt/WindowControlButton.h>
#include <UI/Qt/WindowToolbar.h>
@@ -629,24 +630,30 @@ static constexpr int SIDEBAR_FIELD_BOTTOM_MARGIN = 8;
static constexpr int SIDEBAR_FOOTER_HEIGHT = 40;
// NB: The container draws the line under the toolbar along its bottom edge, below the toolbar's full height.
static constexpr int TOOLBAR_BOTTOM_LINE_HEIGHT = 1;
-// 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.
+// The address field and its suggestions are one surface. While it lists suggestions, the field grows downward into it,
+// as wide as the field in a toolbar row, but no narrower than the minimum, and this wide over the page in the sidebar. It
+// stays this far from the window's edge.
static constexpr int FIELD_PANEL_WIDTH = 600;
+static constexpr int FIELD_PANEL_MINIMUM_WIDTH = 100;
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 {
+// A line inset this far from the surface's sides sets the suggestions apart from the field, and the list has this much
+// room around it.
+static constexpr int FIELD_PANEL_DIVIDER_INSET = 8;
+static constexpr int FIELD_PANEL_LIST_PADDING = 6;
+
+// The address field floats over its place in the toolbar, in a panel that is a native child of the window wherever the
+// page is native. 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 FieldPanel final : public QWidget {
public:
AK_ALLOC_WITH_KMALLOC;
- explicit SidebarFieldPanel(QWidget& window)
+ explicit FieldPanel(QWidget& window)
: QWidget(&window)
{
- setObjectName("LadybirdSidebarFieldPanel");
-#if defined(AK_OS_MACOS)
+ setObjectName("LadybirdFieldPanel");
+#if defined(AK_OS_MACOS) || defined(LADYBIRD_QT_USE_RHI_WIDGET) || defined(LADYBIRD_QT_USE_VULKAN_WINDOW)
+ // The web view is native on these presentation paths, so the panel that grows over it must also be native.
setAttribute(Qt::WA_DontCreateNativeAncestors);
setAttribute(Qt::WA_NativeWindow);
#endif
@@ -658,20 +665,30 @@ public:
if (m_expanded == expanded)
return;
m_expanded = expanded;
- // The grown panel casts the shadow of a popover.
+ // The surface casts the shadow of a popover, and the field's focus ring goes around all of it.
if (expanded)
- m_shadow = &cast_floating_shadow(*this, FIELD_PANEL_CORNER_RADIUS);
+ m_shadow = &cast_floating_shadow(*this, ChromeStyle::LOCATION_EDIT_CORNER_RADIUS, FloatingShadowTone::FollowTheme, FloatingShadowKind::FocusedField);
else
delete m_shadow.data();
round_corners();
update();
}
+ void set_in_sidebar(bool in_sidebar)
+ {
+ if (m_in_sidebar == in_sidebar)
+ return;
+ m_in_sidebar = in_sidebar;
+ update();
+ }
+
+ void set_suggestions_area(QWidget& suggestions_area) { m_suggestions_area = &suggestions_area; }
+
void round_corners()
{
#if defined(AK_OS_MACOS)
if (m_expanded)
- round_appkit_view_corners(*this, FIELD_PANEL_CORNER_RADIUS, Theme::panel().edge);
+ round_appkit_view_corners(*this, ChromeStyle::LOCATION_EDIT_CORNER_RADIUS, Qt::transparent);
else
round_appkit_view_corners(*this, 0, Qt::transparent);
#endif
@@ -681,19 +698,31 @@ private:
virtual void paintEvent(QPaintEvent*) override
{
QPainter painter(this);
- // At rest, the panel is the sidebar under the field.
+ // At rest, the panel is the toolbar or the sidebar under the field.
+ auto const& chrome = Theme::chrome(*this);
if (!m_expanded) {
- painter.fillRect(rect(), Theme::chrome(*this).sidebar);
+ painter.fillRect(rect(), m_in_sidebar ? chrome.sidebar : chrome.toolbar);
return;
}
+#if defined(AK_OS_MACOS)
+ // NB: The view rounds its corners itself (see round_corners()). Every pixel is painted, as one that isn't shows
+ // whatever was last drawn behind the native view.
+ painter.fillRect(rect(), chrome.field_focused);
+#else
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);
+ painter.setPen(Qt::NoPen);
+ painter.setBrush(chrome.field_focused);
+ auto radius = static_cast<qreal>(ChromeStyle::LOCATION_EDIT_CORNER_RADIUS);
+ painter.drawRoundedRect(QRectF(rect()), radius, radius);
+#endif
+ if (m_suggestions_area)
+ painter.fillRect(QRect(FIELD_PANEL_DIVIDER_INSET, m_suggestions_area->y(), width() - (2 * FIELD_PANEL_DIVIDER_INSET), 1), Theme::panel().separator_soft);
}
bool m_expanded { false };
+ bool m_in_sidebar { false };
QPointer<QWidget> m_shadow;
+ QPointer<QWidget> m_suggestions_area;
};
// NB: Window controls run to the window's edge. Without them, the address field ends the toolbar, and the room for its
@@ -1245,12 +1274,13 @@ WindowToolbar::WindowToolbar(WebView::IsPrivate is_private, QWidget* parent)
}
// 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.
+// as it is in a row. A window that was never shown deletes the panel first, and the container with it.
WindowToolbar::~WindowToolbar()
{
qApp->removeEventFilter(this);
- if (m_location_edit_container->parentWidget() != m_toolbar)
- m_location_edit_container->setParent(m_toolbar);
+ if (!m_field_panel)
+ return;
+ m_location_edit_container->setParent(m_toolbar);
delete m_field_panel.data();
}
@@ -1264,7 +1294,7 @@ void WindowToolbar::lay_out_as_row()
toolbar_layout->addWidget(m_window_controls, 0, Qt::AlignVCenter);
toolbar_layout->addWidget(m_sidebar_slot, 0, Qt::AlignVCenter);
toolbar_layout->addWidget(m_navigation_buttons, 0, Qt::AlignVCenter);
- toolbar_layout->addWidget(m_location_edit_container, 1);
+ toolbar_layout->addWidget(&field_slot(), 1);
toolbar_layout->addWidget(m_downloads_button, 0, Qt::AlignVCenter);
if (m_feedback_button)
toolbar_layout->addWidget(m_feedback_button, 0, Qt::AlignVCenter);
@@ -1274,11 +1304,7 @@ 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);
+ host_field_in_panel();
layout()->setContentsMargins(0, 0, 0, TOOLBAR_BOTTOM_LINE_HEIGHT);
}
@@ -1301,18 +1327,9 @@ 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);
- if (!m_field_panel)
- create_field_panel();
- field_row->addWidget(m_field_slot, 1);
+ field_row->addWidget(&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);
+ host_field_in_panel();
// 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();
@@ -1329,32 +1346,62 @@ void WindowToolbar::lay_out_in_sidebar()
layout()->setContentsMargins(0, 0, 0, 0);
}
+QWidget& WindowToolbar::field_slot()
+{
+ if (!m_field_panel)
+ create_field_panel();
+ return *m_field_slot;
+}
+
+// The field leaves room for its focus ring around it in its panel, except where the toolbar's margin already does.
+QMargins WindowToolbar::field_margins_at_rest() const
+{
+ auto ring_room = TOOLBAR_LOCATION_FOCUS_RING_ROOM;
+ if (m_arrangement == ToolbarArrangement::Sidebar)
+ return { ring_room, ring_room, ring_room, ring_room };
+ return { TOOLBAR_LOCATION_EDIT_MARGIN, ring_room, ring_room, ring_room };
+}
+
+void WindowToolbar::host_field_in_panel()
+{
+ m_location_edit_layout->setContentsMargins(field_margins_at_rest());
+ 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);
+}
+
void WindowToolbar::create_field_panel()
{
- m_field_panel = new SidebarFieldPanel(*window());
+ m_field_panel = new FieldPanel(*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.
+ // The suggestions are below the line under the field.
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);
+ auto padding = FIELD_PANEL_LIST_PADDING;
+ suggestions_layout->setContentsMargins(padding, 1 + padding, padding, padding);
suggestions_layout->setSpacing(0);
panel_layout->addWidget(m_suggestions_area);
m_suggestions_area->hide();
+ m_field_panel->set_suggestions_area(*m_suggestions_area);
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.
+// The field grows into the panel while it lists suggestions. Without any, it stays where it is.
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()
+ return m_location_edit && m_location_edit->is_editing_location() && m_location_edit->isVisible()
&& m_location_edit->property("autocompleteOpen").toBool();
}
@@ -1380,6 +1427,8 @@ void WindowToolbar::update_field_panel()
if (expanded != m_field_panel->is_expanded()) {
m_field_panel->set_expanded(expanded);
m_suggestions_area->setVisible(expanded);
+ // NB: The grown panel is the field's surface, so the field fills its top row, where it was.
+ m_location_edit_layout->setContentsMargins(expanded ? QMargins() : field_margins_at_rest());
// A click outside the grown panel shrinks it back.
if (expanded)
qApp->installEventFilter(this);
@@ -1387,13 +1436,12 @@ void WindowToolbar::update_field_panel()
qApp->removeEventFilter(this);
}
- if (m_arrangement != ToolbarArrangement::Sidebar) {
- m_field_panel->hide();
- return;
- }
+ m_field_panel->set_in_sidebar(m_arrangement == ToolbarArrangement::Sidebar);
- // NB: The slot keeps the field's place in the sidebar.
- m_field_slot->setFixedHeight(m_location_edit_container->sizeHint().height());
+ // NB: The slot keeps the field's place in the toolbar.
+ auto margins_at_rest = field_margins_at_rest();
+ auto field_height = m_location_edit_container->sizeHint().height() - m_location_edit_layout->contentsMargins().top() - m_location_edit_layout->contentsMargins().bottom();
+ m_field_slot->setFixedHeight(field_height + margins_at_rest.top() + margins_at_rest.bottom());
if (!m_field_slot->isVisible()) {
m_field_panel->hide();
return;
@@ -1408,9 +1456,16 @@ void WindowToolbar::update_field_panel()
for (QWidget* widget = m_field_slot; widget->parentWidget() && widget->parentWidget() != &window; widget = widget->parentWidget())
visible_rect = visible_rect.translated(widget->pos()) & QRect(QPoint(), widget->parentWidget()->size());
size.setWidth(max(0, min(size.width(), visible_rect.width())));
+ if (m_field_clip.has_value() && !expanded) {
+ auto clipped = QRect(position, size) & *m_field_clip;
+ size.setHeight(max(0, clipped.bottom() + 1 - position.y()));
+ }
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()));
+ auto field_rect = QRect(position, m_field_slot->size()).marginsRemoved(margins_at_rest);
+ position = field_rect.topLeft();
+ auto wanted_width = m_arrangement == ToolbarArrangement::Sidebar ? FIELD_PANEL_WIDTH : FIELD_PANEL_MINIMUM_WIDTH;
+ auto width = max(field_rect.width(), min(wanted_width, window.width() - position.x() - FIELD_PANEL_WINDOW_MARGIN));
+ size = QSize(width, min(m_field_panel->sizeHint().height(), window.height() - position.y()));
}
m_field_panel->setGeometry(QRect(position, size));
m_field_panel->show();
@@ -1476,7 +1531,7 @@ void WindowToolbar::set_arrangement(ToolbarArrangement arrangement, int sidebar_
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);
+ m_location_edit->set_suggestions_host(m_suggestions_area);
}
update_field_panel();
apply_contents_opacity();
@@ -1528,9 +1583,17 @@ void WindowToolbar::apply_contents_opacity()
};
fade(*m_toolbar, m_contents_opacity);
fade(*m_sidebar_footer, m_contents_opacity);
- // NB: In the sidebar, the field is in its panel rather than in the toolbar. The panel at rest looks like the sidebar
- // under it, so only the field fades.
- fade(*m_location_edit_container, m_arrangement == ToolbarArrangement::Sidebar ? m_contents_opacity : 1);
+ // NB: The field is in its panel rather than in the toolbar. The panel at rest looks like the toolbar under it, so only
+ // the field fades.
+ fade(*m_location_edit_container, m_contents_opacity);
+}
+
+void WindowToolbar::set_field_clip(Optional<QRect> clip)
+{
+ if (m_field_clip == clip)
+ return;
+ m_field_clip = clip;
+ update_field_panel();
}
int WindowToolbar::toolbar_height(bool)
@@ -1590,7 +1653,7 @@ void WindowToolbar::show_location_edit(LocationEdit* location_edit)
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);
+ location_edit->set_suggestions_host(m_suggestions_area);
QObject::connect(location_edit, &LocationEdit::editing_location_changed, this, &WindowToolbar::update_field_panel, Qt::UniqueConnection);
location_edit->installEventFilter(this);
location_edit->show();
diff --git a/UI/Qt/WindowToolbar.h b/UI/Qt/WindowToolbar.h
index 3fd30bfb5f2..458d931e73e 100644
--- a/UI/Qt/WindowToolbar.h
+++ b/UI/Qt/WindowToolbar.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Optional.h>
#include <AK/kmalloc.h>
#include <LibWebView/FileDownloader.h>
#include <LibWebView/Forward.h>
@@ -13,6 +14,7 @@
#include <UI/Qt/Menu.h>
#include <QPointer>
+#include <QRect>
#include <QWidget>
class QAction;
@@ -30,7 +32,7 @@ class LocationEdit;
class PerformanceMonitorWidget;
class Popover;
class ReloadButton;
-class SidebarFieldPanel;
+class FieldPanel;
class Tab;
enum class ToolbarArrangement : u8 {
@@ -79,6 +81,9 @@ public:
int height_in_sidebar() const;
// While the sidebar opens or closes, the toolbar's contents fade out of one arrangement and into the other.
void set_contents_opacity(qreal);
+ // While the sidebar opens or closes, the field's panel shows only within this part of the window, as the page and the
+ // sidebar slide under it.
+ void set_field_clip(Optional<QRect>);
void show_downloads_popover();
void update_downloads_button();
@@ -109,6 +114,9 @@ private:
void lay_out_as_row();
void lay_out_in_sidebar();
void create_field_panel();
+ QWidget& field_slot();
+ QMargins field_margins_at_rest() const;
+ void host_field_in_panel();
bool field_panel_should_expand() const;
void update_field_panel();
void raise_field_panel();
@@ -124,13 +132,14 @@ 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;
+ // The field's container floats in this panel over the field's slot, and grows while it lists suggestions.
+ QPointer<FieldPanel> m_field_panel;
QWidget* m_field_slot { nullptr };
QWidget* m_suggestions_area { nullptr };
bool m_window_controls_visible { false };
int m_sidebar_inset { 0 };
qreal m_contents_opacity { 1 };
+ Optional<QRect> m_field_clip;
// NB: The fields of the tabs shown so far share this layout, which leaves room around them for the focus ring.
QHBoxLayout* m_location_edit_layout { nullptr };
QPointer<LocationEdit> m_location_edit;