ESPHome  2024.10.2
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 #include "esphome/core/log.h"
18 
19 namespace esphome {
20 namespace display_menu_base {
21 
31 };
32 
35 
36 class MenuItem;
37 class MenuItemMenu;
38 using value_getter_t = std::function<std::string(const MenuItem *)>;
39 
40 class MenuItem {
41  public:
42  explicit MenuItem(MenuItemType t) : item_type_(t) {}
43  void set_parent(MenuItemMenu *parent) { this->parent_ = parent; }
44  MenuItemMenu *get_parent() { return this->parent_; }
45  MenuItemType get_type() const { return this->item_type_; }
46  template<typename V> void set_text(V val) { this->text_ = val; }
47  void add_on_enter_callback(std::function<void()> &&cb) { this->on_enter_callbacks_.add(std::move(cb)); }
48  void add_on_leave_callback(std::function<void()> &&cb) { this->on_leave_callbacks_.add(std::move(cb)); }
49  void add_on_value_callback(std::function<void()> &&cb) { this->on_value_callbacks_.add(std::move(cb)); }
50 
51  std::string get_text() const { return const_cast<MenuItem *>(this)->text_.value(this); }
52  virtual bool get_immediate_edit() const { return false; }
53  virtual bool has_value() const { return false; }
54  virtual std::string get_value_text() const { return ""; }
55 
56  virtual bool select_next() { return false; }
57  virtual bool select_prev() { return false; }
58 
59  void on_enter();
60  void on_leave();
61 
62  protected:
63  void on_value_();
64 
66  MenuItemMenu *parent_{nullptr};
68 
72 };
73 
74 class MenuItemMenu : public MenuItem {
75  public:
77  void add_item(MenuItem *item) {
78  item->set_parent(this);
79  this->items_.push_back(item);
80  }
81  size_t items_size() const { return this->items_.size(); }
82  MenuItem *get_item(size_t i) { return this->items_[i]; }
83 
84  protected:
85  std::vector<MenuItem *> items_;
86 };
87 
88 class MenuItemEditable : public MenuItem {
89  public:
91  void set_immediate_edit(bool val) { this->immediate_edit_ = val; }
92  bool get_immediate_edit() const override { return this->immediate_edit_; }
93  void set_value_lambda(value_getter_t &&getter) { this->value_getter_ = getter; }
94 
95  protected:
96  bool immediate_edit_{false};
97  optional<value_getter_t> value_getter_{};
98 };
99 
100 #ifdef USE_SELECT
102  public:
104  void set_select_variable(select::Select *var) { this->select_var_ = var; }
105 
106  bool has_value() const override { return true; }
107  std::string get_value_text() const override;
108 
109  bool select_next() override;
110  bool select_prev() override;
111 
112  protected:
113  select::Select *select_var_{nullptr};
114 };
115 #endif
116 
117 #ifdef USE_NUMBER
119  public:
121  void set_number_variable(number::Number *var) { this->number_var_ = var; }
122  void set_format(const std::string &fmt) { this->format_ = fmt; }
123 
124  bool has_value() const override { return true; }
125  std::string get_value_text() const override;
126 
127  bool select_next() override;
128  bool select_prev() override;
129 
130  protected:
131  float get_number_value_() const;
132 
133  number::Number *number_var_{nullptr};
134  std::string format_;
135 };
136 #endif
137 
138 #ifdef USE_SWITCH
140  public:
142  void set_switch_variable(switch_::Switch *var) { this->switch_var_ = var; }
143  void set_on_text(const std::string &t) { this->switch_on_text_ = t; }
144  void set_off_text(const std::string &t) { this->switch_off_text_ = t; }
145 
146  bool has_value() const override { return true; }
147  std::string get_value_text() const override;
148 
149  bool select_next() override;
150  bool select_prev() override;
151 
152  protected:
153  bool get_switch_state_() const;
154  bool toggle_switch_();
155 
156  switch_::Switch *switch_var_{nullptr};
157  std::string switch_on_text_;
158  std::string switch_off_text_;
159 };
160 #endif
161 
162 class MenuItemCommand : public MenuItem {
163  public:
165 
166  bool select_next() override;
167  bool select_prev() override;
168 };
169 
171  public:
173  void add_on_next_callback(std::function<void()> &&cb) { this->on_next_callbacks_.add(std::move(cb)); }
174  void add_on_prev_callback(std::function<void()> &&cb) { this->on_prev_callbacks_.add(std::move(cb)); }
175 
176  bool has_value() const override { return this->value_getter_.has_value(); }
177  std::string get_value_text() const override;
178 
179  bool select_next() override;
180  bool select_prev() override;
181 
182  protected:
183  void on_next_();
184  void on_prev_();
185 
186  CallbackManager<void()> on_next_callbacks_{};
187  CallbackManager<void()> on_prev_callbacks_{};
188 };
189 
190 } // namespace display_menu_base
191 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
std::vector< MenuItem * > items_
Definition: menu_item.h:85
void set_off_text(const std::string &t)
Definition: menu_item.h:144
CallbackManager< void()> on_leave_callbacks_
Definition: menu_item.h:70
virtual bool has_value() const
Definition: menu_item.h:53
MenuItemType get_type() const
Definition: menu_item.h:45
mopeka_std_values val[4]
void add_on_prev_callback(std::function< void()> &&cb)
Definition: menu_item.h:174
void set_parent(MenuItemMenu *parent)
Definition: menu_item.h:43
void set_on_text(const std::string &t)
Definition: menu_item.h:143
std::function< std::string(const MenuItem *)> value_getter_t
Definition: menu_item.h:38
virtual bool get_immediate_edit() const
Definition: menu_item.h:52
virtual std::string get_value_text() const
Definition: menu_item.h:54
Base-class for all numbers.
Definition: number.h:39
void set_switch_variable(switch_::Switch *var)
Definition: menu_item.h:142
void set_select_variable(select::Select *var)
Definition: menu_item.h:104
void add_on_next_callback(std::function< void()> &&cb)
Definition: menu_item.h:173
void set_number_variable(number::Number *var)
Definition: menu_item.h:121
uint8_t type
CallbackManager< void()> on_enter_callbacks_
Definition: menu_item.h:69
void add_on_enter_callback(std::function< void()> &&cb)
Definition: menu_item.h:47
bool get_immediate_edit() const override
Definition: menu_item.h:92
TemplatableValue< std::string, const MenuItem * > text_
Definition: menu_item.h:67
CallbackManager< void()> on_value_callbacks_
Definition: menu_item.h:71
void add_on_value_callback(std::function< void()> &&cb)
Definition: menu_item.h:49
std::string get_text() const
Definition: menu_item.h:51
Base-class for all selects.
Definition: select.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
num_t cb(num_t x)
Definition: sun.cpp:31
void set_format(const std::string &fmt)
Definition: menu_item.h:122
void add_on_leave_callback(std::function< void()> &&cb)
Definition: menu_item.h:48
void set_value_lambda(value_getter_t &&getter)
Definition: menu_item.h:93
const LogString * menu_item_type_to_string(MenuItemType type)
Returns a string representation of a menu item type suitable for logging.
Definition: menu_item.cpp:8