Skip to content

Commit

Permalink
Display Tab Buttons When Necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaopengLin committed Sep 15, 2024
1 parent 9b7c49f commit 63b7d1d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
4 changes: 3 additions & 1 deletion resources/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* -----------------------------------------
Expand Down
30 changes: 30 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -136,6 +165,7 @@ void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
KiwixApp::instance()->getContentManager()->getView()->updateSizeHint();
updateTabButtons();
}

void MainWindow::readingListToggled(bool state)
Expand Down
1 change: 1 addition & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private slots:
void readingListToggled(bool state);
void hideTabAndTop();
void showTabAndTop();
void updateTabButtons();

private:
Ui::MainWindow *mp_ui;
Expand Down
22 changes: 20 additions & 2 deletions src/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 63b7d1d

Please sign in to comment.