Ladybird/Qt: Specify a minimum and maxium tab width

By default, Qt will grow the width of a tab button to fit the title text
of the tab. For long titles or file:// URLs, this looks rather bad. This
sets a min/max tab width to prevent such infinite growth.

To do this, we have to subclass both QTabWidget and QTabBar, because the
functions to be called/overridden are protected.
This commit is contained in:
Timothy Flynn 2024-04-02 21:38:01 -04:00 committed by Andreas Kling
parent 0234add5fa
commit 2713d4651d
Notes: sideshowbarker 2024-07-17 04:01:41 +09:00
3 changed files with 45 additions and 8 deletions

View file

@ -46,18 +46,12 @@ static QIcon const& app_icon()
} }
BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::CookieJar& cookie_jar, WebContentOptions const& web_content_options, StringView webdriver_content_ipc_path) BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::CookieJar& cookie_jar, WebContentOptions const& web_content_options, StringView webdriver_content_ipc_path)
: m_cookie_jar(cookie_jar) : m_tabs_container(new TabWidget(this))
, m_cookie_jar(cookie_jar)
, m_web_content_options(web_content_options) , m_web_content_options(web_content_options)
, m_webdriver_content_ipc_path(webdriver_content_ipc_path) , m_webdriver_content_ipc_path(webdriver_content_ipc_path)
{ {
setWindowIcon(app_icon()); setWindowIcon(app_icon());
m_tabs_container = new QTabWidget(this);
m_tabs_container->installEventFilter(this);
m_tabs_container->setElideMode(Qt::TextElideMode::ElideRight);
m_tabs_container->setMovable(true);
m_tabs_container->setTabsClosable(true);
m_tabs_container->setDocumentMode(true);
m_tabs_container->setTabBarAutoHide(true);
// Listen for DPI changes // Listen for DPI changes
m_device_pixel_ratio = devicePixelRatio(); m_device_pixel_ratio = devicePixelRatio();

View file

@ -4,12 +4,39 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/StdLibExtras.h>
#include <Ladybird/Qt/TabBar.h> #include <Ladybird/Qt/TabBar.h>
#include <QEvent> #include <QEvent>
#include <QPushButton> #include <QPushButton>
namespace Ladybird { namespace Ladybird {
QSize TabBar::tabSizeHint(int index) const
{
auto width = this->width() / count();
width = min(225, width);
width = max(64, width);
auto hint = QTabBar::tabSizeHint(index);
hint.setWidth(width);
return hint;
}
TabWidget::TabWidget(QWidget* parent)
: QTabWidget(parent)
{
// This must be called first, otherwise several of the options below have no effect.
setTabBar(new TabBar());
setDocumentMode(true);
setElideMode(Qt::TextElideMode::ElideRight);
setMovable(true);
setTabBarAutoHide(true);
setTabsClosable(true);
installEventFilter(parent);
}
TabBarButton::TabBarButton(QIcon const& icon, QWidget* parent) TabBarButton::TabBarButton(QIcon const& icon, QWidget* parent)
: QPushButton(icon, {}, parent) : QPushButton(icon, {}, parent)
{ {

View file

@ -7,6 +7,8 @@
#pragma once #pragma once
#include <QPushButton> #include <QPushButton>
#include <QTabBar>
#include <QTabWidget>
class QEvent; class QEvent;
class QIcon; class QIcon;
@ -14,6 +16,20 @@ class QWidget;
namespace Ladybird { namespace Ladybird {
class TabBar : public QTabBar {
Q_OBJECT
public:
virtual QSize tabSizeHint(int index) const override;
};
class TabWidget : public QTabWidget {
Q_OBJECT
public:
explicit TabWidget(QWidget* parent = nullptr);
};
class TabBarButton : public QPushButton { class TabBarButton : public QPushButton {
Q_OBJECT Q_OBJECT