diff --git a/src/NotepadNext/NotepadNextApplication.cpp b/src/NotepadNext/NotepadNextApplication.cpp index f223f49db..ec156e98e 100644 --- a/src/NotepadNext/NotepadNextApplication.cpp +++ b/src/NotepadNext/NotepadNextApplication.cpp @@ -440,6 +440,19 @@ void NotepadNextApplication::saveSettings() getSettings()->setValue("App/RecentFilesList", recentFilesListManager->fileList()); } +void NotepadNextApplication::saveSession() +{ + // Iterate all the opened editors and add them to the recent file list + for (const auto &editor : window->editors()) { + if (editor->isFile()) { + recentFilesListManager->addFile(editor->getFilePath()); + } + } + saveSettings(); + + getSessionManager()->saveSession(window); +} + MainWindow *NotepadNextApplication::createNewWindow() { Q_ASSERT(window == Q_NULLPTR); @@ -451,16 +464,14 @@ MainWindow *NotepadNextApplication::createNewWindow() LuaExtension::Instance().setEditor(editor); }); - // Since these editors don't actually get "closed" go ahead and add them to the recent file list - connect(window, &MainWindow::aboutToClose, this, [=]() { - for (const auto &editor : window->editors()) { - if (editor->isFile()) { - recentFilesListManager->addFile(editor->getFilePath()); - } - } + // TODO: this shouldn't be dependent on a MainWindow closing, but this works for now + // since the assumption is MainWindow::aboutToClose() infers the application is shutting + // down but the editors are still active. + connect(window, &MainWindow::aboutToClose, this, &NotepadNextApplication::saveSession); - getSessionManager()->saveSession(window); - }); + // Timer to autosave the session + connect(&autoSaveTimer, &QTimer::timeout, this, &NotepadNextApplication::saveSession); + autoSaveTimer.start(60 * 1000); return window; } diff --git a/src/NotepadNext/NotepadNextApplication.h b/src/NotepadNext/NotepadNextApplication.h index cdf48ca2c..b44af9673 100644 --- a/src/NotepadNext/NotepadNextApplication.h +++ b/src/NotepadNext/NotepadNextApplication.h @@ -26,6 +26,7 @@ #include #include +#include class MainWindow; @@ -75,6 +76,7 @@ class NotepadNextApplication : public SingleApplication private slots: void saveSettings(); void receiveInfoFromSecondaryInstance(quint32 instanceId, QByteArray message); + void saveSession(); private: void openFiles(const QStringList &files); @@ -95,6 +97,8 @@ private slots: MainWindow *createNewWindow(); QCommandLineParser parser; + + QTimer autoSaveTimer; //save automatically the session }; #endif // NOTEPADNEXTAPPLICATION_H diff --git a/src/NotepadNext/SessionManager.cpp b/src/NotepadNext/SessionManager.cpp index 9249fcb27..92f2f9ec0 100644 --- a/src/NotepadNext/SessionManager.cpp +++ b/src/NotepadNext/SessionManager.cpp @@ -316,10 +316,10 @@ ScintillaNext *SessionManager::loadUnsavedFileDetails(QSettings &settings) editor->setFileInfo(filePath); editor->setTemporary(true); - loadEditorViewDetails(editor, settings); - app->getEditorManager()->manageEditor(editor); + loadEditorViewDetails(editor, settings); + return editor; } else {