2024-04-02 21:24:17 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
2024-04-28 15:07:23 +01:00
|
|
|
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
|
2024-04-02 21:24:17 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-04-28 15:07:23 +01:00
|
|
|
#include "Tab.h"
|
2024-04-02 21:38:01 -04:00
|
|
|
#include <AK/StdLibExtras.h>
|
2024-04-28 15:07:23 +01:00
|
|
|
#include <AK/TypeCasts.h>
|
2024-04-02 21:24:17 -04:00
|
|
|
#include <Ladybird/Qt/TabBar.h>
|
2024-04-28 15:07:23 +01:00
|
|
|
#include <QContextMenuEvent>
|
2024-04-02 21:24:17 -04:00
|
|
|
#include <QEvent>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
namespace Ladybird {
|
|
|
|
|
2024-04-28 15:07:23 +01:00
|
|
|
TabBar::TabBar(QWidget* parent)
|
|
|
|
: QTabBar(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-02 21:38:01 -04:00
|
|
|
QSize TabBar::tabSizeHint(int index) const
|
|
|
|
{
|
|
|
|
auto width = this->width() / count();
|
|
|
|
width = min(225, width);
|
2024-04-28 12:12:47 +01:00
|
|
|
width = max(128, width);
|
2024-04-02 21:38:01 -04:00
|
|
|
|
|
|
|
auto hint = QTabBar::tabSizeHint(index);
|
|
|
|
hint.setWidth(width);
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2024-04-28 15:07:23 +01: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-02 21:38:01 -04:00
|
|
|
TabWidget::TabWidget(QWidget* parent)
|
|
|
|
: QTabWidget(parent)
|
|
|
|
{
|
|
|
|
// This must be called first, otherwise several of the options below have no effect.
|
2024-04-28 15:07:23 +01:00
|
|
|
setTabBar(new TabBar(this));
|
2024-04-02 21:38:01 -04:00
|
|
|
|
|
|
|
setDocumentMode(true);
|
|
|
|
setElideMode(Qt::TextElideMode::ElideRight);
|
|
|
|
setMovable(true);
|
|
|
|
setTabsClosable(true);
|
|
|
|
|
|
|
|
installEventFilter(parent);
|
|
|
|
}
|
|
|
|
|
2024-04-02 21:24:17 -04: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|