From 5c15c249769e0f854d43734bf7841eaed3a0ae26 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 20 Apr 2020 21:31:49 +0200 Subject: [PATCH] LibGUI: Keep scrolling while drag-selecting outside a TextEditor If you start selecting text and move the cursor outside the TextEditor widget area without letting go of the mouse button, we will now keep scrolling a little bit every 100ms. --- Libraries/LibGUI/TextEditor.cpp | 24 ++++++++++++++++++++++++ Libraries/LibGUI/TextEditor.h | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/Libraries/LibGUI/TextEditor.cpp b/Libraries/LibGUI/TextEditor.cpp index 94337e838f..a79163a0c9 100644 --- a/Libraries/LibGUI/TextEditor.cpp +++ b/Libraries/LibGUI/TextEditor.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,10 @@ TextEditor::TextEditor(Type type) // FIXME: Recompute vertical scrollbar step size on font change. vertical_scrollbar().set_step(line_height()); m_cursor = { 0, 0 }; + m_automatic_selection_scroll_timer = add(100, [this] { + automatic_selection_scroll_timer_fired(); + }); + m_automatic_selection_scroll_timer->stop(); create_actions(); } @@ -261,6 +266,7 @@ void TextEditor::mousedown_event(MouseEvent& event) } m_in_drag_select = true; + m_automatic_selection_scroll_timer->start(); set_cursor(text_position_at(event.position())); @@ -289,6 +295,7 @@ void TextEditor::mouseup_event(MouseEvent& event) void TextEditor::mousemove_event(MouseEvent& event) { + m_last_mousemove_position = event.position(); if (m_in_drag_select) { set_cursor(text_position_at(event.position())); m_selection.set_end(m_cursor); @@ -298,6 +305,18 @@ void TextEditor::mousemove_event(MouseEvent& event) } } +void TextEditor::automatic_selection_scroll_timer_fired() +{ + if (!m_in_drag_select) { + m_automatic_selection_scroll_timer->stop(); + return; + } + set_cursor(text_position_at(m_last_mousemove_position)); + m_selection.set_end(m_cursor); + did_update_selection(); + update(); +} + int TextEditor::ruler_width() const { if (!m_ruler_visible) @@ -1189,12 +1208,17 @@ void TextEditor::enter_event(Core::Event&) { ASSERT(window()); window()->set_override_cursor(StandardCursor::IBeam); + + m_automatic_selection_scroll_timer->stop(); } void TextEditor::leave_event(Core::Event&) { ASSERT(window()); window()->set_override_cursor(StandardCursor::None); + + if (m_in_drag_select) + m_automatic_selection_scroll_timer->start(); } void TextEditor::did_change() diff --git a/Libraries/LibGUI/TextEditor.h b/Libraries/LibGUI/TextEditor.h index 7d70474cdd..15bc844973 100644 --- a/Libraries/LibGUI/TextEditor.h +++ b/Libraries/LibGUI/TextEditor.h @@ -200,6 +200,8 @@ private: size_t visual_line_containing(size_t line_index, size_t column) const; void recompute_visual_lines(size_t line_index); + void automatic_selection_scroll_timer_fired(); + template inline void execute(Args&&... args) { @@ -247,6 +249,9 @@ private: NonnullOwnPtrVector m_line_visual_data; OwnPtr m_highlighter; + + RefPtr m_automatic_selection_scroll_timer; + Gfx::Point m_last_mousemove_position; }; }