1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Add ClosedWindowsManager allowing to restore closed windows

Closes #2306
This commit is contained in:
David Rosca 2018-01-02 14:22:24 +01:00
parent b3bd08c5e4
commit ef06ed567a
8 changed files with 220 additions and 5 deletions

View File

@ -185,6 +185,7 @@ set(SRCS ${SRCS}
tools/certificateinfowidget.cpp
tools/clickablelabel.cpp
tools/closedtabsmanager.cpp
tools/closedwindowsmanager.cpp
tools/colors.cpp
tools/delayedfilewatcher.cpp
tools/docktitlebarwidget.cpp

View File

@ -40,7 +40,7 @@
#include "iconprovider.h"
#include "progressbar.h"
#include "adblockicon.h"
#include "closedtabsmanager.h"
#include "closedwindowsmanager.h"
#include "statusbarmessage.h"
#include "browsinglibrary.h"
#include "navigationbar.h"
@ -476,6 +476,9 @@ void BrowserWindow::setupMenu()
QShortcut* inspectorAction = new QShortcut(QKeySequence(QSL("F12")), this);
connect(inspectorAction, SIGNAL(activated()), this, SLOT(toggleWebInspector()));
QShortcut* restoreClosedWindow = new QShortcut(QKeySequence(QSL("Ctrl+Shift+N")), this);
connect(restoreClosedWindow, &QShortcut::activated, mApp->closedWindowsManager(), &ClosedWindowsManager::restoreClosedWindow);
}
void BrowserWindow::updateStartupFocus()
@ -1483,6 +1486,7 @@ void BrowserWindow::closeEvent(QCloseEvent* event)
}
saveSettings();
mApp->closedWindowsManager()->saveWindow(this);
#ifndef Q_OS_MACOS
if (mApp->windowCount() == 1)

View File

@ -47,6 +47,7 @@
#include "html5permissions/html5permissionsmanager.h"
#include "scripts.h"
#include "sessionmanager.h"
#include "closedwindowsmanager.h"
#include <QWebEngineSettings>
#include <QDesktopServices>
@ -96,6 +97,7 @@ MainApplication::MainApplication(int &argc, char** argv)
, m_downloadManager(0)
, m_userAgentManager(0)
, m_searchEnginesManager(0)
, m_closedWindowsManager(0)
, m_html5PermissionsManager(0)
, m_desktopNotifications(0)
, m_webProfile(0)
@ -577,6 +579,14 @@ SearchEnginesManager* MainApplication::searchEnginesManager()
return m_searchEnginesManager;
}
ClosedWindowsManager* MainApplication::closedWindowsManager()
{
if (!m_closedWindowsManager) {
m_closedWindowsManager = new ClosedWindowsManager(this);
}
return m_closedWindowsManager;
}
HTML5PermissionsManager* MainApplication::html5PermissionsManager()
{
if (!m_html5PermissionsManager) {

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -50,6 +50,7 @@ class RegisterQAppAssociation;
class DesktopNotificationsFactory;
class ProxyStyle;
class SessionManager;
class ClosedWindowsManager;
class FALKON_EXPORT MainApplication : public QtSingleApplication
{
@ -107,6 +108,7 @@ public:
DownloadManager* downloadManager();
UserAgentManager* userAgentManager();
SearchEnginesManager* searchEnginesManager();
ClosedWindowsManager* closedWindowsManager();
HTML5PermissionsManager* html5PermissionsManager();
DesktopNotificationsFactory* desktopNotifications();
QWebEngineProfile* webProfile() const;
@ -178,6 +180,7 @@ private:
DownloadManager* m_downloadManager;
UserAgentManager* m_userAgentManager;
SearchEnginesManager* m_searchEnginesManager;
ClosedWindowsManager* m_closedWindowsManager;
HTML5PermissionsManager* m_html5PermissionsManager;
DesktopNotificationsFactory* m_desktopNotifications;
QWebEngineProfile* m_webProfile;

View File

@ -26,6 +26,7 @@
#include "history.h"
#include "qzsettings.h"
#include "sqldatabase.h"
#include "closedwindowsmanager.h"
#include <QApplication>
#include <QWebEngineHistory>
@ -79,8 +80,8 @@ void HistoryMenu::aboutToShow()
actions().at(1)->setEnabled(view->history()->canGoForward());
}
while (actions().count() != 7) {
QAction* act = actions().at(7);
while (actions().count() != 8) {
QAction* act = actions().at(8);
if (act->menu()) {
act->menu()->clear();
}
@ -162,6 +163,34 @@ void HistoryMenu::aboutToShowClosedTabs()
}
}
void HistoryMenu::aboutToShowClosedWindows()
{
m_menuClosedWindows->clear();
ClosedWindowsManager *manager = mApp->closedWindowsManager();
const auto closedWindows = manager->closedWindows();
for (int i = 0; i < closedWindows.count(); ++i) {
const ClosedWindowsManager::Window window = closedWindows.at(i);
const QString title = QzTools::truncatedText(window.title, 40);
QAction *act = m_menuClosedWindows->addAction(window.icon, title, manager, SLOT(restoreClosedWindow()));
if (i == 0) {
act->setShortcut(QKeySequence(QSL("Ctrl+Shift+N")));
act->setShortcutContext(Qt::WidgetShortcut);
} else {
act->setData(i);
}
}
if (m_menuClosedWindows->isEmpty()) {
m_menuClosedWindows->addAction(tr("Empty"))->setEnabled(false);
} else {
m_menuClosedWindows->addSeparator();
m_menuClosedWindows->addAction(tr("Restore All Closed Windows"), manager, SLOT(restoreAllClosedWindows()));
m_menuClosedWindows->addAction(tr("Clear list"), manager, SLOT(clearClosedWindows()));
}
}
void HistoryMenu::historyEntryActivated()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
@ -229,6 +258,10 @@ void HistoryMenu::init()
m_menuClosedTabs = new Menu(tr("Closed Tabs"));
connect(m_menuClosedTabs, SIGNAL(aboutToShow()), this, SLOT(aboutToShowClosedTabs()));
m_menuClosedWindows = new Menu(tr("Closed Windows"));
connect(m_menuClosedWindows, &QMenu::aboutToShow, this, &HistoryMenu::aboutToShowClosedWindows);
addMenu(m_menuMostVisited);
addMenu(m_menuClosedTabs);
addMenu(m_menuClosedWindows);
}

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
* Copyright (C) 2014-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -47,6 +47,7 @@ private slots:
void aboutToShowMostVisited();
void aboutToShowClosedTabs();
void aboutToShowClosedWindows();
void historyEntryActivated();
void historyEntryCtrlActivated();
@ -62,6 +63,7 @@ private:
QPointer<BrowserWindow> m_window;
Menu* m_menuMostVisited;
Menu* m_menuClosedTabs;
Menu *m_menuClosedWindows;
};
#endif // HISTORYMENU_H

View File

@ -0,0 +1,99 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "closedwindowsmanager.h"
#include "mainapplication.h"
#include "tabbedwebview.h"
#include "qztools.h"
#include <QAction>
ClosedWindowsManager::ClosedWindowsManager(QObject *parent)
: QObject(parent)
{
}
bool ClosedWindowsManager::isClosedWindowAvailable() const
{
return !m_closedWindows.isEmpty();
}
QVector<ClosedWindowsManager::Window> ClosedWindowsManager::closedWindows() const
{
return m_closedWindows;
}
void ClosedWindowsManager::saveWindow(BrowserWindow *window)
{
if (mApp->isPrivate()) {
return;
}
Window closedWindow;
closedWindow.icon = window->weView()->icon();
closedWindow.title = window->weView()->title();
closedWindow.windowState = BrowserWindow::SavedWindow(window);
m_closedWindows.prepend(closedWindow);
}
ClosedWindowsManager::Window ClosedWindowsManager::takeLastClosedWindow()
{
Window window;
if (!m_closedWindows.isEmpty()) {
window = m_closedWindows.takeFirst();
}
return window;
}
ClosedWindowsManager::Window ClosedWindowsManager::takeClosedWindowAt(int index)
{
Window window;
if (QzTools::containsIndex(m_closedWindows, index)) {
window = m_closedWindows.takeAt(index);
}
return window;
}
void ClosedWindowsManager::restoreClosedWindow()
{
Window window;
QAction *act = qobject_cast<QAction*>(sender());
if (act) {
window = takeClosedWindowAt(act->data().toInt());
} else {
window = takeLastClosedWindow();
}
if (!window.isValid()) {
return;
}
mApp->createWindow(Qz::BW_OtherRestoredWindow)->restoreWindow(window.windowState);
}
void ClosedWindowsManager::restoreAllClosedWindows()
{
const int count = m_closedWindows.count();
for (int i = 0; i < count; ++i) {
restoreClosedWindow();
}
}
void ClosedWindowsManager::clearClosedWindows()
{
m_closedWindows.clear();
}

View File

@ -0,0 +1,63 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include <QObject>
#include <QVector>
#include "qzcommon.h"
#include "browserwindow.h"
class QUPZILLA_EXPORT ClosedWindowsManager : public QObject
{
Q_OBJECT
public:
struct Window {
QIcon icon;
QString title;
BrowserWindow::SavedWindow windowState;
bool isValid() const {
return windowState.isValid();
}
};
explicit ClosedWindowsManager(QObject *parent = nullptr);
bool isClosedWindowAvailable() const;
QVector<Window> closedWindows() const;
void saveWindow(BrowserWindow *window);
// Takes window that was most recently closed
Window takeLastClosedWindow();
// Takes window at given index
Window takeClosedWindowAt(int index);
public slots:
void restoreClosedWindow();
void restoreAllClosedWindows();
void clearClosedWindows();
private:
QVector<Window> m_closedWindows;
};
// Hint to Qt to use std::realloc on item moving
Q_DECLARE_TYPEINFO(ClosedWindowsManager::Window, Q_MOVABLE_TYPE);