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/gui_event.hpp"
9#include "../text/semantic_text_style.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
59
62 observable<alignment> alignment = hi::alignment::middle_center();
63
66 observable<semantic_text_style> text_style = semantic_text_style::label;
67
77 template<
78 typename Text,
79 typename Alignment = hi::alignment,
80 typename VerticalAlignment = hi::vertical_alignment,
81 typename TextStyle = hi::semantic_text_style>
83 gui_window& window,
84 widget *parent,
85 Text&& text,
86 Alignment&& alignment = hi::alignment::middle_center(),
87 TextStyle&& text_style = semantic_text_style::label) noexcept :
88 text_widget(window, parent)
89 {
90 this->text = std::forward<Text>(text);
91 this->alignment = std::forward<Alignment>(alignment);
92 this->text_style = std::forward<TextStyle>(text_style);
93 }
94
96 widget_constraints const& set_constraints() noexcept override;
97 void set_layout(widget_layout const& layout) noexcept override;
98 void draw(draw_context const& context) noexcept override;
99 bool handle_event(gui_event const& event) noexcept override;
100 hitbox hitbox_test(point3 position) const noexcept override;
101 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
103private:
104 enum class add_type { append, insert, dead };
105
106 struct undo_type {
107 gstring text;
108 text_selection selection;
109 };
110
111 enum class cursor_state_type { off, on, busy, none };
112
113 text_shaper _shaped_text;
114 float _base_line;
115
116 decltype(text)::token_type _text_cbt;
117 decltype(text_style)::token_type _text_style_cbt;
118
119 text_selection _selection;
120
121 scoped_task<> _blink_cursor;
122
123 observable<cursor_state_type> _cursor_state = cursor_state_type::none;
124 decltype(_cursor_state)::token_type _cursor_state_cbt;
125
128 bool _request_scroll = false;
129
136 gui_event _last_drag_mouse_event = {};
137
140 utc_nanoseconds _last_drag_mouse_event_next_repeat = {};
141
144 float _vertical_movement_x = std::numeric_limits<float>::quiet_NaN();
145
146 bool _overwrite_mode = false;
147
153 grapheme _has_dead_character = {};
154
155 undo_stack<undo_type> _undo_stack = {1000};
156
157 text_widget(gui_window& window, widget *parent) noexcept;
158
163 void update_shaped_text() noexcept;
164
167 void scroll_to_show_selection() noexcept;
168
169 void request_scroll() noexcept;
170
180 void reset_state(char const *states) noexcept;
181
182 [[nodiscard]] gstring_view selected_text() const noexcept;
183 void undo_push() noexcept;
184 void undo() noexcept;
185 void redo() noexcept;
186
187 scoped_task<> blink_cursor() noexcept;
188
191 void fix_cursor_position() noexcept;
192
193 void replace_selection(gstring const &replacement) noexcept;
194
200 void add_character(grapheme c, add_type mode) noexcept;
201 void delete_dead_character() noexcept;
202 void delete_character_next() noexcept;
203 void delete_character_prev() noexcept;
204 void delete_word_next() noexcept;
205 void delete_word_prev() noexcept;
206};
207
208} // namespace hi::inline v1
@ off
The 'off' state of a button.
Definition alignment.hpp:64
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:52
A user interface event.
Definition gui_event.hpp:58
Definition gui_window.hpp:39
Definition hitbox.hpp:16
An observable value.
Definition observable.hpp:359
Definition text_style.hpp:175
A text widget.
Definition text_widget.hpp:52
observable< gstring > text
The text to be displayed.
Definition text_widget.hpp:58
text_widget(gui_window &window, widget *parent, Text &&text, Alignment &&alignment=hi::alignment::middle_center(), TextStyle &&text_style=semantic_text_style::label) noexcept
Construct a text widget.
Definition text_widget.hpp:82
An interactive graphical object as part of the user-interface.
Definition widget.hpp:39
Definition widget_constraints.hpp:13
Definition widget_layout.hpp:18
T quiet_NaN(T... args)