Windows, canvases and scenesSlide 77 of 86
77windowWindows, canvases and scenes

A window is a service

Shown in a window, the same tree 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:

  • view is a func(Model): View, called again after every turn. It is a func, so drawing can never change anything.
  • update is a proc(mut Model, Msg): Msg that changes the model in place for each event. If it takes a third parameter, window: Address<Msg>, it is handed the window's own address every turn. Its answer goes back to a request from another node, and is dropped for the window's own events.
  • An event carries a message. on(msg) is for a click and onDismiss(msg) for a click on an overlay's backdrop. Events that report something take a function that builds the message, like Msg.Changed(_): onInput, onSubmit and onChoose report a Str, onToggle a Bool, onSort a column's index, and onPick a table row's, not counting the header. A link in a window sends its message instead of following its href, and a served page keeps a link only if it is a path or an http, https or mailto link.

Opening a window does not wait for it, so a program can open several. On Windows and Linux the program draws the window itself, every widget included, with no browser involved. It gets a browser window instead if the program ships its own typeface or opens it with hive.ui.webview, which takes the same three arguments as window. On other systems it is always a browser window: an application-mode window on a profile of its own, with none of anybody's session or extensions in it, or a tab on a machine with no Chromium-family browser. Closing a window kills its service, closing the last one ends the program, and a main that returns while a window is open waits for them.

Work that must not block the screen needs no command type: update is a proc, so it can start the work with async, and given the window's address, that work can send its result 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 is assets/font.woff2; without one, a window uses the system's own. 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, and it makes a window the program would draw itself a page too, so there is one to show.

A window, with no command type
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))
}