HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_widget.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "widget.hpp"
8#include "../GUI/theme_text_style.hpp"
9#include "../GUI/mouse_event.hpp"
10#include "../text/text_selection.hpp"
11#include "../text/text_shaper.hpp"
12#include "../observable.hpp"
13#include "../alignment.hpp"
14#include "../i18n/translate.hpp"
15#include "../undo_stack.hpp"
16#include "../scoped_task.hpp"
17#include <memory>
18#include <string>
19#include <array>
20#include <optional>
21#include <future>
22#include <limits>
23#include <chrono>
24
25namespace hi::inline v1 {
26
52class text_widget final : public widget {
53public:
54 using super = widget;
55
58 enum class edit_mode_type {
61 fixed,
62
65 selectable,
66
69 line_editable,
70
73 fully_editable
74 };
75
79
82 observable<alignment> alignment = hi::alignment::middle_center();
83
86 observable<theme_text_style> text_style = theme_text_style::label;
87
90 observable<edit_mode_type> edit_mode = edit_mode_type::selectable;
91
101 template<
102 typename Text,
103 typename Alignment = hi::alignment,
104 typename VerticalAlignment = hi::vertical_alignment,
105 typename TextStyle = hi::theme_text_style>
107 gui_window& window,
108 widget *parent,
109 Text&& text,
110 Alignment&& alignment = hi::alignment::middle_center(),
111 TextStyle&& text_style = theme_text_style::label) noexcept :
112 text_widget(window, parent)
113 {
114 this->text = std::forward<Text>(text);
115 this->alignment = std::forward<Alignment>(alignment);
116 this->text_style = std::forward<TextStyle>(text_style);
117 }
118
120 widget_constraints const& set_constraints() noexcept override;
121 void set_layout(widget_layout const& layout) noexcept override;
122 void draw(draw_context const& context) noexcept override;
123 bool handle_event(hi::command command) noexcept override;
124 bool handle_event(keyboard_event const& event) noexcept override;
125 bool handle_event(mouse_event const& event) noexcept override;
126 hitbox hitbox_test(point3 position) const noexcept override;
127 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
129private:
130 enum class add_type { append, insert, dead };
131
132 struct undo_type {
133 gstring text;
134 text_selection selection;
135 };
136
137 text_shaper _shaped_text;
138 float _shaped_text_cap_height;
139
140 decltype(text)::token_type _text_cbt;
141
142 text_selection _selection;
143
144 scoped_task<> _blink_cursor;
145
146 bool _cursor_visible = false;
147
148 utc_nanoseconds _cursor_blink_time_point = {};
149
156 mouse_event _last_drag_mouse_event = {};
157
160 utc_nanoseconds _last_drag_mouse_event_next_repeat = {};
161
164 float _vertical_movement_x = std::numeric_limits<float>::quiet_NaN();
165
166 bool _overwrite_mode = false;
167
173 grapheme _has_dead_character = {};
174
175 undo_stack<undo_type> _undo_stack = {1000};
176
177 text_widget(gui_window& window, widget *parent) noexcept;
178
181 void scroll_to_show_selection() noexcept;
182
192 void reset_state(char const *states) noexcept;
193
194 [[nodiscard]] gstring_view selected_text() const noexcept;
195 void undo_push() noexcept;
196 void undo() noexcept;
197 void redo() noexcept;
198
199 //scoped_task<> blink_cursor() noexcept;
200
205 void fix_cursor_position(size_t size) noexcept;
206
211 void fix_cursor_position() noexcept
212 {
213 fix_cursor_position(_shaped_text.size());
214 }
215
216 void replace_selection(gstring replacement) noexcept;
217
223 void add_character(grapheme c, add_type mode) noexcept;
224 void delete_dead_character() noexcept;
225 void delete_character_next() noexcept;
226 void delete_character_prev() noexcept;
227 void delete_word_next() noexcept;
228 void delete_word_prev() noexcept;
229};
230
231} // namespace hi::inline v1
Definition alignment.hpp:64
Definition fixed.hpp:17
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:51
Definition gui_window.hpp:40
Definition hitbox.hpp:16
Definition keyboard_event.hpp:47
Definition mouse_event.hpp:15
An observable value.
Definition observable.hpp:356
Definition text_style.hpp:18
A text widget.
Definition text_widget.hpp:52
observable< gstring > text
The text to be displayed.
Definition text_widget.hpp:78
edit_mode_type
Mode of the text-widget.
Definition text_widget.hpp:58
text_widget(gui_window &window, widget *parent, Text &&text, Alignment &&alignment=hi::alignment::middle_center(), TextStyle &&text_style=theme_text_style::label) noexcept
Construct a text widget.
Definition text_widget.hpp:106
An interactive graphical object as part of the user-interface.
Definition widget.hpp:40
Definition widget_constraints.hpp:12
Definition widget_layout.hpp:17
T quiet_NaN(T... args)