ESPHome 2025.5.0
Loading...
Searching...
No Matches
menu_item.h
Go to the documentation of this file.
1#pragma once
2
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
19namespace esphome {
20namespace display_menu_base {
21
32
35
36class MenuItem;
37class MenuItemMenu;
38using value_getter_t = std::function<std::string(const MenuItem *)>;
39
40class 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
68
72};
73
74class 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
88class 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};
98};
99
100#ifdef USE_SELECT
102 public:
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:
114};
115#endif
116
117#ifdef USE_NUMBER
119 public:
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
134 std::string format_;
135};
136#endif
137
138#ifdef USE_SWITCH
140 public:
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
157 std::string switch_on_text_;
158 std::string switch_off_text_;
159};
160#endif
161
162class 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
188};
189
190} // namespace display_menu_base
191} // namespace esphome
CallbackManager< void()> on_next_callbacks_
Definition menu_item.h:186
void add_on_prev_callback(std::function< void()> &&cb)
Definition menu_item.h:174
std::string get_value_text() const override
void add_on_next_callback(std::function< void()> &&cb)
Definition menu_item.h:173
CallbackManager< void()> on_prev_callbacks_
Definition menu_item.h:187
void set_value_lambda(value_getter_t &&getter)
Definition menu_item.h:93
optional< value_getter_t > value_getter_
Definition menu_item.h:97
virtual std::string get_value_text() const
Definition menu_item.h:54
void set_parent(MenuItemMenu *parent)
Definition menu_item.h:43
CallbackManager< void()> on_leave_callbacks_
Definition menu_item.h:70
void add_on_value_callback(std::function< void()> &&cb)
Definition menu_item.h:49
CallbackManager< void()> on_value_callbacks_
Definition menu_item.h:71
void add_on_leave_callback(std::function< void()> &&cb)
Definition menu_item.h:48
CallbackManager< void()> on_enter_callbacks_
Definition menu_item.h:69
virtual bool has_value() const
Definition menu_item.h:53
void add_on_enter_callback(std::function< void()> &&cb)
Definition menu_item.h:47
MenuItemType get_type() const
Definition menu_item.h:45
TemplatableValue< std::string, const MenuItem * > text_
Definition menu_item.h:67
virtual bool get_immediate_edit() const
Definition menu_item.h:52
std::vector< MenuItem * > items_
Definition menu_item.h:85
std::string get_value_text() const override
Definition menu_item.cpp:76
void set_number_variable(number::Number *var)
Definition menu_item.h:121
void set_format(const std::string &fmt)
Definition menu_item.h:122
void set_select_variable(select::Select *var)
Definition menu_item.h:104
std::string get_value_text() const override
Definition menu_item.cpp:38
void set_off_text(const std::string &t)
Definition menu_item.h:144
std::string get_value_text() const override
void set_on_text(const std::string &t)
Definition menu_item.h:143
void set_switch_variable(switch_::Switch *var)
Definition menu_item.h:142
Base-class for all numbers.
Definition number.h:39
bool has_value() const
Definition optional.h:87
Base-class for all selects.
Definition select.h:31
Base class for all switches.
Definition switch.h:39
uint8_t type
mopeka_std_values val[4]
std::function< std::string(const MenuItem *)> value_getter_t
Definition menu_item.h:38
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7