Skip to content

ui

import ui

The guide’s Windows chapter explains how a window program is made. This page lists what ui has.

WrittenDoes
ui.run(title, state, view, update)shows a window titled title, drawn by view(state), until it is closed. At each click, update(var state, event) runs, and the window is drawn again.
ui.run(…) width(w) height(h)the same, the window opening w pixels wide and h high; each part may be left out.
ui.run(…) frame(false)the window without the system’s frame (your own frame).
ui.close, ui.minimize, ui.maximize, ui.full_screen, ui.dragfrom update: closes the window, minimizes it, maximizes it or restores it, puts it in full screen or takes it out, moves it with the mouse after a press.
ui.window -> ui.Area, ui.screen -> ui.Areawhere the window is, what the screen leaves it: x, y, width, height.
ui.place(x, y) size(width, height)from update: puts the window there; does nothing on Linux yet.
ui.animate(area) time(ms) curve { t: Float => … }from update: takes the window to area over time, 300 ms if time is left out, starting and ending slowly if curve is (how curves work); does nothing on Linux yet.
ui.text(s: String) -> ui.Viewa line of text.
ui.button(label: String, event: E) -> ui.Viewa button; a click sends event to update.
ui.input(value: String) { s: String => E } -> ui.Viewa text field holding value; at each change the user makes, the block’s event, made from the text the field now holds, goes to update (Text fields).
ui.image(path: String) -> ui.Viewan image file: PNG or JPEG on every system. A path that does not exist stops the program, naming it. .size(w) gives its width in pixels, the height following.
ui.canvas(width: Int, height: Int) { d: ui.Draw => … } -> ui.Viewa drawing area (Drawing). ui.fill in place of a size takes all the room there is.
ui.checkbox(label, on: Bool) { b: Bool => E } -> ui.Viewa box to tick; the block’s event goes to update at each change (Widgets that hold a value).
ui.slider(value, least, most) { v: Int => E } -> ui.Viewa slider between two whole numbers.
ui.choice(items: List<String>, selected: Int) { i: Int => E } -> ui.Viewa drop-down list; selected counts from 0.
ui.list(items: List<String>, selected: Int) { i: Int => E } -> ui.Viewa list that shows its lines and scrolls; -1 selects none.
ui.table(headers, lines: List<List<String>>, selected: Int) { i: Int => E } -> ui.Viewa table with its column titles; the block gets the line clicked, -1 selects none.
ui.tabs(names: List<String>, selected: Int) { i: Int => E } -> ui.Viewa row of tabs. It only says which one is chosen: the program shows that page under it.
ui.gpu(width: Int, height: Int) { g: ui.Gpu => … } -> ui.Viewan area painted by the graphics card (Drawing with the graphics card). Its size and events are a canvas’s.
ui.column(items: List<ui.View>) -> ui.Viewthe views one under the other.
ui.row(items: List<ui.View>) -> ui.Viewthe views side by side.
ui.column(…) align(left|center|right)the column takes the whole width it is given and places its views there.
ui.row(…) align(top|center|bottom)the views at the top, in the middle or at the bottom of the row.
view.color(c: ui.Color) -> ui.Viewthe color of a text or a field. A button keeps the system’s color (on an iPhone, the one you give it).
view.size(pixels: Int) -> ui.Viewthe height of the text, for a text, a button or a field.
view.bold -> ui.Viewthe text in bold.
view.margin(pixels: Int) -> ui.Viewempty pixels all around the view.
view.on_mouse { m: ui.Mouse => E } -> ui.Viewon a canvas: the block’s event at each press, release and move of the mouse.
view.on_key { k: ui.Key => E } -> ui.Viewon a canvas: the block’s event at each key pressed.
view.every(ms: Int, event: E) -> ui.Viewsends event every ms milliseconds, on any view.
view.menus(all: List<ui.Menu>) -> ui.Viewthe window’s menu bar; on the whole view, the outermost one.
ui.menu(title, items: List<ui.Item>) -> ui.Menuone menu of the bar.
ui.item(label, event: E) key(shortcut := "") -> ui.Item, ui.separatora line of a menu, which sends event; a line that separates. key("o") also sends it on Ctrl+O (⌘O on a Mac), key("shift+s") on Ctrl+Shift+S; a letter or a digit. q, x, c, v and a alone are the system’s (quit, cut, copy, paste, select all), refused.
ui.rgb(r: Int, g: Int, b: Int) -> ui.Colorany color, each part from 0 to 255.
ui.red, ui.green, ui.blue, ui.orange, ui.yellow, ui.purple, ui.gray, ui.black, ui.whiteready-made colors.

On an iPhone or iPad, a view you made with import ios can sit inside a ui window, between the texts and the buttons the system draws for you:

import ui
import ios
fn view(app: App) -> ui.View {
return ui.column([
ui.text("Count: {app.count}"),
ui.native(app.swatch, 200, 80),
ui.button("Add", Event.Add),
])
}

ui.native(view, width, height) takes the view’s number (swatch.object.id, kept in the state) and the size it gets, in points. The view keeps whatever you set on it, and a rebuilt window puts it back where it was. Anywhere but on an iPhone or iPad it shows nothing, so the same program runs everywhere.

Each one waits for the user, and belongs in update.

WrittenDoes
ui.open_file(title) -> String?the system’s window to choose a file; none if the user cancels.
ui.save_file(title) name(n) -> String?the system’s window to choose where to save; name is the name it suggests, and may be left out.
ui.open_folder(title) -> String?the same for a folder.
ui.message(title, text)says something; the user answers OK.
ui.confirm(title, text) -> Boolasks; true if the user answers yes.
WrittenDoes
d.clear(c: ui.Color)paints the whole canvas.
d.rect(x, y) size(w, h) color(c) outline(n)a rectangle from its top left corner.
d.circle(x, y) radius(r) color(c) outline(n)a circle from its center.
d.line(x, y) to(x2, y2) color(c) thickness(n)a line, 1 pixel thick by default.
d.text(s, x, y) size(n) color(c)a text from its top left corner, 16 pixels high by default.
d.image(path, x, y) size(w, h)a PNG or JPEG file from its top left corner, at its own size without size.
d.width, d.heightthe canvas’s size.

All numbers are Int, in pixels. color is black by default. outline(n) draws only the edge; without it, the shape is filled.

FieldIs
m.x, m.ywhere the mouse is in the canvas.
m.pressed, m.released, m.movedwhat just happened.
m.rightit is the right button.
k.namewhat the key writes, or Left, Right, Up, Down, Space, Enter, Escape, Tab, Backspace, Delete, Home, End, PageUp, PageDown, F1 to F12.
WrittenDoes
g.clear(c: ui.Color)paints the whole area, and resets the depth.
g.shader(vertex: String, fragment: String)the two GLSL programs (#version 330 core) for what follows.
g.uniform(name: String, values: List<Float>)a value for the shaders: 1 to 4 numbers, or 16 for a mat4, column by column.
g.triangles(vertices: List<Float>) parts(sizes: List<Int>)draws three vertices at a time; sizes gives each attribute’s numbers, [2] if left out.
g.texture(width, height, pixels: List<Int>)an image for what follows, 4 bytes per pixel: red, green, blue, alpha.
g.width, g.heightthe area’s size.

Everything is placed from the top left, on every system, unless align says otherwise.

Style methods follow each other in any order: ui.text("Total").size(20).bold.color(ui.red). On a column or a row, color, size and bold go to all its views, and a view’s own style wins. margin stays on the view it is written on.

The program writes the two functions ui.run takes:

  • fn view(app: App) -> ui.View, where App is the type of the state;
  • fn update(app: var App, e: Event), where Event is the type of the buttons’ events.

Their names are free, and a program has one ui.run. The compiler checks both shapes, and that every button’s event is an Event.

SystemWindows made withNeeds
LinuxGTK 4GTK 4, there on most desktops
WindowsWin32nothing
macOSAppKitnothing

Everything here changes how a program looks on an iPhone or an iPad; on the other systems it is ignored (a page pushed over a view shows below it, a mark in a text shows as written). The program is still drawn by UIKit’s real controls, so it has the system’s large titles, tab bar, navigation bar and, in dark mode, its colors.

view.title(name)the title of the page this view is, in the navigation bar; large, and small once the page scrolls.
ui.tabs(…).icons(["note.text", "chart.bar.fill"])an SF Symbol for each tab, by its name.
ui.menu(title, items) icon("ellipsis.circle")an SF Symbol in the bar instead of the menu’s title.
view.background(c).rounded(px)a color behind the view and its margin, with rounded corners: a card.
ui.panel, ui.page, ui.cardthe system’s backgrounds, light or dark as the system is: a panel, a whole page of grouped sections, one of those sections. .background(ui.page) on the view view gives paints the window.
ui.list(lines, selected)its rows in a white card, a chevron at the end of each. A line break inside a row gives it a title, a subtitle and a detail line.
view.pushing(page, closed: E)page as a new screen over this one, with a back button; closed is the event the back button and the swipe back send, and the program stops giving a page when it comes. Give it before menus.
view.nav_button(label, event)a button in the navigation bar of this page.
view.searchable(value, { s: String => E })a search field for this page: in the navigation bar, where the system puts it. Elsewhere it shows above the page.
ui.list(lines, selected, { i: Int => E }) delete { i: Int => E }the same list; a swipe to the left on a row shows a red Delete, and delete gives the event for that row. On Android the swipe deletes the row at once. Other systems ignore the swipe: give a button too.
view.lines(n)on a ui.input: a text field of n lines, where Return starts a new line (Android and iPhone: elsewhere it stays one line).
view.markdownon a text: **bold**, *stressed*, `code` and [label](address) show as what they mean.

A column inside a card spreads across it, so a switch ends at the card’s right edge as in the Settings app.

VariableEffect
KOVA_UI_SCRIPT=1no window: each view is printed as text, and the button to click (its number from 0, in the order they are written), or k=text to type into field k, or a gesture such as k=press 10 20 1 or k=key Left for a canvas, is read from the input; the end of the input closes the window. For tests.
KOVA_UI_HIDDEN=1the window exists but is never shown (Windows, macOS), for a program that drives it.

Without width and height, the window opens at the size of its content, at least 320 pixels wide. It is never smaller than its content, whatever size is asked, and whoever uses it can resize it: a click never puts back the first size, it only makes the window grow when the content needs more room.

A ui program runs as native code, even under kova run.