Added show line endings option.

Fixed monospace font command.
This commit is contained in:
Martín Lucas Golini
2023-01-20 02:30:04 -03:00
parent 4edfa15e52
commit fb502ab8ae
7 changed files with 54 additions and 3 deletions

View File

@@ -562,6 +562,10 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {
void setScroll( const Vector2f& val, bool emmitEvent = true );
bool getShowLineEndings() const;
void setShowLineEndings( bool showLineEndings );
protected:
struct LastXOffset {
TextPosition position;
@@ -578,6 +582,7 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {
bool mMouseDown{ false };
bool mShowLineNumber{ true };
bool mShowWhitespaces{ true };
bool mShowLineEndings{ false };
bool mLocked{ false };
bool mHighlightCurrentLine{ true };
bool mHighlightMatchingBracket{ true };
@@ -768,6 +773,9 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {
virtual void drawWhitespaces( const std::pair<int, int>& lineRange, const Vector2f& startScroll,
const Float& lineHeight );
virtual void drawLineEndings( const std::pair<int, int>& lineRange, const Vector2f& startScroll,
const Float& lineHeight );
virtual void drawTextRange( const TextRange& range, const std::pair<int, int>& lineRange,
const Vector2f& startScroll, const Float& lineHeight,
const Color& backgroundColor );

View File

@@ -277,6 +277,10 @@ void UICodeEditor::draw() {
drawWhitespaces( lineRange, startScroll, lineHeight );
}
if ( mShowLineEndings ) {
drawLineEndings( lineRange, startScroll, lineHeight );
}
for ( unsigned long i = lineRange.first; i <= lineRange.second; i++ ) {
Vector2f curScroll(
{ startScroll.x, static_cast<float>( startScroll.y + lineHeight * (double)i ) } );
@@ -1349,7 +1353,7 @@ void UICodeEditor::findLongestLine() {
Float UICodeEditor::getLineWidth( const Int64& lineIndex ) {
if ( mFont && !mFont->isMonospace() )
return getLineText( lineIndex ).getTextWidth();
return getLineText( lineIndex ).getTextWidth() + getGlyphWidth();
return getTextWidth( mDoc->line( lineIndex ).getText() );
}
@@ -1857,6 +1861,17 @@ void UICodeEditor::setScroll( const Vector2f& val, bool emmitEvent ) {
setScrollY( val.y, emmitEvent );
}
bool UICodeEditor::getShowLineEndings() const {
return mShowLineEndings;
}
void UICodeEditor::setShowLineEndings( bool showLineEndings ) {
if ( mShowLineEndings != showLineEndings ) {
mShowLineEndings = showLineEndings;
invalidateDraw();
}
}
UICodeEditor* UICodeEditor::setFontStyle( const Uint32& fontStyle ) {
if ( mFontStyleConfig.Style != fontStyle ) {
mFontStyleConfig.Style = fontStyle;
@@ -2765,6 +2780,23 @@ void UICodeEditor::drawWhitespaces( const std::pair<int, int>& lineRange,
}
}
void UICodeEditor::drawLineEndings( const std::pair<int, int>& lineRange,
const Vector2f& startScroll, const Float& lineHeight ) {
Color color( Color( mWhitespaceColor ).blendAlpha( mAlpha ) );
unsigned int fontSize = getCharacterSize();
GlyphDrawable* nl = mFont->getGlyphDrawable( L'', fontSize );
if ( nl->getPixelsSize() == Sizef::Zero )
nl = mFont->getGlyphDrawable( L'¬', fontSize );
nl->setDrawMode( GlyphDrawable::DrawMode::Text );
nl->setColor( color );
for ( int index = lineRange.first; index <= lineRange.second; index++ ) {
Vector2f position( { startScroll.x + getLineWidth( index ) - getGlyphWidth(),
startScroll.y + lineHeight * index } );
nl->draw( Vector2f( position.x, position.y ) );
}
}
void UICodeEditor::registerCommands() {
mDoc->setCommand( "move-to-previous-line", [&] { moveToPreviousLine(); } );
mDoc->setCommand( "move-to-next-line", [&] { moveToNextLine(); } );

View File

@@ -78,6 +78,7 @@ void AppConfig::load( const std::string& confPath, std::string& keybindingsPath,
windowState.position.y = iniState.getValueI( "window", "y", -1 );
editor.showLineNumbers = ini.getValueB( "editor", "show_line_numbers", true );
editor.showWhiteSpaces = ini.getValueB( "editor", "show_white_spaces", true );
editor.showLineEndings = ini.getValueB( "editor", "show_line_endings", true );
editor.highlightMatchingBracket =
ini.getValueB( "editor", "highlight_matching_brackets", true );
editor.highlightCurrentLine = ini.getValueB( "editor", "highlight_current_line", true );
@@ -187,6 +188,7 @@ void AppConfig::save( const std::vector<std::string>& recentFiles,
String::join( urlEncode( recentFolders ), ';' ) );
ini.setValueB( "editor", "show_line_numbers", editor.showLineNumbers );
ini.setValueB( "editor", "show_white_spaces", editor.showWhiteSpaces );
ini.setValueB( "editor", "show_line_endings", editor.showLineEndings );
ini.setValueB( "editor", "highlight_matching_brackets", editor.highlightMatchingBracket );
ini.setValueB( "editor", "highlight_current_line", editor.highlightCurrentLine );
ini.setValueB( "editor", "vertical_scrollbar", editor.verticalScrollbar );

View File

@@ -49,6 +49,7 @@ struct CodeEditorConfig {
StyleSheetLength lineSpacing{ 0, StyleSheetLength::Dp };
bool showLineNumbers{ true };
bool showWhiteSpaces{ true };
bool showLineEndings{ false };
bool highlightMatchingBracket{ true };
bool verticalScrollbar{ true };
bool horizontalScrollbar{ true };

View File

@@ -1576,6 +1576,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) {
editor->setColorScheme( mSplitter->getCurrentColorScheme() );
editor->setShowLineNumber( config.showLineNumbers );
editor->setShowWhitespaces( config.showWhiteSpaces );
editor->setShowLineEndings( config.showLineEndings );
editor->setHighlightMatchingBracket( config.highlightMatchingBracket );
editor->setVerticalScrollBarEnabled( config.verticalScrollbar );
editor->setHorizontalScrollBarEnabled( config.horizontalScrollbar );

View File

@@ -223,8 +223,7 @@ class App : public UICodeEditorSplitter::Client {
t.setCommand( "terminal-font-size", [&] { setTerminalFontSize(); } );
t.setCommand( "ui-font-size", [&] { setUIFontSize(); } );
t.setCommand( "serif-font", [&] { openFontDialog( mConfig.ui.serifFont, false ); } );
t.setCommand( "monospace-font",
[&] { openFontDialog( mConfig.ui.monospaceFont, false ); } );
t.setCommand( "monospace-font", [&] { openFontDialog( mConfig.ui.monospaceFont, true ); } );
t.setCommand( "terminal-font", [&] { openFontDialog( mConfig.ui.terminalFont, false ); } );
t.setCommand( "fallback-font", [&] { openFontDialog( mConfig.ui.fallbackFont, false ); } );
mSplitter->registerSplitterCommands( t );

View File

@@ -1007,6 +1007,9 @@ UIMenu* SettingsMenu::createViewMenu() {
mViewMenu->addCheckBox( i18n( "show_white_spaces", "Show White Spaces" ) )
->setActive( mApp->getConfig().editor.showWhiteSpaces )
->setId( "show-white-spaces" );
mViewMenu->addCheckBox( i18n( "show_line_endings", "Show Line Endings" ) )
->setActive( mApp->getConfig().editor.showLineEndings )
->setId( "show-line-endings" );
mViewMenu->addCheckBox( i18n( "show_doc_info", "Show Document Info" ) )
->setActive( mApp->getConfig().editor.showDocInfo )
->setId( "show-doc-info" );
@@ -1075,6 +1078,11 @@ UIMenu* SettingsMenu::createViewMenu() {
mSplitter->forEachEditor( [&]( UICodeEditor* editor ) {
editor->setShowWhitespaces( mApp->getConfig().editor.showWhiteSpaces );
} );
} else if ( item->getId() == "show-line-endings" ) {
mApp->getConfig().editor.showLineEndings = item->asType<UIMenuCheckBox>()->isActive();
mSplitter->forEachEditor( [&]( UICodeEditor* editor ) {
editor->setShowLineEndings( mApp->getConfig().editor.showLineEndings );
} );
} else if ( item->getId() == "show-doc-info" ) {
mApp->getConfig().editor.showDocInfo = item->asType<UIMenuCheckBox>()->isActive();
if ( mApp->getDocInfo() )