HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_field_delegate.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 <string>
8#include <string_view>
9#include <optional>
10#include <type_traits>
11#include <concepts>
12#include <memory>
13#include "../l10n.hpp"
14#include "../concepts.hpp"
15
16namespace tt {
17template<typename T>
18class text_field_widget;
19
20template<typename T>
22public:
23 using value_type = T;
25
31 size_t text_width(sender_type &sender) const noexcept
32 {
33 return 20;
34 }
35
42 {
43 return {};
44 }
45
52 virtual std::string to_string(sender_type &sender, value_type const &value) noexcept
53 {
54 // XXX Need to pass the current local to format.
55 return fmt::format("{}", value);
56 }
57
66 virtual std::optional<value_type> from_string(sender_type &sender, std::string_view text, l10n &error) noexcept
67 {
68 try {
69 error = {};
70 return tt::from_string<value_type>(text);
71 } catch (parse_error) {
72 error = l10n("Invalid character entered.");
73 return {};
74 }
75 }
76};
77
78template<typename T>
79inline std::shared_ptr<text_field_delegate<T>> text_field_delegate_default() noexcept
80{
81 static std::shared_ptr<text_field_delegate<T>> delegate = std::make_shared<text_field_delegate<T>>();
82 return delegate;
83}
84
85} // namespace tt
Exception thrown during parsing on an error.
Definition exception.hpp:26
A localizable string.
Definition l10n.hpp:12
A single line text field.
Definition text_field_widget.hpp:54
Definition text_field_delegate.hpp:21
virtual std::optional< value_type > from_string(sender_type &sender, std::string_view text, l10n &error) noexcept
Convert a string to a value.
Definition text_field_delegate.hpp:66
size_t text_width(sender_type &sender) const noexcept
The width of the text field in characters.
Definition text_field_delegate.hpp:31
std::vector< std::string > suggestions(sender_type &sender) const noexcept
The list of suggestions to show in the popup box.
Definition text_field_delegate.hpp:41
virtual std::string to_string(sender_type &sender, value_type const &value) noexcept
Convert a value to a string.
Definition text_field_delegate.hpp:52