1
0
mirror of https://github.com/s00500/ESPUI.git synced 2025-07-04 01:00:19 +00:00

Support for dynamic custom inline styling.

Adds two functions in ESPUI.h:
setPanelStyle()
setElementStyle()
These allow for custom inline CSS styles to be applied to the panel and to
the specific UI element repectively.

For example:

```
char stylecol1[30]
sprintf(stylecol1, "background-color: #%06X;", (unsigned int) random(0x0, 0xFFFFFF));
ESPUI.setPanelStyle(switch1, stylecol1);
```
This will set the panel of the given control to a random hex colour.

This is supported by both the initial UI message, and by control update messages, so you
can change these styles dynamically in response to other events.

setElementStyle() is not perfect. Because CSS inline styles can only style one specific
DOM element, for controls made up of multiple elements (like the "pad") this is limited.
I have tried to make an appropriate choice for each supported control.
This commit is contained in:
Ian Gray
2022-01-02 21:56:32 +00:00
parent 78317ce5f2
commit ec1862cfe6
5 changed files with 132 additions and 62 deletions

View File

@ -756,6 +756,8 @@ void ESPUIClass::updateControl(Control* control, int clientId)
root["value"] = control->value;
root["id"] = control->id;
root["color"] = (int)control->color;
if(control->panelStyle != 0) root["panelStyle"] = control->panelStyle;
if(control->elementStyle != 0) root["elementStyle"] = control->elementStyle;
serializeJson(document, json);
#if defined(DEBUG_ESPUI)
@ -796,6 +798,22 @@ void ESPUIClass::updateControl(Control* control, int clientId)
}
}
void ESPUIClass::setPanelStyle(uint16_t id, String style, int clientId) {
Control* control = getControl(id);
if(control) {
control->panelStyle = style;
updateControl(control, clientId);
}
}
void ESPUIClass::setElementStyle(uint16_t id, String style, int clientId) {
Control* control = getControl(id);
if(control) {
control->elementStyle = style;
updateControl(control, clientId);
}
}
void ESPUIClass::updateControl(uint16_t id, int clientId)
{
Control* control = getControl(id);
@ -998,6 +1016,8 @@ Control *ESPUIClass::prepareJSONChunk(AsyncWebSocketClient* client, Control* con
item["value"] = String(control->value);
item["color"] = (int)control->color;
item["visible"] = control->visible;
if(control->panelStyle != 0) item["panelStyle"] = String(control->panelStyle);
if(control->elementStyle != 0) item["elementStyle"] = String(control->elementStyle);
if (control->parentControl != Control::noParent) {
item["parentControl"] = String(control->parentControl);