ESPHome 2026.11.0b6
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
20
31
34
35class MenuItem;
36class MenuItemMenu;
37using value_getter_t = std::function<std::string(const MenuItem *)>;
38
39class MenuItem {
40 public:
41 explicit MenuItem(MenuItemType t) : item_type_(t) {}
42 void set_parent(MenuItemMenu *parent) { this->parent_ = parent; }
43 MenuItemMenu *get_parent() { return this->parent_; }
44 MenuItemType get_type() const { return this->item_type_; }
45 template<typename V> void set_text(V val) { this->text_ = val; }
46 template<typename F> void add_on_enter_callback(F &&cb) { this->on_enter_callbacks_.add(std::forward<F>(cb)); }
47 template<typename F> void add_on_leave_callback(F &&cb) { this->on_leave_callbacks_.add(std::forward<F>(cb)); }
48 template<typename F> void add_on_value_callback(F &&cb) { this->on_value_callbacks_.add(std::forward<F>(cb)); }
49
50 std::string get_text() const { return const_cast<MenuItem *>(this)->text_.value(this); }
51 virtual bool get_immediate_edit() const { return false; }
52 virtual bool has_value() const { return false; }
53 virtual std::string get_value_text() const { return ""; }
54
55 virtual bool select_next() { return false; }
56 virtual bool select_prev() { return false; }
57
58 void on_enter();
59 void on_leave();
60
61 protected:
62 void on_value_();
63
67
71};
72
73class MenuItemMenu final : public MenuItem {
74 public:
76 void add_item(MenuItem *item) {
77 item->set_parent(this);
78 this->items_.push_back(item);
79 }
80 size_t items_size() const { return this->items_.size(); }
81 MenuItem *get_item(size_t i) { return this->items_[i]; }
82
83 protected:
84 std::vector<MenuItem *> items_;
85};
86
87class MenuItemEditable : public MenuItem {
88 public:
90 void set_immediate_edit(bool val) { this->immediate_edit_ = val; }
91 bool get_immediate_edit() const override { return this->immediate_edit_; }
92 void set_value_lambda(value_getter_t &&getter) { this->value_getter_ = getter; }
93
94 protected:
95 bool immediate_edit_{false};
96 optional<value_getter_t> value_getter_{};
97};
98
99#ifdef USE_SELECT
100class MenuItemSelect final : public MenuItemEditable {
101 SUB_SELECT(variable)
102
103 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#endif
113
114#ifdef USE_NUMBER
115class MenuItemNumber final : public MenuItemEditable {
116 SUB_NUMBER(variable)
117
118 public:
120 // Keeps the pointer; the string must outlive the item (codegen passes a literal)
121 void set_format(const char *fmt) { this->format_ = fmt; }
122
123 bool has_value() const override { return true; }
124 std::string get_value_text() const override;
125
126 bool select_next() override;
127 bool select_prev() override;
128
129 protected:
130 float get_number_value_() const;
131
132 const char *format_{""};
133};
134#endif
135
136#ifdef USE_SWITCH
137class MenuItemSwitch final : public MenuItemEditable {
138 SUB_SWITCH(variable)
139
140 public:
142 // Both keep the pointer; the strings must outlive the item (codegen passes literals)
143 void set_on_text(const char *t) { this->switch_on_text_ = t; }
144 void set_off_text(const char *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 const char *switch_on_text_{""};
157 const char *switch_off_text_{""};
158};
159#endif
160
161class MenuItemCommand final : public MenuItem {
162 public:
164
165 bool select_next() override;
166 bool select_prev() override;
167};
168
169class MenuItemCustom final : public MenuItemEditable {
170 public:
172 template<typename F> void add_on_next_callback(F &&cb) { this->on_next_callbacks_.add(std::forward<F>(cb)); }
173 template<typename F> void add_on_prev_callback(F &&cb) { this->on_prev_callbacks_.add(std::forward<F>(cb)); }
174
175 bool has_value() const override { return this->value_getter_.has_value(); }
176 std::string get_value_text() const override;
177
178 bool select_next() override;
179 bool select_prev() override;
180
181 protected:
182 void on_next_();
183 void on_prev_();
184
187};
188
189} // namespace esphome::display_menu_base
Primary TemplatableValue: stores either a constant value or a function pointer.
Definition automation.h:169
CallbackManager< void()> on_next_callbacks_
Definition menu_item.h:185
std::string get_value_text() const override
CallbackManager< void()> on_prev_callbacks_
Definition menu_item.h:186
void set_value_lambda(value_getter_t &&getter)
Definition menu_item.h:92
optional< value_getter_t > value_getter_
Definition menu_item.h:96
virtual std::string get_value_text() const
Definition menu_item.h:53
void set_parent(MenuItemMenu *parent)
Definition menu_item.h:42
CallbackManager< void()> on_leave_callbacks_
Definition menu_item.h:69
CallbackManager< void()> on_value_callbacks_
Definition menu_item.h:70
CallbackManager< void()> on_enter_callbacks_
Definition menu_item.h:68
virtual bool has_value() const
Definition menu_item.h:52
MenuItemType get_type() const
Definition menu_item.h:44
TemplatableValue< std::string, const MenuItem * > text_
Definition menu_item.h:66
virtual bool get_immediate_edit() const
Definition menu_item.h:51
std::vector< MenuItem * > items_
Definition menu_item.h:84
uint8_t type
mopeka_std_values val[3]
std::function< std::string(const MenuItem *)> value_getter_t
Definition menu_item.h:37
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:7
size_t size_t const char * fmt
Definition helpers.h:1334