From 5e4db57189457c0fdc26f8dd1279a3ca0cacd187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Fri, 1 Jan 2021 00:59:19 -0300 Subject: [PATCH] ecode: Minor refactor. --- src/tools/codeeditor/codeeditor.cpp | 132 +++++++++++++++------------- src/tools/codeeditor/codeeditor.hpp | 3 + 2 files changed, 72 insertions(+), 63 deletions(-) diff --git a/src/tools/codeeditor/codeeditor.cpp b/src/tools/codeeditor/codeeditor.cpp index 1c8dbb611..4f1e3021d 100644 --- a/src/tools/codeeditor/codeeditor.cpp +++ b/src/tools/codeeditor/codeeditor.cpp @@ -705,6 +705,68 @@ void App::updateGlobalSearchBarResults( const std::string& search, mGlobalSearchTree->expandAll(); } +void App::doGlobalSearch( const String& text, bool caseSensitive, bool wholeWord, + bool luaPattern ) { + if ( mDirTree && mDirTree->getFilesCount() > 0 && !text.empty() ) { + UILoader* loader = UILoader::New(); + loader->setId( "loader" ); + loader->setRadius( 48 ); + loader->setOutlineThickness( 6 ); + loader->setFillColor( Color::Red ); + loader->setParent( mGlobalSearchTree->getParent() ); + loader->setPosition( mGlobalSearchTree->getPosition() + + mGlobalSearchTree->getSize() * 0.5f - loader->getSize() * 0.5f ); + Clock* clock = eeNew( Clock, () ); + std::string search( text.toUtf8() ); + ProjectSearch::find( + mDirTree->getFiles(), search, +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) + mThreadPool, +#endif + [&, clock, search, loader]( const ProjectSearch::Result& res ) { + Log::info( "Global search for \"%s\" took %.2fms", search.c_str(), + clock->getElapsedTime().asMilliseconds() ); + eeDelete( clock ); + mUISceneNode->runOnMainThread( [&, loader, res, search] { + auto model = ProjectSearch::asModel( res ); + mGlobalSearchHistory.push_back( std::make_pair( search, model ) ); + if ( mGlobalSearchHistory.size() > 10 ) + mGlobalSearchHistory.pop_front(); + + std::vector items; + for ( auto item = mGlobalSearchHistory.rbegin(); + item != mGlobalSearchHistory.rend(); item++ ) { + items.push_back( item->first ); + } + + auto listBox = mGlobalSearchHistoryList->getListBox(); + listBox->clear(); + listBox->addListBoxItems( items ); + if ( mGlobalSearchHistoryOnItemSelectedCb ) + mGlobalSearchHistoryList->removeEventListener( + mGlobalSearchHistoryOnItemSelectedCb ); + listBox->setSelected( 0 ); + mGlobalSearchHistoryOnItemSelectedCb = + mGlobalSearchHistoryList->addEventListener( + Event::OnItemSelected, [&]( const Event* ) { + auto idx = + mGlobalSearchHistoryList->getListBox()->getItemSelectedIndex(); + auto idxItem = mGlobalSearchHistory.at( + mGlobalSearchHistory.size() - 1 - idx ); + updateGlobalSearchBarResults( idxItem.first, idxItem.second ); + } ); + + updateGlobalSearchBarResults( search, model ); + loader->setVisible( false ); + loader->close(); + } ); + }, + caseSensitive, wholeWord, + luaPattern ? TextDocument::FindReplaceType::LuaPattern + : TextDocument::FindReplaceType::Normal ); + } +} + void App::initGlobalSearchBar() { auto addClickListener = [&]( UIWidget* widget, std::string cmd ) { widget->addEventListener( Event::MouseClick, [this, cmd]( const Event* event ) { @@ -721,67 +783,11 @@ void App::initGlobalSearchBar() { mGlobalSearchInput = mGlobalSearchBarLayout->find( "global_search_find" ); mGlobalSearchHistoryList = mGlobalSearchBarLayout->find( "global_search_history" ); - mGlobalSearchBarLayout->addCommand( "search-in-files", [&, caseSensitiveChk, wholeWordChk, - luaPatternChk] { - if ( mDirTree && mDirTree->getFilesCount() > 0 && !mGlobalSearchInput->getText().empty() ) { - UILoader* loader = UILoader::New(); - loader->setId( "loader" ); - loader->setRadius( 48 ); - loader->setOutlineThickness( 6 ); - loader->setFillColor( Color::Red ); - loader->setParent( mGlobalSearchTree->getParent() ); - loader->setPosition( mGlobalSearchTree->getPosition() + - mGlobalSearchTree->getSize() * 0.5f - loader->getSize() * 0.5f ); - Clock* clock = eeNew( Clock, () ); - std::string search( mGlobalSearchInput->getText().toUtf8() ); - ProjectSearch::find( - mDirTree->getFiles(), search, -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) - mThreadPool, -#endif - [&, clock, search, loader]( const ProjectSearch::Result& res ) { - Log::info( "Global search for \"%s\" took %.2fms", search.c_str(), - clock->getElapsedTime().asMilliseconds() ); - eeDelete( clock ); - mUISceneNode->runOnMainThread( [&, loader, res, search] { - auto model = ProjectSearch::asModel( res ); - mGlobalSearchHistory.push_back( std::make_pair( search, model ) ); - if ( mGlobalSearchHistory.size() > 10 ) - mGlobalSearchHistory.pop_front(); - - std::vector items; - for ( auto item = mGlobalSearchHistory.rbegin(); - item != mGlobalSearchHistory.rend(); item++ ) { - items.push_back( item->first ); - } - - auto listBox = mGlobalSearchHistoryList->getListBox(); - listBox->clear(); - listBox->addListBoxItems( items ); - if ( mGlobalSearchHistoryOnItemSelectedCb ) - mGlobalSearchHistoryList->removeEventListener( - mGlobalSearchHistoryOnItemSelectedCb ); - listBox->setSelected( 0 ); - mGlobalSearchHistoryOnItemSelectedCb = - mGlobalSearchHistoryList->addEventListener( - Event::OnItemSelected, [&]( const Event* ) { - auto idx = mGlobalSearchHistoryList->getListBox() - ->getItemSelectedIndex(); - auto idxItem = mGlobalSearchHistory.at( - mGlobalSearchHistory.size() - 1 - idx ); - updateGlobalSearchBarResults( idxItem.first, idxItem.second ); - } ); - - updateGlobalSearchBarResults( search, model ); - loader->setVisible( false ); - loader->close(); - } ); - }, - caseSensitiveChk->isChecked(), wholeWordChk->isChecked(), - luaPatternChk->isChecked() ? TextDocument::FindReplaceType::LuaPattern - : TextDocument::FindReplaceType::Normal ); - } - } ); + mGlobalSearchBarLayout->addCommand( + "search-in-files", [&, caseSensitiveChk, wholeWordChk, luaPatternChk] { + doGlobalSearch( mGlobalSearchInput->getText(), caseSensitiveChk->isChecked(), + wholeWordChk->isChecked(), luaPatternChk->isChecked() ); + } ); mGlobalSearchBarLayout->addCommand( "close-global-searchbar", [&] { hideGlobalSearchBar(); if ( mEditorSplitter->getCurEditor() ) @@ -1810,7 +1816,7 @@ void App::loadFileFromPath( const std::string& path, bool inNewTab, UICodeEditor if ( imageView ) { mImageLayout->setEnabled( true )->setVisible( true ); loaderView->setVisible( true ); -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) mThreadPool->run( [&, imageView, loaderView, path]() { #endif @@ -1826,7 +1832,7 @@ void App::loadFileFromPath( const std::string& path, bool inNewTab, UICodeEditor imageView->setDrawable( nullptr ); loaderView->setVisible( false ); } -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) }, []() {} ); #endif diff --git a/src/tools/codeeditor/codeeditor.hpp b/src/tools/codeeditor/codeeditor.hpp index 8a18fe0dc..22e6146bd 100644 --- a/src/tools/codeeditor/codeeditor.hpp +++ b/src/tools/codeeditor/codeeditor.hpp @@ -148,6 +148,8 @@ class App : public UICodeEditorSplitter::Client { void saveAll(); + void doGlobalSearch( const String& text, bool caseSensitive, bool wholeWord, bool luaPattern ); + protected: EE::Window::Window* mWindow{ nullptr }; UISceneNode* mUISceneNode{ nullptr }; @@ -192,6 +194,7 @@ class App : public UICodeEditorSplitter::Client { std::shared_ptr mFileSystemModel; UITableView* mLocateTable{ nullptr }; UITextInput* mLocateInput{ nullptr }; + UILayout* mGlobalSearchLayout{ nullptr }; UITreeViewGlobalSearch* mGlobalSearchTree{ nullptr }; UITextInput* mGlobalSearchInput{ nullptr }; UIDropDownList* mGlobalSearchHistoryList{ nullptr };