mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-06 16:45:03 +00:00
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:
committed by
Alexander Kalenik
parent
75216182c9
commit
e17362ab79
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user