24class WideInputBase :
public ComponentBase {
26 WideInputBase(WideStringRef content,
27 ConstStringRef placeholder,
28 Ref<InputOption> option)
29 : content_(content), placeholder_(placeholder), option_(option) {}
31 int cursor_position_internal_ = 0;
32 int& cursor_position() {
33 int& opt = option_->cursor_position();
36 return cursor_position_internal_;
41 std::wstring password_content;
42 if (option_->password())
43 password_content = std::wstring(content_->size(), U
'•');
44 std::wstring& content = option_->password() ? password_content : *content_;
47 std::max(0, std::min<int>(content.size(), cursor_position()));
52 if (content.size() == 0) {
62 return text(content) | main_decorator |
reflect(box_);
64 std::wstring part_before_cursor = content.substr(0, cursor_position());
65 std::wstring part_at_cursor = cursor_position() < (int)content.size()
66 ? content.substr(cursor_position(), 1)
68 std::wstring part_after_cursor = cursor_position() < (int)content.size() - 1
69 ? content.substr(cursor_position() + 1)
76 text(part_before_cursor),
78 text(part_after_cursor)
83 bool OnEvent(Event event)
override {
85 std::max(0, std::min<int>(content_->size(), cursor_position()));
88 return OnMouseEvent(event);
94 if (cursor_position() == 0)
96 content_->erase(cursor_position() - 1, 1);
104 if (cursor_position() ==
int(content_->size()))
106 content_->erase(cursor_position(), 1);
107 option_->on_change();
127 cursor_position() < (
int)content_->size()) {
133 cursor_position() = 0;
138 cursor_position() = (int)content_->size();
143 if (event.is_character()) {
144 content_->insert(cursor_position(), 1,
to_wstring(event.character())[0]);
146 option_->on_change();
153 bool OnMouseEvent(Event event) {
156 if (!box_.
Contain(event.mouse().x, event.mouse().y))
163 int new_cursor_position =
164 cursor_position() +
event.mouse().x - cursor_box_.
x_min;
165 new_cursor_position =
166 std::max(0, std::min<int>(content_->size(), new_cursor_position));
167 if (cursor_position() != new_cursor_position) {
168 cursor_position() = new_cursor_position;
169 option_->on_change();
175 bool Focusable() const final {
return true; }
177 WideStringRef content_;
178 ConstStringRef placeholder_;
182 Ref<InputOption> option_;
190class InputBase :
public WideInputBase {
192 InputBase(StringRef content,
193 ConstStringRef placeholder,
194 Ref<InputOption> option)
195 : WideInputBase(&wrapped_content_,
196 std::move(placeholder),
198 content_(std::move(content)),
203 return WideInputBase::Render();
206 bool OnEvent(Event event)
override {
208 if (WideInputBase::OnEvent(event)) {
216 std::wstring wrapped_content_;
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
void TakeFocus()
Configure all the ancestors to give focus to this component.
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
An adapter. Own or reference an mutable object.
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Element flex(Element)
Make a child element to expand proportionnally to the space left in a container.
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
Component Input(StringRef content, ConstStringRef placeholder, Ref< InputOption > option={})
An input box for editing text.
Element hbox(Elements)
A container displaying elements horizontally one by one.
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Element underlined(Element)
Make the underlined element to be underlined.
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Element text(std::wstring text)
Display a piece of unicode text.
Decorator reflect(Box &box)
Element dim(Element)
Use a light font, for elements with less emphasis.
Element frame(Element)
Allow an element to be displayed inside a 'virtual' area. It size can be larger than its container....
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
std::shared_ptr< ComponentBase > Component
bool Contain(int x, int y)
static const Event Backspace
static const Event Return
static const Event ArrowLeft
static const Event Delete
static const Event ArrowRight