1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 14:25:08 +00:00

GTextEditor: Expand the content "to-fill" for right aligned text.

When using a right-side text alignment, hook the resize event to make sure
that the content width is at least frame_inner_rect().width() wide.

This allows us to use content_width() as the anchor point for right aligned
text, rather than using the frame inner rect, which was clearly wrong in
the overflow case.
This commit is contained in:
Andreas Kling 2019-04-24 23:42:49 +02:00
parent f1eba2295d
commit 0c88ce9ee3
2 changed files with 12 additions and 7 deletions

View file

@ -95,6 +95,8 @@ void GTextEditor::update_content_size()
for (auto& line : m_lines)
content_width = max(line->width(font()), content_width);
content_width += m_horizontal_content_padding * 2;
if (is_right_text_alignment(m_text_alignment))
content_width = max(frame_inner_rect().width(), content_width);
int content_height = line_count() * line_height();
set_content_size({ content_width, content_height });
set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
@ -115,7 +117,7 @@ GTextPosition GTextEditor::text_position_at(const Point& a_position) const
column_index = position.x() / glyph_width();
break;
case TextAlignment::CenterRight:
column_index = (position.x() - (frame_inner_rect().right() + 1 - (line.length() * glyph_width()))) / glyph_width();
column_index = (position.x() - (content_width() - (line.length() * glyph_width()))) / glyph_width();
break;
default:
ASSERT_NOT_REACHED();
@ -566,10 +568,8 @@ int GTextEditor::content_x_for_position(const GTextPosition& position) const
switch (m_text_alignment) {
case TextAlignment::CenterLeft:
return m_horizontal_content_padding + position.column() * glyph_width();
break;
case TextAlignment::CenterRight:
return frame_inner_rect().right() + 1 - m_horizontal_content_padding - (line.length() * glyph_width()) + (position.column() * glyph_width());
break;
return content_width() - m_horizontal_content_padding - (line.length() * glyph_width()) + (position.column() * glyph_width());
default:
ASSERT_NOT_REACHED();
}
@ -606,9 +606,7 @@ Rect GTextEditor::line_widget_rect(int line_index) const
void GTextEditor::scroll_cursor_into_view()
{
auto rect = cursor_content_rect();
rect.set_x(content_x_for_position(m_cursor));
scroll_into_view(rect, true, true);
scroll_into_view(cursor_content_rect(), true, true);
}
Rect GTextEditor::line_content_rect(int line_index) const
@ -954,3 +952,9 @@ void GTextEditor::set_text_alignment(TextAlignment alignment)
m_text_alignment = alignment;
update();
}
void GTextEditor::resize_event(GResizeEvent& event)
{
GScrollableWidget::resize_event(event);
update_content_size();
}