ESPHome  2023.5.5
menu_item.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
5 
6 #ifdef USE_NUMBER
8 #endif
9 #ifdef USE_SELECT
11 #endif
12 #ifdef USE_SWITCH
14 #endif
15 
16 #include <vector>
17 
18 namespace esphome {
19 namespace display_menu_base {
20 
30 };
31 
32 class MenuItem;
33 class MenuItemMenu;
34 using value_getter_t = std::function<std::string(const MenuItem *)>;
35 
36 class MenuItem {
37  public:
38  explicit MenuItem(MenuItemType t) : item_type_(t) {}
39  void set_parent(MenuItemMenu *parent) { this->parent_ = parent; }
40  MenuItemMenu *get_parent() { return this->parent_; }
41  MenuItemType get_type() const { return this->item_type_; }
42  template<typename V> void set_text(V val) { this->text_ = val; }
43  void add_on_enter_callback(std::function<void()> &&cb) { this->on_enter_callbacks_.add(std::move(cb)); }
44  void add_on_leave_callback(std::function<void()> &&cb) { this->on_leave_callbacks_.add(std::move(cb)); }
45  void add_on_value_callback(std::function<void()> &&cb) { this->on_value_callbacks_.add(std::move(cb)); }
46 
47  std::string get_text() const { return const_cast<MenuItem *>(this)->text_.value(this); }
48  virtual bool get_immediate_edit() const { return false; }
49  virtual bool has_value() const { return false; }
50  virtual std::string get_value_text() const { return ""; }
51 
52  virtual bool select_next() { return false; }
53  virtual bool select_prev() { return false; }
54 
55  void on_enter();
56  void on_leave();
57 
58  protected:
59  void on_value_();
60 
62  MenuItemMenu *parent_{nullptr};
64 
68 };
69 
70 class MenuItemMenu : public MenuItem {
71  public:
73  void add_item(MenuItem *item) {
74  item->set_parent(this);
75  this->items_.push_back(item);
76  }
77  size_t items_size() const { return this->items_.size(); }
78  MenuItem *get_item(size_t i) { return this->items_[i]; }
79 
80  protected:
81  std::vector<MenuItem *> items_;
82 };
83 
84 class MenuItemEditable : public MenuItem {
85  public:
87  void set_immediate_edit(bool val) { this->immediate_edit_ = val; }
88  bool get_immediate_edit() const override { return this->immediate_edit_; }
89  void set_value_lambda(value_getter_t &&getter) { this->value_getter_ = getter; }
90 
91  protected:
92  bool immediate_edit_{false};
93  optional<value_getter_t> value_getter_{};
94 };
95 
96 #ifdef USE_SELECT
98  public:
100  void set_select_variable(select::Select *var) { this->select_var_ = var; }
101 
102  bool has_value() const override { return true; }
103  std::string get_value_text() const override;
104 
105  bool select_next() override;
106  bool select_prev() override;
107 
108  protected:
109  select::Select *select_var_{nullptr};
110 };
111 #endif
112 
113 #ifdef USE_NUMBER
115  public:
117  void set_number_variable(number::Number *var) { this->number_var_ = var; }
118  void set_format(const std::string &fmt) { this->format_ = fmt; }
119 
120  bool has_value() const override { return true; }
121  std::string get_value_text() const override;
122 
123  bool select_next() override;
124  bool select_prev() override;
125 
126  protected:
127  float get_number_value_() const;
128 
129  number::Number *number_var_{nullptr};
130  std::string format_;
131 };
132 #endif
133 
134 #ifdef USE_SWITCH
136  public:
138  void set_switch_variable(switch_::Switch *var) { this->switch_var_ = var; }
139  void set_on_text(const std::string &t) { this->switch_on_text_ = t; }
140  void set_off_text(const std::string &t) { this->switch_off_text_ = t; }
141 
142  bool has_value() const override { return true; }
143  std::string get_value_text() const override;
144 
145  bool select_next() override;
146  bool select_prev() override;
147 
148  protected:
149  bool get_switch_state_() const;
150  bool toggle_switch_();
151 
152  switch_::Switch *switch_var_{nullptr};
153  std::string switch_on_text_;
154  std::string switch_off_text_;
155 };
156 #endif
157 
158 class MenuItemCommand : public MenuItem {
159  public:
161 
162  bool select_next() override;
163  bool select_prev() override;
164 };
165 
167  public:
169  void add_on_next_callback(std::function<void()> &&cb) { this->on_next_callbacks_.add(std::move(cb)); }
170  void add_on_prev_callback(std::function<void()> &&cb) { this->on_prev_callbacks_.add(std::move(cb)); }
171 
172  bool has_value() const override { return this->value_getter_.has_value(); }
173  std::string get_value_text() const override;
174 
175  bool select_next() override;
176  bool select_prev() override;
177 
178  protected:
179  void on_next_();
180  void on_prev_();
181 
182  CallbackManager<void()> on_next_callbacks_{};
183  CallbackManager<void()> on_prev_callbacks_{};
184 };
185 
186 } // namespace display_menu_base
187 } // namespace esphome
Base class for all switches.
Definition: switch.h:32
std::vector< MenuItem * > items_
Definition: menu_item.h:81
void set_off_text(const std::string &t)
Definition: menu_item.h:140
CallbackManager< void()> on_leave_callbacks_
Definition: menu_item.h:66
virtual bool has_value() const
Definition: menu_item.h:49
MenuItemType get_type() const
Definition: menu_item.h:41
mopeka_std_values val[4]
void add_on_prev_callback(std::function< void()> &&cb)
Definition: menu_item.h:170
void set_parent(MenuItemMenu *parent)
Definition: menu_item.h:39
void set_on_text(const std::string &t)
Definition: menu_item.h:139
std::function< std::string(const MenuItem *)> value_getter_t
Definition: menu_item.h:34
virtual bool get_immediate_edit() const
Definition: menu_item.h:48
virtual std::string get_value_text() const
Definition: menu_item.h:50
Base-class for all numbers.
Definition: number.h:39
void set_switch_variable(switch_::Switch *var)
Definition: menu_item.h:138
void set_select_variable(select::Select *var)
Definition: menu_item.h:100
void add_on_next_callback(std::function< void()> &&cb)
Definition: menu_item.h:169
void set_number_variable(number::Number *var)
Definition: menu_item.h:117
CallbackManager< void()> on_enter_callbacks_
Definition: menu_item.h:65
void add_on_enter_callback(std::function< void()> &&cb)
Definition: menu_item.h:43
bool get_immediate_edit() const override
Definition: menu_item.h:88
TemplatableValue< std::string, const MenuItem * > text_
Definition: menu_item.h:63
CallbackManager< void()> on_value_callbacks_
Definition: menu_item.h:67
void add_on_value_callback(std::function< void()> &&cb)
Definition: menu_item.h:45
std::string get_text() const
Definition: menu_item.h:47
Base-class for all selects.
Definition: select.h:24
Definition: a4988.cpp:4
num_t cb(num_t x)
Definition: sun.cpp:31
void set_format(const std::string &fmt)
Definition: menu_item.h:118
void add_on_leave_callback(std::function< void()> &&cb)
Definition: menu_item.h:44
void set_value_lambda(value_getter_t &&getter)
Definition: menu_item.h:89