#pragma once #include "UI/Element/Div.h" #include "UI/Element/Text.h" #include "UI/Style/Class.h" namespace Seele { namespace UI { // A text input field: a box that displays a placeholder (when empty) or a // text value (when non-empty). The actual text editing (key handling, cursor, // IME) is the owning view's responsibility — the view intercepts key events // and calls setValue() with the updated text. // // HTML analog: // // Usage: // auto* field = new InputField(UI::Attributes{}, "Enter name..."); // // per-frame: field->setValue(viewState.inputText); template class InputField : public Div { public: using Div::attr; using Div::style; using Div::markDirty; using Div::getTheme; using Div::children; InputField(Attributes attr, std::string placeholder = "") : Div(std::move(attr), std::initializer_list{}) { attr.focusable = true; _placeholder = std::move(placeholder); auto* t = new Text(_placeholder); children.add(OElement(t)); children.back()->setParent(this); _text = PElement(t); style.paddingLeft = 12; style.paddingRight = 12; style.paddingTop = 8; style.paddingBottom = 8; style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); } virtual ~InputField() override {} void setValue(std::string value) { _value = std::move(value); markDirty(); } std::string getValue() const { return _value; } std::string getPlaceholder() const { return _placeholder; } void setFocused(bool focused) override { _focused = focused; } void setHovered(bool hovered) override { _hovered = hovered; } bool isFocusable() const override { return attr.focusable; } void applyStyle(Style parentStyle, const Theme& theme) override { Div::applyStyle(parentStyle, theme); } void applyOverrides() override { const Theme& th = getTheme(); if (_focused) { style.backgroundColor = Vector(0.14f, 0.15f, 0.18f); } else if (_hovered) { style.backgroundColor = Vector(0.12f, 0.12f, 0.14f); } else { style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); } // Switch between placeholder (dimmed) and value (full color) display. if (_text == nullptr) { return; } auto* t = dynamic_cast*>(_text.getHandle()); if (t == nullptr) { return; } if (_value.empty()) { t->setText(_placeholder); style.textColor = th.color(ColorToken::Gray600); } else { t->setText(_value); style.textColor = th.color(ColorToken::White); } markDirty(); } private: std::string _placeholder; std::string _value; bool _focused = false; bool _hovered = false; PElement _text; }; } // namespace UI } // namespace Seele