ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
TextInput.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "Common.hpp"
9#include "Layout.hpp"
10
11#include <deque>
12
13namespace ZenitUI::UI
14{
18
26 class TextInput : public TLayout<TextInput>
27 {
28 public:
33 TextInput(std::string initial = "");
34
36 void setText(std::string t);
38 const std::string &getText() const { return text; }
40 void clear() { setText(""); }
41
43 void setPlaceholder(std::string p);
45 const std::string &getPlaceholder() const { return placeholder; }
47 void setPlaceholderWrap(bool w);
49 bool getPlaceholderWrap() const { return placeholderWrap; }
50
52 int getCursorPos() const { return cursorPos; }
54 void setCursorPos(int pos);
55
56 const std::string &getCompositionText() const noexcept { return compositionText_; }
57 int getCompositionCursor() const noexcept { return compositionCursor_; }
58 bool isComposing() const noexcept { return !compositionText_.empty(); }
59
62
64 std::function<void(const std::string &)> onTextChanged;
66 std::function<void(const std::string &)> onSubmit;
67
68 static constexpr const char *PART_SELECTION = "selection";
69 static constexpr const char *PART_PLACEHOLDER = "placeholder";
70 static constexpr const char *PART_CURSOR = "cursor";
71
72 protected:
73 Vec2 computeIntrinsicSize(float availW, float availH) override;
74 void onEnabledChanged(bool nowEnabled) override;
75 void onUpdate(float dt) override;
76 void renderContent(float op, const ComputedStyle &style) override;
77
78 private:
79 TextMeasureSpec makeSpec(const ComputedStyle &style, float lineHeight = 0.0f) const;
80 int cursorIndexFromX(float localX);
81
82 struct HistoryState
83 {
84 std::string text;
85 int cursorPos;
86 };
87 std::deque<HistoryState> undoStack;
88 std::deque<HistoryState> redoStack;
89
90 void saveHistory();
91 void undo();
92 void redo();
93 void selectWordAtCursor();
94
95 std::string text;
96 std::string placeholder;
97 bool placeholderWrap{false};
98 int cursorPos{0};
99 int selectionStart{-1};
100 std::string compositionText_;
101 int compositionCursor_{0};
102 int compositionSelLen_{0};
103 bool hasFocusCache{false};
104 float cursorBlink{0.0f};
105 float scrollX{0.0f};
106 FontHandle defaultFont{0};
107
108 int clickCount{0};
109 double lastClickTime{0.0};
110
111 struct RepeatState
112 {
113 float holdTime{0.0f};
114 float nextFireTime{0.0f};
115 };
116 std::unordered_map<int, RepeatState> repeatStates;
117 };
118
119} // namespace ZenitUI::UI
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
TLayout(Args &&...args)
Definition Layout.hpp:1401
void onUpdate(float dt) override
Called every frame from update, after input dispatch.
const std::string & getText() const
Current content.
Definition TextInput.hpp:38
void onEnabledChanged(bool nowEnabled) override
Called from setEnabled after the state change.
void setText(std::string t)
Replace the content, snap cursor to a valid UTF-8 boundary, clear selection, and save undo history.
void renderContent(float op, const ComputedStyle &style) override
Draw the node's content (text, images, custom geometry).
TextInput(std::string initial="")
Construct with initial text.
std::function< void(const std::string &)> onTextChanged
Fired on every content change (typing, paste, delete, cut, undo, redo).
Definition TextInput.hpp:64
void clear()
Reset to empty content.
Definition TextInput.hpp:40
bool getPlaceholderWrap() const
true if placeholder wrapping is enabled.
Definition TextInput.hpp:49
std::function< void(const std::string &)> onSubmit
Fired on Enter; the field loses focus immediately after.
Definition TextInput.hpp:66
void setPlaceholder(std::string p)
Placeholder shown when body is empty.
void setCursorPos(int pos)
Move the cursor (clamped and snapped to a valid UTF-8 boundary); clears selection.
Vec2 computeIntrinsicSize(float availW, float availH) override
Compute the intrinsic size, excluding padding and border.
void setFont(FontHandle f)
Override the font used for measurement and rendering.
const std::string & getPlaceholder() const
Current placeholder text.
Definition TextInput.hpp:45
int getCursorPos() const
Current cursor position in bytes from the start.
Definition TextInput.hpp:52
void setPlaceholderWrap(bool w)
Enable or disable multi-line wrapping for the placeholder.
Dense style: every property has a concrete value.
Definition Style.hpp:1036
Opaque handle to a font loaded by TextEngine.
Definition CoreTypes.hpp:135
Typographic parameters governing shaping and measurement.
Definition TextEngine.hpp:51
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25