ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
TextArea.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
27 class TextArea : public TLayout<TextArea>
28 {
29 public:
30 TextArea(std::string initial = "", int rows = 3);
31
32 void setText(std::string t);
33 const std::string &getText() const { return text; }
34 void clear() { setText(""); }
35
36 int getCursorPos() const { return cursorPos; }
37 void setCursorPos(int pos);
38
39 const std::string &getCompositionText() const noexcept { return compositionText_; }
40 int getCompositionCursor() const noexcept { return compositionCursor_; }
41 bool isComposing() const noexcept { return !compositionText_.empty(); }
42
43 void setPlaceholder(std::string p);
44 const std::string &getPlaceholder() const { return placeholder; }
45
46 void setPlaceholderWrap(bool w);
47 bool getPlaceholderWrap() const { return placeholderWrap_; }
48
49 void setRows(int r);
50 int getRows() const { return rows_; }
51
52 void setFont(FontHandle f);
53
54 std::function<void(const std::string &)> onTextChanged;
55
56 static constexpr const char *PART_SELECTION = "selection";
57 static constexpr const char *PART_PLACEHOLDER = "placeholder";
58 static constexpr const char *PART_CURSOR = "cursor";
59
60 protected:
61 Vec2 computeIntrinsicSize(float availW, float availH) override;
62 void onEnabledChanged(bool nowEnabled) override;
63 void onUpdate(float dt) override;
64 void renderContent(float op, const ComputedStyle &style) override;
65
66 private:
67 TextMeasureSpec makeSpec(const ComputedStyle &style) const;
68 void ensureLines(float availW, const ComputedStyle &style);
69 void ensurePlaceholderLines(float availW, const ComputedStyle &style);
70 int cursorIndexFromPos(float localX, float localY, float pl, float pt) const;
71
72 struct HistoryState
73 {
74 std::string text;
75 int cursorPos;
76 };
77 std::deque<HistoryState> undoStack;
78 std::deque<HistoryState> redoStack;
79
80 void saveHistory();
81 void undo();
82 void redo();
83 void selectWordAtCursor();
84
85 std::string text;
86 std::string placeholder;
87 bool placeholderWrap_{true};
88 bool placeholder_dirty{true};
89 float lastPhWrapW{-1.0f};
90 float lastPhFontSize{-1.0f};
91 float lastPhLetterSpacing{-1.0f};
92 uint32_t lastPhFontId{0};
93 FontStack lastPhFontStack;
94 std::shared_ptr<const TextLayout> placeholderLayout_;
95
96 int rows_{3};
97 int cursorPos{0};
98 int selectionStart{-1};
99 std::string compositionText_;
100 std::string displayText_;
101 int compositionCursor_{0};
102 int compositionSelLen_{0};
103 bool text_dirty{true};
104 bool hasFocusCache{false};
105 float cursorBlink{0.0f};
106 float scrollY{0.0f};
107 float lastWrapW{-1.0f};
108 float lastFontSize{-1.0f};
109 float lastLetterSpacing{-1.0f};
110 uint32_t lastFontId{0};
111 FontStack lastFontStack;
112 FontHandle defaultFont{0};
115
116 std::shared_ptr<const TextLayout> textLayout_;
117 int clickCount{0};
118 double lastClickTime{0.0};
119
120 struct RepeatState
121 {
122 float holdTime{0.0f};
123 float nextFireTime{0.0f};
124 };
125 std::unordered_map<int, RepeatState> repeatStates;
126 };
127
128} // 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.
void onEnabledChanged(bool nowEnabled) override
Called from setEnabled after the state change.
Vec2 computeIntrinsicSize(float availW, float availH) override
Compute the intrinsic size, excluding padding and border.
void renderContent(float op, const ComputedStyle &style) override
Draw the node's content (text, images, custom geometry).
TextDirection
Inline / layout writing direction for Bidi text and RTL layout mirroring (ยง6.2).
Definition UIEnums.hpp:83
@ Auto
Infer paragraph direction from first strong Bidi character (fallback LTR).
Definition UIEnums.hpp:84
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