UI/Qt: Make TabWidget responsible for new-tab button positioning

Makes use of the fact that QTabWidget automatically reserves space
for "corner widgets", and gives us the ability to override their
location with a custom style.

Also, determine the height of the new-tab button using the height of the
tab bar, instead of a hard-coded 32px which was too tall on MacOS.
This commit is contained in:
Sam Atkins 2024-07-26 11:38:26 +01:00 committed by Alexander Kalenik
parent 75216182c9
commit e17362ab79
Notes: github-actions[bot] 2024-07-29 10:03:01 +00:00
4 changed files with 40 additions and 21 deletions

View file

@ -3,6 +3,7 @@
* Copyright (c) 2022, Matthew Costa <ucosty@gmail.com>
* Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
* Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -679,6 +680,7 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::Cook
m_new_tab_button_toolbar->setMovable(false);
m_new_tab_button_toolbar->setStyleSheet("QToolBar { background: transparent; }");
m_new_tab_button_toolbar->setIconSize(QSize(16, 16));
m_tabs_container->setCornerWidget(m_new_tab_button_toolbar, Qt::TopRightCorner);
setCentralWidget(m_tabs_container);
setContextMenuPolicy(Qt::PreventContextMenu);
@ -693,20 +695,6 @@ void BrowserWindow::set_current_tab(Tab* tab)
}
}
void BrowserWindow::update_new_tab_button()
{
if (m_new_tab_button_toolbar == nullptr || m_tabs_container == nullptr || m_tabs_container->count() < 1)
return;
QSize tab_bar_size = m_tabs_container->tabBar()->sizeHint();
int window_width = this->rect().width();
QRect new_rect;
new_rect.setX(qMin(tab_bar_size.width(), window_width - 32));
new_rect.setWidth(32);
new_rect.setHeight(32);
m_tabs_container->tabBar()->setMaximumWidth(window_width - 32);
m_new_tab_button_toolbar->setGeometry(new_rect);
}
void BrowserWindow::debug_request(ByteString const& request, ByteString const& argument)
{
if (!m_current_tab)
@ -854,8 +842,6 @@ void BrowserWindow::initialize_tab(Tab* tab)
tab->set_navigator_compatibility_mode(navigator_compatibility_mode());
tab->set_enable_do_not_track(Settings::the()->enable_do_not_track());
tab->view().set_preferred_color_scheme(m_preferred_color_scheme);
update_new_tab_button();
}
void BrowserWindow::activate_tab(int index)
@ -869,8 +855,6 @@ void BrowserWindow::close_tab(int index)
m_tabs_container->removeTab(index);
tab->deleteLater();
update_new_tab_button();
if (m_tabs_container->count() == 0)
close();
}
@ -1197,7 +1181,6 @@ void BrowserWindow::resizeEvent(QResizeEvent* event)
for_each_tab([&](auto& tab) {
tab.view().set_window_size({ frameSize().width() * m_device_pixel_ratio, frameSize().height() * m_device_pixel_ratio });
});
update_new_tab_button();
}
void BrowserWindow::moveEvent(QMoveEvent* event)

View file

@ -184,8 +184,6 @@ private:
Web::CSS::PreferredColorScheme m_preferred_color_scheme;
void set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme);
void update_new_tab_button();
QTabWidget* m_tabs_container { nullptr };
Tab* m_current_tab { nullptr };
QMenu* m_zoom_menu { nullptr };

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -12,6 +13,7 @@
#include <QContextMenuEvent>
#include <QEvent>
#include <QPushButton>
#include <QStylePainter>
namespace Ladybird {
@ -50,6 +52,8 @@ TabWidget::TabWidget(QWidget* parent)
setMovable(true);
setTabsClosable(true);
setStyle(new TabStyle(this));
installEventFilter(parent);
}
@ -70,4 +74,27 @@ bool TabBarButton::event(QEvent* event)
return QPushButton::event(event);
}
TabStyle::TabStyle(QObject* parent)
{
setParent(parent);
}
QRect TabStyle::subElementRect(QStyle::SubElement sub_element, QStyleOption const* option, QWidget const* widget) const
{
// Place our add-tab button (set as the top-right corner widget) directly after the last tab
if (sub_element == QStyle::SE_TabWidgetRightCorner) {
auto* tab_widget = verify_cast<TabWidget>(widget);
auto tab_bar_size = tab_widget->tabBar()->sizeHint();
auto new_tab_button_size = tab_bar_size.height();
return QRect {
qMin(tab_bar_size.width(), tab_widget->width() - new_tab_button_size),
0,
new_tab_button_size,
new_tab_button_size
};
}
return QProxyStyle::subElementRect(sub_element, option, widget);
}
}

View file

@ -1,12 +1,14 @@
/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <QProxyStyle>
#include <QPushButton>
#include <QTabBar>
#include <QTabWidget>
@ -45,4 +47,13 @@ protected:
virtual bool event(QEvent* event) override;
};
class TabStyle : public QProxyStyle {
Q_OBJECT
public:
explicit TabStyle(QObject* parent = nullptr);
virtual QRect subElementRect(QStyle::SubElement, QStyleOption const*, QWidget const*) const override;
};
}