A window is a service
The same tree, shown by a window, becomes a live interface. hive.ui.window(title, view, update) is a service's handler, and spawn opens it with the state it starts from:
viewis afunc(Model): View— afunc, so drawing can never change anything — called again after every turn.updateis aproc(mut Model, Msg): Msg, writing the model in place for each event. One that takes a third parameter,window: Address<Msg>, is handed the window's own address every turn. What it answers with goes back to a request from another node, and is dropped for the window's own events.- An event carries a message:
on(msg)for a click andonDismiss(msg)for a click on an overlay's backdrop, and a function to one — likeMsg.Changed(_)— for events that report something:onInput,onSubmitandonChooseaStr,onToggleaBool,onSorta column's index, andonPicka table row's, the header not counted. Alinkin a window sends its message rather than following itshref, and a served page keeps only a path,http,httpsormailtolink.
Opening a window does not wait for it, so a program may open several. It is a real window rather than a tab — an application-mode browser window on a profile of its own, none of anybody's session or extensions in it — and a machine with no Chromium-family browser falls back to a tab. Closing a window kills its service, closing the last ends the program, and a main that returns while one is open waits for them.
A window needs no command type for work that must not block the screen: update is a proc, so it may start work with async, and given its own address that work can send its answer back as an ordinary message. The program's icon is assets/icon.png beside the entrypoint, a PNG of at most 256×256, and its typeface assets/font.woff2: nothing in the source names either, the whole assets/ folder is built into the executable, and on Windows the executable carries the icon too, so a file manager and a pinned shortcut show it. HIVE_WINDOW=print prints the window's address, hive-window <url>, instead of opening a browser, for whatever else is going to show the page.
type Model {
rows: Str[dyn]
loading: Bool
}
type Msg {
Refresh
Loaded { rows: Str[dyn] }
}
func view(model: Model): ui.View {
mut ui.View[dyn] kids = [ui.button([ui.on(Msg.Refresh())], "Refresh")]
if model.loading {
append(kids, ui.spinner([]))
}
for each row in model.rows {
append(kids, ui.text([], row))
}
return ui.column([ui.pad(24), ui.gap(8)], kids)
}
proc update(model: mut Model, msg: Msg, window: Address<Msg>): Msg {
if msg is Msg.Refresh {
async reload(window) // off it goes; nothing to hold
model.loading = true // ...and the spinner starts now
} else if msg is Msg.Loaded(rows) {
model = Model(rows, false) // it comes back as a message
}
return msg
}
proc reload(window: Address<Msg>): void {
hive.task.sleep(400)
async window(Msg.Loaded(["north", "south"]))
}
proc main(): void {
spawn(ui.window("Colonies", view, update), Model([], false))
}