TermOx
selectable.hpp
1 #ifndef TERMOX_WIDGET_WIDGETS_SELECTABLE_HPP
2 #define TERMOX_WIDGET_WIDGETS_SELECTABLE_HPP
3 #include <termox/painter/trait.hpp>
4 #include <termox/widget/pipe.hpp>
5 
6 namespace ox {
7 
9 template <typename Widget_t,
10  typename Select_method = void,
11  typename Unselect_method = void>
12 class Selectable : public Widget_t {
13  public:
15  Selectable(Select_method s, Unselect_method u)
16  : Widget_t{}, select_{std::move(s)}, unselect_{std::move(u)}
17  {}
18 
19  Selectable(Select_method s,
20  Unselect_method u,
21  typename Widget_t::Parameters p)
22  : Widget_t{std::move(p)}, select_{std::move(s)}, unselect_{std::move(u)}
23  {}
24 
25  public:
27  void select() { select_(*this); }
28 
30  void unselect() { unselect_(*this); }
31 
32  private:
33  Select_method select_;
34  Unselect_method unselect_;
35 };
36 
38 template <typename Widget_t, typename Select_method, typename Unselect_method>
39 [[nodiscard]] auto selectable(Select_method s, Unselect_method u)
40  -> std::unique_ptr<Selectable<Widget_t, Select_method, Unselect_method>>
41 {
42  return std::make_unique<
43  Selectable<Widget_t, Select_method, Unselect_method>>(std::move(s),
44  std::move(u));
45 }
46 
48 template <typename Widget_t, typename Select_method, typename Unselect_method>
49 [[nodiscard]] auto selectable(Select_method s,
50  Unselect_method u,
51  typename Widget_t::Parameters p)
52  -> std::unique_ptr<Selectable<Widget_t, Select_method, Unselect_method>>
53 {
54  return std::make_unique<
55  Selectable<Widget_t, Select_method, Unselect_method>>(
56  std::move(s), std::move(u), std::move(p));
57 }
58 
60 template <typename Widget_t>
61 class Selectable<Widget_t, void, void> : public Widget_t {
62  public:
63  using Parameters = typename Widget_t::Parameters;
64 
65  public:
66  Selectable() = default;
67 
68  Selectable(Parameters p) : Widget_t{std::move(p)} {}
69 
70  public:
72  void select()
73  {
74  *this | Trait::Inverse;
75  *this | pipe::descendants() | Trait::Inverse;
76  }
77 
79  void unselect()
80  {
81  *this | pipe::discard(Trait::Inverse);
82  *this | pipe::descendants() | pipe::discard(Trait::Inverse);
83  }
84 };
85 
87 template <typename Widget_t>
88 [[nodiscard]] auto selectable() -> std::unique_ptr<Selectable<Widget_t>>
89 {
90  return std::make_unique<Selectable<Widget_t>>();
91 }
92 
94 template <typename Widget_t>
95 [[nodiscard]] auto selectable(typename Selectable<Widget_t>::Parameters p)
96  -> std::unique_ptr<Selectable<Widget_t>>
97 {
98  return std::make_unique<Selectable<Widget_t>>(std::move(p));
99 }
100 
101 } // namespace ox
102 #endif // TERMOX_WIDGET_WIDGETS_SELECTABLE_HPP
void select()
Change visual to mark as selected.
Definition: selectable.hpp:72
void unselect()
Change visual to mark as unselected.
Definition: selectable.hpp:79
Wraps Widget_t to provide select() and unselect() methods to modify display.
Definition: selectable.hpp:12
void select()
Change visual to mark as selected.
Definition: selectable.hpp:27
void unselect()
Change visual to mark as unselected.
Definition: selectable.hpp:30
Selectable(Select_method s, Unselect_method u)
Provide two methods to modify Widget_t. Signature: void(Widget_t&);.
Definition: selectable.hpp:15