f7c5cc09ba
A node was a Lua table of ~625 bytes, of which 21 keys pushed it over a
power-of-two hash boundary and eight were style copies inheritance had
splattered down from its parent. A 400 node screen cost ~250 KB and could not
coexist with wifi's buffers.
The tree now lives in src/ui/layout.h as a 16 byte struct in a flat arena, and
splits by lifetime: Node holds what hit testing and repainting need forever,
Spec holds what only measure/place read and is dropped when layout ends. Style
is sparse and resolved by walking parents, so a node naming no colours costs
nothing. Re-layout rebuilds from Lua rather than retaining the inputs.
401 nodes: 8218 B steady, 21050 B peak
Lua: ~250000 B steady
sdcard/lib/ui.lua stays the toolkit and keeps every constructor signature, but
returns integer handles: 627 lines to 374. Composition, the palette and custom
painters are still Lua on the SD card; only primitives now need a reflash.
BREAKING CHANGE: ui constructors return handles, not tables. Use
ui.setText(id, text) and keep per-node app data in a table keyed by id.
109 lines
4.1 KiB
Lua
109 lines
4.1 KiB
Lua
-- Run: lua test/settings_calibration.lua
|
|
-- Drives the settings app on a desktop Lua, through the shared fake device. Controls are
|
|
-- pressed by label rather than by coordinate: where a row lands is layout, and layout is
|
|
-- asserted in test/ui_layout_test.cpp against the C++ the panel actually runs.
|
|
package.path = "sdcard/lib/?.lua;test/?.lua;" .. package.path
|
|
|
|
local device = require("fake_device").install()
|
|
|
|
dofile("sdcard/apps/Settings/main.lua")
|
|
|
|
local tapRow = device.tap
|
|
|
|
-- A perfectly linear panel spanning raw 200..3800 over 320x480 must round-trip
|
|
-- to those same extremes from the two inset samples.
|
|
local inset, w, h = 30, 320, 480
|
|
local function raw(pixel, size) return 200 + (3800 - 200) * pixel / size end
|
|
local s1 = {x = raw(inset, w), y = raw(inset, h)}
|
|
local s2 = {x = raw(w - inset, w), y = raw(h - inset, h)}
|
|
|
|
local x0, y0, x1, y1 = computeCalibration(s1, s2, w, h, inset)
|
|
assert(math.abs(x0 - 200) <= 1, "x0 " .. x0)
|
|
assert(math.abs(y0 - 200) <= 1, "y0 " .. y0)
|
|
assert(math.abs(x1 - 3800) <= 1, "x1 " .. x1)
|
|
assert(math.abs(y1 - 3800) <= 1, "y1 " .. y1)
|
|
|
|
-- A flipped panel (raw decreasing with pixel) must yield a descending range.
|
|
local s3 = {x = 3800 - raw(inset, w) + 200, y = s1.y}
|
|
local s4 = {x = 3800 - raw(w - inset, w) + 200, y = s2.y}
|
|
local fx0, _, fx1 = computeCalibration(s3, s4, w, h, inset)
|
|
assert(fx0 > fx1, "flipped axis should descend")
|
|
|
|
-- Timezone cycles through the picker list.
|
|
local zones = require("timezones")
|
|
init()
|
|
tapRow("Timezone")
|
|
assert(settings.getTimezone() == zones[2].tz, "timezone " .. settings.getTimezone())
|
|
-- The card's value is replaced in place rather than by rebuilding the screen, so the
|
|
-- other four cards keep the nodes they already had.
|
|
local beforeCycle = node.getCount()
|
|
assert(device.labelled(zones[2].name), "timezone card did not take its new value")
|
|
assert(node.getCount() == beforeCycle, "cycling the timezone rebuilt the screen")
|
|
tapRow("Timezone")
|
|
assert(settings.getTimezone() == zones[3].tz, "timezone " .. settings.getTimezone())
|
|
|
|
-- Rotation cycles through the four quarter turns and wraps back to 0.
|
|
init()
|
|
for _, expected in ipairs({90, 180, 270, 0}) do
|
|
tapRow("Rotation")
|
|
assert(settings.getRotation() == expected, "rotation " .. settings.getRotation())
|
|
end
|
|
|
|
-- Calibration collects one sample per target and saves on the second release.
|
|
init()
|
|
tapRow("Calibrate")
|
|
on_tick() -- release after the menu tap arms sampling
|
|
device.raw = {s1.x, s1.y}
|
|
on_tick()
|
|
device.raw = nil
|
|
on_tick()
|
|
device.raw = {s2.x, s2.y}
|
|
on_tick()
|
|
device.raw = nil
|
|
on_tick()
|
|
local saved = device.calibration
|
|
assert(saved, "calibration was not saved")
|
|
assert(math.abs(saved[1] - 200) <= 1, "saved x0 " .. saved[1])
|
|
|
|
-- Open networks connect directly from scan results.
|
|
init()
|
|
device.networks = {{ssid = "qemu", rssi = -25, secure = false}}
|
|
tapRow("WiFi")
|
|
tapRow("scan networks")
|
|
on_tick()
|
|
tapRow("qemu")
|
|
assert(device.connected, "open network should connect without a keyboard")
|
|
assert(device.connected[1] == "qemu" and device.connected[2] == "", "open wifi connect")
|
|
|
|
-- Secure networks route through the keyboard and preserve typed punctuation.
|
|
init()
|
|
device.networks = {{ssid = "secure", rssi = -40, secure = true}}
|
|
tapRow("WiFi")
|
|
tapRow("scan networks")
|
|
on_tick()
|
|
tapRow("secure")
|
|
on_tick()
|
|
|
|
-- The keyboard is one custom node with no child per key, so its keys are reached through
|
|
-- its own geometry rather than by label. getRect() answers the same box to the test and
|
|
-- to the widget, which is what makes the two agree on where "q" is.
|
|
local keyboard = require("keyboard")
|
|
local board = device.findKind("custom")
|
|
assert(board, "the secure network did not open a keyboard")
|
|
local boardRect = {x = 0, y = 0, w = 10000, h = 10000}
|
|
|
|
local function typeKey(label)
|
|
local pressed
|
|
keyboard.eachKey("lower", boardRect, function(_, keyLabel, x, y, width)
|
|
if keyLabel == label and not pressed then pressed = {x = x + width // 2, y = y + 15} end
|
|
end)
|
|
assert(pressed, "no key labelled " .. label)
|
|
device.press(board, pressed.x, pressed.y)
|
|
end
|
|
|
|
for key in ("qemuqemu"):gmatch(".") do typeKey(key) end
|
|
typeKey("OK")
|
|
assert(device.connected[1] == "secure" and device.connected[2] == "qemuqemu", "secure wifi password")
|
|
|
|
print("ok")
|