build: add a format target and one shared editor config

The Lua modules were split between tabs and spaces because nothing pinned a
style; .editorconfig is what lua-language-server reads on save, so the editor
and the tree now agree. clang-format already had a config and left native/
unchanged. Embedded modules regenerate from the reformatted ui.lua.
This commit is contained in:
2026-08-04 13:19:58 -04:00
parent bf286eefa6
commit 291f0f20b5
8 changed files with 499 additions and 322 deletions
+100 -49
View File
@@ -6,7 +6,9 @@ local cleared, invalidated = nil, {}
fs = {
readFile = function(path, maxBytes)
local value = files[path]
if value and #value > maxBytes then return nil, "too large" end
if value and #value > maxBytes then
return nil, "too large"
end
return value, value and nil or "missing"
end,
writeFile = function(path, value)
@@ -24,12 +26,24 @@ gui = {
FONT_LARGE = 3,
STYLE_NORMAL = 0,
STYLE_BOLD = 1,
color = function(r, g, b) return r * 65536 + g * 256 + b end,
getWidth = function() return frameWidth end,
getHeight = function() return frameHeight end,
getTextWidth = function(_, text) return #text * 6 end,
getFontHeight = function() return 8 end,
clear = function(color) cleared = color end,
color = function(r, g, b)
return r * 65536 + g * 256 + b
end,
getWidth = function()
return frameWidth
end,
getHeight = function()
return frameHeight
end,
getTextWidth = function(_, text)
return #text * 6
end,
getFontHeight = function()
return 8
end,
clear = function(color)
cleared = color
end,
}
local nodes, focus, painter = {}, nil, nil
@@ -38,7 +52,9 @@ local buttonCount = 0
local function interactiveNodes()
local result = {}
for id, entry in ipairs(nodes) do
if entry.interactive then result[#result + 1] = id end
if entry.interactive then
result[#result + 1] = id
end
end
return result
end
@@ -59,18 +75,24 @@ node = {
type = spec.type,
label = spec.label,
interactive = spec.interactive,
rect = {x, 0, 90, 50},
rect = { x, 0, 90, 50 },
pressed = false,
style = {},
}
return id
end,
attach = function(parent, child) nodes[child].parent = parent end,
attach = function(parent, child)
nodes[child].parent = parent
end,
setSize = function() end,
setStyle = function(id, style)
for key, value in pairs(style) do nodes[id].style[key] = value end
for key, value in pairs(style) do
nodes[id].style[key] = value
end
end,
layout = function()
return true
end,
layout = function() return true end,
dropScratch = function() end,
hit = function(_, x, y)
for _, id in ipairs(interactiveNodes()) do
@@ -80,17 +102,33 @@ node = {
end
end
end,
getRect = function(id) return table.unpack(nodes[id].rect) end,
setLabel = function(id, text) nodes[id].label = text end,
getLabel = function(id) return nodes[id].label end,
invalidate = function(id) invalidated[#invalidated + 1] = id end,
setPressed = function(id, pressed) nodes[id].pressed = pressed end,
isPressed = function(id) return nodes[id].pressed end,
setPainter = function(callback) painter = callback end,
getRect = function(id)
return table.unpack(nodes[id].rect)
end,
setLabel = function(id, text)
nodes[id].label = text
end,
getLabel = function(id)
return nodes[id].label
end,
invalidate = function(id)
invalidated[#invalidated + 1] = id
end,
setPressed = function(id, pressed)
nodes[id].pressed = pressed
end,
isPressed = function(id)
return nodes[id].pressed
end,
setPainter = function(callback)
painter = callback
end,
draw = function()
if painter then
for id, entry in ipairs(nodes) do
if entry.type == "custom" then painter(id, table.unpack(entry.rect)) end
if entry.type == "custom" then
painter(id, table.unpack(entry.rect))
end
end
end
end,
@@ -98,49 +136,58 @@ node = {
focus = interactiveNodes()[1]
return focus
end,
setFocus = function(id) focus = id end,
getFocus = function() return focus end,
setFocus = function(id)
focus = id
end,
getFocus = function()
return focus
end,
moveFocus = function(_, direction)
local items, current = interactiveNodes(), nil
for index, id in ipairs(items) do
if id == focus then current = index break end
if id == focus then
current = index
break
end
end
local step = (direction == "right" or direction == "down") and 1 or -1
local nextIndex = current and current + step or 1
if items[nextIndex] then focus = items[nextIndex] end
if items[nextIndex] then
focus = items[nextIndex]
end
return focus
end,
}
local ui = require("ui")
local ui = require "ui"
assert(ui.getTheme() == "light")
assert(table.concat(ui.themeNames(), ",") == "dark,light,mono")
local ok, err = ui.setTheme("missing")
local ok, err = ui.setTheme "missing"
assert(ok == nil and err == "Unknown theme")
assert(ui.setTheme("dark") == true)
assert(ui.setTheme "dark" == true)
assert(files["/.lua/theme"] == "dark" and ui.getTheme() == "dark")
local events = {}
local function handler(name)
return function(id, x, y)
events[#events + 1] = {name, id, x, y}
events[#events + 1] = { name, id, x, y }
end
end
local first = ui.button{
local first = ui.button {
label = "one",
on_enter = handler("enter"),
on_exit = handler("exit"),
on_click = handler("click"),
on_enter = handler "enter",
on_exit = handler "exit",
on_click = handler "click",
}
local second = ui.button{
local second = ui.button {
label = "two",
on_enter = handler("enter"),
on_exit = handler("exit"),
on_click = handler("click"),
on_enter = handler "enter",
on_exit = handler "exit",
on_click = handler "click",
}
local screen = ui.screen(ui.box{row = true, first, second})
local screen = ui.screen(ui.box { row = true, first, second })
assert(screen:down(10, 10))
assert(node.isPressed(first))
@@ -151,7 +198,7 @@ assert(node.isPressed(first))
assert(screen:up(10, 10))
assert(not node.isPressed(first))
local expectedTouch = {"enter", "exit", "enter", "exit", "click"}
local expectedTouch = { "enter", "exit", "enter", "exit", "click" }
for index, name in ipairs(expectedTouch) do
local event = events[index]
assert(event and event[1] == name and event[2] == first)
@@ -172,10 +219,10 @@ assert(not node.isPressed(second))
assert(screen:button("back", true) == false)
local expectedButtons = {
{"enter", first},
{"exit", first},
{"enter", second},
{"click", second},
{ "enter", first },
{ "exit", first },
{ "enter", second },
{ "click", second },
}
for index, expected in ipairs(expectedButtons) do
local event = events[index]
@@ -184,7 +231,7 @@ for index, expected in ipairs(expectedButtons) do
end
local before = #invalidated
assert(ui.setTheme("mono") == true)
assert(ui.setTheme "mono" == true)
assert(ui.getTheme() == "mono" and #invalidated == before + 1)
assert(cleared == ui.theme.background)
@@ -204,12 +251,16 @@ frameWidth, frameHeight = 320, 480
-- still is. Which node was hit is geometry, so the test names the target directly.
ui.reset()
local pressedCalls = {}
node.setPressed = function(_, on) pressedCalls[#pressedCalls + 1] = on end
local own = ui.custom{h = 20, press_style = false, on_click = function() end}
local styled = ui.button{h = 20, label = "ok", on_click = function() end}
local board = ui.screen(ui.box{own, styled})
node.setPressed = function(_, on)
pressedCalls[#pressedCalls + 1] = on
end
local own = ui.custom { h = 20, press_style = false, on_click = function() end }
local styled = ui.button { h = 20, label = "ok", on_click = function() end }
local board = ui.screen(ui.box { own, styled })
local target
node.hit = function() return target end
node.hit = function()
return target
end
target = own
board:down(0, 0)
@@ -220,4 +271,4 @@ target = styled
board:down(0, 0)
assert(pressedCalls[1] == true, "an ordinary widget still gets its pressed style")
print("ok")
print "ok"