2024-04-03 04:24:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
2024-04-28 17:07:23 +03:00
|
|
|
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
|
2024-07-26 13:38:26 +03:00
|
|
|
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
|
2024-04-03 04:24:17 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-04-28 17:07:23 +03:00
|
|
|
#include "Tab.h"
|
2024-04-03 04:38:01 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2024-04-28 17:07:23 +03:00
|
|
|
#include <AK/TypeCasts.h>
|
2024-04-03 04:24:17 +03:00
|
|
|
#include <Ladybird/Qt/TabBar.h>
|
2024-04-28 17:07:23 +03:00
|
|
|
#include <QContextMenuEvent>
|
2024-04-03 04:24:17 +03:00
|
|
|
#include <QEvent>
|
|
|
|
#include <QPushButton>
|
2024-07-26 13:38:26 +03:00
|
|
|
#include <QStylePainter>
|
2024-04-03 04:24:17 +03:00
|
|
|
|
|
|
|
namespace Ladybird {
|
|
|
|
|
2024-04-28 17:07:23 +03:00
|
|
|
TabBar::TabBar(QWidget* parent)
|
|
|
|
: QTabBar(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-03 04:38:01 +03:00
|
|
|
QSize TabBar::tabSizeHint(int index) const
|
|
|
|
{
|
|
|
|
auto width = this->width() / count();
|
|
|
|
width = min(225, width);
|
2024-04-28 14:12:47 +03:00
|
|
|
width = max(128, width);
|
2024-04-03 04:38:01 +03:00
|
|
|
|
|
|
|
auto hint = QTabBar::tabSizeHint(index);
|
|
|
|
hint.setWidth(width);
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:07:23 +03:00
|
|
|
void TabBar::contextMenuEvent(QContextMenuEvent* event)
|
|
|
|
{
|
|
|
|
auto* tab_widget = verify_cast<QTabWidget>(this->parent());
|
|
|
|
auto* tab = verify_cast<Tab>(tab_widget->widget(tabAt(event->pos())));
|
|
|
|
if (tab)
|
|
|
|
tab->context_menu()->exec(event->globalPos());
|
|
|
|
}
|
|
|
|
|
2024-04-03 04:38:01 +03:00
|
|
|
TabWidget::TabWidget(QWidget* parent)
|
|
|
|
: QTabWidget(parent)
|
|
|
|
{
|
|
|
|
// This must be called first, otherwise several of the options below have no effect.
|
2024-04-28 17:07:23 +03:00
|
|
|
setTabBar(new TabBar(this));
|
2024-04-03 04:38:01 +03:00
|
|
|
|
|
|
|
setDocumentMode(true);
|
|
|
|
setElideMode(Qt::TextElideMode::ElideRight);
|
|
|
|
setMovable(true);
|
|
|
|
setTabsClosable(true);
|
|
|
|
|
2024-07-26 13:38:26 +03:00
|
|
|
setStyle(new TabStyle(this));
|
|
|
|
|
2024-04-03 04:38:01 +03:00
|
|
|
installEventFilter(parent);
|
|
|
|
}
|
|
|
|
|
2024-04-03 04:24:17 +03:00
|
|
|
TabBarButton::TabBarButton(QIcon const& icon, QWidget* parent)
|
|
|
|
: QPushButton(icon, {}, parent)
|
|
|
|
{
|
|
|
|
resize({ 20, 20 });
|
|
|
|
setFlat(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabBarButton::event(QEvent* event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::Enter)
|
|
|
|
setFlat(false);
|
|
|
|
if (event->type() == QEvent::Leave)
|
|
|
|
setFlat(true);
|
|
|
|
|
|
|
|
return QPushButton::event(event);
|
|
|
|
}
|
|
|
|
|
2024-07-26 13:38:26 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-04-03 04:24:17 +03:00
|
|
|
}
|