Truly independent web browser
UI/Qt: Make favicons and titles in tabs and bookmarks match
Andreas Kling32dd6b42552e
diff --git a/UI/Qt/BookmarksBar.cpp b/UI/Qt/BookmarksBar.cppindex 8ec2dfd41f4..5a809a9d4b4 100644--- a/UI/Qt/BookmarksBar.cpp+++ b/UI/Qt/BookmarksBar.cpp@@ -38,6 +38,11 @@ static constexpr int BOOKMARK_BUTTON_TEXT_ELISION_PADDING = 2;static constexpr char const* BOOKMARK_ITEM_PROPERTY = "bookmark_item";static constexpr char const* BOOKMARK_CONTEXT_MENU_OPEN_PROPERTY = "bookmark_context_menu_open";+static QFont bookmark_title_font(QToolButton const& button)+{+ return ChromeStyle::chrome_title_font(button.font());+}+static int bookmark_button_height(QFontMetrics const& font_metrics, int icon_height){return max(BOOKMARK_BUTTON_MIN_HEIGHT, max(font_metrics.height(), icon_height) + BOOKMARK_BUTTON_VERTICAL_PADDING);@@ -125,7 +130,7 @@ static BookmarkButtonLayout bookmark_button_layout(QToolButton const& button, QSauto preferred_width = (BOOKMARK_BUTTON_HORIZONTAL_PADDING * 2)+ layout.icon_width+ layout.icon_text_spacing- + button.fontMetrics().horizontalAdvance(text)+ + QFontMetrics(bookmark_title_font(button)).horizontalAdvance(text)+ BOOKMARK_BUTTON_TEXT_ELISION_PADDING+ layout.menu_indicator_width;layout.preferred_width = min(preferred_width, button_rect.width());@@ -153,16 +158,13 @@ static void paint_bookmark_button(QToolButton& button)option.icon.paint(&painter, layout.icon_rect, Qt::AlignCenter, mode, button.isChecked() ? QIcon::On : QIcon::Off);}- auto elided_text = button.fontMetrics().elidedText(option.text, Qt::ElideRight, layout.available_text_width);+ auto title_font = bookmark_title_font(button);+ auto elided_text = QFontMetrics(title_font).elidedText(option.text, Qt::ElideRight, layout.available_text_width);- button.style()->drawItemText(- &painter,- layout.text_rect,- Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine,- option.palette,- button.isEnabled(),- elided_text,- QPalette::ButtonText);+ // NB: The style would draw the title in the button's own font, so draw it here.+ painter.setFont(title_font);+ painter.setPen(option.palette.color(button.isEnabled() ? QPalette::Active : QPalette::Disabled, QPalette::ButtonText));+ painter.drawText(layout.text_rect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, elided_text);}static void install_menu_event_filter(QObject* filter, QMenu* menu)@@ -214,13 +216,14 @@ void BookmarksBar::rebuild()auto option = bookmark_button_style_option(*button);- auto height = bookmark_button_height(button->fontMetrics(), button->iconSize().height());+ QFontMetrics title_font_metrics(bookmark_title_font(*button));+ auto height = bookmark_button_height(title_font_metrics, button->iconSize().height());auto max_size_rect = QRect { 0, 0, BOOKMARK_BUTTON_MAX_WIDTH, height };auto layout = bookmark_button_layout(*button, option, title, max_size_rect);auto available_title_width = max(layout.available_text_width - BOOKMARK_BUTTON_TEXT_ELISION_PADDING, 0);- auto text = button->fontMetrics().elidedText(title, Qt::ElideRight, available_title_width);+ auto text = title_font_metrics.elidedText(title, Qt::ElideRight, available_title_width);button->setText(text);layout = bookmark_button_layout(*button, option, text, max_size_rect);diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cppindex a15ee56298b..2d68bd67e61 100644--- a/UI/Qt/ChromeStyle.cpp+++ b/UI/Qt/ChromeStyle.cpp@@ -11,6 +11,7 @@#include <QAbstractButton>#include <QElapsedTimer>+#include <QFont>#include <QGuiApplication>#include <QImage>#include <QPainter>@@ -256,6 +257,18 @@ QColor chrome_muted_text(QPalette const& palette)return palette.color(QPalette::PlaceholderText);}+// The titles of tabs and bookmarks share one size.+QFont chrome_title_font(QFont font)+{+#if defined(AK_OS_MACOS)+ // NB: The macOS style gives tab bars and tool buttons small fonts of different sizes when it polishes them, so the+ // titles are drawn a point smaller than the application's font rather than in the font of their widget.+ if (auto application_point_size = QGuiApplication::font().pointSizeF(); application_point_size > 0)+ font.setPointSizeF(application_point_size - 1);+#endif+ return font;+}+static QColor chrome_destructive_hover(){return QColor(196, 43, 28);diff --git a/UI/Qt/ChromeStyle.h b/UI/Qt/ChromeStyle.hindex 30da9e95645..f776be1123d 100644--- a/UI/Qt/ChromeStyle.h+++ b/UI/Qt/ChromeStyle.h@@ -10,6 +10,7 @@#include <QString>class QAbstractButton;+class QFont;class QPainter;class QPainterPath;class QPalette;@@ -37,6 +38,7 @@ QColor chrome_separator(QPalette const&);QColor chrome_window_outline(QPalette const&);QColor chrome_accent();QColor chrome_muted_text(QPalette const&);+QFont chrome_title_font(QFont);QString style_sheet_color(QColor const&);QString application_style_sheet(QPalette const&);diff --git a/UI/Qt/Icon.cpp b/UI/Qt/Icon.cppindex 1eecda5b0c9..9c0096b796c 100644--- a/UI/Qt/Icon.cpp+++ b/UI/Qt/Icon.cpp@@ -46,6 +46,39 @@ QIcon icon_from_png(ReadonlyBytes favicon_png, int logical_size)return icon;}+// Favicons get slightly rounded corners, so that square ones sit well in the round shapes of the chrome around them.+static constexpr qreal FAVICON_CORNER_RADIUS_FRACTION = 3.0 / 16.0;++QPixmap favicon_with_rounded_corners(QPixmap const& favicon)+{+ auto image = favicon.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied);+ image.setDevicePixelRatio(1);++ QImage rounded(image.size(), QImage::Format_ARGB32_Premultiplied);+ rounded.fill(Qt::transparent);+ {+ QPainter painter(&rounded);+ painter.setRenderHint(QPainter::Antialiasing, true);+ painter.setPen(Qt::NoPen);+ painter.setBrush(QBrush(image));+ auto radius = image.width() * FAVICON_CORNER_RADIUS_FRACTION;+ painter.drawRoundedRect(QRectF(image.rect()), radius, radius);+ }+ rounded.setDevicePixelRatio(favicon.devicePixelRatio());+ return QPixmap::fromImage(rounded);+}++QIcon favicon_with_rounded_corners(QIcon const& favicon, int logical_size)+{+ if (favicon.isNull())+ return {};++ QIcon rounded;+ for (auto device_pixel_ratio : ICON_DEVICE_PIXEL_RATIOS)+ rounded.addPixmap(favicon_with_rounded_corners(favicon.pixmap(QSize(logical_size, logical_size), device_pixel_ratio)));+ return rounded;+}+static QPen chrome_icon_pen(QColor const& color, qreal width){return QPen(color, width, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);@@ -255,7 +288,7 @@ static QPixmap create_transparent_icon_pixmap(QSize logical_size, qreal device_pQPixmap create_chrome_icon_pixmap(ChromeIcon icon, QColor color, qreal device_pixel_ratio){- auto pixmap = create_transparent_icon_pixmap({ 20, 20 }, device_pixel_ratio);+ auto pixmap = create_transparent_icon_pixmap({ CHROME_ICON_CANVAS_SIZE, CHROME_ICON_CANVAS_SIZE }, device_pixel_ratio);QPainter painter(&pixmap);painter.setRenderHint(QPainter::Antialiasing, true);diff --git a/UI/Qt/Icon.h b/UI/Qt/Icon.hindex 54d2ec58804..3822a21cb48 100644--- a/UI/Qt/Icon.h+++ b/UI/Qt/Icon.h@@ -51,9 +51,13 @@ enum class ChromeIcon {};constexpr inline auto ICON_DEVICE_PIXEL_RATIOS = to_array({ 1, 2, 3 });+// Chrome icons are drawn on a canvas of this size, with room around their shapes.+constexpr inline int CHROME_ICON_CANVAS_SIZE = 20;QIcon load_icon_from_uri(StringView);QIcon icon_from_png(ReadonlyBytes, int logical_size);+QPixmap favicon_with_rounded_corners(QPixmap const&);+QIcon favicon_with_rounded_corners(QIcon const&, int logical_size);QIcon create_chrome_icon(ChromeIcon, QPalette const&);QPixmap create_chrome_icon_pixmap(ChromeIcon, QColor, qreal device_pixel_ratio);QIcon loading_spinner_icon(QPalette const& palette, int frame);diff --git a/UI/Qt/Menu.cpp b/UI/Qt/Menu.cppindex 123e5759eeb..c41295f503d 100644--- a/UI/Qt/Menu.cpp+++ b/UI/Qt/Menu.cpp@@ -225,7 +225,7 @@ static void initialize_native_control(WebView::Action& action, QAction& qaction,break;case WebView::ActionID::BookmarkItem:if (auto icon = action.png_icon(); icon.has_value())- qaction.setIcon(icon_from_png(icon->bytes(), MENU_ICON_SIZE));+ qaction.setIcon(favicon_with_rounded_corners(icon_from_png(icon->bytes(), MENU_ICON_SIZE), MENU_ICON_SIZE));elseqaction.setIcon(create_chrome_icon(ChromeIcon::Globe, palette));break;diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex f636de796a5..1f1922f23f0 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -360,6 +360,7 @@ QToolButton* Tab::create_navigation_history_toolbar_button(QWidget& parent, QActreturn button;}+static constexpr int TAB_FAVICON_SIZE = 16;static constexpr int TOOLBAR_HORIZONTAL_MARGIN = 12;#ifdef AK_OS_MACOSstatic constexpr int WEB_VIEW_INSET = 5;@@ -683,7 +684,7 @@ Tab::Tab(BrowserWindow* window, DeferredTab deferred_tab): QWidget(window), m_window(window), m_title(deferred_tab.title)- , m_favicon(deferred_tab.favicon)+ , m_favicon(favicon_with_rounded_corners(deferred_tab.favicon, TAB_FAVICON_SIZE)), m_session_tab_id(deferred_tab.session_tab_id), m_deferred_tab(AK::move(deferred_tab)){@@ -1047,7 +1048,7 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)if (qpixmap.isNull())return;- m_favicon = qpixmap;+ m_favicon = favicon_with_rounded_corners(qpixmap);if (m_content_blocking_popover)m_content_blocking_popover->set_favicon(m_favicon);update_tab_icon();diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex 4206e71d794..1e0986c0d29 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -357,17 +357,6 @@ static void paint_load_sheen(QPainter& painter, QPalette const& palette, QPaintepainter.restore();}-static QFont tab_title_font(QFont font)-{-#if defined(AK_OS_MACOS)- // The macOS style gives tab bars a small font when it polishes them, so tab titles are drawn a point larger than- // the tab bar's font rather than by setting the tab bar's font.- if (font.pointSizeF() > 0)- font.setPointSizeF(font.pointSizeF() + 1);-#endif- return font;-}-static QColor tab_hover_surface(QPalette const& palette, qreal hover_progress){auto dark = ChromeStyle::is_dark(palette);@@ -472,7 +461,7 @@ private:if (hovered || isDown())text_color = ChromeStyle::chrome_button_text(palette());painter.setPen(text_color);- auto text_font = tab_title_font(m_tab_bar.font());+ auto text_font = ChromeStyle::chrome_title_font(m_tab_bar.font());painter.setFont(text_font);QFontMetrics font_metrics(text_font);@@ -956,7 +945,7 @@ void TabBar::paintEvent(QPaintEvent*)// Titles are shaped once and kept for as long as some tab shows them, since resizing the window repaints the tab// bar on every step without changing any title.- auto title_font = tab_title_font(font());+ auto title_font = ChromeStyle::chrome_title_font(font());if (m_tab_title_texts_font != title_font) {m_tab_title_texts.clear();m_tab_title_texts_font = title_font;@@ -1046,8 +1035,16 @@ void TabBar::paintEvent(QPaintEvent*)paint_load_sheen(painter, palette(), tab_path, tab_path.boundingRect(), *progress);auto icon = tabIcon(index);- if (icon.isNull())- icon = m_fallback_tab_icon;+ auto paint_icon = [&](QRect const& icon_rect) {+ if (!icon.isNull()) {+ icon.paint(&painter, icon_rect);+ return;+ }+ // NB: The fallback globe is drawn on a chrome icon's larger canvas, with room around it, so it is painted at+ // that size to be about as large as a favicon.+ auto overhang = (CHROME_ICON_CANVAS_SIZE - TAB_ICON_SIZE) / 2;+ m_fallback_tab_icon.paint(&painter, icon_rect.adjusted(-overhang, -overhang, overhang, overhang));+ };if (is_collapsed_vertical || is_pinned_tile) {// The audio button takes the favicon's place on a pinned tile.@@ -1064,7 +1061,7 @@ void TabBar::paintEvent(QPaintEvent*)};if (!is_selected)painter.setOpacity(is_hovered ? 0.92 : (dark ? 0.88 : 0.84));- icon.paint(&painter, icon_rect);+ paint_icon(icon_rect);painter.setOpacity(1.0);continue;}@@ -1088,7 +1085,7 @@ void TabBar::paintEvent(QPaintEvent*)};if (!is_selected)painter.setOpacity(is_hovered ? 0.92 : (dark ? 0.88 : 0.84));- icon.paint(&painter, icon_rect);+ paint_icon(icon_rect);painter.setOpacity(1.0);continue;}@@ -1101,7 +1098,7 @@ void TabBar::paintEvent(QPaintEvent*)};if (!is_selected)painter.setOpacity(is_hovered ? 0.92 : (dark ? 0.88 : 0.84));- icon.paint(&painter, icon_rect);+ paint_icon(icon_rect);painter.setOpacity(1.0);contents_rect.setLeft(icon_rect.right() + 8);