ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
display_menu_base.cpp
Go to the documentation of this file.
1#include "display_menu_base.h"
2#include <algorithm>
3
5
7 if (this->check_healthy_and_active_()) {
8 bool changed = false;
9
10 if (this->editing_) {
11 switch (this->mode_) {
13 changed = this->get_selected_item_()->select_prev();
14 break;
15 default:
16 break;
17 }
18 } else {
19 changed = this->cursor_up_();
20 }
21
22 if (changed)
23 this->draw_and_update();
24 }
25}
26
28 if (this->check_healthy_and_active_()) {
29 bool changed = false;
30
31 if (this->editing_) {
32 switch (this->mode_) {
34 changed = this->get_selected_item_()->select_next();
35 break;
36 default:
37 break;
38 }
39 } else {
40 changed = this->cursor_down_();
41 }
42
43 if (changed)
44 this->draw_and_update();
45 }
46}
47
49 if (this->check_healthy_and_active_()) {
50 bool changed = false;
51
52 switch (this->get_selected_item_()->get_type()) {
57 switch (this->mode_) {
59 if (this->editing_) {
60 this->finish_editing_();
61 changed = true;
62 } else {
63 changed = this->leave_menu_();
64 }
65 break;
67 if (this->editing_ || this->get_selected_item_()->get_immediate_edit())
68 changed = this->get_selected_item_()->select_prev();
69 break;
70 default:
71 break;
72 }
73 break;
74 case MENU_ITEM_BACK:
75 changed = this->leave_menu_();
76 break;
77 default:
78 break;
79 }
80
81 if (changed)
82 this->draw_and_update();
83 }
84}
85
87 if (this->check_healthy_and_active_()) {
88 bool changed = false;
89
90 switch (this->get_selected_item_()->get_type()) {
95 switch (this->mode_) {
97 if (this->editing_ || this->get_selected_item_()->get_immediate_edit())
98 changed = this->get_selected_item_()->select_next();
99 default:
100 break;
101 }
102 break;
103 case MENU_ITEM_MENU:
104 changed = this->enter_menu_();
105 break;
106 default:
107 break;
108 }
109
110 if (changed)
111 this->draw_and_update();
112 }
113}
114
116 if (this->check_healthy_and_active_()) {
117 bool changed = false;
118 MenuItem *item = this->get_selected_item_();
119
120 if (this->editing_) {
121 this->finish_editing_();
122 changed = true;
123 } else {
124 switch (item->get_type()) {
125 case MENU_ITEM_MENU:
126 changed = this->enter_menu_();
127 break;
128 case MENU_ITEM_BACK:
129 changed = this->leave_menu_();
130 break;
131 case MENU_ITEM_SELECT:
132 case MENU_ITEM_SWITCH:
133 case MENU_ITEM_CUSTOM:
134 if (item->get_immediate_edit()) {
135 changed = item->select_next();
136 } else {
137 this->editing_ = true;
138 item->on_enter();
139 changed = true;
140 }
141 break;
142 case MENU_ITEM_NUMBER:
143 // A number cannot be immediate in the rotary mode
144 if (!item->get_immediate_edit() || this->mode_ == MENU_MODE_ROTARY) {
145 this->editing_ = true;
146 item->on_enter();
147 changed = true;
148 }
149 break;
151 changed = item->select_next();
152 break;
153 default:
154 break;
155 }
156 }
157
158 if (changed)
159 this->draw_and_update();
160 }
161}
162
164 if (this->check_healthy_and_active_())
165 this->draw_menu();
166}
167
169 bool disp_changed = false;
170
171 if (this->is_failed())
172 return;
173
174 this->process_initial_();
175
176 this->on_before_show();
177
178 if (this->active_ && this->editing_)
179 this->finish_editing_();
180
181 if (this->displayed_item_ != this->root_item_) {
182 this->displayed_item_->on_leave();
183 disp_changed = true;
184 }
185
186 this->reset_();
187 this->active_ = true;
188
189 if (disp_changed) {
190 this->displayed_item_->on_enter();
191 }
192
193 this->draw_and_update();
194
195 this->on_after_show();
196}
197
199 if (this->is_failed())
200 return;
201
202 this->process_initial_();
203
204 this->on_before_show();
205
206 if (!this->active_) {
207 this->active_ = true;
208 this->draw_and_update();
209 }
210
211 this->on_after_show();
212}
213
215 if (this->check_healthy_and_active_()) {
216 this->on_before_hide();
217
218 if (this->editing_)
219 this->finish_editing_();
220 this->active_ = false;
221 this->update();
222
223 this->on_after_hide();
224 }
225}
226
228 this->displayed_item_ = this->root_item_;
229 this->cursor_index_ = this->top_index_ = 0;
230 this->selection_stack_.clear();
231}
232
234 if (!this->root_on_enter_called_) {
235 this->root_item_->on_enter();
236 this->root_on_enter_called_ = true;
237 }
238}
239
241 if (this->is_failed())
242 return false;
243
244 this->process_initial_();
245
246 return this->active_;
247}
248
250 bool changed = false;
251
252 if (this->cursor_index_ > 0) {
253 changed = true;
254
255 --this->cursor_index_;
256
258 this->top_index_ = this->cursor_index_;
259 }
260
261 return changed;
262}
263
265 bool changed = false;
266
267 if (this->cursor_index_ + 1 < this->displayed_item_->items_size()) {
268 changed = true;
269
270 ++this->cursor_index_;
271
272 if (this->cursor_index_ >= this->top_index_ + this->rows_)
273 this->top_index_ = this->cursor_index_ - this->rows_ + 1;
274 }
275
276 return changed;
277}
278
280 this->displayed_item_->on_leave();
281 this->displayed_item_ = static_cast<MenuItemMenu *>(this->get_selected_item_());
282 this->selection_stack_.emplace_front(this->top_index_, this->cursor_index_);
283 this->cursor_index_ = this->top_index_ = 0;
284 this->displayed_item_->on_enter();
285
286 return true;
287}
288
290 bool changed = false;
291
292 if (this->displayed_item_->get_parent() != nullptr) {
293 this->displayed_item_->on_leave();
295 this->top_index_ = this->selection_stack_.front().first;
296 this->cursor_index_ = this->selection_stack_.front().second;
297 this->selection_stack_.pop_front();
298 this->displayed_item_->on_enter();
299 changed = true;
300 }
301
302 return changed;
303}
304
306 switch (this->get_selected_item_()->get_type()) {
307 case MENU_ITEM_SELECT:
308 case MENU_ITEM_NUMBER:
309 case MENU_ITEM_SWITCH:
310 case MENU_ITEM_CUSTOM:
311 this->get_selected_item_()->on_leave();
312 break;
313 default:
314 break;
315 }
316
317 this->editing_ = false;
318}
319
321 for (size_t i = 0; i < this->rows_ && this->top_index_ + i < this->displayed_item_->items_size(); ++i) {
322 this->draw_item(this->displayed_item_->get_item(this->top_index_ + i), i,
323 this->top_index_ + i == this->cursor_index_);
324 }
325}
326
327} // namespace esphome::display_menu_base
bool is_failed() const
Definition component.h:284
virtual void draw_item(const MenuItem *item, uint8_t row, bool selected)=0
std::forward_list< std::pair< uint8_t, uint8_t > > selection_stack_
MenuItemType get_type() const
Definition menu_item.h:44
virtual bool get_immediate_edit() const
Definition menu_item.h:51