ludic/packages/ludic.ui/lib.ludic

83 lines
3.2 KiB
Text

# lib.ludic - template files. A file's <component>s are its own unless marked export="true";
# <import src="kit.xml"/> brings a file's exported components in by their names, and
# <import src="kit.xml" as="kit"/> as <kit:Name>, so a component library is a file of them and two
# libraries never collide. A file's <style>s, and its imports' before them, style what it writes. A <screen> is the program's, by name, from whichever file has it.
export property UiFile {
path: string = ""
names: []string = null
comps: []UiTpl = null
exported: []bool = null
imp_as: []string = null
imp: []UiFile = null
own: []UiRule = null
sheet: UiSheet = null
src: string = "" # the text it was read from
disk: bool = false # read from a file (ui_reload looks again), or handed over as text
}
# the file a path names, read once
export function ui_load(ui_st: mut UiState, path: string) -> bool { return lib_file(ui_st, path) != null }
function lib_file(ui_st: mut UiState, path: string) -> UiFile {
for i in 0 .. len(ui_st.lib_files) {
if ui_st.lib_files[i].path == path { return ui_st.lib_files[i] }
}
let src = lib_text(ui_st, path)
if len(src) == 0 {
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
ui_err(ui_st, `{path}: cannot read it`)
return null
}
var f: UiFile = null
if lss_is(path) { f = lss_file(ui_st, src, path) } else { f = lib_read(ui_st, src, path) }
f.disk = true
return f
}
# a file's text, as if read from `path` (its imports are found beside it)
export function ui_load_text(ui_st: mut UiState, src: string, path: string) -> void {
if lss_is(path) { lss_file(ui_st, src, path) } else { lib_read(ui_st, src, path) }
}
function lib_read(ui_st: mut UiState, src: string, path: string) -> UiFile {
let f = lib_new(ui_st, path)
f.src = src
tp_lines(ui_st, src)
let root = tpl_read(ui_st, Xml.parse(src), path)
var tops = root.kids
if root.tag != "ui" { tops = [root] }
for i in 0 .. len(tops) { lib_top(ui_st, f, tops[i]) }
f.sheet = sheet_of(f)
for i in 0 .. len(f.comps) { lib_link(ui_st, f, f.comps[i]) }
for i in 0 .. len(tops) {
if tops[i].tag == "screen" { lib_link(ui_st, f, tops[i]) }
}
return f
}
function lib_top(ui_st: mut UiState, f: UiFile, t: UiTpl) -> void {
if t.tag == "style" {
sheet_read(ui_st, f, t)
return
}
if t.tag == "import" {
let other = lib_file(ui_st, lib_beside(f.path, bd_name(t, "src", "")))
if other != null {
push(f.imp, other)
push(f.imp_as, bd_name(t, "as", ""))
}
return
}
let name = bd_name(t, "name", "")
if name == "" or (t.tag != "component" and t.tag != "screen") {
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
ui_err(ui_st, `{f.path}: <{t.tag}> - a file holds <import>s, <style>s, <component name=...>s and <screen name=...>s`)
return
}
if t.tag == "screen" {
let at = lib_find(ui_st.lib_screen_names, name)
if at >= 0 { ui_st.lib_screens[at] = t } else {
push(ui_st.lib_screen_names, name)
push(ui_st.lib_screens, t)
}
return
}
push(f.names, name)
push(f.comps, t)
push(f.exported, bd_name(t, "export", "") == "true")
}