TermOx
size_policy.hpp
1 #ifndef TERMOX_WIDGET_SIZE_POLICY_HPP
2 #define TERMOX_WIDGET_SIZE_POLICY_HPP
3 #include <limits>
4 #include <utility>
5 
6 #include <signals_light/signal.hpp>
7 
8 namespace ox {
9 
11 class Size_policy {
12  private:
13  struct Data {
14  int hint = 0;
15  int min = 0;
16  int max = maximum_max;
17  double stretch = 1.;
18  bool can_ignore_min = true;
19  } data_;
20 
21  public:
23  sl::Signal<void()> policy_updated;
24 
26  static auto constexpr maximum_max =
27  std::numeric_limits<decltype(data_.max)>::max();
28 
29  public:
30  explicit Size_policy(int hint = 0,
31  int min = 0,
32  int max = maximum_max,
33  double stretch = 1.,
34  bool can_ignore_min = true);
35 
37  Size_policy(Size_policy const& x);
38 
41 
43  auto operator=(Size_policy const& x) -> Size_policy&;
44 
46  auto operator=(Size_policy&& x) -> Size_policy&;
47 
48  friend auto operator==(Size_policy const& a, Size_policy const& b) -> bool;
49  friend auto operator!=(Size_policy const& a, Size_policy const& b) -> bool;
50 
51  public:
53  void hint(int value);
54 
56  [[nodiscard]] auto hint() const -> int;
57 
59  void min(int value);
60 
62  [[nodiscard]] auto min() const -> int;
63 
65  void max(int value);
66 
68  [[nodiscard]] auto max() const -> int;
69 
71 
72  void stretch(double value);
73 
75  [[nodiscard]] auto stretch() const -> double;
76 
78  void can_ignore_min(bool enable);
79 
81  [[nodiscard]] auto can_ignore_min() const -> bool;
82 
83  public:
84  /* _Helper Methods_ */
86  [[nodiscard]] static auto fixed(int hint) -> Size_policy;
87 
89  [[nodiscard]] static auto minimum(int hint) -> Size_policy;
90 
92  [[nodiscard]] static auto maximum(int hint) -> Size_policy;
93 
95  [[nodiscard]] static auto preferred(int hint) -> Size_policy;
96 
98  [[nodiscard]] static auto expanding(int hint) -> Size_policy;
99 
101  [[nodiscard]] static auto minimum_expanding(int hint) -> Size_policy;
102 
104  [[nodiscard]] static auto ignored() -> Size_policy;
105 };
106 
108 template <int Hint, typename Widget_t>
109 struct Fixed_height : Widget_t {
110  template <typename... Args>
111  explicit Fixed_height(Args&&... args)
112  : Widget_t{std::forward<Args>(args)...}
113  {
114  this->height_policy = Size_policy::fixed(Hint);
115  }
116 
117  explicit Fixed_height(typename Widget_t::Parameters parameters)
118  : Widget_t{std::move(parameters)}
119  {
120  this->height_policy = Size_policy::fixed(Hint);
121  }
122 };
123 
125 template <int Hint, typename Widget_t>
126 struct Fixed_width : Widget_t {
127  template <typename... Args>
128  explicit Fixed_width(Args&&... args) : Widget_t{std::forward<Args>(args)...}
129  {
130  this->width_policy = Size_policy::fixed(Hint);
131  }
132 
133  explicit Fixed_width(typename Widget_t::Parameters parameters)
134  : Widget_t{std::move(parameters)}
135  {
136  this->width_policy = Size_policy::fixed(Hint);
137  }
138 };
139 
141 template <int Hint, typename Widget_t>
142 struct Minimum_height : Widget_t {
143  template <typename... Args>
144  explicit Minimum_height(Args&&... args)
145  : Widget_t{std::forward<Args>(args)...}
146  {
147  this->height_policy = Size_policy::minimum(Hint);
148  }
149 
150  explicit Minimum_height(typename Widget_t::Parameters parameters)
151  : Widget_t{std::move(parameters)}
152  {
153  this->height_policy = Size_policy::minimum(Hint);
154  }
155 };
156 
158 template <int Hint, typename Widget_t>
159 struct Minimum_width : Widget_t {
160  template <typename... Args>
161  explicit Minimum_width(Args&&... args)
162  : Widget_t{std::forward<Args>(args)...}
163  {
164  this->width_policy = Size_policy::minimum(Hint);
165  }
166 
167  explicit Minimum_width(typename Widget_t::Parameters parameters)
168  : Widget_t{std::move(parameters)}
169  {
170  this->width_policy = Size_policy::minimum(Hint);
171  }
172 };
173 
175 template <int Hint, typename Widget_t>
176 struct Maximum_height : Widget_t {
177  template <typename... Args>
178  explicit Maximum_height(Args&&... args)
179  : Widget_t{std::forward<Args>(args)...}
180  {
181  this->height_policy = Size_policy::maximum(Hint);
182  }
183 
184  explicit Maximum_height(typename Widget_t::Parameters parameters)
185  : Widget_t{std::move(parameters)}
186  {
187  this->height_policy = Size_policy::maximum(Hint);
188  }
189 };
190 
192 template <int Hint, typename Widget_t>
193 struct Maximum_width : Widget_t {
194  template <typename... Args>
195  explicit Maximum_width(Args&&... args)
196  : Widget_t{std::forward<Args>(args)...}
197  {
198  this->width_policy = Size_policy::maximum(Hint);
199  }
200 
201  explicit Maximum_width(typename Widget_t::Parameters parameters)
202  : Widget_t{std::move(parameters)}
203  {
204  this->width_policy = Size_policy::maximum(Hint);
205  }
206 };
207 
209 template <int Hint, typename Widget_t>
210 struct Preferred_height : Widget_t {
211  template <typename... Args>
212  explicit Preferred_height(Args&&... args)
213  : Widget_t{std::forward<Args>(args)...}
214  {
215  this->height_policy = Size_policy::preferred(Hint);
216  }
217 
218  explicit Preferred_height(typename Widget_t::Parameters parameters)
219  : Widget_t{std::move(parameters)}
220  {
221  this->height_policy = Size_policy::preferred(Hint);
222  }
223 };
224 
226 template <int Hint, typename Widget_t>
227 struct Preferred_width : Widget_t {
228  template <typename... Args>
229  explicit Preferred_width(Args&&... args)
230  : Widget_t{std::forward<Args>(args)...}
231  {
232  this->width_policy = Size_policy::preferred(Hint);
233  }
234 
235  explicit Preferred_width(typename Widget_t::Parameters parameters)
236  : Widget_t{std::move(parameters)}
237  {
238  this->width_policy = Size_policy::preferred(Hint);
239  }
240 };
241 
243 template <int Hint, typename Widget_t>
244 struct Expanding_height : Widget_t {
245  template <typename... Args>
246  explicit Expanding_height(Args&&... args)
247  : Widget_t{std::forward<Args>(args)...}
248  {
249  this->height_policy = Size_policy::expanding(Hint);
250  }
251 
252  explicit Expanding_height(typename Widget_t::Parameters parameters)
253  : Widget_t{std::move(parameters)}
254  {
255  this->height_policy = Size_policy::expanding(Hint);
256  }
257 };
258 
260 template <int Hint, typename Widget_t>
261 struct Expanding_width : Widget_t {
262  template <typename... Args>
263  explicit Expanding_width(Args&&... args)
264  : Widget_t{std::forward<Args>(args)...}
265  {
266  this->width_policy = Size_policy::expanding(Hint);
267  }
268 
269  explicit Expanding_width(typename Widget_t::Parameters parameters)
270  : Widget_t{std::move(parameters)}
271  {
272  this->width_policy = Size_policy::expanding(Hint);
273  }
274 };
275 
277 template <int Hint, typename Widget_t>
278 struct Minimum_expanding_height : Widget_t {
279  template <typename... Args>
280  explicit Minimum_expanding_height(Args&&... args)
281  : Widget_t{std::forward<Args>(args)...}
282  {
283  this->height_policy = Size_policy::minimum_expanding(Hint);
284  }
285 
286  explicit Minimum_expanding_height(typename Widget_t::Parameters parameters)
287  : Widget_t{std::move(parameters)}
288  {
289  this->height_policy = Size_policy::minimum_expanding(Hint);
290  }
291 };
292 
294 template <int Hint, typename Widget_t>
295 struct Minimum_expanding_width : Widget_t {
296  template <typename... Args>
297  explicit Minimum_expanding_width(Args&&... args)
298  : Widget_t{std::forward<Args>(args)...}
299  {
300  this->width_policy = Size_policy::minimum_expanding(Hint);
301  }
302 
303  explicit Minimum_expanding_width(typename Widget_t::Parameters parameters)
304  : Widget_t{std::move(parameters)}
305  {
306  this->width_policy = Size_policy::minimum_expanding(Hint);
307  }
308 };
309 
311 template <typename Widget_t>
312 struct Ignored_height : Widget_t {
313  template <typename... Args>
314  explicit Ignored_height(Args&&... args)
315  : Widget_t{std::forward<Args>(args)...}
316  {
317  this->height_policy = Size_policy::ignored();
318  }
319 
320  explicit Ignored_height(typename Widget_t::Parameters parameters)
321  : Widget_t{std::move(parameters)}
322  {
323  this->height_policy = Size_policy::ignored();
324  }
325 };
326 
328 template <typename Widget_t>
329 struct Ignored_width : Widget_t {
330  template <typename... Args>
331  explicit Ignored_width(Args&&... args)
332  : Widget_t{std::forward<Args>(args)...}
333  {
334  this->width_policy = Size_policy::ignored();
335  }
336 
337  explicit Ignored_width(typename Widget_t::Parameters parameters)
338  : Widget_t{std::move(parameters)}
339  {
340  this->width_policy = Size_policy::ignored();
341  }
342 };
343 
345 [[nodiscard]] auto is_valid(Size_policy const& p) -> bool;
346 
347 } // namespace ox
348 #endif // TERMOX_WIDGET_SIZE_POLICY_HPP
Defines how a Layout should resize a Widget in one length Dimension.
Definition: size_policy.hpp:11
static auto ignored() -> Size_policy
Ignored: Stretch is the only consideration.
Definition: size_policy.cpp:91
auto can_ignore_min() const -> bool
Return if min can be ignored for the last displayed widget in a layout.
Definition: size_policy.cpp:50
sl::Signal< void()> policy_updated
Emitted on any changes to the Size_policy.
Definition: size_policy.hpp:23
static auto fixed(int hint) -> Size_policy
Fixed: hint is the only acceptable size.
Definition: size_policy.cpp:55
auto min() const -> int
Return the minimum length currently set.
Definition: size_policy.cpp:30
static auto minimum(int hint) -> Size_policy
Minimum: hint is the minimum acceptable size, may be larger.
Definition: size_policy.cpp:61
static auto minimum_expanding(int hint) -> Size_policy
Minimum Expanding: hint is minimum, it will expand into unused space.
Definition: size_policy.cpp:85
static auto maximum(int hint) -> Size_policy
Maximum: hint is the maximum acceptable size, may be smaller.
Definition: size_policy.cpp:67
auto hint() const -> int
Return the size hint currently being used.
Definition: size_policy.cpp:22
static auto preferred(int hint) -> Size_policy
Preferred: hint is preferred, though it can be any size.
Definition: size_policy.cpp:73
auto max() const -> int
Return the maximum length currently set.
Definition: size_policy.cpp:38
auto operator=(Size_policy const &x) -> Size_policy &
Specifically does not copy the Signal, so Widget is still notified.
Definition: size_policy.cpp:101
static auto expanding(int hint) -> Size_policy
Expanding: hint is preferred, but it will expand to use extra space.
Definition: size_policy.cpp:79
auto stretch() const -> double
Return the stretch value currently being used.
Definition: size_policy.cpp:46
static constexpr auto maximum_max
Largest possible value for max().
Definition: size_policy.hpp:26
Wrapper type to set the height Size_policy at construction.
Definition: size_policy.hpp:244
Wrapper type to set the width Size_policy at construction.
Definition: size_policy.hpp:261
Wrapper type to set the height Size_policy at construction.
Definition: size_policy.hpp:109
Wrapper type to set the width Size_policy at construction.
Definition: size_policy.hpp:126
Wrapper type to set the height Size_policy at construction.
Definition: size_policy.hpp:312
Wrapper type to set the width Size_policy at construction.
Definition: size_policy.hpp:329
Wrapper type to set the height Size_policy at construction.
Definition: size_policy.hpp:176
Wrapper type to set the width Size_policy at construction.
Definition: size_policy.hpp:193
Wrapper type to set the height Size_policy at construction.
Definition: size_policy.hpp:278
Wrapper type to set the width Size_policy at construction.
Definition: size_policy.hpp:295
Wrapper type to set the height Size_policy at construction.
Definition: size_policy.hpp:142
Wrapper type to set the width Size_policy at construction.
Definition: size_policy.hpp:159
Wrapper type to set the height Size_policy at construction.
Definition: size_policy.hpp:210
Wrapper type to set the width Size_policy at construction.
Definition: size_policy.hpp:227