From 2fc6714c80fba1140a62f886c29f3b6607f24831 Mon Sep 17 00:00:00 2001 From: Michael Davidson Date: Tue, 26 Dec 2023 16:00:43 +1100 Subject: [PATCH] Provide a bounds to the DisplayRenderingPanel This allows users to customise their drawing depending on the available space --- .../graphical_layout/display_rendering_panel.cpp | 2 +- .../graphical_layout/display_rendering_panel.h | 7 +++---- .../graphical_layout/display_rendering_panel.py | 11 ++++++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/esphome/components/graphical_layout/display_rendering_panel.cpp b/esphome/components/graphical_layout/display_rendering_panel.cpp index e96f7cfaf9..03d01906ae 100644 --- a/esphome/components/graphical_layout/display_rendering_panel.cpp +++ b/esphome/components/graphical_layout/display_rendering_panel.cpp @@ -19,7 +19,7 @@ display::Rect DisplayRenderingPanel::measure_item_internal(display::Display *dis } void DisplayRenderingPanel::render_internal(display::Display *display, display::Rect bounds) { - this->lambda_(*display); + this->lambda_(*display, bounds); } } // namespace graphical_layout diff --git a/esphome/components/graphical_layout/display_rendering_panel.h b/esphome/components/graphical_layout/display_rendering_panel.h index e06e203239..86bdd8c709 100644 --- a/esphome/components/graphical_layout/display_rendering_panel.h +++ b/esphome/components/graphical_layout/display_rendering_panel.h @@ -9,8 +9,7 @@ namespace esphome { namespace graphical_layout { -/* See display.h for original declaration */ -using display_writer_t = std::function; +using display_panel_writer_t = std::function; /** The DisplayRenderingPanel is a UI item that renders a custom lambda to the display whilst * participating in the layout process @@ -23,12 +22,12 @@ class DisplayRenderingPanel : public LayoutItem { template void set_width(V width) { this->width_ = width; }; template void set_height(V height) { this->height_ = height; }; - void set_lambda(display_writer_t lambda) { this->lambda_ = std::move(lambda); }; + void set_lambda(display_panel_writer_t lambda) { this->lambda_ = std::move(lambda); }; protected: TemplatableValue width_{0}; TemplatableValue height_{0}; - display_writer_t lambda_{nullptr}; + display_panel_writer_t lambda_{nullptr}; }; } // namespace graphical_layout diff --git a/esphome/components/graphical_layout/display_rendering_panel.py b/esphome/components/graphical_layout/display_rendering_panel.py index e1a792f049..f20e15ae6b 100644 --- a/esphome/components/graphical_layout/display_rendering_panel.py +++ b/esphome/components/graphical_layout/display_rendering_panel.py @@ -1,9 +1,10 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.const import CONF_WIDTH, CONF_HEIGHT, CONF_LAMBDA -from esphome.components.display import DisplayRef +from esphome.components.display import display_ns, DisplayRef graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") +Rect = display_ns.class_("Rect") DisplayRenderingPanel = graphical_layout_ns.class_("DisplayRenderingPanel") CONF_DISPLAY_RENDERING_PANEL = "display_rendering_panel" @@ -26,11 +27,15 @@ async def config_to_layout_item(pvariable_builder, item_config, child_item_build width = await cg.templatable(item_config[CONF_WIDTH], args=[], output_type=cg.int_) cg.add(var.set_width(width)) - height = await cg.templatable(item_config[CONF_HEIGHT], args=[], output_type=cg.int_) + height = await cg.templatable( + item_config[CONF_HEIGHT], args=[], output_type=cg.int_ + ) cg.add(var.set_height(height)) lambda_ = await cg.process_lambda( - item_config[CONF_LAMBDA], [(DisplayRef, "it")], return_type=cg.void + item_config[CONF_LAMBDA], + [(DisplayRef, "it"), (Rect, "bounds")], + return_type=cg.void, ) cg.add(var.set_lambda(lambda_))