KScript docs
EN

Desktop apps

How a window works

A window works like a <live> block on a page. Variables and fn functions directly inside Window run once, and hold the window's state. Everything else is the layout.

After every handler runs, KScript draws the layout again from the variables. if, for and { } work as they do anywhere else. Controls stay the same object from one draw to the next, so typing and focus aren't lost:

<KScript.Window title="Shopping">
  <let name="items" value="['Milk', 'Bread']" />
  <let name="item" value="''" />

  <Column gap="8">
    <for each="name" in="$items">
      <Text>{$name}</Text>
    </for>
    <if $items->count() == 0>
      <Text color="gray">Nothing to buy</Text>
    </if>
    <TextBox bind="$item" placeholder="Add an item" on_enter="$items = $items->push($item)" />
  </Column>
</KScript.Window>
import KScript.UI

Window(title: 'Shopping') {
  $items = ['Milk', 'Bread']
  $item = ''

  Column(gap: 8) {
    for name in $items {
      Text "{$name}"
    }
    if $items->count() == 0 {
      Text(color: 'gray') "Nothing to buy"
    }
    TextBox(bind: $item, placeholder: 'Add an item', on_enter: $items = $items->push($item))
  }
}

Window can also go in the middle of a script. The script waits while the window is open, then carries on with the window's changes.