From f061ff4ee4a59e447ae81b6e4e1d5426888b4d1c Mon Sep 17 00:00:00 2001 From: Michael Davidson Date: Fri, 29 Dec 2023 18:35:55 +1100 Subject: [PATCH] Add setup_complete hook Called at the end of setup() of the underlying Component. Last chance to do pre-run-time initialisation in LayoutItems --- esphome/components/graphical_layout/container_layout_item.h | 6 ++++++ esphome/components/graphical_layout/fixed_dimension_panel.h | 1 + esphome/components/graphical_layout/graphical_layout.cpp | 4 +++- esphome/components/graphical_layout/layout_item.h | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/esphome/components/graphical_layout/container_layout_item.h b/esphome/components/graphical_layout/container_layout_item.h index c6dca3fde9..bc27e6782d 100644 --- a/esphome/components/graphical_layout/container_layout_item.h +++ b/esphome/components/graphical_layout/container_layout_item.h @@ -18,6 +18,12 @@ class ContainerLayoutItem : public LayoutItem { /** Adds an item to this container */ void add_item(LayoutItem *child) { this->children_.push_back(child); } + void setup_complete() override { + for (LayoutItem *child : this->children_) { + child->setup_complete(); + } + } + protected: std::vector children_; }; diff --git a/esphome/components/graphical_layout/fixed_dimension_panel.h b/esphome/components/graphical_layout/fixed_dimension_panel.h index 667427c56b..e199de3599 100644 --- a/esphome/components/graphical_layout/fixed_dimension_panel.h +++ b/esphome/components/graphical_layout/fixed_dimension_panel.h @@ -13,6 +13,7 @@ class FixedDimensionPanel : public LayoutItem { display::Rect measure_item_internal(display::Display *display) override; void render_internal(display::Display *display, display::Rect bounds) override; void dump_config(int indent_depth, int additional_level_depth) override; + void setup_complete() override { this->child_->setup_complete(); } void set_child(LayoutItem *child) { this->child_ = child; }; template void set_width(V width) { this->width_ = width; }; diff --git a/esphome/components/graphical_layout/graphical_layout.cpp b/esphome/components/graphical_layout/graphical_layout.cpp index a3c04580b3..3e68252d9a 100644 --- a/esphome/components/graphical_layout/graphical_layout.cpp +++ b/esphome/components/graphical_layout/graphical_layout.cpp @@ -8,7 +8,9 @@ namespace graphical_layout { static const char *const TAG = "rootlayoutcomponent"; -void RootLayoutComponent::setup() {} +void RootLayoutComponent::setup() { + this->layout_root_->setup_complete(); +} void RootLayoutComponent::dump_config() { ESP_LOGCONFIG(TAG, "Graphical Layout"); diff --git a/esphome/components/graphical_layout/layout_item.h b/esphome/components/graphical_layout/layout_item.h index d6c62f76ca..3b221ac103 100644 --- a/esphome/components/graphical_layout/layout_item.h +++ b/esphome/components/graphical_layout/layout_item.h @@ -83,6 +83,11 @@ class LayoutItem { */ virtual void dump_config(int indent_depth, int additional_level_depth) = 0; + /** Called once all setup has been completed (i.e. after code generation and all your set_ methods + * have been called). Can be used to finalise any configuration + */ + virtual void setup_complete() {}; + void set_margin(int margin) { this->margin_ = margin; }; void set_padding(int padding) { this->padding_ = padding; }; void set_border(int border) { this->border_ = border; };