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

GTextEditor: Fix line_content_rect() behavior with custom alignments.

This commit is contained in:
Andreas Kling 2019-04-24 22:46:27 +02:00
parent 62f0aae4ca
commit 5c240af3f7
2 changed files with 8 additions and 8 deletions

View file

@ -243,7 +243,8 @@ void GTextEditor::paint_event(GPaintEvent& event)
for (int i = first_visible_line; i <= last_visible_line; ++i) { for (int i = first_visible_line; i <= last_visible_line; ++i) {
auto& line = *m_lines[i]; auto& line = *m_lines[i];
auto line_rect = line_content_rect(i); auto line_rect = line_content_rect(i);
line_rect.set_width(exposed_width); // FIXME: Make sure we always fill the entire line.
//line_rect.set_width(exposed_width);
if (is_multi_line() && i == m_cursor.line()) if (is_multi_line() && i == m_cursor.line())
painter.fill_rect(line_rect, Color(230, 230, 230)); painter.fill_rect(line_rect, Color(230, 230, 230));
painter.draw_text(line_rect, line.characters(), line.length(), m_text_alignment, Color::Black); painter.draw_text(line_rect, line.characters(), line.length(), m_text_alignment, Color::Black);
@ -608,26 +609,24 @@ Rect GTextEditor::line_widget_rect(int line_index) const
void GTextEditor::scroll_cursor_into_view() void GTextEditor::scroll_cursor_into_view()
{ {
auto rect = cursor_content_rect(); auto rect = cursor_content_rect();
if (m_cursor.column() == 0) rect.set_x(content_x_for_position(m_cursor));
rect.set_x(0);
else if (m_cursor.column() >= m_lines[m_cursor.line()]->length())
rect.set_x(m_lines[m_cursor.line()]->width(font()) + m_horizontal_content_padding * 2);
scroll_into_view(rect, true, true); scroll_into_view(rect, true, true);
} }
Rect GTextEditor::line_content_rect(int line_index) const Rect GTextEditor::line_content_rect(int line_index) const
{ {
auto& line = *m_lines[line_index];
if (is_single_line()) { if (is_single_line()) {
Rect line_rect = { m_horizontal_content_padding, 0, content_width() - m_horizontal_content_padding * 2, font().glyph_height() + 2 }; Rect line_rect = { content_x_for_position({ line_index, 0 }), 0, line.length() * glyph_width(), font().glyph_height() + 2 };
line_rect.center_vertically_within(rect()); line_rect.center_vertically_within(rect());
// FIXME: This would not be necessary if we knew more about the font and could center it based on baseline and x-height. // FIXME: This would not be necessary if we knew more about the font and could center it based on baseline and x-height.
line_rect.move_by(0, 1); line_rect.move_by(0, 1);
return line_rect; return line_rect;
} }
return { return {
m_horizontal_content_padding, content_x_for_position({ line_index, 0 }),
line_index * line_height(), line_index * line_height(),
content_width() - m_horizontal_content_padding * 2, line.length() * glyph_width(),
line_height() line_height()
}; };
} }

View file

@ -90,6 +90,7 @@ public:
int line_height() const { return font().glyph_height() + m_line_spacing; } int line_height() const { return font().glyph_height() + m_line_spacing; }
GTextPosition cursor() const { return m_cursor; } GTextPosition cursor() const { return m_cursor; }
GTextRange normalized_selection() const { return m_selection.normalized(); } GTextRange normalized_selection() const { return m_selection.normalized(); }
// FIXME: This should take glyph spacing into account, no?
int glyph_width() const { return font().glyph_width('x'); } int glyph_width() const { return font().glyph_width('x'); }
bool write_to_file(const String& path); bool write_to_file(const String& path);