mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:18:12 +00:00
GTextBox: Avoid one instance of overdraw + naming cleanup.
This commit is contained in:
parent
0e73aa36c8
commit
90e898b771
2 changed files with 33 additions and 34 deletions
|
@ -18,7 +18,7 @@ GTextBox::~GTextBox()
|
||||||
void GTextBox::set_text(String&& text)
|
void GTextBox::set_text(String&& text)
|
||||||
{
|
{
|
||||||
m_text = move(text);
|
m_text = move(text);
|
||||||
m_cursorPosition = m_text.length();
|
m_cursor_position = m_text.length();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,34 +26,33 @@ void GTextBox::paint_event(GPaintEvent&)
|
||||||
{
|
{
|
||||||
Painter painter(*this);
|
Painter painter(*this);
|
||||||
|
|
||||||
// FIXME: Reduce overdraw.
|
painter.fill_rect({ rect().x() + 1, rect().y() + 1, rect().width() - 2, rect().height() - 2 }, background_color());
|
||||||
painter.fill_rect(rect(), background_color());
|
|
||||||
painter.draw_rect(rect(), foreground_color());
|
painter.draw_rect(rect(), foreground_color());
|
||||||
|
|
||||||
if (is_focused())
|
if (is_focused())
|
||||||
painter.draw_focus_rect(rect());
|
painter.draw_focus_rect(rect());
|
||||||
|
|
||||||
Rect innerRect = rect();
|
Rect inner_rect = rect();
|
||||||
innerRect.shrink(6, 6);
|
inner_rect.shrink(6, 6);
|
||||||
|
|
||||||
size_t maxCharsToPaint = innerRect.width() / font().glyph_width();
|
size_t max_chars_to_paint = inner_rect.width() / font().glyph_width();
|
||||||
|
|
||||||
int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
|
int first_visible_char = max((int)m_cursor_position - (int)max_chars_to_paint, 0);
|
||||||
size_t charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint);
|
size_t chars_to_paint = min(m_text.length() - first_visible_char, max_chars_to_paint);
|
||||||
|
|
||||||
int y = innerRect.center().y() - font().glyph_height() / 2;
|
int y = inner_rect.center().y() - font().glyph_height() / 2;
|
||||||
for (size_t i = 0; i < charsToPaint; ++i) {
|
for (size_t i = 0; i < chars_to_paint; ++i) {
|
||||||
char ch = m_text[firstVisibleChar + i];
|
char ch = m_text[first_visible_char + i];
|
||||||
if (ch == ' ')
|
if (ch == ' ')
|
||||||
continue;
|
continue;
|
||||||
int x = innerRect.x() + (i * font().glyph_width());
|
int x = inner_rect.x() + (i * font().glyph_width());
|
||||||
painter.draw_bitmap({x, y}, font().glyph_bitmap(ch), Color::Black);
|
painter.draw_bitmap({x, y}, font().glyph_bitmap(ch), Color::Black);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_focused() && m_cursorBlinkState) {
|
if (is_focused() && m_cursor_blink_state) {
|
||||||
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
|
unsigned visible_cursor_position = m_cursor_position - first_visible_char;
|
||||||
Rect cursorRect(innerRect.x() + visibleCursorPosition * font().glyph_width(), innerRect.y(), 1, innerRect.height());
|
Rect cursor_rect(inner_rect.x() + visible_cursor_position * font().glyph_width(), inner_rect.y(), 1, inner_rect.height());
|
||||||
painter.fill_rect(cursorRect, foreground_color());
|
painter.fill_rect(cursor_rect, foreground_color());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,12 +62,12 @@ void GTextBox::mousedown_event(GMouseEvent&)
|
||||||
|
|
||||||
void GTextBox::handle_backspace()
|
void GTextBox::handle_backspace()
|
||||||
{
|
{
|
||||||
if (m_cursorPosition == 0)
|
if (m_cursor_position == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (m_text.length() == 1) {
|
if (m_text.length() == 1) {
|
||||||
m_text = String::empty();
|
m_text = String::empty();
|
||||||
m_cursorPosition = 0;
|
m_cursor_position = 0;
|
||||||
update();
|
update();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -76,11 +75,11 @@ void GTextBox::handle_backspace()
|
||||||
char* buffer;
|
char* buffer;
|
||||||
auto newText = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
|
auto newText = StringImpl::create_uninitialized(m_text.length() - 1, buffer);
|
||||||
|
|
||||||
memcpy(buffer, m_text.characters(), m_cursorPosition - 1);
|
memcpy(buffer, m_text.characters(), m_cursor_position - 1);
|
||||||
memcpy(buffer + m_cursorPosition - 1, m_text.characters() + m_cursorPosition, m_text.length() - (m_cursorPosition - 1));
|
memcpy(buffer + m_cursor_position - 1, m_text.characters() + m_cursor_position, m_text.length() - (m_cursor_position - 1));
|
||||||
|
|
||||||
m_text = move(newText);
|
m_text = move(newText);
|
||||||
--m_cursorPosition;
|
--m_cursor_position;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,15 +87,15 @@ void GTextBox::keydown_event(GKeyEvent& event)
|
||||||
{
|
{
|
||||||
switch (event.key()) {
|
switch (event.key()) {
|
||||||
case KeyCode::Key_Left:
|
case KeyCode::Key_Left:
|
||||||
if (m_cursorPosition)
|
if (m_cursor_position)
|
||||||
--m_cursorPosition;
|
--m_cursor_position;
|
||||||
m_cursorBlinkState = true;
|
m_cursor_blink_state = true;
|
||||||
update();
|
update();
|
||||||
return;
|
return;
|
||||||
case KeyCode::Key_Right:
|
case KeyCode::Key_Right:
|
||||||
if (m_cursorPosition < m_text.length())
|
if (m_cursor_position < m_text.length())
|
||||||
++m_cursorPosition;
|
++m_cursor_position;
|
||||||
m_cursorBlinkState = true;
|
m_cursor_blink_state = true;
|
||||||
update();
|
update();
|
||||||
return;
|
return;
|
||||||
case KeyCode::Key_Backspace:
|
case KeyCode::Key_Backspace:
|
||||||
|
@ -113,12 +112,12 @@ void GTextBox::keydown_event(GKeyEvent& event)
|
||||||
char* buffer;
|
char* buffer;
|
||||||
auto newText = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
|
auto newText = StringImpl::create_uninitialized(m_text.length() + 1, buffer);
|
||||||
|
|
||||||
memcpy(buffer, m_text.characters(), m_cursorPosition);
|
memcpy(buffer, m_text.characters(), m_cursor_position);
|
||||||
buffer[m_cursorPosition] = event.text()[0];
|
buffer[m_cursor_position] = event.text()[0];
|
||||||
memcpy(buffer + m_cursorPosition + 1, m_text.characters() + m_cursorPosition, m_text.length() - m_cursorPosition);
|
memcpy(buffer + m_cursor_position + 1, m_text.characters() + m_cursor_position, m_text.length() - m_cursor_position);
|
||||||
|
|
||||||
m_text = move(newText);
|
m_text = move(newText);
|
||||||
++m_cursorPosition;
|
++m_cursor_position;
|
||||||
update();
|
update();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -130,6 +129,6 @@ void GTextBox::timerEvent(GTimerEvent&)
|
||||||
if (!is_focused())
|
if (!is_focused())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_cursorBlinkState = !m_cursorBlinkState;
|
m_cursor_blink_state = !m_cursor_blink_state;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ private:
|
||||||
void handle_backspace();
|
void handle_backspace();
|
||||||
|
|
||||||
String m_text;
|
String m_text;
|
||||||
unsigned m_cursorPosition { 0 };
|
unsigned m_cursor_position { 0 };
|
||||||
bool m_cursorBlinkState { false };
|
bool m_cursor_blink_state { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue