mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:17:34 +00:00
LibGUI: Convert mouse events from a visual to a physical position
When clicking a position within a TextEditor, we should interpret that position as a visual location. That location should be converted to a "physical" location before using it to set the physical cursor position. For example, consider a document with 2 emoji, each consisting of 3 code points. Visually, these will occupy 2 columns. When a mouse click occurs between these columns, we need to convert the visual column number 1 to the physical column number 3 when storing the new cursor location.
This commit is contained in:
parent
b823f3d29f
commit
2eb2207f50
2 changed files with 24 additions and 3 deletions
|
@ -33,6 +33,7 @@
|
||||||
#include <LibGfx/Palette.h>
|
#include <LibGfx/Palette.h>
|
||||||
#include <LibGfx/StandardCursor.h>
|
#include <LibGfx/StandardCursor.h>
|
||||||
#include <LibSyntax/Highlighter.h>
|
#include <LibSyntax/Highlighter.h>
|
||||||
|
#include <LibUnicode/Segmentation.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -276,7 +277,7 @@ void TextEditor::mousedown_event(MouseEvent& event)
|
||||||
|
|
||||||
m_in_drag_select = true;
|
m_in_drag_select = true;
|
||||||
|
|
||||||
set_cursor(text_position_at(event.position()));
|
set_cursor_to_text_position(event.position());
|
||||||
|
|
||||||
if (!(event.modifiers() & Mod_Shift)) {
|
if (!(event.modifiers() & Mod_Shift)) {
|
||||||
if (!has_selection())
|
if (!has_selection())
|
||||||
|
@ -306,7 +307,7 @@ void TextEditor::mousemove_event(MouseEvent& event)
|
||||||
m_last_mousemove_position = event.position();
|
m_last_mousemove_position = event.position();
|
||||||
if (m_in_drag_select) {
|
if (m_in_drag_select) {
|
||||||
auto constrained = event.position().constrained(widget_inner_rect());
|
auto constrained = event.position().constrained(widget_inner_rect());
|
||||||
set_cursor(text_position_at(constrained));
|
set_cursor_to_text_position(constrained);
|
||||||
m_selection.set_end(m_cursor);
|
m_selection.set_end(m_cursor);
|
||||||
did_update_selection();
|
did_update_selection();
|
||||||
update();
|
update();
|
||||||
|
@ -336,7 +337,7 @@ void TextEditor::automatic_scrolling_timer_did_fire()
|
||||||
set_automatic_scrolling_timer_active(false);
|
set_automatic_scrolling_timer_active(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
set_cursor(text_position_at(m_last_mousemove_position));
|
set_cursor_to_text_position(m_last_mousemove_position);
|
||||||
m_selection.set_end(m_cursor);
|
m_selection.set_end(m_cursor);
|
||||||
did_update_selection();
|
did_update_selection();
|
||||||
update();
|
update();
|
||||||
|
@ -1395,6 +1396,25 @@ void TextEditor::set_cursor(TextPosition const& a_position)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextEditor::set_cursor_to_text_position(Gfx::IntPoint position)
|
||||||
|
{
|
||||||
|
auto visual_position = text_position_at(position);
|
||||||
|
size_t physical_column = 0;
|
||||||
|
|
||||||
|
auto const& line = document().line(visual_position.line());
|
||||||
|
size_t boundary_index = 0;
|
||||||
|
|
||||||
|
Unicode::for_each_grapheme_segmentation_boundary(line.view(), [&](auto boundary) {
|
||||||
|
physical_column = boundary;
|
||||||
|
|
||||||
|
if (boundary_index++ >= visual_position.column())
|
||||||
|
return IterationDecision::Break;
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
|
||||||
|
set_cursor({ visual_position.line(), physical_column });
|
||||||
|
}
|
||||||
|
|
||||||
void TextEditor::focusin_event(FocusEvent& event)
|
void TextEditor::focusin_event(FocusEvent& event)
|
||||||
{
|
{
|
||||||
if (event.source() == FocusSource::Keyboard)
|
if (event.source() == FocusSource::Keyboard)
|
||||||
|
|
|
@ -190,6 +190,7 @@ public:
|
||||||
void set_cursor_and_focus_line(size_t line, size_t column);
|
void set_cursor_and_focus_line(size_t line, size_t column);
|
||||||
void set_cursor(size_t line, size_t column);
|
void set_cursor(size_t line, size_t column);
|
||||||
virtual void set_cursor(TextPosition const&);
|
virtual void set_cursor(TextPosition const&);
|
||||||
|
void set_cursor_to_text_position(Gfx::IntPoint);
|
||||||
|
|
||||||
Syntax::Highlighter* syntax_highlighter();
|
Syntax::Highlighter* syntax_highlighter();
|
||||||
Syntax::Highlighter const* syntax_highlighter() const;
|
Syntax::Highlighter const* syntax_highlighter() const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue