1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:57:44 +00:00

GTextEditor: Make the cursor invalidation work with the padding().

This commit is contained in:
Andreas Kling 2019-03-07 13:54:02 +01:00
parent 60c1ab5fbe
commit 9997992907
2 changed files with 12 additions and 4 deletions

View file

@ -62,16 +62,21 @@ void GTextEditor::resize_event(GResizeEvent& event)
void GTextEditor::update_scrollbar_ranges() void GTextEditor::update_scrollbar_ranges()
{ {
int available_height = height() - m_horizontal_scrollbar->height(); int available_height = height() - m_horizontal_scrollbar->height();
int excess_height = max(0, (line_count() * line_height()) - available_height); int excess_height = max(0, (content_height() + padding() * 2) - available_height);
m_vertical_scrollbar->set_range(0, excess_height); m_vertical_scrollbar->set_range(0, excess_height);
int available_width = width() - m_vertical_scrollbar->width(); int available_width = width() - m_vertical_scrollbar->width();
int excess_width = max(0, content_width() - available_width); int excess_width = max(0, (content_width() + padding() * 2) - available_width);
m_horizontal_scrollbar->set_range(0, excess_width); m_horizontal_scrollbar->set_range(0, excess_width);
m_vertical_scrollbar->set_big_step(visible_content_rect().height()); m_vertical_scrollbar->set_big_step(visible_content_rect().height());
} }
int GTextEditor::content_height() const
{
return line_count() * line_height();
}
int GTextEditor::content_width() const int GTextEditor::content_width() const
{ {
// FIXME: Cache this somewhere. // FIXME: Cache this somewhere.
@ -196,6 +201,7 @@ Rect GTextEditor::line_widget_rect(int line_index) const
ASSERT(m_vertical_scrollbar); ASSERT(m_vertical_scrollbar);
auto rect = line_content_rect(line_index); auto rect = line_content_rect(line_index);
rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding())); rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
rect.intersect(this->rect()); rect.intersect(this->rect());
return rect; return rect;
} }
@ -243,10 +249,11 @@ void GTextEditor::set_cursor(int line, int column)
{ {
if (m_cursor.line() == line && m_cursor.column() == column) if (m_cursor.line() == line && m_cursor.column() == column)
return; return;
update_cursor(); auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
m_cursor = GTextPosition(line, column); m_cursor = GTextPosition(line, column);
m_cursor_state = true; m_cursor_state = true;
scroll_cursor_into_view(); scroll_cursor_into_view();
update(old_cursor_line_rect);
update_cursor(); update_cursor();
if (on_cursor_change) if (on_cursor_change)
on_cursor_change(*this); on_cursor_change(*this);

View file

@ -37,12 +37,13 @@ public:
void set_text(const String&); void set_text(const String&);
int content_width() const; int content_width() const;
int content_height() const;
Rect visible_content_rect() const; Rect visible_content_rect() const;
void scroll_cursor_into_view(); void scroll_cursor_into_view();
int line_count() const { return m_lines.size(); } int line_count() const { return m_lines.size(); }
int line_spacing() const { return m_line_spacing; } int line_spacing() const { return m_line_spacing; }
int line_height() const { return font().glyph_height() + m_line_spacing; } int line_height() const { return font().glyph_height() + m_line_spacing; }
int padding() const { return 2; } int padding() const { return 3; }
GTextPosition cursor() const { return m_cursor; } GTextPosition cursor() const { return m_cursor; }
private: private: