diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index 7b905414f..a69832dea 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -1001,11 +1001,11 @@ bool UICodeEditor::onCreateContextMenu( const Vector2i& position, const Uint32& UIPopUpMenu* menu = UIPopUpMenu::New(); + createDefaultContextMenuOptions( menu ); + ContextMenuEvent event( this, menu, Event::OnCreateContextMenu, position, flags ); sendEvent( &event ); - createDefaultContextMenuOptions( menu ); - for ( auto& plugin : mPlugins ) if ( plugin->onCreateContextMenu( this, menu, position, flags ) ) return false; diff --git a/src/eepp/ui/uitextinput.cpp b/src/eepp/ui/uitextinput.cpp index 8dc87091c..abad9a80f 100644 --- a/src/eepp/ui/uitextinput.cpp +++ b/src/eepp/ui/uitextinput.cpp @@ -869,11 +869,11 @@ bool UITextInput::onCreateContextMenu( const Vector2i& position, const Uint32& f UIPopUpMenu* menu = UIPopUpMenu::New(); + createDefaultContextMenuOptions( menu ); + ContextMenuEvent event( this, menu, Event::OnCreateContextMenu, position, flags ); sendEvent( &event ); - createDefaultContextMenuOptions( menu ); - if ( menu->getCount() == 0 ) { menu->close(); return false; diff --git a/src/modules/eterm/src/eterm/ui/uiterminal.cpp b/src/modules/eterm/src/eterm/ui/uiterminal.cpp index 0bb178196..4627d2795 100644 --- a/src/modules/eterm/src/eterm/ui/uiterminal.cpp +++ b/src/modules/eterm/src/eterm/ui/uiterminal.cpp @@ -543,11 +543,11 @@ bool UITerminal::onCreateContextMenu( const Vector2i& position, const Uint32& fl UIPopUpMenu* menu = UIPopUpMenu::New(); + createDefaultContextMenuOptions( menu ); + ContextMenuEvent event( this, menu, Event::OnCreateContextMenu, position, flags ); sendEvent( &event ); - createDefaultContextMenuOptions( menu ); - if ( menu->getCount() == 0 ) { menu->close(); return false; diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 83a1be7e1..57cd96b01 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -1821,35 +1821,67 @@ void App::createDocAlert( UICodeEditor* editor ) { } ); } +void App::loadImageFromMemory( const std::string& content ) { + UIImage* imageView = mImageLayout->findByType( UI_TYPE_IMAGE ); + UILoader* loaderView = mImageLayout->findByType( UI_TYPE_LOADER ); + if ( imageView ) { + mImageLayout->setEnabled( true )->setVisible( true ); + loaderView->setVisible( true ); +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) + mThreadPool->run( [&, imageView, loaderView, content]() { +#endif + Texture* image = + TextureFactory::instance()->getTexture( TextureFactory::instance()->loadFromMemory( + reinterpret_cast( content.c_str() ), content.size() ) ); + if ( mImageLayout->isVisible() ) { + imageView->runOnMainThread( [imageView, loaderView, image]() { + imageView->setDrawable( image, true ); + loaderView->setVisible( false ); + } ); + } else { + TextureFactory::instance()->remove( image ); + imageView->setDrawable( nullptr ); + loaderView->setVisible( false ); + } +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) + } ); +#endif + } +} + +void App::loadImageFromPath( const std::string& path ) { + UIImage* imageView = mImageLayout->findByType( UI_TYPE_IMAGE ); + UILoader* loaderView = mImageLayout->findByType( UI_TYPE_LOADER ); + if ( imageView ) { + mImageLayout->setEnabled( true )->setVisible( true ); + loaderView->setVisible( true ); +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) + mThreadPool->run( [&, imageView, loaderView, path]() { +#endif + Texture* image = TextureFactory::instance()->getTexture( + TextureFactory::instance()->loadFromFile( path ) ); + if ( mImageLayout->isVisible() ) { + imageView->runOnMainThread( [imageView, loaderView, image]() { + imageView->setDrawable( image, true ); + loaderView->setVisible( false ); + } ); + } else { + TextureFactory::instance()->remove( image ); + imageView->setDrawable( nullptr ); + loaderView->setVisible( false ); + } +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) + } ); +#endif + } +} + void App::loadFileFromPath( const std::string& path, bool inNewTab, UICodeEditor* codeEditor, std::function onLoaded ) { if ( Image::isImageExtension( path ) && Image::isImage( path ) && FileSystem::fileExtension( path ) != "svg" ) { - UIImage* imageView = mImageLayout->findByType( UI_TYPE_IMAGE ); - UILoader* loaderView = mImageLayout->findByType( UI_TYPE_LOADER ); - if ( imageView ) { - mImageLayout->setEnabled( true )->setVisible( true ); - loaderView->setVisible( true ); -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) - mThreadPool->run( [&, imageView, loaderView, path]() { -#endif - Texture* image = TextureFactory::instance()->getTexture( - TextureFactory::instance()->loadFromFile( path ) ); - if ( mImageLayout->isVisible() ) { - imageView->runOnMainThread( [imageView, loaderView, image]() { - imageView->setDrawable( image, true ); - loaderView->setVisible( false ); - } ); - } else { - TextureFactory::instance()->remove( image ); - imageView->setDrawable( nullptr ); - loaderView->setVisible( false ); - } -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) - } ); -#endif - } + loadImageFromPath( path ); } else { UITab* tab = mSplitter->isDocumentOpen( path ); @@ -2060,7 +2092,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { editor->setSyntaxDefinition( editor->getDocument().getSyntaxDefinition() ); } ); - auto docChanged = [&]( const Event* event ) { + auto docChanged = [this]( const Event* event ) { const DocEvent* synEvent = static_cast( event ); UICodeEditor* editor = event->getNode()->asType(); UIIcon* icon = mUISceneNode->findIcon( @@ -2073,6 +2105,19 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { UITab* tab = (UITab*)editor->getData(); tab->setIcon( icon->getSize( mMenuIconSize ) ); } + + if ( editor->getDocument().getFileInfo().getExtension() == "svg" ) { + editor->getDocument().setCommand( "show-image-preview", [this, editor]() { + loadImageFromMemory( editor->getDocument().getText().toUtf8() ); + } ); + editor->addEventListener( Event::OnCreateContextMenu, [this]( const Event* event ) { + auto cevent = static_cast( event ); + cevent->getMenu() + ->add( i18n( "show_image_preview", "Show Image Preview" ), + findIcon( "filetype-jpg" ) ) + ->setId( "show-image-preview" ); + } ); + } }; editor->addEventListener( Event::OnDocumentLoaded, docChanged ); diff --git a/src/tools/ecode/ecode.hpp b/src/tools/ecode/ecode.hpp index 1184c47cb..516589669 100644 --- a/src/tools/ecode/ecode.hpp +++ b/src/tools/ecode/ecode.hpp @@ -334,6 +334,10 @@ class App : public UICodeEditorSplitter::Client { void setTheme( const std::string& path ); + void loadImageFromPath( const std::string& path ); + + void loadImageFromMemory( const std::string& content ); + protected: std::vector mArgs; EE::Window::Window* mWindow{ nullptr };