From 9a5a9fbee032ab94933cf7dc6120c6cc0f7d85f2 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Sat, 11 Sep 2021 11:08:01 -0400 Subject: [PATCH] LibGUI+TextEditor: Allow cursor line highlighting to be toggled --- Userland/Applications/TextEditor/MainWidget.cpp | 9 +++++++++ Userland/Applications/TextEditor/MainWidget.h | 1 + Userland/Libraries/LibGUI/TextEditor.cpp | 10 +++++++++- Userland/Libraries/LibGUI/TextEditor.h | 4 ++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Userland/Applications/TextEditor/MainWidget.cpp b/Userland/Applications/TextEditor/MainWidget.cpp index 41b36b94a4..a3c2353ecb 100644 --- a/Userland/Applications/TextEditor/MainWidget.cpp +++ b/Userland/Applications/TextEditor/MainWidget.cpp @@ -526,6 +526,15 @@ void MainWidget::initialize_menubar(GUI::Window& window) view_menu.add_action(*m_visualize_trailing_whitespace_action); view_menu.add_action(*m_visualize_leading_whitespace_action); + m_cursor_line_highlighting_action = GUI::Action::create_checkable("Line High&lighting", [&](auto&) { + m_editor->set_cursor_line_highlighting(m_cursor_line_highlighting_action->is_checked()); + }); + + m_cursor_line_highlighting_action->set_checked(true); + m_cursor_line_highlighting_action->set_status_tip("Highlight text on the cursor's line"); + + view_menu.add_action(*m_cursor_line_highlighting_action); + view_menu.add_separator(); view_menu.add_action(*m_no_preview_action); view_menu.add_action(*m_markdown_preview_action); diff --git a/Userland/Applications/TextEditor/MainWidget.h b/Userland/Applications/TextEditor/MainWidget.h index c620affe1e..925dda707c 100644 --- a/Userland/Applications/TextEditor/MainWidget.h +++ b/Userland/Applications/TextEditor/MainWidget.h @@ -104,6 +104,7 @@ private: RefPtr m_visualize_trailing_whitespace_action; RefPtr m_visualize_leading_whitespace_action; + RefPtr m_cursor_line_highlighting_action; GUI::ActionGroup m_soft_tab_width_actions; RefPtr m_soft_tab_1_width_action; diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index 1e1581ba9f..ba97cfa2eb 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -511,7 +511,7 @@ void TextEditor::paint_event(PaintEvent& event) size_t visual_line_index = 0; for_each_visual_line(line_index, [&](Gfx::IntRect const& visual_line_rect, auto& visual_line_text, size_t start_of_visual_line, [[maybe_unused]] bool is_last_visual_line) { - if (is_multi_line() && line_index == m_cursor.line()) + if (is_multi_line() && line_index == m_cursor.line() && is_cursor_line_highlighted()) painter.fill_rect(visual_line_rect, widget_background_color.darkened(0.9f)); if constexpr (TEXTEDITOR_DEBUG) painter.draw_rect(visual_line_rect, Color::Cyan); @@ -1973,6 +1973,14 @@ void TextEditor::set_gutter_visible(bool visible) update(); } +void TextEditor::set_cursor_line_highlighting(bool highlighted) +{ + if (m_cursor_line_highlighting == highlighted) + return; + m_cursor_line_highlighting = highlighted; + update(); +} + void TextEditor::undo() { clear_selection(); diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index 30b57f1794..1e2059d6e0 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -68,6 +68,9 @@ public: void set_visualize_leading_whitespace(bool); bool visualize_leading_whitespace() const { return m_visualize_leading_whitespace; } + bool is_cursor_line_highlighted() const { return m_cursor_line_highlighting; } + void set_cursor_line_highlighting(bool); + virtual bool is_automatic_indentation_enabled() const final { return m_automatic_indentation_enabled; } void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; } @@ -337,6 +340,7 @@ private: WrappingMode m_wrapping_mode { WrappingMode::NoWrap }; bool m_visualize_trailing_whitespace { true }; bool m_visualize_leading_whitespace { false }; + bool m_cursor_line_highlighting { true }; int m_line_spacing { 4 }; size_t m_soft_tab_width { 4 }; int m_horizontal_content_padding { 3 };