From 0ef95ec3d4105e45f20b115f83ffa6355e319df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 2 Apr 2022 01:05:03 -0300 Subject: [PATCH] ecode: Fixed load callback when loading a file from the GlobalSearchController. --- include/eepp/ui/models/variant.hpp | 2 +- .../window/backend/SDL2/clipboardsdl2.cpp | 4 +-- .../window/backend/SDL2/clipboardsdl2.hpp | 2 +- src/tools/ecode/ecode.cpp | 16 +++------ src/tools/ecode/ecode.hpp | 4 ++- src/tools/ecode/globalsearchcontroller.cpp | 34 ++++++++++++------- src/tools/ecode/globalsearchcontroller.hpp | 2 ++ 7 files changed, 35 insertions(+), 29 deletions(-) diff --git a/include/eepp/ui/models/variant.hpp b/include/eepp/ui/models/variant.hpp index 4ead32f48..8379b82a8 100644 --- a/include/eepp/ui/models/variant.hpp +++ b/include/eepp/ui/models/variant.hpp @@ -95,7 +95,7 @@ class EE_API Variant { } mType = Type::Invalid; } - bool isValid() { return mType != Type::Invalid; } + bool isValid() const { return mType != Type::Invalid; } std::string toString() const { switch ( mType ) { diff --git a/src/eepp/window/backend/SDL2/clipboardsdl2.cpp b/src/eepp/window/backend/SDL2/clipboardsdl2.cpp index 42528aa8a..781b6c288 100644 --- a/src/eepp/window/backend/SDL2/clipboardsdl2.cpp +++ b/src/eepp/window/backend/SDL2/clipboardsdl2.cpp @@ -11,8 +11,8 @@ ClipboardSDL::~ClipboardSDL() {} void ClipboardSDL::init() {} -void ClipboardSDL::setText( const std::string& Text ) { - SDL_SetClipboardText( Text.c_str() ); +void ClipboardSDL::setText( const std::string& text ) { + SDL_SetClipboardText( text.c_str() ); } std::string ClipboardSDL::getText() { diff --git a/src/eepp/window/backend/SDL2/clipboardsdl2.hpp b/src/eepp/window/backend/SDL2/clipboardsdl2.hpp index 35a06dd21..43c264f08 100644 --- a/src/eepp/window/backend/SDL2/clipboardsdl2.hpp +++ b/src/eepp/window/backend/SDL2/clipboardsdl2.hpp @@ -19,7 +19,7 @@ class EE_API ClipboardSDL : public Clipboard { String getWideText(); - void setText( const std::string& Text ); + void setText( const std::string& text ); protected: friend class WindowSDL; diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 25a086830..fe0dacb7b 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -1295,7 +1295,9 @@ void App::createDocAlert( UICodeEditor* editor ) { } ); } -void App::loadFileFromPath( const std::string& path, bool inNewTab, UICodeEditor* codeEditor ) { +void App::loadFileFromPath( + const std::string& path, bool inNewTab, UICodeEditor* codeEditor, + std::function onLoaded ) { if ( Image::isImageExtension( path ) && Image::isImage( path ) ) { UIImage* imageView = mImageLayout->findByType( UI_TYPE_IMAGE ); UILoader* loaderView = mImageLayout->findByType( UI_TYPE_LOADER ); @@ -1324,19 +1326,11 @@ void App::loadFileFromPath( const std::string& path, bool inNewTab, UICodeEditor #endif } } else { -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) if ( inNewTab ) { - mEditorSplitter->loadAsyncFileFromPathInNewTab( path, mThreadPool ); + mEditorSplitter->loadAsyncFileFromPathInNewTab( path, mThreadPool, onLoaded ); } else { - mEditorSplitter->loadAsyncFileFromPath( path, mThreadPool, codeEditor ); + mEditorSplitter->loadAsyncFileFromPath( path, mThreadPool, codeEditor, onLoaded ); } -#else - if ( inNewTab ) { - mEditorSplitter->loadFileFromPathInNewTab( path ); - } else { - mEditorSplitter->loadFileFromPath( path, codeEditor ); - } -#endif } } diff --git a/src/tools/ecode/ecode.hpp b/src/tools/ecode/ecode.hpp index 6812c67b4..36944576f 100644 --- a/src/tools/ecode/ecode.hpp +++ b/src/tools/ecode/ecode.hpp @@ -59,7 +59,9 @@ class App : public UICodeEditorSplitter::Client { std::shared_ptr getThreadPool() const; void loadFileFromPath( const std::string& path, bool inNewTab = true, - UICodeEditor* codeEditor = nullptr ); + UICodeEditor* codeEditor = nullptr, + std::function onLoaded = + std::function() ); void hideGlobalSearchBar(); diff --git a/src/tools/ecode/globalsearchcontroller.cpp b/src/tools/ecode/globalsearchcontroller.cpp index 231b98f45..12ef61956 100644 --- a/src/tools/ecode/globalsearchcontroller.cpp +++ b/src/tools/ecode/globalsearchcontroller.cpp @@ -411,6 +411,16 @@ void GlobalSearchController::doGlobalSearch( String text, bool caseSensitive, bo } } +void GlobalSearchController::onLoadDone( const Variant& lineNum, const Variant& colNum ) { + if ( mEditorSplitter->getCurEditor() && lineNum.isValid() && colNum.isValid() && + lineNum.is( Variant::Type::Int64 ) && colNum.is( Variant::Type::Int64 ) ) { + TextPosition pos{ lineNum.asInt64(), colNum.asInt64() }; + mEditorSplitter->getCurEditor()->getDocument().setSelection( pos ); + mEditorSplitter->getCurEditor()->goToLine( pos ); + hideGlobalSearchBar(); + } +} + void GlobalSearchController::initGlobalSearchTree( UITreeViewGlobalSearch* searchTree ) { searchTree->addClass( "search_tree" ); searchTree->setParent( mGlobalSearchLayout ); @@ -451,13 +461,6 @@ void GlobalSearchController::initGlobalSearchTree( UITreeViewGlobalSearch* searc if ( vPath.isValid() && vPath.is( Variant::Type::cstr ) ) { std::string path( vPath.asCStr() ); UITab* tab = mEditorSplitter->isDocumentOpen( path ); - if ( !tab ) { - FileInfo fileInfo( path ); - if ( fileInfo.exists() && fileInfo.isRegularFile() ) - mApp->loadFileFromPath( path ); - } else { - tab->getTabWidget()->setTabSelected( tab ); - } Variant lineNum( model->data( model->index( modelEvent->getModelIndex().row(), ProjectSearch::ResultModel::FileOrPosition, @@ -467,12 +470,17 @@ void GlobalSearchController::initGlobalSearchTree( UITreeViewGlobalSearch* searc ProjectSearch::ResultModel::ColumnStart, modelEvent->getModelIndex().parent() ), ModelRole::Custom ) ); - if ( mEditorSplitter->getCurEditor() && lineNum.isValid() && colNum.isValid() && - lineNum.is( Variant::Type::Int64 ) && colNum.is( Variant::Type::Int64 ) ) { - TextPosition pos{ lineNum.asInt64(), colNum.asInt64() }; - mEditorSplitter->getCurEditor()->getDocument().setSelection( pos ); - mEditorSplitter->getCurEditor()->goToLine( pos ); - hideGlobalSearchBar(); + if ( !tab ) { + FileInfo fileInfo( path ); + if ( fileInfo.exists() && fileInfo.isRegularFile() ) + mApp->loadFileFromPath( + path, true, nullptr, + [&, lineNum, colNum]( UICodeEditor*, const std::string& ) { + onLoadDone( lineNum, colNum ); + } ); + } else { + tab->getTabWidget()->setTabSelected( tab ); + onLoadDone( lineNum, colNum ); } } } diff --git a/src/tools/ecode/globalsearchcontroller.hpp b/src/tools/ecode/globalsearchcontroller.hpp index d06d72a81..88bfceb99 100644 --- a/src/tools/ecode/globalsearchcontroller.hpp +++ b/src/tools/ecode/globalsearchcontroller.hpp @@ -52,6 +52,8 @@ class GlobalSearchController { Uint32 mGlobalSearchHistoryOnItemSelectedCb{ 0 }; std::deque>> mGlobalSearchHistory; + + void onLoadDone( const Variant& lineNum, const Variant& colNum ); }; #endif // GLOBALSEARCHCONTROLLER_HPP