TermOx
float.hpp
1 #ifndef TERMOX_WIDGET_LAYOUT_FLOAT_HPP
2 #define TERMOX_WIDGET_LAYOUT_FLOAT_HPP
3 #include <memory>
4 #include <utility>
5 
6 #include <termox/widget/layouts/horizontal.hpp>
7 #include <termox/widget/layouts/vertical.hpp>
8 #include <termox/widget/widget.hpp>
9 
10 namespace ox {
11 
12 // TODO limit child add/remove functions, invariant should be only every having
13 // the widgets you have at construction.
14 
16 
17 template <template <typename> typename Layout_t, typename Widget_t>
18 class Float : public Layout_t<Widget> {
19  private:
20  using Base_t = Layout_t<Widget>;
21 
22  public:
23  Widget& buffer_1;
24  Widget_t& widget;
25  Widget& buffer_2;
26 
27  public:
28  template <typename... Args>
29  Float(Args&&... args)
30  : buffer_1{this->template make_child()},
31  widget{
32  this->template make_child<Widget_t>(std::forward<Args>(args)...)},
33  buffer_2{this->template make_child()}
34  {
35  this->update_policy();
36  }
37 
38  protected:
39  auto child_polished_event(Widget& child) -> bool override
40  {
41  if (&child == &widget)
42  this->update_policy();
43  return Base_t::child_polished_event(child);
44  }
45 
46  private:
47  void update_policy()
48  {
49  if constexpr (layout::is_horizontal_v<Base_t>)
50  this->height_policy = widget.height_policy;
51  else
52  this->width_policy = widget.width_policy;
53  }
54 };
55 
56 template <typename Widget_t>
58 
59 template <typename Widget_t, typename... Args>
60 [[nodiscard]] inline auto vfloat(Args&&... args)
61  -> std::unique_ptr<VFloat<Widget_t>>
62 {
63  return std::make_unique<VFloat<Widget_t>>(std::forward<Args>(args)...);
64 }
65 
66 template <typename Widget_t>
67 using HFloat = Float<layout::Horizontal, Widget_t>;
68 
69 template <typename Widget_t, typename... Args>
70 [[nodiscard]] inline auto hfloat(Args&&... args)
71  -> std::unique_ptr<HFloat<Widget_t>>
72 {
73  return std::make_unique<HFloat<Widget_t>>(std::forward<Args>(args)...);
74 }
75 
76 template <typename Widget_t>
77 using Float_2d = VFloat<HFloat<Widget_t>>;
78 
79 template <typename Widget_t, typename... Args>
80 [[nodiscard]] inline auto float_2d(Args&&... args)
81  -> std::unique_ptr<Float_2d<Widget_t>>
82 {
83  return std::make_unique<Float_2d<Widget_t>>(std::forward<Args>(args)...);
84 }
85 
86 } // namespace ox
87 #endif //
Holds a given Widget_t object between two empty 'buffer' Widgets.
Definition: float.hpp:18
Definition: widget.hpp:31