From 63b7d1df094e06b4181223abb4568f24eb49c749 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Sun, 8 Sep 2024 23:09:31 -0400 Subject: [PATCH] Display Tab Buttons When Necessary --- resources/css/style.css | 4 +++- src/mainwindow.cpp | 30 ++++++++++++++++++++++++++++++ src/mainwindow.h | 1 + src/tabbar.cpp | 22 ++++++++++++++++++++-- src/tabbar.h | 6 ++++++ 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/resources/css/style.css b/resources/css/style.css index 779338785..deb1375f1 100644 --- a/resources/css/style.css +++ b/resources/css/style.css @@ -216,7 +216,9 @@ QTabBar::tab:first { QTabBar::scroller { width: 0px; height: 0px; - border: none; + + /* Last tab size is off by 1 if border not set. */ + border: 1px solid transparent; } /* ----------------------------------------- diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index dac25eac1..b5a68c8ae 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -31,6 +31,11 @@ MainWindow::MainWindow(QWidget *parent) : mp_ui->nextTabButton->setDefaultAction(app->getAction(KiwixApp::ScrollTabBarTowardEndAction)); mp_ui->prevTabButton->setDefaultAction(app->getAction(KiwixApp::ScrollTabBarTowardStartAction)); + connect(mp_ui->tabBar, &TabBar::sizeChanged, this, &MainWindow::updateTabButtons); + connect(mp_ui->tabBar, &QTabBar::currentChanged, this, &MainWindow::updateTabButtons); + connect(mp_ui->tabBar, &TabBar::tabRemovedSignal, this, &MainWindow::updateTabButtons); + connect(mp_ui->tabBar, &TabBar::tabInsertedSignal, this, &MainWindow::updateTabButtons); + connect(mp_ui->nextTabButton, &QToolButton::triggered, mp_ui->tabBar, &TabBar::scrollTabBarTowardEnd); connect(mp_ui->prevTabButton, &QToolButton::triggered, mp_ui->tabBar, &TabBar::scrollTabBarTowardStart); @@ -109,6 +114,30 @@ void MainWindow::showTabAndTop() { getTopWidget()->show(); } +void MainWindow::updateTabButtons() +{ + auto tabBar = getTabBar(); + QRect tabBarRect = getTabBar()->rect(); + QRect newButtonTabRect = tabBar->tabRect(tabBar->count() - 1); + + /* Decision is made at half way of the new button tab for smoothness */ + newButtonTabRect.setWidth(newButtonTabRect.width() / 2); + bool newTabVisible = tabBarRect.contains(newButtonTabRect); + if (mp_ui->newTabSideButton->isHidden()) + mp_ui->newTabSideButton->setVisible(!newTabVisible); + else + mp_ui->newTabSideButton->setHidden(newTabVisible); + + QRect firstTabRect = tabBar->tabRect(0); + QRect lastRealTabRect = tabBar->tabRect(tabBar->count() - 2); + + bool firstVisible = tabBarRect.contains(firstTabRect); + bool lastVisible = tabBarRect.contains(lastRealTabRect); + + firstVisible ? mp_ui->prevTabButton->hide() : mp_ui->prevTabButton->show(); + lastVisible ? mp_ui->nextTabButton->hide() : mp_ui->nextTabButton->show(); +} + bool MainWindow::eventFilter(QObject* /*object*/, QEvent* event) { if (event->type() == QEvent::MouseMove && isFullScreen()) @@ -136,6 +165,7 @@ void MainWindow::resizeEvent(QResizeEvent *event) { QMainWindow::resizeEvent(event); KiwixApp::instance()->getContentManager()->getView()->updateSizeHint(); + updateTabButtons(); } void MainWindow::readingListToggled(bool state) diff --git a/src/mainwindow.h b/src/mainwindow.h index 2dbb882aa..a97bef15f 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -37,6 +37,7 @@ private slots: void readingListToggled(bool state); void hideTabAndTop(); void showTabAndTop(); + void updateTabButtons(); private: Ui::MainWindow *mp_ui; diff --git a/src/tabbar.cpp b/src/tabbar.cpp index 85ac64e31..6f45c54b5 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -137,13 +137,13 @@ void TabBar::moveToPreviousTab() void TabBar::scrollTabBarTowardEnd() { const int index = currentIndex(); - setCurrentIndex(index + 1); + setCurrentIndex(index == realTabCount() - 1 ? index : index + 1); } void TabBar::scrollTabBarTowardStart() { const int index = currentIndex(); - setCurrentIndex(index - 1); + setCurrentIndex(index <= 0 ? index : index - 1); } void TabBar::setCloseTabButton(int index) @@ -505,6 +505,24 @@ void TabBar::paintEvent(QPaintEvent *e) } } +void TabBar::tabRemoved(int index) +{ + QTabBar::tabRemoved(index); + emit tabRemovedSignal(index); +} + +void TabBar::tabInserted(int index) +{ + QTabBar::tabInserted(index); + emit tabInsertedSignal(index); +} + +void TabBar::resizeEvent(QResizeEvent *event) +{ + QTabBar::resizeEvent(event); + emit sizeChanged(); +} + void TabBar::onTabMoved(int from, int to) { // avoid infinitive recursion diff --git a/src/tabbar.h b/src/tabbar.h index 86ce08d2f..e57bc2a45 100644 --- a/src/tabbar.h +++ b/src/tabbar.h @@ -55,11 +55,17 @@ class TabBar : public QTabBar protected: void mousePressEvent(QMouseEvent *event); void paintEvent(QPaintEvent *); + void tabRemoved(int index) override; + void tabInserted(int index) override; + void resizeEvent(QResizeEvent *) override; signals: void webActionEnabledChanged(QWebEnginePage::WebAction action, bool enabled); void tabDisplayed(TabType tabType); void currentTitleChanged(const QString& title); + void tabRemovedSignal(int index); + void tabInsertedSignal(int index); + void sizeChanged(); public slots: void closeTab(int index);