Merge pull request #867 from kiwix/tabNav

Add shortcuts for navigating tabs.
This commit is contained in:
Kelson 2022-08-11 08:20:21 +02:00 committed by GitHub
commit 7d8b26368d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 7 deletions

View File

@ -133,5 +133,7 @@
"monitor-dir-dialog-msg":"The new monitor directory path will be:\n{{DIRECTORY}}", "monitor-dir-dialog-msg":"The new monitor directory path will be:\n{{DIRECTORY}}",
"monitor-clear-dir-dialog-title":"Are you sure you want to clear the monitor directory?", "monitor-clear-dir-dialog-title":"Are you sure you want to clear the monitor directory?",
"monitor-clear-dir-dialog-msg":"This will stop checking the monitor directory for new ZIM files.", "monitor-clear-dir-dialog-msg":"This will stop checking the monitor directory for new ZIM files.",
"monitor-directory-tooltip":"All ZIM files in this directory will be automatically added to the library." "monitor-directory-tooltip":"All ZIM files in this directory will be automatically added to the library.",
"next-tab":"Move to next tab",
"previous-tab":"Move to previous tab"
} }

View File

@ -409,6 +409,10 @@ void KiwixApp::createAction()
CREATE_ACTION_SHORTCUT(ZoomResetAction, gt("zoom-reset"), QKeySequence(Qt::CTRL+Qt::Key_0)); CREATE_ACTION_SHORTCUT(ZoomResetAction, gt("zoom-reset"), QKeySequence(Qt::CTRL+Qt::Key_0));
CREATE_ACTION_SHORTCUT(NextTabAction, gt("next-tab"), QKeySequence(Qt::CTRL + Qt::Key_Tab));
CREATE_ACTION_SHORTCUT(PreviousTabAction, gt("previous-tab"), QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Tab));
CREATE_ACTION_SHORTCUT(HelpAction, gt("help"), QKeySequence::HelpContents); CREATE_ACTION_SHORTCUT(HelpAction, gt("help"), QKeySequence::HelpContents);
HIDE_ACTION(HelpAction); HIDE_ACTION(HelpAction);

View File

@ -47,6 +47,8 @@ public:
ZoomInAction, ZoomInAction,
ZoomOutAction, ZoomOutAction,
ZoomResetAction, ZoomResetAction,
NextTabAction,
PreviousTabAction,
HistoryBackAction, HistoryBackAction,
HistoryForwardAction, HistoryForwardAction,
HelpAction, HelpAction,

View File

@ -22,7 +22,9 @@ MainWindow::MainWindow(QWidget *parent) :
mp_ui->tabBar->setStackedWidget(mp_ui->mainView); mp_ui->tabBar->setStackedWidget(mp_ui->mainView);
auto app = KiwixApp::instance(); auto app = KiwixApp::instance();
addAction(KiwixApp::instance()->getAction(KiwixApp::ToggleFullscreenAction)); addAction(app->getAction(KiwixApp::ToggleFullscreenAction));
addAction(app->getAction(KiwixApp::NextTabAction));
addAction(app->getAction(KiwixApp::PreviousTabAction));
connect(app->getAction(KiwixApp::ExitAction), &QAction::triggered, connect(app->getAction(KiwixApp::ExitAction), &QAction::triggered,
this, &QMainWindow::close); this, &QMainWindow::close);

View File

@ -24,6 +24,8 @@ TabBar::TabBar(QWidget *parent) :
connect(this, &QTabBar::currentChanged, this, &TabBar::onCurrentChanged, Qt::QueuedConnection); connect(this, &QTabBar::currentChanged, this, &TabBar::onCurrentChanged, Qt::QueuedConnection);
auto app = KiwixApp::instance(); auto app = KiwixApp::instance();
connect(app->getAction(KiwixApp::NextTabAction), &QAction::triggered, this, &TabBar::moveToNextTab);
connect(app->getAction(KiwixApp::PreviousTabAction), &QAction::triggered, this, &TabBar::moveToPreviousTab);
connect(app->getAction(KiwixApp::NewTabAction), &QAction::triggered, connect(app->getAction(KiwixApp::NewTabAction), &QAction::triggered,
this, [=]() { this, [=]() {
this->createNewTab(true, false); this->createNewTab(true, false);
@ -127,6 +129,26 @@ void TabBar::setNewTabButton()
setTabButton(idx, QTabBar::RightSide, Q_NULLPTR); setTabButton(idx, QTabBar::RightSide, Q_NULLPTR);
} }
int TabBar::realTabCount() const
{
// The last tab is "+" in TabBar, but that isn't a real tab which displays any content hence the real count is tab count - 1
if (count() < 1)
return 0;
return count() - 1;
}
void TabBar::moveToNextTab()
{
const int index = currentIndex();
setCurrentIndex(index == realTabCount() - 1 ? 0 : index + 1);
}
void TabBar::moveToPreviousTab()
{
const int index = currentIndex();
setCurrentIndex(index <= 0 ? realTabCount() - 1 : index - 1);
}
ZimView* TabBar::createNewTab(bool setCurrent, bool adjacentToCurrentTab) ZimView* TabBar::createNewTab(bool setCurrent, bool adjacentToCurrentTab)
{ {
auto tab = new ZimView(this, this); auto tab = new ZimView(this, this);
@ -134,7 +156,7 @@ ZimView* TabBar::createNewTab(bool setCurrent, bool adjacentToCurrentTab)
if(adjacentToCurrentTab) { if(adjacentToCurrentTab) {
index = currentIndex() + 1; index = currentIndex() + 1;
} else { } else {
index = count() - 1; // for New Tab Button index = realTabCount(); // for New Tab Button
} }
mp_stackedWidget->insertWidget(index, tab); mp_stackedWidget->insertWidget(index, tab);
index = insertTab(index, ""); index = insertTab(index, "");
@ -255,7 +277,7 @@ void TabBar::closeTabsByZimId(const QString &id)
void TabBar::closeTab(int index) void TabBar::closeTab(int index)
{ {
// the last tab is + button, cannot be closed // the last tab is + button, cannot be closed
if (index == this->count() - 1) if (index == this->realTabCount())
return; return;
setSelectionBehaviorOnRemove(index); setSelectionBehaviorOnRemove(index);
@ -289,8 +311,8 @@ void TabBar::onCurrentChanged(int index)
return; return;
// if somehow the last tab (+ button) became active, switch to the previous // if somehow the last tab (+ button) became active, switch to the previous
if (index >= (count() - 1)) { if (index >= realTabCount()) {
setCurrentIndex(count() - 2); setCurrentIndex(realTabCount() - 1);
return; return;
} }

View File

@ -58,13 +58,18 @@ public slots:
void closeTab(int index); void closeTab(int index);
void fullScreenRequested(QWebEngineFullScreenRequest request); void fullScreenRequested(QWebEngineFullScreenRequest request);
void on_webview_titleChanged(const QString& title); void on_webview_titleChanged(const QString& title);
void moveToNextTab();
void moveToPreviousTab();
private: private:
QStackedWidget* mp_stackedWidget; QStackedWidget* mp_stackedWidget;
QScopedPointer<FullScreenWindow> m_fullScreenWindow; QScopedPointer<FullScreenWindow> m_fullScreenWindow;
void setSelectionBehaviorOnRemove(int index); void setSelectionBehaviorOnRemove(int index);
// The "+" (new tab) button is implemented as a tab (that is always placed at the end).
// This function returns the count of real tabs.
int realTabCount() const;
private slots: private slots:
void onTabMoved(int from, int to); void onTabMoved(int from, int to);
void onCurrentChanged(int index); void onCurrentChanged(int index);