libui/InputField: Fix a few bugs and add a clear() method

This commit is contained in:
apio 2024-07-31 19:32:22 +02:00
parent 140910763e
commit 15dc71e8e1
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 15 additions and 4 deletions

View File

@ -23,6 +23,8 @@ namespace ui
Result<void> draw(ui::Canvas& canvas) override;
void clear();
StringView data();
void on_submit(os::Function<StringView>&& action)

View File

@ -17,6 +17,8 @@ namespace ui
{
InputField::InputField(SharedPtr<ui::Font> font) : ui::TextInput(), m_font(font)
{
u8 zero = 0;
m_data.append_data(&zero, 1);
}
Result<ui::EventResult> InputField::handle_key_event(const ui::KeyEventRequest& request)
@ -35,7 +37,7 @@ namespace ui
if (request.code == moon::K_RightArrow)
{
if (m_cursor < m_data.size()) m_cursor++;
if (m_cursor < (m_data.size() - 1)) m_cursor++;
else
return ui::EventResult::DidNotHandle;
update_cursor();
@ -67,9 +69,7 @@ namespace ui
if (iscntrl(request.letter)) return ui::EventResult::DidNotHandle;
if (m_cursor == m_data.size()) TRY(m_data.append_data((const u8*)&request.letter, 1));
else
TRY(insert_character(request.letter));
TRY(insert_character(request.letter));
m_cursor++;
@ -78,6 +78,14 @@ namespace ui
return ui::EventResult::DidHandle;
}
void InputField::clear()
{
m_data.try_resize(0);
u8 zero = 0;
m_data.append_data(&zero, 1);
m_cursor = 0;
}
Result<void> InputField::draw(ui::Canvas& canvas)
{
int visible_characters = canvas.width / m_font->width();
@ -107,6 +115,7 @@ namespace ui
StringView InputField::data()
{
if (!m_data.size()) return StringView {};
return StringView { (const char*)m_data.data(), m_data.size() };
}
}