MeshLib C++ Docs
Loading...
Searching...
No Matches
MRUITestEngine.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMesh/MRExpected.h"
4#include "MRViewer/exports.h"
5
6#include <cstdint>
7#include <filesystem>
8#include <limits>
9#include <map>
10#include <optional>
11#include <string_view>
12#include <string>
13#include <variant>
14#include <vector>
15
18
20{
21
24{
26 std::string_view disabledReason;
27
29 bool ignoreBlockingModal = false;
30};
31
32namespace detail
33{
34 template <typename T>
36 {
37 T value{};
38 T min{};
39 T max{};
40 };
41 template <>
42 struct BoundedValue<std::string>
43 {
44 std::string value;
45
46 std::optional<std::vector<std::string>> allowedValues;
47 };
48
49 template <typename T>
50 [[nodiscard]] MRVIEWER_API std::optional<T> createValueLow( std::string_view name, std::optional<BoundedValue<T>> value, bool consumeValueOverride = true, const EntryAttributes& attrs = {} );
51
52 extern template MRVIEWER_API std::optional<std::int64_t> createValueLow( std::string_view name, std::optional<BoundedValue<std::int64_t>> value, bool consumeValueOverride, const EntryAttributes& attrs );
53 extern template MRVIEWER_API std::optional<std::uint64_t> createValueLow( std::string_view name, std::optional<BoundedValue<std::uint64_t>> value, bool consumeValueOverride, const EntryAttributes& attrs );
54 extern template MRVIEWER_API std::optional<double> createValueLow( std::string_view name, std::optional<BoundedValue<double>> value, bool consumeValueOverride, const EntryAttributes& attrs );
55 extern template MRVIEWER_API std::optional<std::string> createValueLow( std::string_view name, std::optional<BoundedValue<std::string>> value, bool consumeValueOverride, const EntryAttributes& attrs );
56
57 template <typename T, typename = void> struct UnderlyingValueTypeHelper {};
58 template <typename T> struct UnderlyingValueTypeHelper<T, std::enable_if_t<std::is_floating_point_v<T>>> {using type = double;};
59 template <typename T> struct UnderlyingValueTypeHelper<T, std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>> {using type = std::int64_t;};
60 template <typename T> struct UnderlyingValueTypeHelper<T, std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>>> {using type = std::uint64_t;};
61 template <> struct UnderlyingValueTypeHelper<std::string> {using type = std::string;};
62
63 template <typename T>
65}
66
69[[nodiscard]] MRVIEWER_API bool createButton( std::string_view name, const EntryAttributes& attrs = {} );
70
71template <typename T>
72concept AllowedValueType = std::is_arithmetic_v<T> || std::is_same_v<T, std::string>;
73
82template <AllowedValueType T>
83requires std::is_arithmetic_v<T>
84[[nodiscard]] std::optional<T> createValue( std::string_view name, T value, T min, T max, bool consumeValueOverride = true, const EntryAttributes& attrs = {} )
85{
86 if ( !( min < max ) )
87 {
88 min = std::numeric_limits<T>::lowest();
89 max = std::numeric_limits<T>::max();
90 }
91
93 static_assert(sizeof(T) <= sizeof(U), "The used type is too large.");
94
95 auto ret = detail::createValueLow<U>( name, detail::BoundedValue<U>{ .value = U( value ), .min = U( min ), .max = U( max ) }, consumeValueOverride, attrs );
96 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
97}
98
99[[nodiscard]] MRVIEWER_API std::optional<std::string> createValue( std::string_view name, std::string value, bool consumeValueOverride = true, std::optional<std::vector<std::string>> allowedValues = std::nullopt, const EntryAttributes& attrs = {} );
100
105template <AllowedValueType T>
106[[nodiscard]] std::optional<T> createValueTentative( std::string_view name, bool consumeValueOverride = true, const EntryAttributes& attrs = {} )
107{
108 auto ret = detail::createValueLow<detail::UnderlyingValueType<T>>( name, std::nullopt, consumeValueOverride, attrs );
109 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
110}
111
113MRVIEWER_API void pushTree( std::string_view name );
114MRVIEWER_API void popTree();
115
119{
120public:
121 explicit TreeGuard( std::string_view name ) { pushTree( name ); }
123 TreeGuard( const TreeGuard& ) = delete;
124 TreeGuard& operator=( const TreeGuard& ) = delete;
125};
126
127struct Entry;
128
130{
132 mutable bool simulateClick = false;
133
136 std::string disabledReason;
137
138 static constexpr std::string_view kindName = "button";
139};
140
143{
144 template <typename T>
145 struct Value
146 {
148 T value = 0;
149
151 T min = 0;
152 T max = 0;
153
155 mutable std::optional<T> simulatedValue;
156
157 Value() {}
158 };
159 template <std::same_as<std::string> T>
160 struct Value<T>
161 {
163 std::string value;
164
165 std::optional<std::vector<std::string>> allowedValues;
166
168 mutable std::optional<std::string> simulatedValue;
169
170 Value() {}
171 };
172 using ValueVar = std::variant<Value<std::int64_t>, Value<std::uint64_t>, Value<double>, Value<std::string>>;
174
177 std::string disabledReason;
178
179 static constexpr std::string_view kindName = "value";
180};
181
183{
185 std::map<std::string, Entry, std::less<>> elems;
186
187 static constexpr std::string_view kindName = "group";
188};
189
190struct Entry
191{
192 std::variant<ButtonEntry, ValueEntry, GroupEntry> value;
193
196 bool visitedOnThisFrame = false;
197
199 [[nodiscard]] MRVIEWER_API std::string_view getKindName() const;
200
204 template <typename T>
205 [[nodiscard]] Expected<T *> getAs( std::string_view selfName = {} )
206 {
207 Expected<T *> ret = std::get_if<T>( &value );
208 if ( !*ret )
209 ret = unexpected_( selfName, T::kindName );
210 return ret;
211 }
212 template <typename T>
213 [[nodiscard]] Expected<const T *> getAs( std::string_view selfName = {} ) const
214 {
215 return const_cast<Entry *>( this )->template getAs<T>( selfName );
216 }
217
218private:
219 [[nodiscard]] MRVIEWER_API Unexpected<std::string> unexpected_( std::string_view selfName, std::string_view tKindName );
220};
221
223[[nodiscard]] MRVIEWER_API const GroupEntry& getRootEntry();
224
231[[nodiscard]] MRVIEWER_API bool wasFrameTriggered();
232
238MRVIEWER_API void markFrameTriggered();
239
243MRVIEWER_API void stageFileDialogPaths( std::vector<std::filesystem::path> paths );
244
247[[nodiscard]] MRVIEWER_API std::vector<std::filesystem::path> consumeStagedFileDialogPaths();
248
252MRVIEWER_API void appendStatusMessage( std::string msg );
253
255[[nodiscard]] MRVIEWER_API std::vector<std::string> consumeStatusMessages();
256
257}
TreeGuard & operator=(const TreeGuard &)=delete
TreeGuard(const TreeGuard &)=delete
TreeGuard(std::string_view name)
Definition MRUITestEngine.h:121
~TreeGuard()
Definition MRUITestEngine.h:122
Definition MRUITestEngine.h:72
tl::expected< T, E > Expected
Definition MRExpected.h:31
tl::unexpected< E > Unexpected
Definition MRExpected.h:34
Definition MRUITestEngine.h:33
std::optional< T > createValueLow(std::string_view name, std::optional< BoundedValue< T > > value, bool consumeValueOverride=true, const EntryAttributes &attrs={})
typename UnderlyingValueTypeHelper< T >::type UnderlyingValueType
Definition MRUITestEngine.h:64
Definition MRUITestEngine.h:20
const GroupEntry & getRootEntry()
Returns the current entry tree.
void stageFileDialogPaths(std::vector< std::filesystem::path > paths)
std::optional< T > createValueTentative(std::string_view name, bool consumeValueOverride=true, const EntryAttributes &attrs={})
Definition MRUITestEngine.h:106
std::optional< T > createValue(std::string_view name, T value, T min, T max, bool consumeValueOverride=true, const EntryAttributes &attrs={})
Definition MRUITestEngine.h:84
std::vector< std::filesystem::path > consumeStagedFileDialogPaths()
bool createButton(std::string_view name, const EntryAttributes &attrs={})
void pushTree(std::string_view name)
Use those to group buttons into named groups.
std::vector< std::string > consumeStatusMessages()
Drain and return all status messages accumulated since the last drain.
void appendStatusMessage(std::string msg)
Definition MRUITestEngine.h:130
bool simulateClick
Set this to true to simulate a button click.
Definition MRUITestEngine.h:132
std::string disabledReason
Definition MRUITestEngine.h:136
static constexpr std::string_view kindName
Definition MRUITestEngine.h:138
Optional attributes reported to the test engine each frame alongside a widget registration.
Definition MRUITestEngine.h:24
bool ignoreBlockingModal
If true, the entry stays enabled while a modal popup is open, e.g. for a value registered outside of ...
Definition MRUITestEngine.h:29
std::string_view disabledReason
Non-empty marks the widget as disabled with this reason. Only read during the call.
Definition MRUITestEngine.h:26
Definition MRUITestEngine.h:191
std::variant< ButtonEntry, ValueEntry, GroupEntry > value
Definition MRUITestEngine.h:192
std::string_view getKindName() const
Returns a string describing the type currently stored in value, which is T::kindName.
Expected< const T * > getAs(std::string_view selfName={}) const
Definition MRUITestEngine.h:213
bool visitedOnThisFrame
Definition MRUITestEngine.h:196
Expected< T * > getAs(std::string_view selfName={})
Definition MRUITestEngine.h:205
Definition MRUITestEngine.h:183
static constexpr std::string_view kindName
Definition MRUITestEngine.h:187
std::map< std::string, Entry, std::less<> > elems
Using std::map over std::unordered_map to be able to search by std::string_view keys directly.
Definition MRUITestEngine.h:185
Value()
Definition MRUITestEngine.h:170
std::optional< std::string > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:168
std::string value
The current value.
Definition MRUITestEngine.h:163
std::optional< std::vector< std::string > > allowedValues
Definition MRUITestEngine.h:165
Definition MRUITestEngine.h:146
Value()
Definition MRUITestEngine.h:157
T max
Definition MRUITestEngine.h:152
T value
The current value.
Definition MRUITestEngine.h:148
T min
Min/max bounds, INCLUSIVE. If none, those are set to the min/max values representable in this type.
Definition MRUITestEngine.h:151
std::optional< T > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:155
For sliders, drags, etc.
Definition MRUITestEngine.h:143
std::variant< Value< std::int64_t >, Value< std::uint64_t >, Value< double >, Value< std::string > > ValueVar
Definition MRUITestEngine.h:172
ValueVar value
Definition MRUITestEngine.h:173
std::string disabledReason
Definition MRUITestEngine.h:177
static constexpr std::string_view kindName
Definition MRUITestEngine.h:179
Definition MRUITestEngine.h:36
T min
Definition MRUITestEngine.h:38
T max
Definition MRUITestEngine.h:39
T value
Definition MRUITestEngine.h:37