1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:48:11 +00:00

LibGUI: Improve up/down arrow behavior in TextEditor with wrapping

Instead of moving between physical lines, the up/down arrow keys now
move between visual lines.
This commit is contained in:
Andreas Kling 2021-01-03 00:11:56 +01:00
parent bd2a7c414c
commit 906e310411
2 changed files with 35 additions and 14 deletions

View file

@ -148,13 +148,9 @@ void TextEditor::update_content_size()
set_size_occupied_by_fixed_elements({ ruler_width(), 0 }); set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
} }
TextPosition TextEditor::text_position_at(const Gfx::IntPoint& a_position) const TextPosition TextEditor::text_position_at_content_position(const Gfx::IntPoint& content_position) const
{ {
auto position = a_position; auto position = content_position;
position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
position.move_by(-(m_horizontal_content_padding + ruler_width()), 0);
position.move_by(-frame_thickness(), -frame_thickness());
if (is_single_line() && icon()) if (is_single_line() && icon())
position.move_by(-(icon_size() + icon_padding()), 0); position.move_by(-(icon_size() + icon_padding()), 0);
@ -212,6 +208,15 @@ TextPosition TextEditor::text_position_at(const Gfx::IntPoint& a_position) const
return { line_index, column_index }; return { line_index, column_index };
} }
TextPosition TextEditor::text_position_at(const Gfx::IntPoint& widget_position) const
{
auto content_position = widget_position;
content_position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
content_position.move_by(-(m_horizontal_content_padding + ruler_width()), 0);
content_position.move_by(-frame_thickness(), -frame_thickness());
return text_position_at_content_position(content_position);
}
void TextEditor::doubleclick_event(MouseEvent& event) void TextEditor::doubleclick_event(MouseEvent& event)
{ {
if (event.button() != MouseButton::Left) if (event.button() != MouseButton::Left)
@ -760,15 +765,22 @@ void TextEditor::keydown_event(KeyEvent& event)
return; return;
} }
if (is_multi_line() && event.key() == KeyCode::Key_Up) { if (is_multi_line() && event.key() == KeyCode::Key_Up) {
if (m_cursor.line() > 0) { if (m_cursor.line() > 0 || m_line_wrapping_enabled) {
if (event.ctrl() && event.shift()) { if (event.ctrl() && event.shift()) {
move_selected_lines_up(); move_selected_lines_up();
return; return;
} }
size_t new_line = m_cursor.line() - 1; TextPosition new_cursor;
size_t new_column = min(m_cursor.column(), line(new_line).length()); if (m_line_wrapping_enabled) {
auto position_above = cursor_content_rect().location().translated(0, -line_height());
new_cursor = text_position_at_content_position(position_above);
} else {
size_t new_line = m_cursor.line() - 1;
size_t new_column = min(m_cursor.column(), line(new_line).length());
new_cursor = { new_line, new_column };
}
toggle_selection_if_needed_for_event(event); toggle_selection_if_needed_for_event(event);
set_cursor(new_line, new_column); set_cursor(new_cursor);
if (event.shift() && m_selection.start().is_valid()) { if (event.shift() && m_selection.start().is_valid()) {
m_selection.set_end(m_cursor); m_selection.set_end(m_cursor);
did_update_selection(); did_update_selection();
@ -781,15 +793,23 @@ void TextEditor::keydown_event(KeyEvent& event)
return; return;
} }
if (is_multi_line() && event.key() == KeyCode::Key_Down) { if (is_multi_line() && event.key() == KeyCode::Key_Down) {
if (m_cursor.line() < (line_count() - 1)) { if (m_cursor.line() < (line_count() - 1) || m_line_wrapping_enabled) {
if (event.ctrl() && event.shift()) { if (event.ctrl() && event.shift()) {
move_selected_lines_down(); move_selected_lines_down();
return; return;
} }
size_t new_line = m_cursor.line() + 1; TextPosition new_cursor;
size_t new_column = min(m_cursor.column(), line(new_line).length()); if (m_line_wrapping_enabled) {
new_cursor = text_position_at_content_position(cursor_content_rect().location().translated(0, line_height()));
auto position_below = cursor_content_rect().location().translated(0, line_height());
new_cursor = text_position_at_content_position(position_below);
} else {
size_t new_line = m_cursor.line() + 1;
size_t new_column = min(m_cursor.column(), line(new_line).length());
new_cursor = { new_line, new_column };
}
toggle_selection_if_needed_for_event(event); toggle_selection_if_needed_for_event(event);
set_cursor(new_line, new_column); set_cursor(new_cursor);
if (event.shift() && m_selection.start().is_valid()) { if (event.shift() && m_selection.start().is_valid()) {
m_selection.set_end(m_cursor); m_selection.set_end(m_cursor);
did_update_selection(); did_update_selection();

View file

@ -191,6 +191,7 @@ protected:
Gfx::IntRect ruler_content_rect(size_t line) const; Gfx::IntRect ruler_content_rect(size_t line) const;
TextPosition text_position_at(const Gfx::IntPoint&) const; TextPosition text_position_at(const Gfx::IntPoint&) const;
TextPosition text_position_at_content_position(const Gfx::IntPoint&) const;
bool ruler_visible() const { return m_ruler_visible; } bool ruler_visible() const { return m_ruler_visible; }
Gfx::IntRect content_rect_for_position(const TextPosition&) const; Gfx::IntRect content_rect_for_position(const TextPosition&) const;
int ruler_width() const; int ruler_width() const;