mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
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.
This commit is contained in:
parent
7aef096f85
commit
69333e5dbd
2 changed files with 16 additions and 57 deletions
|
@ -301,15 +301,8 @@ EditingEngine::DidMoveALine EditingEngine::move_one_up(KeyEvent const& event)
|
||||||
}
|
}
|
||||||
return DidMoveALine::No;
|
return DidMoveALine::No;
|
||||||
}
|
}
|
||||||
TextPosition new_cursor;
|
auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
|
||||||
if (m_editor->is_wrapping_enabled()) {
|
TextPosition new_cursor = m_editor->text_position_at_content_position(position_above);
|
||||||
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 };
|
|
||||||
}
|
|
||||||
m_editor->set_cursor(new_cursor);
|
m_editor->set_cursor(new_cursor);
|
||||||
}
|
}
|
||||||
return DidMoveALine::No;
|
return DidMoveALine::No;
|
||||||
|
@ -325,15 +318,8 @@ EditingEngine::DidMoveALine EditingEngine::move_one_down(KeyEvent const& event)
|
||||||
}
|
}
|
||||||
return DidMoveALine::No;
|
return DidMoveALine::No;
|
||||||
}
|
}
|
||||||
TextPosition new_cursor;
|
auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
|
||||||
if (m_editor->is_wrapping_enabled()) {
|
TextPosition new_cursor = m_editor->text_position_at_content_position(position_below);
|
||||||
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 };
|
|
||||||
}
|
|
||||||
m_editor->set_cursor(new_cursor);
|
m_editor->set_cursor(new_cursor);
|
||||||
}
|
}
|
||||||
return DidMoveALine::No;
|
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()) {
|
if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
|
||||||
int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
|
int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
|
||||||
|
auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
|
||||||
TextPosition new_cursor;
|
TextPosition new_cursor = m_editor->text_position_at_content_position(position_above);
|
||||||
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 };
|
|
||||||
}
|
|
||||||
m_editor->set_cursor(new_cursor);
|
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()) {
|
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);
|
int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
|
||||||
TextPosition new_cursor;
|
auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
|
||||||
if (m_editor->is_wrapping_enabled()) {
|
TextPosition new_cursor = m_editor->text_position_at_content_position(position_below);
|
||||||
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 };
|
|
||||||
}
|
|
||||||
m_editor->set_cursor(new_cursor);
|
m_editor->set_cursor(new_cursor);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,18 +149,14 @@ TextPosition TextEditor::text_position_at_content_position(Gfx::IntPoint content
|
||||||
size_t line_index = 0;
|
size_t line_index = 0;
|
||||||
|
|
||||||
if (position.y() >= 0) {
|
if (position.y() >= 0) {
|
||||||
if (is_wrapping_enabled()) {
|
for (size_t i = 0; i < line_count(); ++i) {
|
||||||
for (size_t i = 0; i < line_count(); ++i) {
|
auto& rect = m_line_visual_data[i].visual_rect;
|
||||||
auto& rect = m_line_visual_data[i].visual_rect;
|
if (position.y() >= rect.top() && position.y() <= rect.bottom()) {
|
||||||
if (position.y() >= rect.top() && position.y() <= rect.bottom()) {
|
line_index = i;
|
||||||
line_index = i;
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (position.y() > rect.bottom())
|
|
||||||
line_index = line_count() - 1;
|
|
||||||
}
|
}
|
||||||
} else {
|
if (position.y() > rect.bottom())
|
||||||
line_index = (size_t)(position.y() / line_height());
|
line_index = line_count() - 1;
|
||||||
}
|
}
|
||||||
line_index = max((size_t)0, min(line_index, line_count() - 1));
|
line_index = max((size_t)0, min(line_index, line_count() - 1));
|
||||||
}
|
}
|
||||||
|
@ -1357,14 +1353,7 @@ Gfx::IntRect TextEditor::line_content_rect(size_t line_index) const
|
||||||
line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
|
line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
|
||||||
return line_rect;
|
return line_rect;
|
||||||
}
|
}
|
||||||
if (is_wrapping_enabled())
|
return m_line_visual_data[line_index].visual_rect;
|
||||||
return m_line_visual_data[line_index].visual_rect;
|
|
||||||
return {
|
|
||||||
content_x_for_position({ line_index, 0 }),
|
|
||||||
(int)line_index * line_height(),
|
|
||||||
text_width_for_font(line.view(), font()),
|
|
||||||
line_height()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditor::set_cursor_and_focus_line(size_t line, size_t column)
|
void TextEditor::set_cursor_and_focus_line(size_t line, size_t column)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue