Skip to content

Commit ea4bacf

Browse files
committed
feat: (#1154) Undo Close tab
1 parent 77d5a07 commit ea4bacf

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

src/kiwixapp.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ void KiwixApp::createActions()
420420
CREATE_ACTION_SHORTCUTS(CloseCurrentTabAction, gt("close-tab"), QList<QKeySequence>({QKeySequence(Qt::CTRL | Qt::Key_F4), QKeySequence(Qt::CTRL | Qt::Key_W)}));
421421

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

425427
CREATE_ACTION_SHORTCUT(BrowseLibraryAction, gt("browse-library"), QKeySequence(Qt::CTRL | Qt::Key_E));
426428
HIDE_ACTION(BrowseLibraryAction);
@@ -620,3 +622,20 @@ QString KiwixApp::getPrevSaveDir() const
620622
QDir dir(prevSaveDir);
621623
return dir.exists() ? prevSaveDir : DEFAULT_SAVE_DIR;
622624
}
625+
626+
void KiwixApp::pushClosedTab(const QString& url, const QString& title) {
627+
if (url.isEmpty() || title.isEmpty())
628+
return;
629+
m_closedTabs.push({url, title});
630+
mpa_actions[ReopenClosedTabAction]->setEnabled(true);
631+
}
632+
633+
void KiwixApp::reopenLastClosedTab() {
634+
if (m_closedTabs.isEmpty())
635+
return;
636+
637+
auto tab = m_closedTabs.pop();
638+
openUrl(tab.url, true);
639+
if (m_closedTabs.isEmpty())
640+
mpa_actions[ReopenClosedTabAction]->setEnabled(false);
641+
}

src/kiwixapp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <mutex>
2222
#include <iostream>
23+
#include <QStack>
2324

2425
typedef TabBar::TabType TabType;
2526

@@ -145,6 +146,17 @@ public slots:
145146

146147
QString findLibraryDirectory();
147148
void loadAndInstallTranslations(QTranslator& translator, const QString& filename, const QString& directory);
149+
150+
struct ClosedTabInfo {
151+
QString url;
152+
QString title;
153+
};
154+
QStack<ClosedTabInfo> m_closedTabs;
155+
156+
public:
157+
void pushClosedTab(const QString& url, const QString& title);
158+
bool hasClosedTabs() const { return !m_closedTabs.isEmpty(); }
159+
void reopenLastClosedTab();
148160
};
149161

150162
QString gt(const QString &key);

src/tabbar.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,21 @@ QStringList TabBar::getTabZimIds() const
310310

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

317+
// Save tab info before closing
318+
if (ZimView* zv = qobject_cast<ZimView*>(mp_stackedWidget->widget(index))) {
319+
auto webView = zv->getWebView();
320+
if (webView) {
321+
KiwixApp::instance()->pushClosedTab(
322+
webView->url().toString(),
323+
webView->title()
324+
);
325+
}
326+
}
327+
318328
if ( index == currentIndex() ) {
319329
setCurrentIndex(index + 1 == realTabCount() ? index - 1 : index + 1);
320330
}
@@ -549,3 +559,13 @@ void TabBar::onTabMoved(int from, int to)
549559

550560
KiwixApp::instance()->saveListOfOpenTabs();
551561
}
562+
563+
void TabBar::contextMenuEvent(QContextMenuEvent *event)
564+
{
565+
int tabIndex = tabAt(event->pos());
566+
if (tabIndex == -1) { // Clicked outside tabs
567+
QMenu menu;
568+
menu.addAction(KiwixApp::instance()->getAction(KiwixApp::ReopenClosedTabAction));
569+
menu.exec(event->globalPos());
570+
}
571+
}

src/tabbar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class TabBar : public QTabBar
6262
void tabRemoved(int index) override;
6363
void tabInserted(int index) override;
6464
void resizeEvent(QResizeEvent *) override;
65+
void contextMenuEvent(QContextMenuEvent *event) override;
6566

6667
signals:
6768
void webActionEnabledChanged(QWebEnginePage::WebAction action, bool enabled);

0 commit comments

Comments
 (0)