summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/EditingEngine.cpp
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-02-09 14:21:52 +0000
committerSam Atkins <atkinssj@gmail.com>2023-02-13 16:54:56 +0000
commit69333e5dbddd719787e6e0909b1bad9a5c13782d (patch)
treef62452bf568f64f55d9d82b605d247e033a11723 /Userland/Libraries/LibGUI/EditingEngine.cpp
parent7aef096f857191c5cf36d7ea1f6adf8e69e08923 (diff)
downloadserenity-69333e5dbddd719787e6e0909b1bad9a5c13782d.zip
LibGUI: Combine wrapping/non-wrapping TextEditor code paths
The `is_wrapping_enabled()` paths function just fine when wrapping is disabled. We already calculate `m_line_visual_data`. The only reason to use a special path is for speed, since you can skip some steps if you know each line is only 1 line high visually. However, with code-folding being added, we can't make assumptions about line height because a line could be hidden and have an effective height of 0px. So `text_position_at_content_position()` always needs to loop through the lines to see what our position is, and that function always needs to be called to calculate the real position.
Diffstat (limited to 'Userland/Libraries/LibGUI/EditingEngine.cpp')
-rw-r--r--Userland/Libraries/LibGUI/EditingEngine.cpp46
1 files changed, 8 insertions, 38 deletions
diff --git a/Userland/Libraries/LibGUI/EditingEngine.cpp b/Userland/Libraries/LibGUI/EditingEngine.cpp
index 22293046d3..718da1b6ed 100644
--- a/Userland/Libraries/LibGUI/EditingEngine.cpp
+++ b/Userland/Libraries/LibGUI/EditingEngine.cpp
@@ -301,15 +301,8 @@ EditingEngine::DidMoveALine EditingEngine::move_one_up(KeyEvent const& event)
}
return DidMoveALine::No;
}
- TextPosition new_cursor;
- if (m_editor->is_wrapping_enabled()) {
- auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
- new_cursor = m_editor->text_position_at_content_position(position_above);
- } else {
- size_t new_line = m_editor->cursor().line() - 1;
- size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
- new_cursor = { new_line, new_column };
- }
+ auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
+ TextPosition new_cursor = m_editor->text_position_at_content_position(position_above);
m_editor->set_cursor(new_cursor);
}
return DidMoveALine::No;
@@ -325,15 +318,8 @@ EditingEngine::DidMoveALine EditingEngine::move_one_down(KeyEvent const& event)
}
return DidMoveALine::No;
}
- TextPosition new_cursor;
- if (m_editor->is_wrapping_enabled()) {
- auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
- new_cursor = m_editor->text_position_at_content_position(position_below);
- } else {
- size_t new_line = m_editor->cursor().line() + 1;
- size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
- new_cursor = { new_line, new_column };
- }
+ auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
+ TextPosition new_cursor = m_editor->text_position_at_content_position(position_below);
m_editor->set_cursor(new_cursor);
}
return DidMoveALine::No;
@@ -343,17 +329,8 @@ void EditingEngine::move_up(double page_height_factor)
{
if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
-
- TextPosition new_cursor;
- if (m_editor->is_wrapping_enabled()) {
- auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
- new_cursor = m_editor->text_position_at_content_position(position_above);
- } else {
- size_t page_step = (size_t)pixels / (size_t)m_editor->line_height();
- size_t new_line = m_editor->cursor().line() < page_step ? 0 : m_editor->cursor().line() - page_step;
- size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
- new_cursor = { new_line, new_column };
- }
+ auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
+ TextPosition new_cursor = m_editor->text_position_at_content_position(position_above);
m_editor->set_cursor(new_cursor);
}
};
@@ -362,15 +339,8 @@ void EditingEngine::move_down(double page_height_factor)
{
if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
- TextPosition new_cursor;
- if (m_editor->is_wrapping_enabled()) {
- auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
- new_cursor = m_editor->text_position_at_content_position(position_below);
- } else {
- size_t new_line = min(m_editor->line_count() - 1, m_editor->cursor().line() + pixels / m_editor->line_height());
- size_t new_column = min(m_editor->cursor().column(), m_editor->lines()[new_line].length());
- new_cursor = { new_line, new_column };
- }
+ auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
+ TextPosition new_cursor = m_editor->text_position_at_content_position(position_below);
m_editor->set_cursor(new_cursor);
};
}