ESPHome  2024.11.0
graphical_display_menu.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/color.h"
7 #include <cstdlib>
8 
9 namespace esphome {
10 
11 // forward declare from display namespace
12 namespace display {
13 class Display;
14 class DisplayPage;
15 class BaseFont;
16 class Rect;
17 } // namespace display
18 
19 namespace graphical_display_menu {
20 
21 const Color COLOR_ON(255, 255, 255, 255);
22 const Color COLOR_OFF(0, 0, 0, 0);
23 
25  MenuItemValueArguments(const display_menu_base::MenuItem *item, bool is_item_selected, bool is_menu_editing) {
26  this->item = item;
27  this->is_item_selected = is_item_selected;
28  this->is_menu_editing = is_menu_editing;
29  }
30 
34 };
35 
37  public:
38  void setup() override;
39  void dump_config() override;
40 
41  void set_display(display::Display *display);
42  void set_font(display::BaseFont *font);
43  template<typename V> void set_menu_item_value(V menu_item_value) { this->menu_item_value_ = menu_item_value; }
44  void set_foreground_color(Color foreground_color);
45  void set_background_color(Color background_color);
46 
47  void add_on_redraw_callback(std::function<void()> &&cb) { this->on_redraw_callbacks_.add(std::move(cb)); }
48 
49  void draw(display::Display *display, const display::Rect *bounds);
50 
51  protected:
52  void draw_and_update() override;
53  void draw_menu() override;
54  void draw_menu_internal_(display::Display *display, const display::Rect *bounds);
55  void draw_item(const display_menu_base::MenuItem *item, uint8_t row, bool selected) override;
56  virtual display::Rect measure_item(display::Display *display, const display_menu_base::MenuItem *item,
57  const display::Rect *bounds, bool selected);
58  virtual void draw_item(display::Display *display, const display_menu_base::MenuItem *item,
59  const display::Rect *bounds, bool selected);
60  void update() override;
61 
62  void on_before_show() override;
63  void on_before_hide() override;
64 
65  std::unique_ptr<display::DisplayPage> display_page_{nullptr};
66  const display::DisplayPage *previous_display_page_{nullptr};
67  display::Display *display_{nullptr};
68  display::BaseFont *font_{nullptr};
70  Color foreground_color_{COLOR_ON};
71  Color background_color_{COLOR_OFF};
72 
73  CallbackManager<void()> on_redraw_callbacks_{};
74 };
75 
76 class GraphicalDisplayMenuOnRedrawTrigger : public Trigger<const GraphicalDisplayMenu *> {
77  public:
79  parent->add_on_redraw_callback([this, parent]() { this->trigger(parent); });
80  }
81 };
82 
83 } // namespace graphical_display_menu
84 } // namespace esphome
void setup()
MenuItemValueArguments(const display_menu_base::MenuItem *item, bool is_item_selected, bool is_menu_editing)
const Color COLOR_OFF(0, 0, 0, 0)
Turn the pixel OFF.
Definition: display.h:191
TemplatableValue< std::string, const MenuItemValueArguments * > menu_item_value_
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
Definition: display.h:193
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
num_t cb(num_t x)
Definition: sun.cpp:31
Class to display a hierarchical menu.