From 15dc71e8e16bd9ec6c8eba936ae6fc7419a30a15 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 31 Jul 2024 19:32:22 +0200 Subject: [PATCH] libui/InputField: Fix a few bugs and add a clear() method --- gui/libui/include/ui/InputField.h | 2 ++ gui/libui/src/InputField.cpp | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/gui/libui/include/ui/InputField.h b/gui/libui/include/ui/InputField.h index bd3bab56..8d3f3052 100644 --- a/gui/libui/include/ui/InputField.h +++ b/gui/libui/include/ui/InputField.h @@ -23,6 +23,8 @@ namespace ui Result draw(ui::Canvas& canvas) override; + void clear(); + StringView data(); void on_submit(os::Function&& action) diff --git a/gui/libui/src/InputField.cpp b/gui/libui/src/InputField.cpp index a1e00eeb..145d20ed 100644 --- a/gui/libui/src/InputField.cpp +++ b/gui/libui/src/InputField.cpp @@ -17,6 +17,8 @@ namespace ui { InputField::InputField(SharedPtr font) : ui::TextInput(), m_font(font) { + u8 zero = 0; + m_data.append_data(&zero, 1); } Result 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 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() }; } }