From 7d81892991f320161c1a33fdf7daf7743bac862c Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 26 Jul 2024 14:39:26 +0100 Subject: [PATCH] UI/Qt: Manually paint the tab-bar background where the "add" button was The TabBar itself does not stretch the entire width of the TabWidget, because it leaves space for the width of the new-tab button. So, we manually tell Qt to paint the TabBar's background into the gap. Fixes #768. (cherry picked from commit 06484d0663c120198323547e6515404ba0a594ee) --- Ladybird/Qt/TabBar.cpp | 26 ++++++++++++++++++++++++++ Ladybird/Qt/TabBar.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/Ladybird/Qt/TabBar.cpp b/Ladybird/Qt/TabBar.cpp index 634a82c723b..f10e9390893 100644 --- a/Ladybird/Qt/TabBar.cpp +++ b/Ladybird/Qt/TabBar.cpp @@ -57,6 +57,32 @@ TabWidget::TabWidget(QWidget* parent) installEventFilter(parent); } +void TabWidget::paintEvent(QPaintEvent*) +{ + auto prepare_style_options = [](QTabBar* tab_bar, QSize widget_size) { + QStyleOptionTabBarBase style_options; + QStyleOptionTab tab_overlap; + tab_overlap.shape = tab_bar->shape(); + auto overlap = tab_bar->style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &tab_overlap, tab_bar); + style_options.initFrom(tab_bar); + style_options.shape = tab_bar->shape(); + style_options.documentMode = tab_bar->documentMode(); + // NOTE: This assumes the tab bar is at the top of the tab widget. + style_options.rect = { 0, widget_size.height() - overlap, widget_size.width(), overlap }; + + return style_options; + }; + + QStylePainter painter { this, tabBar() }; + if (auto* widget = cornerWidget(Qt::TopRightCorner)) { + // Manually paint the background for the area where the "new tab" button would have been + // if we hadn't relocated it in `TabStyle::subElementRect()`. + auto style_options = prepare_style_options(tabBar(), widget->size()); + style_options.rect.translate(tabBar()->rect().width(), widget->y()); + painter.drawPrimitive(QStyle::PE_FrameTabBarBase, style_options); + } +} + TabBarButton::TabBarButton(QIcon const& icon, QWidget* parent) : QPushButton(icon, {}, parent) { diff --git a/Ladybird/Qt/TabBar.h b/Ladybird/Qt/TabBar.h index 5100fcc4969..d3d93a7c245 100644 --- a/Ladybird/Qt/TabBar.h +++ b/Ladybird/Qt/TabBar.h @@ -35,6 +35,8 @@ class TabWidget : public QTabWidget { public: explicit TabWidget(QWidget* parent = nullptr); + + virtual void paintEvent(QPaintEvent*) override; }; class TabBarButton : public QPushButton {