15 constexpr uint32_t k_width_px = 520;
16 constexpr uint32_t k_min_row_px = 48;
17 constexpr uint32_t k_row_padding_px = 16;
19 constexpr glm::vec4 k_accepted_color { 1.F, 1.F, 1.F, 1.F };
20 constexpr glm::vec4 k_refused_color { 1.F, 0.45F, 0.4F, 1.F };
25 std::weak_ptr<Core::Window> window;
28 [[nodiscard]]
bool accepts(std::string_view text)
const
30 return !validator || validator(text);
33 void finish(std::optional<std::string> result,
bool close_window)
41 if (
auto w = window.lock()) {
46 callback(std::move(result));
63 auto state = std::make_shared<PromptState>();
64 state->callback = std::move(callback);
65 state->validator = std::move(validator);
66 state->window = surface.
window();
68 const glm::uvec2 dims { surface.
window()->get_create_info().width, row_px };
71 const auto tint = [params, state](std::string_view text) {
72 params->color = state->accepts(text) ? k_accepted_color : k_refused_color;
78 .
on_change([tint](
const std::string& text) { tint(text); })
79 .on_commit([state](
const std::string& text) {
80 if (state->accepts(text)) {
81 state->finish(text,
true);
84 .on_cancel([state] { state->finish(std::nullopt,
true); });
86 field.
place(std::move(target), surface, surface.
full(), params, std::move(initial));
89 return [state] { state->finish(std::nullopt,
false); };
100 uint32_t row_px = k_min_row_px;
102 row_px = std::max(row_px,
static_cast<uint32_t
>(atlas->pixel_size()) + k_row_padding_px);
108 .title = std::move(title) +
" (Enter accepts, Escape cancels)",
114 auto cancel = std::make_shared<std::function<void()>>();
118 .name = window->get_create_info().title,
119 .on_close = [cancel] {
129 row_px, std::move(initial), std::move(validator), std::move(callback));
137 ask(std::move(title), std::move(initial),
AskValidator {}, std::move(callback));
bool set_focus(uint32_t id)
Give keyboard focus to an element from code.
static TypeFaceFoundry & instance()
Configuration for creating a single window instance.
uint32_t element_id
Element id of the text field. Valid after place().
TextField & single_line(bool enabled=true)
Keep the text on one line, with Enter committing it.
MAYAFLUX_API TextField & place(FormaTarget in_buf, Surface &surface, Kinesis::AABB2D bounds, std::shared_ptr< Portal::Text::PressParams > in_params, std::string initial_text={})
Register the text field element and wire text editing onto it.
TextField & on_change(std::function< void(const std::string &)> fn)
Call fn with the new text after every edit, before it is drawn.
An editable text region: keyboard/text-input wiring over an Element the caller already has a buffer f...