Truly independent web browser
UI/Qt: Stylize the autocomplete popup
Timothy Flynn401b10f1c0de
diff --git a/UI/Qt/Autocomplete.cpp b/UI/Qt/Autocomplete.cppindex c596b580be2..b95e77ca79e 100644--- a/UI/Qt/Autocomplete.cpp+++ b/UI/Qt/Autocomplete.cpp@@ -28,6 +28,8 @@#include <QPalette>#include <QPixmap>#include <QPoint>+#include <QStyle>+#include <QStyleOption>#include <QStyledItemDelegate>#include <QTextLayout>#include <QTimer>@@ -56,6 +58,25 @@ enum AutocompleteRole {HighlightInputRole,};+class AutocompletePopup final : public QFrame {+ AK_ALLOC_WITH_KMALLOC;++public:+ using QFrame::QFrame;++protected:+ virtual void paintEvent(QPaintEvent*) override+ {+ // Translucent top-level frames need their styled background painted explicitly.+ QStyleOption option;+ option.initFrom(this);+ QPainter painter(this);+ style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);+ auto separator_color = ChromeStyle::mix(palette().color(QPalette::Window), ChromeStyle::chrome_text(palette()), 0.12);+ painter.fillRect(QRect(CELL_HORIZONTAL_PADDING, 0, width() - CELL_HORIZONTAL_PADDING * 2, 1), separator_color);+ }+};+static QFont autocomplete_primary_font(){QFont font = QApplication::font();@@ -341,14 +362,16 @@ void Autocomplete::create_popup(){// Use a non-activating top-level window so it can stack above native web// content windows without moving keyboard focus away from the address bar.- m_popup = new QFrame(m_anchor->window(), Qt::ToolTip | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);+ m_popup = new AutocompletePopup(m_anchor->window(), Qt::ToolTip | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);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;+ 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.@@ -359,7 +382,6 @@ void Autocomplete::create_popup()m_popup->setFocusPolicy(Qt::NoFocus);m_popup->setFrameShape(QFrame::StyledPanel);m_popup->setFrameShadow(QFrame::Raised);- m_popup->setAutoFillBackground(true);m_popup->hide();m_list_view = new QListView(m_popup);@@ -375,7 +397,7 @@ void Autocomplete::create_popup()update_chrome_style();auto* layout = new QVBoxLayout(m_popup);- layout->setContentsMargins(0, POPUP_PADDING, 0, POPUP_PADDING);+ layout->setContentsMargins(0, POPUP_PADDING, 0, ChromeStyle::LOCATION_EDIT_CORNER_RADIUS);layout->setSpacing(0);layout->addWidget(m_list_view);@@ -389,8 +411,10 @@ void Autocomplete::create_popup()Autocomplete::~Autocomplete(){qApp->removeEventFilter(this);- if (m_popup)+ if (m_popup) {+ disconnect(m_popup, nullptr, this, nullptr);delete m_popup;+ }}void Autocomplete::update_chrome_style()@@ -469,6 +493,16 @@ bool Autocomplete::close()return true;}+void Autocomplete::set_anchor_popup_open(bool open)+{+ if (m_anchor->property("autocompleteOpen").toBool() == open)+ return;+ m_anchor->setProperty("autocompleteOpen", open);+ m_anchor->style()->unpolish(m_anchor);+ m_anchor->style()->polish(m_anchor);+ m_anchor->update();+}+bool Autocomplete::is_visible() const{return m_popup && m_popup->isVisible();@@ -477,6 +511,13 @@ bool Autocomplete::is_visible() constbool 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);++ if (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)))schedule_chrome_style_update();@@ -531,7 +572,7 @@ void Autocomplete::position_popup()int width = std::max(m_anchor->width(), MINIMUM_POPUP_WIDTH);int frame_overhead = m_popup->frameWidth() * 2;- int popup_height = total_height + POPUP_PADDING * 2 + frame_overhead;+ int popup_height = total_height + POPUP_PADDING + ChromeStyle::LOCATION_EDIT_CORNER_RADIUS + frame_overhead;m_list_view->setFixedHeight(total_height);m_popup->setFixedSize(width, popup_height);diff --git a/UI/Qt/Autocomplete.h b/UI/Qt/Autocomplete.hindex 0ba2f80a47f..42b9cd887b9 100644--- a/UI/Qt/Autocomplete.h+++ b/UI/Qt/Autocomplete.h@@ -52,6 +52,7 @@ protected:private:void create_popup();+ void set_anchor_popup_open(bool);bool is_visible() const;void position_popup();bool is_selectable_row(int row) const;diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cppindex 12c601e6d69..14bab4f722d 100644--- a/UI/Qt/ChromeStyle.cpp+++ b/UI/Qt/ChromeStyle.cpp@@ -586,6 +586,18 @@ QWidget#LadybirdHamburgerZoomActions QPushButton#LadybirdHamburgerResetZoomButtotext, background, surface_hover, surface_pressed);}+static QColor autocomplete_surface(QPalette const& palette)+{+ if (is_dark(palette))+ return mix(chrome_background(palette), material_color_anchors(true).recessed, 0.5);+ return chrome_surface(palette);+}++static QColor location_edit_focus_border(QPalette const& palette)+{+ return mix(chrome_border(palette), chrome_accent(), is_dark(palette) ? 0.50 : 0.54);+}+QString location_edit_style_sheet(QPalette const& palette){auto dark = is_dark(palette);@@ -597,11 +609,12 @@ QString location_edit_style_sheet(QPalette const& palette)auto border_color = dark ? mix(chrome_background(palette), chrome_border(palette), 0.36) : chrome_border(palette);auto hover_border_color = border_color;- auto focus_border_color = mix(chrome_border(palette), chrome_accent(), dark ? 0.50 : 0.54);+ auto focus_border_color = location_edit_focus_border(palette);auto surface = style_sheet_color(surface_color);auto hover = style_sheet_color(hover_color);auto focus = style_sheet_color(focus_color);+ auto popup_surface = style_sheet_color(autocomplete_surface(palette));auto border = style_sheet_color(border_color);auto hover_border = style_sheet_color(hover_border_color);auto focus_border = style_sheet_color(focus_border_color);@@ -625,7 +638,7 @@ QLineEdit#LadybirdLocationEdit {{color: {4};background: {0};border: 1px solid {2};- border-radius: 16px;+ border-radius: {20}px;min-height: 32px;padding: 0 16px;selection-background-color: {6};@@ -643,6 +656,14 @@ QLineEdit#LadybirdLocationEdit:focus {{border-color: {3};}}+QLineEdit#LadybirdLocationEdit[autocompleteOpen="true"] {{+ background: {21};+ border-bottom-left-radius: 0;+ border-bottom-right-radius: 0;+ border-bottom: 0;+ padding-bottom: 1px;+}}+QLineEdit#LadybirdLocationEdit:disabled {{color: {5};border-color: {2};@@ -696,7 +717,7 @@ QToolButton#LadybirdLocationAction {{}})",surface, hover, border, focus_border, text, placeholder, selection, selection_text, hover_border,- not_secure_text, not_secure_background, not_secure_hover, not_secure_pressed, not_secure_border, zoom_text, zoom_background, zoom_hover, zoom_pressed, zoom_border, focus);+ not_secure_text, not_secure_background, not_secure_hover, not_secure_pressed, not_secure_border, zoom_text, zoom_background, zoom_hover, zoom_pressed, zoom_border, focus, LOCATION_EDIT_CORNER_RADIUS, popup_surface);}QString bookmarks_bar_style_sheet(QPalette const& palette)@@ -1288,8 +1309,8 @@ QToolButton#LadybirdCloseWindowButton[pressedOutside="true"] {{QString autocomplete_popup_style_sheet(QPalette const& palette){- auto surface = style_sheet_color(chrome_surface(palette));- auto border = style_sheet_color(chrome_border(palette));+ auto surface = style_sheet_color(autocomplete_surface(palette));+ auto border = style_sheet_color(location_edit_focus_border(palette));auto text = style_sheet_color(chrome_text(palette));return qformatted(R"(@@ -1297,7 +1318,9 @@ QFrame#LadybirdAutocompletePopup {{color: {2};background: {0};border: 1px solid {1};- border-radius: 0 0 10px 10px;+ border-top: 0;+ border-bottom-left-radius: {3}px;+ border-bottom-right-radius: {3}px;}}QListView#LadybirdAutocompleteList {{@@ -1307,7 +1330,7 @@ QListView#LadybirdAutocompleteList {{outline: 0;}})",- surface, border, text);+ surface, border, text, LOCATION_EDIT_CORNER_RADIUS);}QString downloads_popover_style_sheet(QPalette const& palette)diff --git a/UI/Qt/ChromeStyle.h b/UI/Qt/ChromeStyle.hindex 313c4bd3f6e..45e1cd2296d 100644--- a/UI/Qt/ChromeStyle.h+++ b/UI/Qt/ChromeStyle.h@@ -15,6 +15,8 @@ class QPalette;namespace Ladybird::ChromeStyle {+constexpr inline int LOCATION_EDIT_CORNER_RADIUS = 16;+bool is_dark(QPalette const&);QColor mix(QColor const& from, QColor const& to, double amount);QColor chrome_text(QPalette const&);diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cppindex edab7effecb..51505a06c10 100644--- a/UI/Qt/LocationEdit.cpp+++ b/UI/Qt/LocationEdit.cpp@@ -556,7 +556,8 @@ void LocationEdit::update_chrome_style()return;m_is_updating_chrome_style = true;- setStyleSheet(ChromeStyle::location_edit_style_sheet(palette()));+ // Match the popup's palette without feeding our styled background into the next style update.+ setStyleSheet(ChromeStyle::location_edit_style_sheet(QApplication::palette()));m_is_updating_chrome_style = false;update_zoom_indicator();}