Standard library · Graphics and games

gui.ui

The native runtime of the Salam page language: a typed builder that makes the element tree, style patches per state, scopes that re-run only what changed, and functions that run a page in a window or render it to a canvas or a text report.

import gui.ui

gui.ui is what runs a Salam page on a desktop. A page: block is compiled into a function that receives a Builder and calls Open, Id, SetText, Text, On and Close on it; the runtime turns that element tree into boxes with gui.style values, lays them out with gui.flow, and paints them with gui.paint. You rarely call the builder by hand, but you can, and then you get the same runtime with no page syntax: that is how the examples below are written.

Three functions use a page function and a list of style patches:

•

RunPage opens a window and runs until it is closed. RunPageWith takes Options for the size, the title and a headless run.

•

RenderPage builds the page, lays it out for a given size and returns the painted paint.Canvas without opening a window. RenderPageScaled does the same at a device scale such as 2.0.

•

DumpPage does the same and returns a text report of the boxes (kind, id, x, y, width, height), which is the easiest thing to compare in a test.

Things to know:

•

Style values are checked. SetText parses its text with style.Parse and prints the exact error for a bad one instead of ignoring it. Use Set with a parsed style.Value when you already have one.

•

A style belongs to a state. Every Set, SetText and Patch names one of the STATE_ constants; the base style always applies and a state style applies on top of it while the element is in that state.

•

A patch is a named group of style values. Build it once with NewPatch and Add, return it from the function you give to RunPage, and apply it to any number of elements with Builder.Patch; the patch is the index in that list.

•

Scopes re-run only what changed. Scope and KeyedScope wrap a part of the view; when state that the scope reads is touched, only that part runs again. The compiler writes these calls, together with Reactive, ScopeSite, Instance and Fresh, for every page that has state.

•

Nothing needs a window to be tested. RenderPage and DumpPage never open a window, so they run on a machine with no display.

Draw a row of cards and check the pixels

Three boxes of the same size in a row with a gap and padding. DumpPage shows where layout put each box, and Pixel on the canvas checks what was painted, including the gap between the cards that shows the background.

gui-ui-guide-cards.salam
import gui.ui
import gui.style
import gui.paint

func none(): Vector<ui.Patch>:
    ret ui.NoPatches()
end

func block(b: ui.Builder, name: str, color: str):
    b.Open("box")
    b.Id(name)
    b.SetText(ui.STATE_BASE, style.P_WIDTH, "90")
    b.SetText(ui.STATE_BASE, style.P_HEIGHT, "50")
    b.SetText(ui.STATE_BASE, style.P_BACKGROUND, color)
    b.SetText(ui.STATE_BASE, style.P_RADIUS, "8")
    b.Close()
end

func view(b: ui.Builder):
    b.Open("box")
    b.SetText(ui.STATE_BASE, style.P_LAYOUT, "row")
    b.SetText(ui.STATE_BASE, style.P_GAP, "12")
    b.SetText(ui.STATE_BASE, style.P_PADDING, "16")
    b.SetText(ui.STATE_BASE, style.P_BACKGROUND, "#f4f6fa")
    block(b, "tea", "#cc3333")
    block(b, "coffee", "#3366aa")
    block(b, "juice", "#33aa55")
    b.Close()
end

func probe(c: paint.Canvas, name: str, x: int, y: int):
    p := c.Pixel(x, y)
    println name, p.r, p.g, p.b
end

func main:
    mut c := ui.RenderPage((): ret none() end, (b: ui.Builder): view(b) end, 400, 120)
    println ui.DumpPage((): ret none() end, (b: ui.Builder): view(b) end, 400, 120)
    probe(c, "tea", 40, 40)
    probe(c, "coffee", 150, 40)
    probe(c, "juice", 260, 40)
    probe(c, "gap", 113, 40)
    c.Free()
end
Output
page 0 0 400 120
  box 0 0 400 82
    box#tea 16 16 90 50
    box#coffee 118 16 90 50
    box#juice 220 16 90 50

tea 204 51 51
coffee 51 102 170
juice 51 170 85
gap 244 246 250

A grid with spans

The same builder with layout set to grid: three tracks (one fraction, two fractions and a fixed 100), a gap, and a cell that spans two columns. The report gives the position of every cell, so a layout rule becomes a test of a few lines.

gui-ui-guide-grid.salam
import gui.ui
import gui.style

func none(): Vector<ui.Patch>:
    ret ui.NoPatches()
end

func cell(b: ui.Builder, name: str, span: str):
    b.Open("box")
    b.Id(name)
    b.SetText(ui.STATE_BASE, style.P_HEIGHT, "40")
    b.SetText(ui.STATE_BASE, style.P_COLUMN_SPAN, span)
    b.Close()
end

func view(b: ui.Builder):
    b.Open("box")
    b.Id("grid")
    b.SetText(ui.STATE_BASE, style.P_LAYOUT, "grid")
    b.SetText(ui.STATE_BASE, style.P_COLUMNS, "1fr, 2fr, 100")
    b.SetText(ui.STATE_BASE, style.P_GAP, "10")
    b.SetText(ui.STATE_BASE, style.P_PADDING, "10")
    cell(b, "a", "1")
    cell(b, "b", "1")
    cell(b, "c", "1")
    cell(b, "wide", "2")
    cell(b, "last", "1")
    b.Close()
end

func main:
    println ui.DumpPage((): ret none() end, (b: ui.Builder): view(b) end, 400, 200)
end
Output
page 0 0 400 200
  box#grid 0 0 400 110
    box#a 10 10 86.67 40
    box#b 106.67 10 173.33 40
    box#c 290 10 100 40
    box#wide 10 60 270 40
    box#last 290 60 100 40

Types

Element

#
struct Element

A handle to one element of the running page, found by id or key, used by tests and by handlers to read and change it.

NameTypeDescription
idstr = ""The id the handle looks for.
keystr = ""The key the handle looks for when no id is given.
gui-ui-Events.salam
/*
 * Salam Programming Language (2018-2026)
 *
 *   +-------------------+
 *   |     S A L A M     |
 *   +-------------------+
 *
 * Designed by Seyyed Ali Mohammadiyeh and the Salam Team
 *
 * Repository: https://github.com/SalamLang/Salam
 *
 */

import gui.ui

func report():
    t := ui.ById("t")
    k := ui.ByKey("agree")
    go := ui.ById("go")
    println "exists", t.Exists(), ui.ById("nope").Exists(), k.Exists()
    println "tag", t.Tag(), k.Tag(), go.Tag()
    println "text", go.Text()
    go.SetText("Again")
    println "text", go.Text()
    println "value", t.Value()
    t.SetValue("hello")
    println "value", t.Value()
    println "checked", k.Checked()
    k.SetChecked(true)
    println "checked", k.Checked()
    println "attr", t.Attr("placeholder")
    t.SetAttr("placeholder", "name")
    println "attr", t.Attr("placeholder")
    t.Focus()
    println "event", ui.CurrentEvent().kind, ui.CurrentEvent().target
    go.ScrollIntoView()
    r := go.Rect()
    println "rect", r.width > 0.0, r.height > 0.0
    s := t.Style()
    println "style", s.visible
    t.Click()
end

func main:
    ui.SetUrlOpener((url: str): ret url.Len() > 0 end)
    ui.SetNavigator((url: str): ret url.Len() > 0 end)
    ui.RunPage((): ret ui.NoPatches() end, (b: ui.Builder): b.Open("input") b.Id("t") b.Attr("type", "text") b.On("click", (): println "clicked t" end) b.Close() b.Open("checkbox") b.Key("agree") b.Close() b.Open("button") b.Id("go") b.On("click", (): report() end) b.Text("Go") b.Close() end)
    println "open", ui.OpenUrl("https://example.com")
    println "opened", ui.OpenedUrls().Len()
    ui.RunSteps("quit")
    ui.Stop()
end

Exists

#
func Exists(): bool

True when the page has an element with this id or key.

The example for this item is shown with the ones above: gui-ui-Events.salam

Tag

#
func Tag(): str

The kind of the element, such as button or input.

The example for this item is shown with the ones above: gui-ui-Events.salam

Text

#
func Text(): str

The text inside the element and its children.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetText

#
func SetText(text: str)

Replaces the content of the element with this text.

The example for this item is shown with the ones above: gui-ui-Events.salam

Value

#
func Value(): str

The value of an input, text area or select.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetValue

#
func SetValue(value: str)

Sets the value of a control and moves its caret to the end.

The example for this item is shown with the ones above: gui-ui-Events.salam

Checked

#
func Checked(): bool

True when a check box or radio button is on.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetChecked

#
func SetChecked(is_on: bool)

Turns a check box or radio button on or off, as a click would, without the click events.

The example for this item is shown with the ones above: gui-ui-Events.salam

Focus

#
func Focus()

Gives the element keyboard focus and scrolls it into view.

The example for this item is shown with the ones above: gui-ui-Events.salam

ScrollIntoView

#
func ScrollIntoView()

Scrolls every scrolling parent so the element is visible.

The example for this item is shown with the ones above: gui-ui-Events.salam

Click

#
func Click()

Clicks the element as the pointer would, including its click handlers.

The example for this item is shown with the ones above: gui-ui-Events.salam

Attr

#
func Attr(name: str): str

Reads an attribute of the element by name.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetAttr

#
func SetAttr(name: str, value: str)

Sets an attribute of the element by name and lays the page out again.

The example for this item is shown with the ones above: gui-ui-Events.salam

Style

#
func Style(): style.Style

The computed style of the element after states such as hover and focus are applied.

The example for this item is shown with the ones above: gui-ui-Events.salam

Rect

#
func Rect(): paint.Rect

The box of the element on screen: x, y, width and height.

The example for this item is shown with the ones above: gui-ui-Events.salam

Options

#
struct Options

Settings for a window: size, title and whether it can be resized.

NameTypeDescription
widthf64 = 1024Width of the window in pixels. 1024 by default.
heightf64 = 768Height of the window in pixels. 768 by default.
titlestr = ""Title of the window. When empty, the title set with Builder.Title is used.
resizablebool = trueWhether the user can resize the window. True by default.
headlessbool = falseOpens the window with the headless backend, which draws to memory and needs no screen. False by default.
scalef64 = 0Device scale of the window. 0 lets the window backend choose.
scriptstr = ""Reserved for scripted test runs. Empty for a normal run; the runtime does not read it yet.
gui-ui-Run.salam
import gui.ui
import gui.style

mut clicks := 0

func none(): Vector<ui.Patch>:
    ret ui.NoPatches()
end

func view(b: ui.Builder):
    b.Title("Counter")
    b.Open("box")
    b.SetText(ui.STATE_BASE, style.P_PADDING, "16")
    b.Open("button")
    b.Id("go")
    b.On("click", (): clicks++ end)
    b.Text("Clicked " + clicks + " times")
    b.Close()
    b.Close()
end

func main:
    o := ui.Options {
        width = 480.0,
        height = 320.0,
        title = "Counter",
        resizable = true
    }
    ui.RunPageWith((): ret none() end, (b: ui.Builder): view(b) end, o)
    println "clicks", clicks
    println ui.Dump()
    ui.RunPage((): ret none() end, (b: ui.Builder): view(b) end)
end

Event

#
struct Event

The fields of an input event: what happened, where and with which key.

NameTypeDescription
kindstr = ""Kind of the event, such as click or key.
targetstr = ""Id of the element the event is for.
xf64 = 0Horizontal position of the pointer in the window.
yf64 = 0Vertical position of the pointer in the window.
buttonint = 0Number of the mouse button, 0 for the left one.
keystr = ""Name of the key for a keyboard event.
textstr = ""Text typed for a text event.
valuestr = ""Current value of the field for an input event.
checkedbool = falseWhether a check box, radio button or switch is on after the event.
modifiersint = 0Bit mask of the held modifier keys.
delta_xf64 = 0Horizontal scroll amount.
delta_yf64 = 0Vertical scroll amount.
scalef64 = 1Size of a pinch compared with its start, 1 when the fingers have not moved, above 1 when they spread. The position is the middle between the two fingers.

The example for this item is shown with the ones above: gui-ui-Run.salam

Profile

#
struct Profile

Time spent in each stage of one frame. The runtime does not fill it yet.

NameTypeDescription
build_msf64 = 0Milliseconds spent building the element tree.
style_msf64 = 0Milliseconds spent computing styles.
layout_msf64 = 0Milliseconds spent laying out.
paint_msf64 = 0Milliseconds spent painting.
elementsint = 0Number of elements in the page.

The example for this item is shown with the ones above: gui-ui-Run.salam

Patch

#
struct Patch

A group of style values with no element yet. Build one, return it in the list you give to a run or render function, and apply it with Builder.Patch.

NameTypeDescription
propsVector<int>The property ids, in the order they were added.
valuesVector<style.Value>The style values, in the same order as props.
gui-ui-Style.salam
import gui.ui
import gui.style

func patches(): Vector<ui.Patch>:
    mut all := ui.NoPatches()
    mut red := ui.NewPatch()
    mut v := style.Value {}
    if style.Parse(style.P_BACKGROUND, "#cc0000", v):
        red.Add(style.P_BACKGROUND, v)
    end
    if style.Parse(style.P_RADIUS, "6", v):
        red.Add(style.P_RADIUS, v)
    end
    println "patch length", red.Len()
    all.Push(red)
    ret all
end

func view(b: ui.Builder):
    b.Open("box")
    b.Id("a")
    b.SetText(ui.STATE_BASE, style.P_WIDTH, "60")
    b.SetText(ui.STATE_BASE, style.P_HEIGHT, "40")
    b.Patch(ui.STATE_BASE, 0)
    b.SetText(ui.STATE_HOVER, style.P_BACKGROUND, "#0000cc")
    b.Close()
    b.Open("box")
    b.Id("b")
    mut v := style.Value {}
    if style.Parse(style.P_BACKGROUND, "#00aa00", v):
        b.Set(ui.STATE_BASE, style.P_BACKGROUND, v)
    end
    b.SetText(ui.STATE_BASE, style.P_WIDTH, "60")
    b.SetText(ui.STATE_BASE, style.P_HEIGHT, "40")
    b.Close()
end

func main:
    mut c := ui.RenderPage((): ret patches() end, (b: ui.Builder): view(b) end, 100, 100)
    a := c.Pixel(30, 20)
    b := c.Pixel(30, 60)
    println "a", a.r, a.g, a.b
    println "b", b.r, b.g, b.b
    println "states",
        ui.STATE_BASE,
        ui.STATE_HOVER,
        ui.STATE_FOCUS,
        ui.STATE_FOCUS_WITHIN,
        ui.STATE_ACTIVE,
        ui.STATE_DISABLED,
        ui.STATE_CHECKED,
        ui.STATE_INVALID,
        ui.STATE_PLACEHOLDER,
        ui.STATE_SELECTION
    c.Free()
end
Output
patch length 2
a 204 0 0
b 0 170 0
states 0 1 2 3 4 5 6 7 8 9

Add

#
func Add(prop: int, v: style.Value)

Adds one property and its value to the patch.

The example for this item is shown with the ones above: gui-ui-Style.salam

Len

#
func Len(): int

Number of properties in the patch.

The example for this item is shown with the ones above: gui-ui-Style.salam

Builder

#
struct Builder

What a page function builds into: the elements, attributes, styles, text and handlers of one page. The compiler writes the calls for a page: block.

gui-ui-Builder.salam
import gui.ui
import gui.style

func none(): Vector<ui.Patch>:
    ret ui.NoPatches()
end

func size(b: ui.Builder, w: str, h: str):
    b.SetText(ui.STATE_BASE, style.P_WIDTH, w)
    b.SetText(ui.STATE_BASE, style.P_HEIGHT, h)
end

func view(b: ui.Builder):
    b.Title("Menu")
    b.Lang("en")
    b.Dir("ltr")
    b.Open("box")
    b.Id("menu")
    b.SetText(ui.STATE_BASE, style.P_LAYOUT, "row")
    b.SetText(ui.STATE_BASE, style.P_GAP, "8")
    b.Open("box")
    b.Id("open")
    b.Key("k1")
    b.Attr("title", "Open the file")
    b.BoolAttr("disabled", false)
    size(b, "80", "30")
    b.Close()
    b.Open("box")
    b.Id("save")
    b.Key("k2")
    b.BoolAttr("disabled", true)
    size(b, "80", "30")
    b.Close()
    b.Close()
end

func main:
    println ui.DumpPage((): ret none() end, (b: ui.Builder): view(b) end, 300, 100)
end
Output
page 0 0 300 100
  box#menu 0 0 300 30
    box#open 0 0 80 30
    box#save 88 0 80 30

Title

#
func Title(s: str)

Sets the title of the page.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Lang

#
func Lang(s: str)

Sets the language of the page, such as en or fa.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Dir

#
func Dir(s: str)

Sets the text direction of the page, ltr or rtl.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Open

#
func Open(kind: str)

Opens an element of this kind as the last child of the current one and makes it current. The kind is a page element name in English or Persian, such as box, heading or جعبه. An HTML tag such as div or h2 is not a kind: it is reported once with the name to use, and the element stays hidden. Every Open needs a Close.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Patch

#
func Patch(state: int, id: int)

Applies the patch with this index, from the list given to the run or render function, to the current element for a state.

The example for this item is shown with the ones above: gui-ui-Style.salam

Set

#
func Set(state: int, prop: int, v: style.Value)

Sets one style value of the current element for a state. A bad property or state is ignored.

The example for this item is shown with the ones above: gui-ui-Style.salam

SetText

#
func SetText(state: int, prop: int, text: str)

Sets one style of the current element for a state from text in the page syntax. A text that does not parse prints the exact error and changes nothing.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Id

#
func Id(s: str)

Sets the id of the current element, which tests and reports use to find it.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Key

#
func Key(s: str)

Sets the key of the current element, which keeps its state when a list is reordered.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Attr

#
func Attr(name: str, value: str)

Sets any attribute of the current element, by name.

The example for this item is shown with the ones above: gui-ui-Builder.salam

BoolAttr

#
func BoolAttr(name: str, value: bool)

Sets a switch attribute such as disabled: present when the value is true, left out when it is false.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Text

#
func Text(s: str)

Adds a text child to the current element.

The example for this item is shown with the ones above: gui-ui-Run.salam

On

#
func On(event: str, fn: func())

Sets the function to run when the current element receives this event, such as click.

The example for this item is shown with the ones above: gui-ui-Run.salam

Close

#
func Close()

Closes the current element and goes back to its parent.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Scope

#
func Scope(site: int, fn: func(Builder))

Runs the function as one reactive scope with this site number. Only this part of the view runs again when the state it reads changes.

gui-ui-Scope.salam
import gui.ui

mut total := 0
mut touched := 0 as i64
mut likes := Vector {} as Vector<int>

func take(w: int): i64:
    mut t := 0 as i64
    if w == 0:
        t = touched
        touched = 0
    end
    ret t
end

func sites():
    ui.ScopeSite(1, -1, 0, 1)
    ui.ScopeSite(2, 0, 0, 0)
end

func none(): Vector<ui.Patch>:
    ret ui.NoPatches()
end

func item(b: ui.Builder, name: str):
    b.KeyedScope(2, name, (b: ui.Builder): k := b.Instance(0) if b.Fresh(): if k < likes.Len(): likes[k] = 0 else: likes.Push(0) end end b.Open("button") b.Id(name) b.On("click", (): likes[k] += 1 ui.TouchInstance(0, k) end) b.Close() end)
end

func view(b: ui.Builder):
    b.Reactive(1, (w: int): ret take(w) end, (): sites() end)
    b.Open("box")
    b.Scope(1, (b: ui.Builder): if b.Memo("" + total): ret end b.Open("box") b.Id("total") b.Attr("data-total", "" + total) b.Close() end)
    b.Open("button")
    b.Id("add")
    b.On("click", (): total++ touched |= 1 end)
    b.Close()
    b.Open("button")
    b.Id("redraw")
    b.On("click", (): ui.Invalidate() end)
    b.Close()
    item(b, "tea")
    item(b, "cake")
    b.Close()
end

func main:
    mut c := ui.RenderPage((): ret none() end, (b: ui.Builder): view(b) end, 200, 100)
    println "scope runs", ui.ScopeRuns()
    println "scope skips", ui.ScopeSkips()
    c.Free()
end

Memo

#
func Memo(key: str): bool

Inside a scope, returns true when the scope is running again and the key equals the key it gave last time, so the scope can return and keep what it built. Generated list rows use it to skip rows whose values did not change.

The example for this item is shown with the ones above: gui-ui-Scope.salam

KeyedScope

#
func KeyedScope(site: int, key: str, fn: func(Builder))

Like Scope, with a key so that a scope in a list keeps its place when the list changes order.

The example for this item is shown with the ones above: gui-ui-Scope.salam

Reactive

#
func Reactive(words: int, take: func(int): i64, sites: func())

Registers how many state words the page has, the function that reads and clears the changed bits of a word, and the function that declares the sites. Only the first call in a run counts.

The example for this item is shown with the ones above: gui-ui-Scope.salam

Env

#
func Env(fn: func(int, int, bool, bool, bool, bool, bool))

Registers the function that receives the window values: logical width and height, dark scheme, touch, reduced motion, high contrast and right to left. It is called at once, when the window opens and again on resize and scheme changes. The compiler writes this call for pages that read page.width and the other window values.

The example for this item is shown with the ones above: gui-ui-Builder.salam

Instance

#
func Instance(comp: int): int

Returns the slot of the component instance for the scope that is running, creating it on first use.

The example for this item is shown with the ones above: gui-ui-Scope.salam

Fresh

#
func Fresh(): bool

Reports whether the instance of the running scope was just created, so that its state can be set up once.

The example for this item is shown with the ones above: gui-ui-Scope.salam

Constants and variables

STATE_BASE

#
const STATE_BASE: i32 = 0

The element with no state: the style that always applies.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_HOVER

#
const STATE_HOVER: i32 = 1

The pointer is over the element.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_FOCUS

#
const STATE_FOCUS: i32 = 2

The element has keyboard focus.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_FOCUS_WITHIN

#
const STATE_FOCUS_WITHIN: i32 = 3

The element or one of its children has keyboard focus.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_ACTIVE

#
const STATE_ACTIVE: i32 = 4

The element is being pressed.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_DISABLED

#
const STATE_DISABLED: i32 = 5

The element is disabled.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_CHECKED

#
const STATE_CHECKED: i32 = 6

A check box, radio button or switch is on.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_INVALID

#
const STATE_INVALID: i32 = 7

The value of a field does not pass its check.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_PLACEHOLDER

#
const STATE_PLACEHOLDER: i32 = 8

The field is empty and shows its placeholder.

The example for this item is shown with the ones above: gui-ui-Style.salam

STATE_SELECTION

#
const STATE_SELECTION: i32 = 9

The selected part of a text.

The example for this item is shown with the ones above: gui-ui-Style.salam

Functions

ById

#
func ById(id: str): Element

Finds an element of the running page by its id. The result can be used even if no element has that id; ask it Exists.

The example for this item is shown with the ones above: gui-ui-Events.salam

ByKey

#
func ByKey(key: str): Element

Finds an element of the running page by its key.

The example for this item is shown with the ones above: gui-ui-Events.salam

CurrentEvent

#
func CurrentEvent(): Event

The event whose handler is running now: kind, target id, pointer position, key, text and the value of a control.

The example for this item is shown with the ones above: gui-ui-Events.salam

Stop

#
func Stop()

Asks the running page to stop after the current step.

The example for this item is shown with the ones above: gui-ui-Events.salam

OnCloseRequest

#
func OnCloseRequest(handler: func(): bool)

Sets the function called when the user closes the window. Returning true quits, false keeps the page running, for example after HideWindow to stay in the tray.

gui-ui-Window.salam
/*
 * Salam Programming Language (2018-2026)
 *
 *   +-------------------+
 *   |     S A L A M     |
 *   +-------------------+
 *
 * Designed by Seyyed Ali Mohammadiyeh and the Salam Team
 *
 * Repository: https://github.com/SalamLang/Salam
 *
 */

import gui.ui

func main:
    ui.OnCloseRequest((): ui.HideWindow() ret false end)
    ui.RunPage((): ret ui.NoPatches() end, (b: ui.Builder): b.Open("button") b.On("click", (): ui.Stop() end) b.Text("Quit") b.Close() b.Open("button") b.On("click", (): if ui.WindowVisible(): ui.HideWindow() end ui.ShowWindow() end) b.Text("Hide and show") b.Close() end)
end

KeyboardDismiss

#
func KeyboardDismiss(on_tap: bool, on_drag: bool)

Keeps the keyboard out of the way: the first flag closes it when the user taps outside an input, the second when the user drags the page.

The example for this item is shown with the ones above: gui-ui-Window.salam

OnPause

#
func OnPause(handler: func())

Sets the function called when the app goes to the background, for example when a phone user switches to another app.

The example for this item is shown with the ones above: gui-ui-Window.salam

OnResume

#
func OnResume(handler: func())

Sets the function called when the app comes back to the foreground.

The example for this item is shown with the ones above: gui-ui-Window.salam

OnLowMemory

#
func OnLowMemory(handler: func())

Sets the function called when the system asks apps to release memory.

The example for this item is shown with the ones above: gui-ui-Window.salam

OnSaveState

#
func OnSaveState(handler: func(): str)

Sets the function that returns a text describing the page state. The host keeps it while the system may end the app, and window.SavedState reads it.

The example for this item is shown with the ones above: gui-ui-Window.salam

HideWindow

#
func HideWindow()

Hides the window of the running page. The page keeps running and can show it again.

The example for this item is shown with the ones above: gui-ui-Window.salam

ShowWindow

#
func ShowWindow()

Shows the window again and brings it to the front.

The example for this item is shown with the ones above: gui-ui-Window.salam

WindowVisible

#
func WindowVisible(): bool

Reports whether the page has a window and it is shown.

The example for this item is shown with the ones above: gui-ui-Window.salam

SetUrlOpener

#
func SetUrlOpener(opener: func(str): bool)

Chooses the function that opens an http, https or mailto link. Without one the system browser is used.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetNavigator

#
func SetNavigator(navigator: func(str): bool)

Chooses the function that handles other links, such as a path to another page.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetBack

#
func SetBack(handler: func(): bool)

Chooses the function that runs for Back: the mouse back button, Alt+Left, or Ctrl/Cmd+[. Return true when it handled the step; a page with routes sets it to page.route.Back for you.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetForward

#
func SetForward(handler: func(): bool)

Chooses the function that runs for Forward: the mouse forward button, Alt+Right, or Ctrl/Cmd+]. Return true when it handled the step; a page with routes sets it to page.route.Forward for you.

The example for this item is shown with the ones above: gui-ui-Events.salam

SetAfterUpdate

#
func SetAfterUpdate(handler: func(): bool)

Chooses a function that runs after event handlers change state, before the page is drawn again. A page with routes uses it to follow a guard that turned false to its redirect.

The example for this item is shown with the ones above: gui-ui-Events.salam

OpenedUrls

#
func OpenedUrls(): Vector<str>

Every link opened so far, in order.

The example for this item is shown with the ones above: gui-ui-Events.salam

OpenUrl

#
func OpenUrl(url: str): bool

Opens a link as a click on it would.

The example for this item is shown with the ones above: gui-ui-Events.salam

AddFontFile

#
func AddFontFile(path: str): int

Adds a font file to the fonts used to draw text, so a page renders the same on every machine. Returns the number of faces added.

gui-ui-Offscreen.salam
import gui.ui
import gui.style

mut moved := false

func none(): Vector<ui.Patch>:
    ret ui.NoPatches()
end

func view(b: ui.Builder):
    b.Open("box")
    b.SetText(ui.STATE_BASE, style.P_BACKGROUND, "#ffffff")
    b.Open("box")
    b.SetText(ui.STATE_BASE, style.P_WIDTH, "40")
    b.SetText(ui.STATE_BASE, style.P_HEIGHT, "40")
    b.SetText(ui.STATE_BASE, style.P_MARGIN, moved ? "40" : "0")
    b.SetText(ui.STATE_BASE, style.P_BACKGROUND, "#3366aa")
    b.Close()
    b.Close()
end

func main:
    ui.AddFontDir("fonts-that-do-not-exist")
    ui.OffscreenOpen((): ret none() end, (b: ui.Builder): view(b) end, 120, 100, 1.0)
    moved = true
    area := ui.OffscreenUpdate(false)
    println "damaged", area > 0
    partial := ui.OffscreenCanvas().Hash()
    ui.OffscreenRepaintAll()
    println "same as full", partial == ui.OffscreenCanvas().Hash()
    ui.OffscreenClose()
end
Output
damaged true
same as full true

AddFontDir

#
func AddFontDir(dir: str): int

Adds every font file in a directory. Returns the number of faces added, and 0 when the directory does not exist.

The example for this item is shown with the ones above: gui-ui-Offscreen.salam

OffscreenOpen

#
func OffscreenOpen(patches: func(): Vector<Patch>, view: func(Builder), width: int, height: int, scale: f64): bool

Builds and draws a page into a canvas without a window and keeps it, so later changes can be drawn as a partial repaint. Returns false when one is already open.

The example for this item is shown with the ones above: gui-ui-Offscreen.salam

OffscreenUpdate

#
func OffscreenUpdate(poison: bool): int

Runs the page again, repaints only the damaged part of the canvas and returns the damaged area in pixels. With true the damaged part is filled with a marker colour first, so a missed pixel shows.

The example for this item is shown with the ones above: gui-ui-Offscreen.salam

OffscreenRepaintAll

#
func OffscreenRepaintAll()

Repaints the whole canvas, to compare with a partial repaint.

The example for this item is shown with the ones above: gui-ui-Offscreen.salam

OffscreenCanvas

#
func OffscreenCanvas(): paint.Canvas

The canvas of the open offscreen page. It is freed by OffscreenClose.

The example for this item is shown with the ones above: gui-ui-Offscreen.salam

OffscreenClose

#
func OffscreenClose()

Closes the offscreen page and frees its canvas.

The example for this item is shown with the ones above: gui-ui-Offscreen.salam

Dump

#
func Dump(): str

The same report for the page that is running now.

The example for this item is shown with the ones above: gui-ui-Run.salam

NewPatch

#
func NewPatch(): Patch

Returns an empty patch.

The example for this item is shown with the ones above: gui-ui-Style.salam

NoPatches

#
func NoPatches(): Vector<Patch>

Returns an empty list of patches, for a page that uses none.

gui-ui-Render.salam
import gui.ui
import gui.style

func none(): Vector<ui.Patch>:
    ret ui.NoPatches()
end

func view(b: ui.Builder):
    b.Open("box")
    b.SetText(ui.STATE_BASE, style.P_PADDING, "10")
    b.SetText(ui.STATE_BASE, style.P_BACKGROUND, "#ffffff")
    b.Open("box")
    b.Id("card")
    b.SetText(ui.STATE_BASE, style.P_WIDTH, "50")
    b.SetText(ui.STATE_BASE, style.P_HEIGHT, "30")
    b.SetText(ui.STATE_BASE, style.P_BACKGROUND, "#3366aa")
    b.Close()
    b.Close()
end

func main:
    mut one := ui.RenderPage((): ret none() end, (b: ui.Builder): view(b) end, 200, 100)
    mut two := ui.RenderPageScaled((): ret none() end, (b: ui.Builder): view(b) end, 200, 100, 2.0)
    println "scale 1", one.width, one.height
    println "scale 2", two.width, two.height
    p := one.Pixel(30, 20)
    q := two.Pixel(60, 40)
    println "card", p.r, p.g, p.b, q.r, q.g, q.b
    println ui.DumpPage((): ret none() end, (b: ui.Builder): view(b) end, 200, 100)
    one.Free()
    two.Free()
end
Output
scale 1 200 100
scale 2 400 200
card 51 102 170 51 102 170
page 0 0 200 100
  box 0 0 200 50
    box#card 10 10 50 30

RunPageWith

#
func RunPageWith(patches: func(): Vector<Patch>, view: func(Builder), options: Options)

Opens a window with these options, builds the page and runs until the window is closed. Prints the reason when no window can be opened.

The example for this item is shown with the ones above: gui-ui-Run.salam

RunPage

#
func RunPage(patches: func(): Vector<Patch>, view: func(Builder))

Opens a window with the default options, builds the page and runs until it is closed.

The example for this item is shown with the ones above: gui-ui-Run.salam

RenderPage

#
func RenderPage(patches: func(): Vector<Patch>, view: func(Builder), width: int, height: int): paint.Canvas

Builds the page, lays it out and paints it for this width and height, and returns the canvas without opening a window. Free the canvas with Free.

The example for this item is shown with the ones above: gui-ui-Render.salam

RenderPageScaled

#
func RenderPageScaled(patches: func(): Vector<Patch>, view: func(Builder), width: int, height: int, scale: f64): paint.Canvas

Like RenderPage at a device scale, so a scale of 2.0 gives a canvas twice as wide and as high.

The example for this item is shown with the ones above: gui-ui-Render.salam

DumpPage

#
func DumpPage(patches: func(): Vector<Patch>, view: func(Builder), width: int, height: int): str

Builds and lays out the page like RenderPage and returns a text report with one line per box: kind, id, x, y, width and height, indented by depth.

The example for this item is shown with the ones above: gui-ui-Builder.salam

PageHas

#
func PageHas<T>(o: Option<T>): bool

PageHas

#
func PageHas<T>(o: T): bool

PageVal

#
func PageVal<T>(o: Option<T>): T

PageVal

#
func PageVal<T>(o: T): T

PageRequired

#
func PageRequired<T>(o: T): T

RunSteps

#
func RunSteps(steps: str)

Plays script steps such as click #save, type hello, key Tab, style #a color, dump and quit, one per line.

The example for this item is shown with the ones above: gui-ui-Events.salam

ScopeSite

#
func ScopeSite(site: int, comp: int, word: int, mask: i64)

Declares that a scope site reads this bit mask of a state word and belongs to this component. Called from the function given to Reactive.

The example for this item is shown with the ones above: gui-ui-Scope.salam

TouchInstance

#
func TouchInstance(comp: int, slot: int)

Marks the state of one component instance as changed, so the scopes that read it run again.

The example for this item is shown with the ones above: gui-ui-Scope.salam

TouchInstanceVars

#
func TouchInstanceVars(comp: int, slot: int, mask: int)

Marks some variables of one component instance as changed (a bit mask), so only the scopes that read those variables run again. TouchInstance marks all of them.

The example for this item is shown with the ones above: gui-ui-Scope.salam

ScopeDerive

#
func ScopeDerive(comp: int, derive: func(int, int): int)

Registers a function for a component that works out its derived values after a write: it gets the instance and the mask of changed variables, and returns the mask with the derived values that really changed added. Written by the compiler.

The example for this item is shown with the ones above: gui-ui-Scope.salam

ScopeVars

#
func ScopeVars(site: int, comp: int, mask: int)

Declares which bits of a component's variables the scope at this site reads, for the instance that encloses the scope. Called from the function given to Reactive.

The example for this item is shown with the ones above: gui-ui-Scope.salam

Invalidate

#
func Invalidate()

Marks the whole view as changed, so every scope runs again.

The example for this item is shown with the ones above: gui-ui-Scope.salam

ScopeNodes

#
func ScopeNodes(): int

Number of nodes built by scope re-runs so far, which tests use to show how little of the view a change rebuilt.

The example for this item is shown with the ones above: gui-ui-Scope.salam

ScopeRuns

#
func ScopeRuns(): int

Number of scopes that ran in the last update, which tests use to prove that only the scope that read the state ran.

The example for this item is shown with the ones above: gui-ui-Scope.salam

ScopeSkips

#
func ScopeSkips(): int

Number of scope re-runs that returned early through Memo, which tests use to show that unchanged rows were not rebuilt.

The example for this item is shown with the ones above: gui-ui-Scope.salam