mirror of
https://github.com/s00500/ESPUI.git
synced 2025-06-14 02:30:41 +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:
@ -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);
|
||||
|
@ -138,6 +138,8 @@ public:
|
||||
ControlColor color;
|
||||
bool visible;
|
||||
uint16_t parentControl;
|
||||
String panelStyle;
|
||||
String elementStyle;
|
||||
Control* next;
|
||||
|
||||
static constexpr uint16_t noParent = 0xffff;
|
||||
@ -281,6 +283,9 @@ public:
|
||||
void clearGraph(uint16_t id, int clientId = -1);
|
||||
void addGraphPoint(uint16_t id, int nValue, int clientId = -1);
|
||||
|
||||
void setPanelStyle(uint16_t id, String style, int clientId = -1);
|
||||
void setElementStyle(uint16_t id, String style, int clientId = -1);
|
||||
|
||||
// Variables
|
||||
const char* ui_title = "ESPUI"; // Store UI Title and Header Name
|
||||
Control* controls = nullptr;
|
||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user