ESPHome 2025.6.3
Loading...
Searching...
No Matches
graphical_display_menu.cpp
Go to the documentation of this file.
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5#include <cstdlib>
7
8namespace esphome {
9namespace graphical_display_menu {
10
11static const char *const TAG = "graphical_display_menu";
12
14 if (this->display_ != nullptr) {
15 display::display_writer_t writer = [this](display::Display &it) { this->draw_menu(); };
17 }
18
19 if (!this->menu_item_value_.has_value()) {
20 this->menu_item_value_ = [](const MenuItemValueArguments *it) {
21 std::string label = " ";
22 if (it->is_item_selected && it->is_menu_editing) {
23 label.append(">");
24 label.append(it->item->get_value_text());
25 label.append("<");
26 } else {
27 label.append("(");
28 label.append(it->item->get_value_text());
29 label.append(")");
30 }
31 return label;
32 };
33 }
34
36}
37
39 ESP_LOGCONFIG(TAG,
40 "Graphical Display Menu\n"
41 "Has Display: %s\n"
42 "Popup Mode: %s\n"
43 "Advanced Drawing Mode: %s\n"
44 "Has Font: %s\n"
45 "Mode: %s\n"
46 "Active: %s\n"
47 "Menu items:",
48 YESNO(this->display_ != nullptr), YESNO(this->display_ != nullptr), YESNO(this->display_ == nullptr),
49 YESNO(this->font_ != nullptr),
50 this->mode_ == display_menu_base::MENU_MODE_ROTARY ? "Rotary" : "Joystick", YESNO(this->active_));
51 for (size_t i = 0; i < this->displayed_item_->items_size(); i++) {
52 auto *item = this->displayed_item_->get_item(i);
53 ESP_LOGCONFIG(TAG, " %i: %s (Type: %s, Immediate Edit: %s)", i, item->get_text().c_str(),
54 LOG_STR_ARG(display_menu_base::menu_item_type_to_string(item->get_type())),
55 YESNO(item->get_immediate_edit()));
56 }
57}
58
60
62
63void GraphicalDisplayMenu::set_foreground_color(Color foreground_color) { this->foreground_color_ = foreground_color; }
64void GraphicalDisplayMenu::set_background_color(Color background_color) { this->background_color_ = background_color; }
65
67 if (this->display_ != nullptr) {
69 this->display_->show_page(this->display_page_.get());
70 this->display_->clear();
71 } else {
72 this->update();
73 }
74}
75
77 if (this->previous_display_page_ != nullptr) {
79 this->display_->clear();
80 this->update();
81 this->previous_display_page_ = nullptr;
82 } else {
83 this->update();
84 }
85}
86
88 this->update();
89
90 // If we're in advanced drawing mode we won't have a display and will instead require the update callback to do
91 // our drawing
92 if (this->display_ != nullptr) {
93 draw_menu();
94 }
95}
96
98 if (this->display_ == nullptr) {
99 ESP_LOGE(TAG, "draw_menu() called without a display_. This is only available when using the menu in pop up mode");
100 return;
101 }
102 display::Rect bounds(0, 0, this->display_->get_width(), this->display_->get_height());
103 this->draw_menu_internal_(this->display_, &bounds);
104}
105
107 this->draw_menu_internal_(display, bounds);
108}
109
111 int16_t total_height = 0;
112 int16_t max_width = 0;
113 int y_padding = 2;
114 bool scroll_menu_items = false;
115 std::vector<display::Rect> menu_dimensions;
116 int number_items_fit_to_screen = 0;
117 const int max_item_index = this->displayed_item_->items_size() - 1;
118
119 for (size_t i = 0; i <= max_item_index; i++) {
120 const auto *item = this->displayed_item_->get_item(i);
121 const bool selected = i == this->cursor_index_;
122 const display::Rect item_dimensions = this->measure_item(display, item, bounds, selected);
123
124 menu_dimensions.push_back(item_dimensions);
125 total_height += item_dimensions.h + (i == 0 ? 0 : y_padding);
126 max_width = std::max(max_width, item_dimensions.w);
127
128 if (total_height <= bounds->h) {
129 number_items_fit_to_screen++;
130 } else {
131 // Scroll the display if the selected item or the item immediately after it overflows
132 if ((selected) || (i == this->cursor_index_ + 1)) {
133 scroll_menu_items = true;
134 }
135 }
136 }
137
138 // Determine what items to draw
139 int first_item_index = 0;
140 int last_item_index = max_item_index;
141
142 if (number_items_fit_to_screen <= 1) {
143 // If only one item can fit to the bounds draw the current cursor item
144 last_item_index = std::min(last_item_index, this->cursor_index_ + 1);
145 first_item_index = this->cursor_index_;
146 } else {
147 if (scroll_menu_items) {
148 // Attempt to draw the item after the current item (+1 for equality check in the draw loop)
149 last_item_index = std::min(last_item_index, this->cursor_index_ + 1);
150
151 // Go back through the measurements to determine how many prior items we can fit
152 int height_left_to_use = bounds->h;
153 for (int i = last_item_index; i >= 0; i--) {
154 const display::Rect item_dimensions = menu_dimensions[i];
155 height_left_to_use -= (item_dimensions.h + y_padding);
156
157 if (height_left_to_use <= 0) {
158 // Ran out of space - this is our first item to draw
159 first_item_index = i;
160 break;
161 }
162 }
163 const int items_to_draw = last_item_index - first_item_index;
164 // Dont't draw last item partially if it is the selected item
165 if ((this->cursor_index_ == last_item_index) && (number_items_fit_to_screen <= items_to_draw) &&
166 (first_item_index < max_item_index)) {
167 first_item_index++;
168 }
169 }
170 }
171
172 // Render the items into the view port
173 display->start_clipping(*bounds);
174
175 display->filled_rectangle(bounds->x, bounds->y, max_width, total_height, this->background_color_);
176 auto y_offset = bounds->y;
177 for (size_t i = first_item_index; i <= last_item_index; i++) {
178 const auto *item = this->displayed_item_->get_item(i);
179 const bool selected = i == this->cursor_index_;
180 display::Rect dimensions = menu_dimensions[i];
181
182 dimensions.y = y_offset;
183 dimensions.x = bounds->x;
184 this->draw_item(display, item, &dimensions, selected);
185
186 y_offset += dimensions.h + y_padding;
187 }
188
189 display->end_clipping();
190}
191
193 const display::Rect *bounds, const bool selected) {
194 display::Rect dimensions(0, 0, 0, 0);
195
196 if (selected) {
197 // TODO: Support selection glyph
198 dimensions.w += 0;
199 dimensions.h += 0;
200 }
201
202 std::string label = item->get_text();
203 if (item->has_value()) {
204 // Append to label
205 MenuItemValueArguments args(item, selected, this->editing_);
206 label.append(this->menu_item_value_.value(&args));
207 }
208
209 int x1;
210 int y1;
211 int width;
212 int height;
213 display->get_text_bounds(0, 0, label.c_str(), this->font_, display::TextAlign::TOP_LEFT, &x1, &y1, &width, &height);
214
215 dimensions.w = std::min((int16_t) width, bounds->w);
216 dimensions.h = std::min((int16_t) height, bounds->h);
217
218 return dimensions;
219}
220
222 const display::Rect *bounds, const bool selected) {
223 const auto background_color = selected ? this->foreground_color_ : this->background_color_;
224 const auto foreground_color = selected ? this->background_color_ : this->foreground_color_;
225
226 // int background_width = std::max(bounds->width, available_width);
227 int background_width = bounds->w;
228
229 display->filled_rectangle(bounds->x, bounds->y, background_width, bounds->h, background_color);
230
231 std::string label = item->get_text();
232 if (item->has_value()) {
233 MenuItemValueArguments args(item, selected, this->editing_);
234 label.append(this->menu_item_value_.value(&args));
235 }
236
237 display->print(bounds->x, bounds->y, this->font_, foreground_color, display::TextAlign::TOP_LEFT, label.c_str(),
238 background_color);
239}
240
241void GraphicalDisplayMenu::draw_item(const display_menu_base::MenuItem *item, const uint8_t row, const bool selected) {
242 ESP_LOGE(TAG, "draw_item(MenuItem *item, uint8_t row, bool selected) called. The graphical_display_menu specific "
243 "draw_item should be called.");
244}
245
247
248} // namespace graphical_display_menu
249} // namespace esphome
uint8_t h
Definition bl0906.h:2
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:54
void show_page(DisplayPage *page)
Definition display.cpp:639
void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:16
void end_clipping()
Reset the invalidation region.
Definition display.cpp:693
void start_clipping(Rect rect)
Set the clipping rectangle for further drawing.
Definition display.cpp:686
const DisplayPage * get_active_page() const
Definition display.h:594
virtual int get_width()
Get the calculated width of the display in pixels with rotation applied.
Definition display.h:218
void print(int x, int y, BaseFont *font, Color color, TextAlign align, const char *text, Color background=COLOR_OFF)
Print text with the anchor point at [x,y] with font.
Definition display.cpp:480
void get_text_bounds(int x, int y, const char *text, BaseFont *font, TextAlign align, int *x1, int *y1, int *width, int *height)
Get the text bounds of the given string.
Definition display.cpp:548
void filled_rectangle(int x1, int y1, int width, int height, Color color=COLOR_ON)
Fill a rectangle with the top left point at [x1,y1] and the bottom right point at [x1+width,...
Definition display.cpp:104
int16_t x
X coordinate of corner.
Definition rect.h:12
int16_t h
Height of region.
Definition rect.h:15
int16_t y
Y coordinate of corner.
Definition rect.h:13
int16_t w
Width of region.
Definition rect.h:14
virtual bool has_value() const
Definition menu_item.h:53
std::unique_ptr< display::DisplayPage > display_page_
TemplatableValue< std::string, const MenuItemValueArguments * > menu_item_value_
void draw_item(const display_menu_base::MenuItem *item, uint8_t row, bool selected) override
virtual display::Rect measure_item(display::Display *display, const display_menu_base::MenuItem *item, const display::Rect *bounds, bool selected)
void draw_menu_internal_(display::Display *display, const display::Rect *bounds)
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
std::function< void(Display &)> display_writer_t
Definition display.h:181
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::unique_ptr< T > make_unique(Args &&...args)
Definition helpers.h:86