diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 1e5dc5041..561260b34 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -503,6 +503,11 @@ void App::updateRecentFiles() { menu->removeEventsOfType( Event::OnItemClicked ); if ( mRecentFiles.empty() ) return; + menu->add( i18n( "reopen_closed_editor", "Reopen Closed Editor" ), nullptr, + getKeybind( "reopen-closed-tab" ) ) + ->setId( "reopen-closed-tab" ) + ->setEnabled( !mRecentClosedFiles.empty() ); + menu->addSeparator(); for ( const auto& file : mRecentFiles ) menu->add( file ); menu->addSeparator(); @@ -511,7 +516,12 @@ void App::updateRecentFiles() { if ( !event->getNode()->isType( UI_TYPE_MENUITEM ) ) return; const std::string& id = event->getNode()->asType()->getId(); - if ( id != "clear-menu" ) { + if ( id == "reopen-closed-tab" ) { + reopenClosedTab(); + } else if ( id == "clear-menu" ) { + mRecentFiles.clear(); + updateRecentFiles(); + } else { const String& txt = event->getNode()->asType()->getText(); std::string path( txt.toUtf8() ); if ( ( FileSystem::fileExists( path ) && !FileSystem::isDirectory( path ) ) || @@ -519,9 +529,6 @@ void App::updateRecentFiles() { String::startsWith( path, "http://" ) ) { loadFileFromPath( path ); } - } else { - mRecentFiles.clear(); - updateRecentFiles(); } } ); } @@ -1946,6 +1953,7 @@ std::map App::getLocalKeybindings() { { { KEY_I, KEYMOD_CTRL | KEYMOD_LALT | KEYMOD_SHIFT }, "terminal-split-top" }, { { KEY_K, KEYMOD_CTRL | KEYMOD_LALT | KEYMOD_SHIFT }, "terminal-split-bottom" }, { { KEY_S, KEYMOD_CTRL | KEYMOD_LALT | KEYMOD_SHIFT }, "terminal-split-swap" }, + { { KEY_T, KEYMOD_CTRL | KEYMOD_LALT | KEYMOD_SHIFT }, "reopen-closed-tab" }, }; } @@ -1955,7 +1963,7 @@ std::vector App::getUnlockedCommands() { "open-global-search", "menu-toggle", "switch-side-panel", "download-file-web", "create-new-terminal", "terminal-split-left", "terminal-split-right", "terminal-split-top", "terminal-split-bottom", - "terminal-split-swap" }; + "terminal-split-swap", "reopen-closed-tab" }; } bool App::isUnlockedCommand( const std::string& command ) { @@ -1964,6 +1972,8 @@ bool App::isUnlockedCommand( const std::string& command ) { } void App::closeEditors() { + mRecentClosedFiles = {}; + mConfig.saveProject( mCurrentProject, mSplitter, mConfigPath, mProjectDocConfig ); std::vector editors = mSplitter->getAllEditors(); while ( !editors.empty() ) { @@ -2250,6 +2260,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { false ); doc.execute( "create-new-terminal" ); } ); + doc.setCommand( "reopen-closed-tab", [&] { reopenClosedTab(); } ); editor->addEventListener( Event::OnDocumentSave, [&]( const Event* event ) { UICodeEditor* editor = event->getNode()->asType(); @@ -2299,6 +2310,8 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { std::string dir( FileSystem::fileRemoveFileName( docEvent->getDoc()->getFilePath() ) ); if ( dir.empty() ) return; + mRecentClosedFiles.push( docEvent->getDoc()->getFilePath() ); + updatedReopenClosedFileState(); Lock l( mWatchesLock ); auto itWatch = mFilesFolderWatches.find( dir ); if ( mFileWatcher && itWatch != mFilesFolderWatches.end() ) { @@ -2462,6 +2475,26 @@ void App::toggleSettingsMenu() { } } +void App::updatedReopenClosedFileState() { + if ( mRecentFilesMenu ) { + auto* reopenBtn = mRecentFilesMenu->find( "reopen-closed-tab" ); + if ( reopenBtn ) + reopenBtn->setEnabled( !mRecentClosedFiles.empty() ); + } +} + +void App::reopenClosedTab() { + if ( mRecentClosedFiles.empty() ) + return; + + auto prevTabPath = mRecentClosedFiles.top(); + mRecentClosedFiles.pop(); + + updatedReopenClosedFileState(); + + loadFileFromPath( prevTabPath ); +} + void App::createSettingsMenu() { Clock clock; mSettingsMenu = UIPopUpMenu::New(); @@ -2488,7 +2521,7 @@ void App::createSettingsMenu() { ->setId( "download-file-web" ); mSettingsMenu ->addSubMenu( i18n( "recent_files", "Recent Files" ), findIcon( "document-recent" ), - UIPopUpMenu::New() ) + ( mRecentFilesMenu = UIPopUpMenu::New() ) ) ->setId( "menu-recent-files" ); mSettingsMenu ->addSubMenu( i18n( "recent_folders", "Recent Folders" ), findIcon( "document-recent" ), diff --git a/src/tools/ecode/ecode.hpp b/src/tools/ecode/ecode.hpp index 04c5486f5..644c83a21 100644 --- a/src/tools/ecode/ecode.hpp +++ b/src/tools/ecode/ecode.hpp @@ -16,6 +16,7 @@ #include #include #include +#include using namespace eterm::UI; @@ -137,6 +138,10 @@ class App : public UICodeEditorSplitter::Client { UISceneNode* uiSceneNode() const { return mUISceneNode; } + void reopenClosedTab(); + + void updatedReopenClosedFileState(); + protected: EE::Window::Window* mWindow{ nullptr }; UISceneNode* mUISceneNode{ nullptr }; @@ -146,6 +151,7 @@ class App : public UICodeEditorSplitter::Client { UILayout* mBaseLayout{ nullptr }; UILayout* mImageLayout{ nullptr }; UIPopUpMenu* mSettingsMenu{ nullptr }; + UIPopUpMenu* mRecentFilesMenu{ nullptr }; UITextView* mSettingsButton{ nullptr }; std::vector mColorSchemeMenues; Float mColorSchemeMenuesCreatedWithHeight{ 0 }; @@ -154,6 +160,7 @@ class App : public UICodeEditorSplitter::Client { UILinearLayout* mDocInfo{ nullptr }; UITextView* mDocInfoText{ nullptr }; std::vector mRecentFiles; + std::stack mRecentClosedFiles; std::vector mRecentFolders; AppConfig mConfig; UIPopUpMenu* mDocMenu{ nullptr }; diff --git a/src/tools/ecode/terminalmanager.cpp b/src/tools/ecode/terminalmanager.cpp index c05183010..5ef8a9e0e 100644 --- a/src/tools/ecode/terminalmanager.cpp +++ b/src/tools/ecode/terminalmanager.cpp @@ -331,6 +331,7 @@ UITerminal* TerminalManager::createNewTerminal( const std::string& title, UITabW ? it->first : mTerminalColorSchemes.begin()->first ); } ); + term->setCommand( "reopen-closed-tab", [&] { mApp->reopenClosedTab(); } ); // debug-draw-highlight-toggle // debug-draw-boxes-toggle // debug-draw-debug-data