From e81531a8e51ff38a593e901b86c43b5333c026ec Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sat, 6 Dec 2025 22:26:16 +0000 Subject: [PATCH] Adding features to the API --- src/UIComponent.cpp | 21 +++++++++++++++++---- src/UIComponent.h | 4 +++- src/windowPool.cpp | 1 + src/windowPool.h | 3 ++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/UIComponent.cpp b/src/UIComponent.cpp index 1828718..01284a2 100644 --- a/src/UIComponent.cpp +++ b/src/UIComponent.cpp @@ -21,6 +21,15 @@ UIElement::UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, strncpy(this->title, title, MAX_TITLE_LEN); } +UIElement::~UIElement() { + // Destruction involves redrawing over the "old" area + + // TODO: should we have a separate place to do all the drawing? + this->display->fillRect(this->dims.x, this->dims.y, + this->dims.width, this->dims.height, + this->decor.bgColor); +} + int16_t UIElement::getContentAreaX0() { if (this->decor.hasBorder) { return this->dims.x + DEFAULT_BORDER_THICKNESS + DEFAULT_MARGIN; @@ -132,26 +141,30 @@ void UIElement::replaceString(char *oldVal, char *newVal) { } } -bool UIElement::AddChild(ComponentType t) { +int16_t UIElement::AddChild(ComponentType t) { if (childrenCount >= MAX_CHILDREN) { Serial2.println("Failed to add child: childrenCount exceeds MAX_CHILDREN"); Serial2.print( childrenCount); Serial2.print( " >= "); Serial2.print( MAX_CHILDREN); - return false; + return -1; } int16_t newElem = WindowPool::Allocate(t); if (newElem < 0) { Serial2.println("Failed to allocate new component"); - return false; + return -1; } this->children[this->curChildIndex] = newElem; this->childrenCount++; this->curChildIndex++; - return true; + return newElem; +} + +void UIElement::RemoveChild(int16_t childId) { + WindowPool::Deallocate(childId); } UIElement* UIElement::GetChild(int16_t childID) { diff --git a/src/UIComponent.h b/src/UIComponent.h index 163808e..379246a 100644 --- a/src/UIComponent.h +++ b/src/UIComponent.h @@ -25,12 +25,14 @@ public: UIElement(); UIElement(Arduino_GFX *d, UIDimensions dims, UIDecorations decor, char *Title); + ~UIElement(); // Children int16_t children[MAX_CHILDREN]; // Children Handling - bool AddChild(ComponentType t); + int16_t AddChild(ComponentType t); + void RemoveChild(int16_t childID); UIElement* GetChild(int16_t childID); uint8_t ChildrenCount(); diff --git a/src/windowPool.cpp b/src/windowPool.cpp index 4345f8a..10a820c 100644 --- a/src/windowPool.cpp +++ b/src/windowPool.cpp @@ -57,6 +57,7 @@ namespace WindowPool { case BAR: break; case STRING: + delete pool[id].str; break; case TABLE: break; diff --git a/src/windowPool.h b/src/windowPool.h index df88642..68edb92 100644 --- a/src/windowPool.h +++ b/src/windowPool.h @@ -1,6 +1,7 @@ #ifndef __WINDOW_POOL__ #define __WINDOW_POOL__ +#include #include #include "values.h" @@ -26,7 +27,7 @@ namespace WindowPool { int16_t Allocate(ComponentType t); // I will finish this later on, still have to figure out how to properly // destroy the UIComponent - void Deallocate(); + void Deallocate(int16_t childID); UIElement* GetHandle(int16_t index); // Debugging