chore: lsp + format
This commit is contained in:
+143
-108
@@ -1,15 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
// The widget tree: structure, block-flow layout, labels and style, over a flat node
|
||||
// arena. Free of Arduino headers so test/ui_layout_test.cpp can exercise it on the host.
|
||||
// Ported from the Lua toolkit's Component:measure/place, whose semantics the tests
|
||||
// still describe.
|
||||
// The widget tree: structure, block-flow layout, labels and style, over a flat
|
||||
// node arena. Free of Arduino headers so test/ui_layout_test.cpp can exercise
|
||||
// it on the host. Ported from the Lua toolkit's Component:measure/place, whose
|
||||
// semantics the tests still describe.
|
||||
//
|
||||
// The tree is split across two arenas because the halves have different lifetimes. A
|
||||
// Node holds what hit testing and repainting need forever: 16 bytes. A Spec holds what
|
||||
// only measure() and place() read -- requested sizes, padding, gap, alignment -- and is
|
||||
// dropped when the pass ends. Re-layout rebuilds from Lua rather than retaining ~20
|
||||
// bytes per node against a rotation nobody measures in milliseconds.
|
||||
// The tree is split across two arenas because the halves have different
|
||||
// lifetimes. A Node holds what hit testing and repainting need forever: 16
|
||||
// bytes. A Spec holds what only measure() and place() read -- requested sizes,
|
||||
// padding, gap, alignment -- and is dropped when the pass ends. Re-layout
|
||||
// rebuilds from Lua rather than retaining ~20 bytes per node against a rotation
|
||||
// nobody measures in milliseconds.
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@@ -22,17 +23,17 @@ namespace ui {
|
||||
|
||||
constexpr uint16_t NONE = 0xFFFF;
|
||||
|
||||
// Distinguishes "no constraint" from a real zero, which a plain int cannot: an auto-sized
|
||||
// parent genuinely has no width to hand down, and a child asking for a fraction of it is
|
||||
// an error rather than a zero-width silence.
|
||||
// Distinguishes "no constraint" from a real zero, which a plain int cannot: an
|
||||
// auto-sized parent genuinely has no width to hand down, and a child asking for
|
||||
// a fraction of it is an error rather than a zero-width silence.
|
||||
constexpr int UNKNOWN = INT32_MIN;
|
||||
|
||||
enum Type : uint8_t { BOX, TEXT, BUTTON, CUSTOM };
|
||||
|
||||
enum Flag : uint8_t {
|
||||
ROW = 1 << 0, // main axis is horizontal
|
||||
CAPTURE = 1 << 1, // swallows the taps its children missed
|
||||
INTERACTIVE = 1 << 2, // has an on_press
|
||||
ROW = 1 << 0, // main axis is horizontal
|
||||
CAPTURE = 1 << 1, // swallows the taps its children missed
|
||||
INTERACTIVE = 1 << 2, // has an on_press
|
||||
DIRTY = 1 << 3,
|
||||
PRESSED = 1 << 4,
|
||||
};
|
||||
@@ -40,14 +41,15 @@ enum Flag : uint8_t {
|
||||
enum SizeMode : uint8_t { AUTO, PX, FRACTION, FILL };
|
||||
enum Align : uint8_t { START, CENTER, END, BETWEEN };
|
||||
|
||||
// Which roles a style states for itself. Anything unset is answered by the nearest
|
||||
// ancestor that does state it, so styling stays in one place and a node that names no
|
||||
// colours costs no bytes at all.
|
||||
// Which roles a style states for itself. Anything unset is answered by the
|
||||
// nearest ancestor that does state it, so styling stays in one place and a node
|
||||
// that names no colours costs no bytes at all.
|
||||
enum StyleField : uint16_t {
|
||||
S_COLOR = 1 << 0,
|
||||
// The background a node offers its descendants to draw text on, which is not the same
|
||||
// as the fill it paints: a dialog layer hands the lit palette down while painting
|
||||
// nothing itself. What is physically behind a node is derived at paint time, never set.
|
||||
// The background a node offers its descendants to draw text on, which is not
|
||||
// the same as the fill it paints: a dialog layer hands the lit palette down
|
||||
// while painting nothing itself. What is physically behind a node is derived
|
||||
// at paint time, never set.
|
||||
S_BG = 1 << 1,
|
||||
S_FILL = 1 << 2,
|
||||
S_BORDER = 1 << 3,
|
||||
@@ -76,8 +78,8 @@ struct Style {
|
||||
uint16_t set = 0;
|
||||
};
|
||||
|
||||
// FRACTION is per-mille rather than a float: 0.85 of 320 is 272 either way, and the
|
||||
// firmware has no business rounding differently to the host.
|
||||
// FRACTION is per-mille rather than a float: 0.85 of 320 is 272 either way, and
|
||||
// the firmware has no business rounding differently to the host.
|
||||
struct Size {
|
||||
SizeMode mode = AUTO;
|
||||
int16_t value = 0;
|
||||
@@ -93,8 +95,8 @@ struct Size {
|
||||
static Size fill() { return make(FILL, 0); }
|
||||
};
|
||||
|
||||
// Persistent. w/h hold the measured size between measure() and place(), and the final
|
||||
// rect afterwards, because the two are never needed at once.
|
||||
// Persistent. w/h hold the measured size between measure() and place(), and the
|
||||
// final rect afterwards, because the two are never needed at once.
|
||||
struct Node {
|
||||
int16_t x = 0, y = 0, w = 0, h = 0;
|
||||
uint16_t first = NONE, next = NONE, parent = NONE;
|
||||
@@ -107,21 +109,23 @@ struct Spec {
|
||||
Size w, h;
|
||||
Size atX, atY;
|
||||
bool absolute = false;
|
||||
int16_t intrinsicW = 0, intrinsicH = 0; // content size of a leaf, e.g. a text run
|
||||
int16_t intrinsicW = 0,
|
||||
intrinsicH = 0; // content size of a leaf, e.g. a text run
|
||||
uint8_t padT = 0, padR = 0, padB = 0, padL = 0;
|
||||
uint8_t gap = 0;
|
||||
Align align = START;
|
||||
Align justify = START;
|
||||
uint16_t last = NONE; // tail of the child list, so append is not a walk
|
||||
uint16_t last = NONE; // tail of the child list, so append is not a walk
|
||||
};
|
||||
|
||||
// Resistive panels land a few pixels off, so a hit box is larger than what was painted.
|
||||
// Resistive panels land a few pixels off, so a hit box is larger than what was
|
||||
// painted.
|
||||
constexpr int SLOP = 4;
|
||||
|
||||
constexpr uint16_t NO_LABEL = 0xFFFF;
|
||||
|
||||
class Tree {
|
||||
public:
|
||||
public:
|
||||
std::vector<Node> nodes;
|
||||
std::vector<Spec> specs;
|
||||
const char* error = nullptr;
|
||||
@@ -137,11 +141,14 @@ class Tree {
|
||||
error = nullptr;
|
||||
}
|
||||
|
||||
uint16_t add(uint16_t parent, const Spec& spec, uint8_t type = BOX, uint8_t flags = 0) {
|
||||
// A tree whose scratch has been dropped cannot be extended: its layout inputs are
|
||||
// gone, so building again is a new screen by definition. Self-healing rather than
|
||||
// advisory, because the alternative is a spec list that no longer indexes the nodes.
|
||||
if (specs.size() != nodes.size()) reset();
|
||||
uint16_t add(uint16_t parent, const Spec& spec, uint8_t type = BOX,
|
||||
uint8_t flags = 0) {
|
||||
// A tree whose scratch has been dropped cannot be extended: its layout
|
||||
// inputs are gone, so building again is a new screen by definition.
|
||||
// Self-healing rather than advisory, because the alternative is a spec list
|
||||
// that no longer indexes the nodes.
|
||||
if (specs.size() != nodes.size())
|
||||
reset();
|
||||
uint16_t id = static_cast<uint16_t>(nodes.size());
|
||||
nodes.push_back(Node());
|
||||
specs.push_back(spec);
|
||||
@@ -149,13 +156,15 @@ class Tree {
|
||||
nodes[id].type = type;
|
||||
nodes[id].flags = flags;
|
||||
nodes[id].parent = parent;
|
||||
if (parent != NONE) attach(parent, id);
|
||||
if (parent != NONE)
|
||||
attach(parent, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
// Lua evaluates inner constructors first, so a child exists before the box that holds
|
||||
// it. Adopting afterwards is what lets each spec table be garbage the moment its node
|
||||
// is created, instead of a whole screen's worth of them living until the end of a build.
|
||||
// Lua evaluates inner constructors first, so a child exists before the box
|
||||
// that holds it. Adopting afterwards is what lets each spec table be garbage
|
||||
// the moment its node is created, instead of a whole screen's worth of them
|
||||
// living until the end of a build.
|
||||
void attach(uint16_t parent, uint16_t child) {
|
||||
nodes[child].parent = parent;
|
||||
uint16_t tail = specs[parent].last;
|
||||
@@ -170,42 +179,49 @@ class Tree {
|
||||
bool layout(uint16_t root, int x, int y, int w, int h) {
|
||||
error = nullptr;
|
||||
measure(root, w, h);
|
||||
if (error) return false;
|
||||
if (error)
|
||||
return false;
|
||||
place(root, x, y, w, h);
|
||||
return error == nullptr;
|
||||
}
|
||||
|
||||
// Deepest interactive node wins, so a tappable child beats its tappable parent. The
|
||||
// list is singly linked, so "last match walking forward" stands in for "first match
|
||||
// walking backward"; they name the same node.
|
||||
// Deepest interactive node wins, so a tappable child beats its tappable
|
||||
// parent. The list is singly linked, so "last match walking forward" stands
|
||||
// in for "first match walking backward"; they name the same node.
|
||||
uint16_t hit(uint16_t id, int px, int py) const {
|
||||
const Node& n = nodes[id];
|
||||
if (px < n.x - SLOP || px >= n.x + n.w + SLOP) return NONE;
|
||||
if (py < n.y - SLOP || py >= n.y + n.h + SLOP) return NONE;
|
||||
if (px < n.x - SLOP || px >= n.x + n.w + SLOP)
|
||||
return NONE;
|
||||
if (py < n.y - SLOP || py >= n.y + n.h + SLOP)
|
||||
return NONE;
|
||||
|
||||
uint16_t found = NONE;
|
||||
for (uint16_t c = n.first; c != NONE; c = nodes[c].next) {
|
||||
uint16_t inner = hit(c, px, py);
|
||||
if (inner != NONE) found = inner;
|
||||
if (inner != NONE)
|
||||
found = inner;
|
||||
}
|
||||
if (found != NONE) return found;
|
||||
if (n.flags & CAPTURE) return id;
|
||||
if (found != NONE)
|
||||
return found;
|
||||
if (n.flags & CAPTURE)
|
||||
return id;
|
||||
return (n.flags & INTERACTIVE) ? id : NONE;
|
||||
}
|
||||
|
||||
// Layout inputs are dead once place() has run. Callers drop them here rather than
|
||||
// carrying ~20 bytes a node for the lifetime of a screen.
|
||||
// Layout inputs are dead once place() has run. Callers drop them here rather
|
||||
// than carrying ~20 bytes a node for the lifetime of a screen.
|
||||
void dropScratch() {
|
||||
specs.clear();
|
||||
specs.shrink_to_fit();
|
||||
}
|
||||
|
||||
// Labels share one NUL-separated arena, so a text node costs two bytes plus its
|
||||
// characters rather than a string object.
|
||||
// Labels share one NUL-separated arena, so a text node costs two bytes plus
|
||||
// its characters rather than a string object.
|
||||
//
|
||||
// ponytail: a longer label appends and abandons the old bytes. The clock repaints
|
||||
// every second at a fixed width, which overwrites in place, so the arena only grows
|
||||
// when a label genuinely gets longer. Compact on rebuild if some app proves otherwise.
|
||||
// ponytail: a longer label appends and abandons the old bytes. The clock
|
||||
// repaints every second at a fixed width, which overwrites in place, so the
|
||||
// arena only grows when a label genuinely gets longer. Compact on rebuild if
|
||||
// some app proves otherwise.
|
||||
void setLabel(uint16_t id, const char* text) {
|
||||
size_t length = strlen(text);
|
||||
uint16_t at = labelAt[id];
|
||||
@@ -223,14 +239,14 @@ class Tree {
|
||||
}
|
||||
|
||||
size_t footprint() const {
|
||||
return nodes.size() * sizeof(Node) + labelAt.size() * sizeof(uint16_t) + labels.size() +
|
||||
styles.size() * sizeof(styles[0]);
|
||||
return nodes.size() * sizeof(Node) + labelAt.size() * sizeof(uint16_t) +
|
||||
labels.size() + styles.size() * sizeof(styles[0]);
|
||||
}
|
||||
|
||||
// Styles are sparse because inheritance means almost every node states nothing: a
|
||||
// screen's root carries the palette and a handful of nodes override one role. Ids are
|
||||
// handed out in increasing order during a build, so appends keep the list sorted and
|
||||
// lookup is a binary search.
|
||||
// Styles are sparse because inheritance means almost every node states
|
||||
// nothing: a screen's root carries the palette and a handful of nodes
|
||||
// override one role. Ids are handed out in increasing order during a build,
|
||||
// so appends keep the list sorted and lookup is a binary search.
|
||||
Style& styleFor(uint16_t id) {
|
||||
size_t low = 0, high = styles.size();
|
||||
while (low < high) {
|
||||
@@ -241,17 +257,21 @@ class Tree {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
if (low < styles.size() && styles[low].first == id) return styles[low].second;
|
||||
return styles.insert(styles.begin() + low, std::make_pair(id, Style()))->second;
|
||||
if (low < styles.size() && styles[low].first == id)
|
||||
return styles[low].second;
|
||||
return styles.insert(styles.begin() + low, std::make_pair(id, Style()))
|
||||
->second;
|
||||
}
|
||||
|
||||
// The nearest style at or above `id` that states `field`, or a default one if nothing
|
||||
// does. Returning the whole style lets a caller read the pair a role comes in.
|
||||
// The nearest style at or above `id` that states `field`, or a default one if
|
||||
// nothing does. Returning the whole style lets a caller read the pair a role
|
||||
// comes in.
|
||||
const Style& inherited(uint16_t id, uint16_t field) const {
|
||||
static const Style fallback;
|
||||
for (uint16_t n = id; n != NONE; n = nodes[n].parent) {
|
||||
const Style* style = styleOf(n);
|
||||
if (style && (style->set & field)) return *style;
|
||||
if (style && (style->set & field))
|
||||
return *style;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
@@ -266,69 +286,78 @@ class Tree {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
return (low < styles.size() && styles[low].first == id) ? &styles[low].second : nullptr;
|
||||
return (low < styles.size() && styles[low].first == id)
|
||||
? &styles[low].second
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
std::vector<uint16_t> labelAt;
|
||||
std::vector<char> labels;
|
||||
std::vector<std::pair<uint16_t, Style> > styles;
|
||||
std::vector<std::pair<uint16_t, Style>> styles;
|
||||
|
||||
void fail(const char* message) {
|
||||
if (!error) error = message;
|
||||
if (!error)
|
||||
error = message;
|
||||
}
|
||||
|
||||
int resolve(Size size, int span) {
|
||||
switch (size.mode) {
|
||||
case AUTO:
|
||||
return UNKNOWN;
|
||||
case PX:
|
||||
return size.value;
|
||||
case FILL:
|
||||
if (span == UNKNOWN) {
|
||||
fail("fill inside an auto-sized parent");
|
||||
return 0;
|
||||
}
|
||||
return span;
|
||||
case FRACTION:
|
||||
if (span == UNKNOWN) {
|
||||
fail("fraction inside an auto-sized parent");
|
||||
return 0;
|
||||
}
|
||||
return span * size.value / 1000;
|
||||
case AUTO:
|
||||
return UNKNOWN;
|
||||
case PX:
|
||||
return size.value;
|
||||
case FILL:
|
||||
if (span == UNKNOWN) {
|
||||
fail("fill inside an auto-sized parent");
|
||||
return 0;
|
||||
}
|
||||
return span;
|
||||
case FRACTION:
|
||||
if (span == UNKNOWN) {
|
||||
fail("fraction inside an auto-sized parent");
|
||||
return 0;
|
||||
}
|
||||
return span * size.value / 1000;
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
void measure(uint16_t id, int availW, int availH) {
|
||||
// By value, and re-index after recursion: measuring a child can push nodes, and a
|
||||
// vector that grows moves every reference taken before it.
|
||||
// By value, and re-index after recursion: measuring a child can push nodes,
|
||||
// and a vector that grows moves every reference taken before it.
|
||||
const Spec s = specs[id];
|
||||
const bool row = (nodes[id].flags & ROW) != 0;
|
||||
int w = resolve(s.w, availW);
|
||||
int h = resolve(s.h, availH);
|
||||
|
||||
int innerW = w != UNKNOWN ? w - s.padL - s.padR
|
||||
: (availW != UNKNOWN ? availW - s.padL - s.padR : UNKNOWN);
|
||||
// Height is only handed down when this node was given one. A parent sized by its
|
||||
// content cannot tell a child what fraction of it to take.
|
||||
int innerW = w != UNKNOWN
|
||||
? w - s.padL - s.padR
|
||||
: (availW != UNKNOWN ? availW - s.padL - s.padR : UNKNOWN);
|
||||
// Height is only handed down when this node was given one. A parent sized
|
||||
// by its content cannot tell a child what fraction of it to take.
|
||||
int innerH = h != UNKNOWN ? h - s.padT - s.padB : UNKNOWN;
|
||||
|
||||
int main = 0, cross = 0, count = 0;
|
||||
for (uint16_t c = nodes[id].first; c != NONE; c = nodes[c].next) {
|
||||
measure(c, innerW, innerH);
|
||||
// Absolutely placed children are measured, because they still need a size, but they
|
||||
// take no part in the flow their siblings share. The Lua original counted them here
|
||||
// and excluded them in place(); nothing depended on the disagreement.
|
||||
if (specs[c].absolute) continue;
|
||||
// Absolutely placed children are measured, because they still need a
|
||||
// size, but they take no part in the flow their siblings share. The Lua
|
||||
// original counted them here and excluded them in place(); nothing
|
||||
// depended on the disagreement.
|
||||
if (specs[c].absolute)
|
||||
continue;
|
||||
const Node& child = nodes[c];
|
||||
if (count) main += s.gap;
|
||||
if (count)
|
||||
main += s.gap;
|
||||
if (row) {
|
||||
main += child.w;
|
||||
if (child.h > cross) cross = child.h;
|
||||
if (child.h > cross)
|
||||
cross = child.h;
|
||||
} else {
|
||||
main += child.h;
|
||||
if (child.w > cross) cross = child.w;
|
||||
if (child.w > cross)
|
||||
cross = child.w;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
@@ -364,17 +393,20 @@ class Tree {
|
||||
int cx = x + s.padL, cy = y + s.padT;
|
||||
int cw = w - s.padL - s.padR, ch = h - s.padT - s.padB;
|
||||
|
||||
// Main-axis distribution, CSS justify-content minus the modes nothing asks for.
|
||||
// Absolutely placed children take no part in the flow.
|
||||
// Main-axis distribution, CSS justify-content minus the modes nothing asks
|
||||
// for. Absolutely placed children take no part in the flow.
|
||||
int flowing = 0, used = 0;
|
||||
for (uint16_t c = nodes[id].first; c != NONE; c = nodes[c].next) {
|
||||
if (specs[c].absolute) continue;
|
||||
if (specs[c].absolute)
|
||||
continue;
|
||||
flowing++;
|
||||
used += row ? nodes[c].w : nodes[c].h;
|
||||
}
|
||||
if (flowing > 1) used += (flowing - 1) * s.gap;
|
||||
if (flowing > 1)
|
||||
used += (flowing - 1) * s.gap;
|
||||
int slack = (row ? cw : ch) - used;
|
||||
if (slack < 0) slack = 0;
|
||||
if (slack < 0)
|
||||
slack = 0;
|
||||
|
||||
int offset = 0, spread = 0;
|
||||
if (s.justify == END) {
|
||||
@@ -388,11 +420,14 @@ class Tree {
|
||||
for (uint16_t c = nodes[id].first; c != NONE; c = nodes[c].next) {
|
||||
const Spec cs = specs[c];
|
||||
int childW = nodes[c].w, childH = nodes[c].h;
|
||||
// Cross axis fills the parent unless the child asked for a size, like CSS blocks.
|
||||
// Cross axis fills the parent unless the child asked for a size, like CSS
|
||||
// blocks.
|
||||
if (row) {
|
||||
if (cs.h.mode == AUTO) childH = ch;
|
||||
if (cs.h.mode == AUTO)
|
||||
childH = ch;
|
||||
} else {
|
||||
if (cs.w.mode == AUTO) childW = cw;
|
||||
if (cs.w.mode == AUTO)
|
||||
childW = cw;
|
||||
}
|
||||
|
||||
int px, py;
|
||||
@@ -423,5 +458,5 @@ class Tree {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ui
|
||||
} // namespace esp32lua
|
||||
} // namespace ui
|
||||
} // namespace esp32lua
|
||||
|
||||
Reference in New Issue
Block a user