Views
hive.ui builds an interface as a value: a View is a tree of widgets. Each widget takes its attributes first and then what it shows, except spacer() and none(), which take nothing, and spinner(attrs), which takes only attributes.
- Layout:
row(attrs, views),column(attrs, views), andoverlay(attrs, child), which is a centred, dimmed dialog, or, with analignorjustify, a panel pinned to an edge that lets clicks through. - Display:
text(attrs, content),image(attrs, src, alt),icon(attrs, Icon),link(attrs, href, label),table(attrs, rows)for aTable, andcanvasfor drawing, on its own slide. - Input:
button(attrs, label),input(attrs, value),textarea(attrs, value),checkbox(attrs, label, checked),select(attrs, options, chosen).
Attributes are a closed set too, and one that does not apply to a widget is ignored:
- layout:
gap,pad,width(capped at the space available),height,grow,align,justify,scroll; - style:
size,heading,tone,background; - state:
disabled,busy(which also disables),placeholder,hint,kind.
The enumerations behind them are closed sets, reached like any library type:
AlignisStart,Center,EndorStretch, andJustifyisStart,Center,EndorBetween;TextSizeisTitle,Subtitle,BodyorCaption, andAxisisHorizontal,VerticalorBoth;InputKindisText,Number,Date,Password,SearchorRange, a slider from 0 to 100;ToneisNormal,Muted,Good,WarnorDanger, or an exact colour throughHEXandRGBA. A malformedHEXisNormal, and anRGBAchannel is clamped;Iconis one of eighteen, so a name that is not one of them is a compile error.
html(view) renders a tree as an HTML fragment, and page(title, view) as a whole document with its stylesheet built in, for light and dark. An httpServe handler can answer with it directly, and every screen of this tour except the code samples is one. On a touch screen, controls grow to a thumb's width and a field's text to 16 pixels. Because a view is a value, an interface can be tested without a browser: a test renders one with html and reads the markup. That is the cheapest place to check that a disabled button is disabled, and that somebody else's text cannot smuggle a script into the page.
proc main(): void {
view := hive.ui.column([hive.ui.pad(24), hive.ui.gap(12)], [
hive.ui.text([hive.ui.size(hive.ui.TextSize.Title()), hive.ui.heading(1)], "Hive Tour"),
hive.ui.row([hive.ui.gap(8)], [
hive.ui.input([hive.ui.placeholder("your name")], ""),
hive.ui.button([hive.ui.tone(hive.ui.Tone.Good())], "Say hi"),
]),
hive.ui.link([hive.ui.background(hive.ui.Tone.Good())], "/1", "Start →"),
])
body := hive.ui.page("Hive", view) // a whole document, served directly
echo len(body)
}type Model {
posts: Str[dyn]
draft: Str
}
func view(model: Model): ui.View {
mut ui.View[dyn] kids = [
ui.input([ui.placeholder("say something")], model.draft),
ui.button([ui.disabled(model.draft == "")], "Post"),
]
for each post in model.posts {
append(kids, ui.text([], post))
}
return ui.column([ui.pad(24), ui.gap(8)], kids)
}
test "the post button is disabled until something is typed" {
empty := ui.html(view(Model([], "")))
assert indexOf(empty, "disabled") is Result.Ok(_)
typed := ui.html(view(Model([], "hi")))
assert indexOf(typed, "disabled") is Result.Error(_)
}
test "a post body cannot smuggle markup into the page" {
page := ui.html(view(Model(["<script>steal()</script>"], "")))
assert indexOf(page, "<script>") is Result.Error(_)
}Run a program to see what it prints here.