ludic/packages/ludic.ui/style.ludic
Orkuncakilkaya 49b5e80af5 ludic ui-preview: ludic.ui components previewed for a studio over stdio (R5)
A prebuilt tool, bin/ludic-ui-preview (built by ludic-dev build, shipped beside ludic-lsp, run as
`ludic ui-preview [--font DIR]`): ludic.ui with a backend that records every draw call as a line,
and mock component classes the studio describes (load / model / calls). frame t runs ui_show at
interface time t; text is measured on the CPU with the game's font.json metrics; locale goes
through ludic.i18n; tree / box / rules inspect the frame. No game code, no GPU, no network. The wire
protocol is frozen as tools/ui-preview/protocol-v1.md; smoke.txt is a transcript to run.

ludic.ui gains, all additive: UiClass.make_of, UiBackend.said / emitted, UiRule.text / at (with
comment line breaks kept so rule lines count true, and a class's styles read under its .lss path),
and ui_root, ui_find, ui_rules_of, ui_rule_value, ui_building, ui_class, ui_class_load,
ui_file_forget, ui_errors_clear; ui_nine_cuts_into exported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 19:53:26 +03:00

161 lines
5.7 KiB
Text

# style.ludic - stylesheets: `.chip { bg: bg2; pad: 6 } button { h: 40 }` in a <style>, or an .lss
# file brought in by <import src="look.lss"/> (which may `@import "base.lss";` in turn). A selector is
# a tag, a .class, both, or *; a property is any attribute, {holes} and all. The more specific rule
# wins, then the later; an attribute beats them all.
export property UiRule {
sel: UiSel = null
spec: int = 0
md: UiMedia = null # the @media it sits in, if any
keys: []string = null
vals: []UiExpr = null
text: string = "" # its selector as written, and where: "file.lss:12" (for an inspector)
at: string = ""
}
export property UiSheet {
rules: []UiRule = null
}
function sheet_read(ui_st: mut UiState, f: UiFile, t: UiTpl) -> void {
var src = t.raw
let from = bd_name(t, "src", "")
if from != "" { src = lib_text(ui_st, lib_beside(f.path, from)) }
lss_text(ui_st, f, src)
}
function lss_is(path: string) -> bool { return len(path) > 4 and sat(path, len(path) - 4, ".lss") }
# a stylesheet file: a file with rules and imports and nothing else
function lss_file(ui_st: mut UiState, src: string, path: string) -> UiFile {
let f = lib_new(ui_st, path)
f.src = src
lss_text(ui_st, f, src)
f.sheet = sheet_of(f)
return f
}
@alloc_ok("a stylesheet being read: once per component class (uc_load)")
function lss_text(ui_st: mut UiState, f: UiFile, src: string) -> void {
let err = new []string
let body = lss_strip(src) # a copy of its own, unless src had no comment (then src itself)
lss_block(ui_st, f, body, null, err, 1)
if lss_has_comment(src) { # else lss_strip handed src itself back, the caller's to keep
unsafe { free(body) } # every rule, selector and value read from it is a copy
}
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
for i in 0 .. len(err) { ui_err(ui_st, `{f.path}: <style> {err[i]}`) }
unsafe { free(err) }
}
# rules, @imports and @media blocks; the rules in a @media block carry its condition. `line` is the
# line src starts on, so each rule knows where it was written (a host's inspector asks)
function lss_block(ui_st: mut UiState, f: UiFile, src: string, md: UiMedia, err: []string, line0: int) -> void {
let r = new UiRd
r.s = src
var line = line0
var seen = 0
while r.err == "" {
rd_ws(r)
if r.i >= len(r.s) { break }
line = line + lss_lines(src, seen, r.i)
seen = r.i
if rd_op(r, "@import") {
lss_import(ui_st, f, lss_upto(r, 59))
continue
}
if rd_op(r, "@keyframes") {
let name = lss_upto(r, 123) # kept: the keyframes' name may be this very text
let kb = lss_upto(r, 125)
an_read(ui_st, name, kb, err)
unsafe { free(kb) }
continue
}
if rd_op(r, "@media") {
let cond = md_parse(lss_upto(r, 123))
let mline = line + lss_lines(src, seen, r.i)
let mb = lss_upto(r, 125)
lss_block(ui_st, f, mb, cond, err, mline)
unsafe { free(mb) }
continue
}
let sels = lss_upto(r, 123)
let body = lss_upto(r, 125)
let at = len(f.own)
lss_rule(f, sels, body, err, line + lss_lead(sels))
for i in at .. len(f.own) { f.own[i].md = md }
unsafe { # a rule keeps what it parsed, never the text it was sliced from
free(sels)
free(body)
}
}
if r.err != "" { push(err, r.err) }
unsafe { free(r) } # the reader only: its text is the caller's
}
# the line breaks in src between two offsets
function lss_lines(src: string, from: int, to: int) -> int {
var n = 0
for i in from .. to {
if src[i] == 10 { n += 1 }
}
return n
}
# the line breaks before a selector's first word (rd_ws stops at the first non-space)
function lss_lead(s: string) -> int {
var n = 0
for i in 0 .. len(s) {
if s[i] == 10 { n += 1 } else if s[i] != 32 and s[i] != 9 and s[i] != 13 { return n }
}
return n
}
# @import "base.lss"; - its rules come before this file's own
function lss_import(ui_st: mut UiState, f: UiFile, what: string) -> void {
var p = tpl_words(what)
if len(p) > 5 and sat(p, 0, "url(") { p = p[4..len(p) - 1] }
if len(p) >= 2 and (p[0] == 34 or p[0] == 39) { p = p[1..len(p) - 1] }
let other = lib_file(ui_st, lib_beside(f.path, p))
if other != null {
push(f.imp, other)
push(f.imp_as, "")
}
}
# the text up to the character `c`, which is taken too; a {hole} inside a body is skipped whole
function lss_upto(r: UiRd, c: int) -> string {
let at = r.i
var depth = 0
while r.i < len(r.s) and (r.s[r.i] != c or depth > 0) {
if c == 125 and r.s[r.i] == 123 { depth += 1 }
if c == 125 and r.s[r.i] == 125 { depth -= 1 }
r.i += 1
}
let out: string = r.s[at..r.i]
if r.i < len(r.s) { r.i += 1 } else { rd_fail(r, "a closing brace") }
return out
}
function lss_strip(src: string) -> string {
if not lss_has_comment(src) { return src } # no comment: nothing to strip, nothing made
# written once into bytes of its own (lss_text frees them): joined a piece at a time, every
# comment left the text so far behind
var p: pointer = null
unsafe { p = bytes(len(src) + 1) }
var n = 0
var i = 0
while i < len(src) {
if i + 1 < len(src) and src[i] == 47 and src[i + 1] == 42 {
var j = i + 2
while j + 1 < len(src) and not (src[j] == 42 and src[j + 1] == 47) {
if src[j] == 10 { # a comment's line breaks stay, so a rule's line is its own
unsafe { p[n] = 10 }
n += 1
}
j += 1
}
i = j + 2
} else {
unsafe { p[n] = src[i] }
n += 1
i += 1
}
}
unsafe { p[n] = 0 }
return p
}
function lss_has_comment(src: string) -> bool {
for i in 0 .. len(src) - 1 {
if src[i] == 47 and src[i + 1] == 42 { return true }
}
return false
}