diff options
author | Snow <i@xkun.dev> | 2022-12-08 17:07:50 +0800 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-12 16:02:06 +0000 |
commit | 0049dfd71716a5c2bb1ed54c312674e1f6b1568c (patch) | |
tree | 74517883a350003e0a965da2b257fe730bba53d5 /Userland/Libraries/LibGUI/TextEditor.cpp | |
parent | 1ca0898b1c1936810670116f424b7db724d8ffdc (diff) | |
download | serenity-0049dfd71716a5c2bb1ed54c312674e1f6b1568c.zip |
LibGUI+TextEditor: Add a relative line number option for TextEditor
This adds an option for displaying relative line numbers in the ruler,
the line numbers are still absolute by default.
Diffstat (limited to 'Userland/Libraries/LibGUI/TextEditor.cpp')
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.cpp | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index dd3e3be72d..d018a1615e 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -488,9 +488,10 @@ void TextEditor::paint_event(PaintEvent& event) if (ruler_line_rect.height() > line_height) ruler_line_rect.set_height(line_height); // NOTE: Use Painter::draw_text() directly here, as we want to always draw the line numbers in clear text. + size_t const line_number = is_relative_line_number() && !is_current_line ? max(i, m_cursor.line()) - min(i, m_cursor.line()) : i + 1; painter.draw_text( ruler_line_rect.shrunken(2, 0), - DeprecatedString::number(i + 1), + DeprecatedString::number(line_number), is_current_line ? font().bold_variant() : font(), Gfx::TextAlignment::CenterRight, is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text()); @@ -1427,6 +1428,8 @@ void TextEditor::set_cursor(TextPosition const& a_position) on_cursor_change(); if (m_highlighter) m_highlighter->cursor_did_change(); + if (m_relative_line_number) + update(); } void TextEditor::focusin_event(FocusEvent& event) @@ -2258,6 +2261,15 @@ int TextEditor::number_of_visible_lines() const return visible_content_rect().height() / line_height(); } +void TextEditor::set_relative_line_number(bool relative) +{ + if (m_relative_line_number == relative) + return; + m_relative_line_number = relative; + recompute_all_visual_lines(); + update(); +} + void TextEditor::set_ruler_visible(bool visible) { if (m_ruler_visible == visible) |