-- A tour of what a Lua app has: run it with `c8-lua demo.lua`.
-- PC settings' layout (c8.pages): a list of pages beside the chosen one,
-- one at a time when the window is narrow.
local c8 = require "c8"
local win, pages
local notes = c8.load("lua-demo") or { "Milk", "Stamps", "Call the dentist" }
local big = false
local function controls(page)
local said = c8.label { text = "Nothing pressed yet", color = "secondary" }
page:add(
c8.row { spacing = 12,
c8.button { label = "Say hello", primary = true,
on_click = function() said:set("Hello from Lua") end },
c8.button { label = "Ask", on_click = function()
win:dialog { title = "A question", message = "Is this easy?",
buttons = { "Yes", "No" },
done = function(choice)
said:set(choice == 1 and "Good" or "Tell me why")
end }
end },
},
said,
c8.toggle { header = "Wi-Fi", on = true,
on_change = function(_, on) said:set(on and "On" or "Off") end },
c8.slider { header = "Volume", min = 0, max = 100, value = 40,
digits = 0, unit = " %",
on_change = function(_, value) said:set("Volume " .. value) end },
c8.combo { header = "Colour", items = { "Red", "Green", "Blue" },
on_change = function(_, i) said:set("Colour " .. i) end },
c8.entry { header = "Name", placeholder = "Type and press Enter",
on_enter = function(_, text) said:set("Hello, " .. text) end }
)
end
local function list(page)
local rows
rows = c8.list {
count = #notes,
item = function(i)
return { title = notes[i], subtitle = "Note " .. i,
icon = "text-x-generic" }
end,
on_open = function(_, i)
win:dialog { title = notes[i], message = "Delete this note?",
buttons = { "Delete", "Keep" },
done = function(choice)
if choice == 1 then
table.remove(notes, i)
c8.save("lua-demo", notes)
rows:count(#notes)
end
end }
end,
}
page:add(
c8.entry { placeholder = "Add a note", enter_only = true,
on_enter = function(entry, text)
if text ~= "" then
notes[#notes + 1] = text
c8.save("lua-demo", notes)
rows:count(#notes)
entry:set("")
end
end },
c8.scroll { rows, expand = true, fill = true }
)
end
local function drawing(page)
local t = 0
local canvas
canvas = c8.canvas { height = 220, fill = true,
draw = function(g, x, y, w, h)
g:rect(x, y, w, h, g.accent, 0.12)
for i = 0, 11 do
local a = t + i * math.pi / 6
g:circle(x + w / 2 + math.cos(a) * 70,
y + h / 2 + math.sin(a) * 70, 6 + i, g.accent, 0.3 + i / 20)
end
g:text("Drawn in Lua", x + 16, y + 12, { style = "title" })
end }
local timer = c8.every(0.05, function()
t = t + 0.05
canvas:redraw()
end)
canvas.on_destroy = function() timer:cancel() end
page:add(canvas, c8.label {
text = "A canvas draws with g: rectangles, circles, lines, text, "
.. "icons and pictures.", color = "secondary" })
end
local function network(page)
local out = c8.label { text = "", lines = 12 }
page:add(
c8.button { label = "Run uname", on_click = function()
c8.run({ "uname", "-sr" }, function(status, text)
out:set(status == 0 and text or "It didn't run")
end)
end },
out
)
end
local builders = { controls, list, drawing, network }
c8.app {
id = "org.cont8nuum.LuaDemo",
open = function()
if not (win and win.open) then
pages = c8.pages { title = "Lua demo",
pages = { "Controls", "A list", "Drawing", "Commands" },
build = function(content, index) builders[index](content) end }
-- The pages go one at a time by themselves when narrow
win = c8.window { title = "Lua demo", pages,
on_key = function(_, ev)
return ev.key == "Escape" and pages:back()
end }
end
win:present()
end,
search = function(query)
local found = {}
for i, note in ipairs(notes) do
if query ~= "" and note:lower():find(query:lower(), 1, true) then
found[#found + 1] = { id = tostring(i), title = note }
end
end
return found
end,
result = function() pages:set(2) end,
settings = {
{ id = "options", label = "Options", build = function(page)
page:toggle { id = "big", label = "Big text", on = big,
on_change = function(on) big = on end }
end },
},
}