Provide a bounds to the DisplayRenderingPanel

This allows users to customise their drawing depending on the available space
This commit is contained in:
Michael Davidson 2023-12-26 16:00:43 +11:00
parent 3fabe545ee
commit 2fc6714c80
No known key found for this signature in database
GPG key ID: B8D1A99712B8B0EB
3 changed files with 12 additions and 8 deletions

View file

@ -19,7 +19,7 @@ display::Rect DisplayRenderingPanel::measure_item_internal(display::Display *dis
} }
void DisplayRenderingPanel::render_internal(display::Display *display, display::Rect bounds) { void DisplayRenderingPanel::render_internal(display::Display *display, display::Rect bounds) {
this->lambda_(*display); this->lambda_(*display, bounds);
} }
} // namespace graphical_layout } // namespace graphical_layout

View file

@ -9,8 +9,7 @@
namespace esphome { namespace esphome {
namespace graphical_layout { namespace graphical_layout {
/* See display.h for original declaration */ using display_panel_writer_t = std::function<void(display::Display &, display::Rect)>;
using display_writer_t = std::function<void(display::Display &)>;
/** The DisplayRenderingPanel is a UI item that renders a custom lambda to the display whilst /** The DisplayRenderingPanel is a UI item that renders a custom lambda to the display whilst
* participating in the layout process * participating in the layout process
@ -23,12 +22,12 @@ class DisplayRenderingPanel : public LayoutItem {
template<typename V> void set_width(V width) { this->width_ = width; }; template<typename V> void set_width(V width) { this->width_ = width; };
template<typename V> void set_height(V height) { this->height_ = height; }; template<typename V> 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: protected:
TemplatableValue<int> width_{0}; TemplatableValue<int> width_{0};
TemplatableValue<int> height_{0}; TemplatableValue<int> height_{0};
display_writer_t lambda_{nullptr}; display_panel_writer_t lambda_{nullptr};
}; };
} // namespace graphical_layout } // namespace graphical_layout

View file

@ -1,9 +1,10 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import CONF_WIDTH, CONF_HEIGHT, CONF_LAMBDA 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") graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout")
Rect = display_ns.class_("Rect")
DisplayRenderingPanel = graphical_layout_ns.class_("DisplayRenderingPanel") DisplayRenderingPanel = graphical_layout_ns.class_("DisplayRenderingPanel")
CONF_DISPLAY_RENDERING_PANEL = "display_rendering_panel" 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_) width = await cg.templatable(item_config[CONF_WIDTH], args=[], output_type=cg.int_)
cg.add(var.set_width(width)) 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)) cg.add(var.set_height(height))
lambda_ = await cg.process_lambda( 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_)) cg.add(var.set_lambda(lambda_))