Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (#1154) undo close tab #1342

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ void KiwixApp::createActions()
CREATE_ACTION_SHORTCUTS(CloseCurrentTabAction, gt("close-tab"), QList<QKeySequence>({QKeySequence(Qt::CTRL | Qt::Key_F4), QKeySequence(Qt::CTRL | Qt::Key_W)}));

CREATE_ACTION_SHORTCUT(ReopenClosedTabAction, gt("reopen-closed-tab"), QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_T));
HIDE_ACTION(ReopenClosedTabAction);
mpa_actions[ReopenClosedTabAction]->setEnabled(false);
connect(mpa_actions[ReopenClosedTabAction], &QAction::triggered,
this, &KiwixApp::reopenLastClosedTab);

CREATE_ACTION_SHORTCUT(BrowseLibraryAction, gt("browse-library"), QKeySequence(Qt::CTRL | Qt::Key_E));
HIDE_ACTION(BrowseLibraryAction);
Expand Down Expand Up @@ -589,3 +591,20 @@ QString KiwixApp::getPrevSaveDir() const
QDir dir(prevSaveDir);
return dir.exists() ? prevSaveDir : DEFAULT_SAVE_DIR;
}

void KiwixApp::pushClosedTab(const QString& url, const QString& title) {
if (url.isEmpty() || title.isEmpty())
return;
m_closedTabs.push({url, title});
mpa_actions[ReopenClosedTabAction]->setEnabled(true);
}

void KiwixApp::reopenLastClosedTab() {
if (m_closedTabs.isEmpty())
return;

auto tab = m_closedTabs.pop();
openUrl(tab.url, true);
if (m_closedTabs.isEmpty())
mpa_actions[ReopenClosedTabAction]->setEnabled(false);
}
12 changes: 12 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <mutex>
#include <iostream>
#include <QStack>

typedef TabBar::TabType TabType;

Expand Down Expand Up @@ -141,6 +142,17 @@ public slots:

QString findLibraryDirectory();
void loadAndInstallTranslations(QTranslator& translator, const QString& filename, const QString& directory);

struct ClosedTabInfo {
QString url;
QString title;
};
QStack<ClosedTabInfo> m_closedTabs;

public:
void pushClosedTab(const QString& url, const QString& title);
bool hasClosedTabs() const { return !m_closedTabs.isEmpty(); }
void reopenLastClosedTab();
};

QString gt(const QString &key);
Expand Down
24 changes: 22 additions & 2 deletions src/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,21 @@ QStringList TabBar::getTabZimIds() const

void TabBar::closeTab(int index)
{
// The first and last tabs (i.e. the library tab and the + (new tab) button)
// cannot be closed
// First and last tabs cannot be closed
if (index <= 0 || index >= this->realTabCount())
return;

// Save tab info before closing
if (ZimView* zv = qobject_cast<ZimView*>(mp_stackedWidget->widget(index))) {
auto webView = zv->getWebView();
if (webView) {
KiwixApp::instance()->pushClosedTab(
webView->url().toString(),
webView->title()
);
}
}

if ( index == currentIndex() ) {
setCurrentIndex(index + 1 == realTabCount() ? index - 1 : index + 1);
}
Expand Down Expand Up @@ -549,3 +559,13 @@ void TabBar::onTabMoved(int from, int to)

KiwixApp::instance()->saveListOfOpenTabs();
}

void TabBar::contextMenuEvent(QContextMenuEvent *event)
{
int tabIndex = tabAt(event->pos());
if (tabIndex == -1) { // Clicked outside tabs
QMenu menu;
menu.addAction(KiwixApp::instance()->getAction(KiwixApp::ReopenClosedTabAction));
menu.exec(event->globalPos());
}
}
1 change: 1 addition & 0 deletions src/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class TabBar : public QTabBar
void tabRemoved(int index) override;
void tabInserted(int index) override;
void resizeEvent(QResizeEvent *) override;
void contextMenuEvent(QContextMenuEvent *event) override;

signals:
void webActionEnabledChanged(QWebEnginePage::WebAction action, bool enabled);
Expand Down