From db45a5a7eca04a48b93e258b2419cc332e673671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 23 Mar 2024 02:04:29 -0300 Subject: [PATCH] Update longest line width right after loading a file. Make auto-reload on disk change configurable. --- src/eepp/ui/uicodeeditor.cpp | 1 + src/tools/ecode/appconfig.cpp | 2 ++ src/tools/ecode/appconfig.hpp | 1 + src/tools/ecode/ecode.cpp | 58 +++++++++++++++++--------------- src/tools/ecode/settingsmenu.cpp | 49 +++++++++++++++++---------- src/tools/ecode/settingsmenu.hpp | 3 ++ 6 files changed, 69 insertions(+), 45 deletions(-) diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index e5a0d4f41..006bd4e0d 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -473,6 +473,7 @@ bool UICodeEditor::loadAsyncFromFile( } runOnMainThread( [this, onLoaded, wasLocked, success] { invalidateEditor(); + updateLongestLineWidth(); invalidateDraw(); if ( !wasLocked ) setLocked( false ); diff --git a/src/tools/ecode/appconfig.cpp b/src/tools/ecode/appconfig.cpp index d212ba832..7ebd0906e 100644 --- a/src/tools/ecode/appconfig.cpp +++ b/src/tools/ecode/appconfig.cpp @@ -137,6 +137,7 @@ void AppConfig::load( const std::string& confPath, std::string& keybindingsPath, editor.cursorBlinkingTime = Time::fromString( ini.getValue( "editor", "cursor_blinking_time", "0.5s" ) ); editor.linesRelativePosition = ini.getValueB( "editor", "lines_relative_position", false ); + editor.autoReloadOnDiskChange = ini.getValueB( "editor", "auto_reload_on_disk_change", false ); searchBarConfig.caseSensitive = ini.getValueB( "search_bar", "case_sensitive", false ); searchBarConfig.luaPattern = ini.getValueB( "search_bar", "lua_pattern", false ); @@ -259,6 +260,7 @@ void AppConfig::save( const std::vector& recentFiles, ini.setValue( "editor", "line_spacing", editor.lineSpacing.toString() ); ini.setValue( "editor", "cursor_blinking_time", editor.cursorBlinkingTime.toString() ); ini.setValueB( "editor", "lines_relative_position", editor.linesRelativePosition ); + ini.setValueB( "editor", "auto_reload_on_disk_change", editor.autoReloadOnDiskChange ); ini.setValueB( "search_bar", "case_sensitive", searchBarConfig.caseSensitive ); ini.setValueB( "search_bar", "lua_pattern", searchBarConfig.luaPattern ); diff --git a/src/tools/ecode/appconfig.hpp b/src/tools/ecode/appconfig.hpp index f1ccfa2bd..abda58910 100644 --- a/src/tools/ecode/appconfig.hpp +++ b/src/tools/ecode/appconfig.hpp @@ -74,6 +74,7 @@ struct CodeEditorConfig { bool syncProjectTreeWithEditor{ true }; bool autoCloseXMLTags{ true }; bool linesRelativePosition{ false }; + bool autoReloadOnDiskChange{ false }; std::string autoCloseBrackets{ "" }; Time cursorBlinkingTime{ Seconds( 0.5f ) }; }; diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index a7bfe1ee3..192ab3c4b 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -2229,6 +2229,8 @@ void App::createDocDirtyAlert( UICodeEditor* editor ) { + enableReportSizeChangeToChilds(); - docAlert->find( "file_reload" ) - ->on( Event::MouseClick, [editor, docAlert]( const Event* event ) { - const MouseEvent* mouseEvent = static_cast( event ); - if ( mouseEvent->getFlags() & EE_BUTTON_LMASK ) { - editor->getDocument().reload(); - editor->disableReportSizeChangeToChilds(); - docAlert->close(); - editor->setFocus(); - } + docAlert->find( "file_autoreload" ) + ->setVisible( !editor->getDocument().isDirty() ) + ->onClick( [editor, docAlert, this]( const MouseEvent* mouseEvent ) { + editor->getDocument().reload(); + editor->disableReportSizeChangeToChilds(); + docAlert->close(); + editor->setFocus(); + mConfig.editor.autoReloadOnDiskChange = true; + mSettings->updateGlobalDocumentSettingsMenu(); } ); + docAlert->find( "file_reload" )->onClick( [editor, docAlert]( const MouseEvent* mouseEvent ) { + editor->getDocument().reload(); + editor->disableReportSizeChangeToChilds(); + docAlert->close(); + editor->setFocus(); + } ); + docAlert->find( "file_overwrite" ) - ->on( Event::MouseClick, [editor, docAlert]( const Event* event ) { - const MouseEvent* mouseEvent = static_cast( event ); - if ( mouseEvent->getFlags() & EE_BUTTON_LMASK ) { - editor->getDocument().save(); - editor->disableReportSizeChangeToChilds(); - docAlert->close(); - editor->setFocus(); - } + ->onClick( [editor, docAlert]( const MouseEvent* mouseEvent ) { + editor->getDocument().save(); + editor->disableReportSizeChangeToChilds(); + docAlert->close(); + editor->setFocus(); } ); - docAlert->find( "file_ignore" ) - ->on( Event::MouseClick, [docAlert, editor]( const Event* event ) { - const MouseEvent* mouseEvent = static_cast( event ); - if ( mouseEvent->getFlags() & EE_BUTTON_LMASK ) { - editor->disableReportSizeChangeToChilds(); - docAlert->close(); - editor->setFocus(); - } - } ); + docAlert->find( "file_ignore" )->onClick( [docAlert, editor]( const MouseEvent* mouseEvent ) { + editor->disableReportSizeChangeToChilds(); + docAlert->close(); + editor->setFocus(); + } ); docAlert->runOnMainThread( [docAlert, editor] { @@ -2279,7 +2281,7 @@ void App::createDocDirtyAlert( UICodeEditor* editor ) { docAlert->close(); editor->setFocus(); }, - Seconds( 10.f ) ); + Seconds( 30.f ) ); } void App::createDocManyLangsAlert( UICodeEditor* editor ) { @@ -2567,7 +2569,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { FileInfo file( docEvent->getDoc()->getFileInfo().getFilepath() ); TextDocument* doc = docEvent->getDoc(); if ( doc->getFileInfo() != file ) { - if ( doc->isDirty() ) { + if ( !mConfig.editor.autoReloadOnDiskChange || doc->isDirty() ) { editor->runOnMainThread( [this, editor]() { createDocDirtyAlert( editor ); } ); } else { auto hash = String::hash( "OnDocumentDirtyOnFileSysten-" + diff --git a/src/tools/ecode/settingsmenu.cpp b/src/tools/ecode/settingsmenu.cpp index a7f7ea509..dcc57574a 100644 --- a/src/tools/ecode/settingsmenu.cpp +++ b/src/tools/ecode/settingsmenu.cpp @@ -391,11 +391,11 @@ UIMenu* SettingsMenu::createDocumentMenu() { // **** GLOBAL SETTINGS **** mDocMenu->addSeparator()->setId( "end_current_document" ); - UIPopUpMenu* globalMenu = UIPopUpMenu::New(); + mGlobalMenu = UIPopUpMenu::New(); mDocMenu->addSubMenu( i18n( "global_settings", "Global Settings" ), - findIcon( "global-settings" ), globalMenu ); + findIcon( "global-settings" ), mGlobalMenu ); - globalMenu + mGlobalMenu ->addCheckBox( i18n( "auto_detect_indent_type_and_width", "Auto Detect Indent Type & Width" ), mApp->getConfig().doc.autoDetectIndentType ) @@ -408,7 +408,7 @@ UIMenu* SettingsMenu::createDocumentMenu() { tabTypeMenuGlobal->addRadioButton( i18n( "spaces", "Spaces" ) ) ->setActive( mApp->getConfig().doc.indentSpaces ) ->setId( "spaces" ); - globalMenu + mGlobalMenu ->addSubMenu( i18n( "indentation_type", "Indentation Type" ), nullptr, tabTypeMenuGlobal ) ->setId( "indent_type" ); tabTypeMenuGlobal->on( Event::OnItemClicked, [this]( const Event* event ) { @@ -422,7 +422,8 @@ UIMenu* SettingsMenu::createDocumentMenu() { ->addRadioButton( String::toString( w ), mApp->getConfig().doc.indentWidth == w ) ->setId( String::format( "indent_width_%d", w ) ) ->setData( w ); - globalMenu->addSubMenu( i18n( "indent_width", "Indent Width" ), nullptr, indentWidthMenuGlobal ) + mGlobalMenu + ->addSubMenu( i18n( "indent_width", "Indent Width" ), nullptr, indentWidthMenuGlobal ) ->setId( "indent_width" ); indentWidthMenuGlobal->on( Event::OnItemClicked, [this]( const Event* event ) { int width = event->getNode()->getData(); @@ -435,7 +436,7 @@ UIMenu* SettingsMenu::createDocumentMenu() { ->addRadioButton( String::toString( w ), mApp->getConfig().doc.tabWidth == w ) ->setId( String::format( "tab_width_%d", w ) ) ->setData( w ); - globalMenu->addSubMenu( i18n( "tab_width", "Tab Width" ), nullptr, tabWidthMenuGlobal ) + mGlobalMenu->addSubMenu( i18n( "tab_width", "Tab Width" ), nullptr, tabWidthMenuGlobal ) ->setId( "tab_width_cur" ); tabWidthMenuGlobal->on( Event::OnItemClicked, [this]( const Event* event ) { int width = event->getNode()->getData(); @@ -455,7 +456,8 @@ UIMenu* SettingsMenu::createDocumentMenu() { ->addRadioButton( "Macintosh (CR)", mApp->getConfig().doc.lineEndings == TextFormat::LineEnding::CR ) ->setId( "CR" ); - globalMenu->addSubMenu( i18n( "line_endings", "Line Endings" ), nullptr, lineEndingsGlobalMenu ) + mGlobalMenu + ->addSubMenu( i18n( "line_endings", "Line Endings" ), nullptr, lineEndingsGlobalMenu ) ->setId( "line_endings" ); lineEndingsGlobalMenu->on( Event::OnItemClicked, [this]( const Event* event ) { mApp->getConfig().doc.lineEndings = @@ -463,8 +465,8 @@ UIMenu* SettingsMenu::createDocumentMenu() { } ); UIPopUpMenu* bracketsMenu = UIPopUpMenu::New(); - globalMenu->addSubMenu( i18n( "auto_close_brackets_and_tags", "Auto-Close Brackets & Tags" ), - nullptr, bracketsMenu ); + mGlobalMenu->addSubMenu( i18n( "auto_close_brackets_and_tags", "Auto-Close Brackets & Tags" ), + nullptr, bracketsMenu ); auto& closeBrackets = mApp->getConfig().editor.autoCloseBrackets; bracketsMenu ->addCheckBox( i18n( "brackets", "Brackets ()" ), @@ -529,32 +531,37 @@ UIMenu* SettingsMenu::createDocumentMenu() { } } ); - globalMenu + mGlobalMenu ->addCheckBox( i18n( "trim_trailing_whitespaces", "Trim Trailing Whitespaces" ), mApp->getConfig().doc.trimTrailingWhitespaces ) ->setId( "trim_whitespaces" ); - globalMenu + mGlobalMenu ->addCheckBox( i18n( "force_new_line_at_end_of_file", "Force New Line at End of File" ), mApp->getConfig().doc.forceNewLineAtEndOfFile ) ->setId( "force_nl" ); - globalMenu + mGlobalMenu ->addCheckBox( i18n( "write_unicode_bom", "Write Unicode BOM" ), mApp->getConfig().doc.writeUnicodeBOM ) ->setId( "write_bom" ); - globalMenu->addSeparator(); + mGlobalMenu + ->addCheckBox( i18n( "autoreload_on_disk_change", "Auto-Reload on Disk Change" ), + mApp->getConfig().editor.autoReloadOnDiskChange ) + ->setId( "autoreload_on_disk_change" ); - globalMenu->add( i18n( "line_breaking_column", "Line Breaking Column" ) ) + mGlobalMenu->addSeparator(); + + mGlobalMenu->add( i18n( "line_breaking_column", "Line Breaking Column" ) ) ->setId( "line_breaking_column" ); - globalMenu->add( i18n( "line_spacing", "Line Spacing" ) )->setId( "line_spacing" ); + mGlobalMenu->add( i18n( "line_spacing", "Line Spacing" ) )->setId( "line_spacing" ); - globalMenu->add( i18n( "cursor_blinking_time", "Cursor Blinking Time" ) ) + mGlobalMenu->add( i18n( "cursor_blinking_time", "Cursor Blinking Time" ) ) ->setId( "cursor_blinking_time" ); - globalMenu->on( Event::OnItemClicked, [this]( const Event* event ) { + mGlobalMenu->on( Event::OnItemClicked, [this]( const Event* event ) { if ( !mSplitter->curEditorExistsAndFocused() || event->getNode()->isType( UI_TYPE_MENU_SEPARATOR ) || event->getNode()->isType( UI_TYPE_MENUSUBMENU ) ) @@ -571,6 +578,8 @@ UIMenu* SettingsMenu::createDocumentMenu() { mApp->getConfig().doc.writeUnicodeBOM = item->isActive(); } else if ( "auto_indent" == id ) { mApp->getConfig().doc.autoDetectIndentType = item->isActive(); + } else if ( "autoreload_on_disk_change" == id ) { + mApp->getConfig().editor.autoReloadOnDiskChange = item->isActive(); } } else if ( "line_breaking_column" == id ) { mApp->setLineBreakingColumn(); @@ -1639,6 +1648,12 @@ void SettingsMenu::updateDocumentMenu() { ->setActive( mSplitter->getCurEditor()->isLocked() ); } +void SettingsMenu::updateGlobalDocumentSettingsMenu() { + mGlobalMenu->find( "autoreload_on_disk_change" ) + ->asType() + ->setActive( mApp->getConfig().editor.autoReloadOnDiskChange ); +} + void SettingsMenu::showProjectTreeMenu() { mProjectTreeMenu->showOverMouseCursor(); } diff --git a/src/tools/ecode/settingsmenu.hpp b/src/tools/ecode/settingsmenu.hpp index 9883839ea..4c2e6a07c 100644 --- a/src/tools/ecode/settingsmenu.hpp +++ b/src/tools/ecode/settingsmenu.hpp @@ -50,6 +50,8 @@ class SettingsMenu { void updateDocumentMenu(); + void updateGlobalDocumentSettingsMenu(); + void showProjectTreeMenu(); void createProjectTreeMenu(); @@ -80,6 +82,7 @@ class SettingsMenu { UISceneNode* mUISceneNode{ nullptr }; UICodeEditorSplitter* mSplitter{ nullptr }; UIPopUpMenu* mDocMenu{ nullptr }; + UIPopUpMenu* mGlobalMenu{ nullptr }; UIPopUpMenu* mTerminalMenu{ nullptr }; UIPopUpMenu* mViewMenu{ nullptr }; UIPopUpMenu* mWindowMenu{ nullptr };