In a window
.
So far the tree has drawn into memory. To put it on a screen, something has to own a window, a GPU surface, the system's input, and the timing of frames. That is the
window host
,
ui-tree-window
.
The host runs the loop. Your part is a client: two functions it calls.
A counter in a window
The counter below. Where the browser has WebGPU this is the program itself, with a canvas in the page as its window, and you can click it.
The counter below. Where the browser has WebGPU this is the program itself, with a canvas in the page as its window, and you can click it.
The program is two files. The client is the part that knows about the counter:
examples/window/src/client.silk
Copy
//! A counter for the window host, on the tree alone: no framework. The host owns the window,
//! the GPU, the input and the frames, and calls this client at two moments: `start` to build
//! the tree, and `pump` after input to answer its events.
//!
//! The host hands the client the tree's engine, `UiTree`, so the client speaks the mutation
//! protocol: a `Batch` of mutations with node numbers of its own.
import
ui_tree.css
import
ui_tree.css { px, percent,
Keyword
}
import
ui_tree.legacy {
Attribute
,
Batch
,
NodeKind
,
TreeError
,
UiTree
}
import
ui_tree.mutation {
Align
,
Role
,
StyleState
,
LISTEN_CLICK
}
import
ui_tree.node {
Event
,
EventKind
}
import
ui_tree_window.host {
WindowClient
}
import
silk.collections.list {
List
}
import
silk.color.rgba {
Rgba
}
import
silk.mem.allocator {
Allocator
,
OutOfMemoryError
}
// The client's node numbers.
const
ROOT
: u32 =
1
const
BUTTON
: u32 =
2
const
LABEL
: u32 =
3
/// What the client remembers between calls.
pub
struct
Counter
{
pub
clicks: u32, }
pub
struct
Client
{}
impl
WindowClient
<
Counter
,
never
,
never
>
for
Client
{
/// Called once, before the first frame.
effect
fn
start
(self: &
mut
Self
, state: &
mut
Counter
, tree: &
mut
UiTree
<'_>) -> () !
TreeError
|
OutOfMemoryError
? &
mut
Allocator
{
let
mut
batch =
Batch
.
make
()
run
batch.
fontFamily
(
1
,
"Noto Sans, sans-serif"
)
run
batch.
create
(
ROOT
,
NodeKind
.
Box
)
run
batch.
style
(
ROOT
, &[ css.
display
(
Keyword
.
Flex
), css.
height
(
percent
(
100.0
)), css.
alignItems
(
Align
.
Center
), css.
justifyContent
(
Align
.
Center
), css.
fontFamily
(
1
), css.
fontSize
(
px
(
18.0
)), ], )
run
batch.
root
(
ROOT
)
run
batch.
box
(
ROOT
,
BUTTON
, &[ css.
padding
(
px
(
12.0
)), css.
borderRadius
(
px
(
10.0
)), css.
color
(
Rgba
<f32> { r:
1.0
, g:
1.0
, b:
1.0
, a:
1.0
}), css.
backgroundColor
(
Rgba
<f32> { r:
0.06
, g:
0.46
, b:
0.43
, a:
1.0
}), ], )
run
batch.
styleFor
(
BUTTON
,
StyleState
.
Hover
, &[css.
backgroundColor
(
Rgba
<f32> { r:
0.05
, g:
0.37
, b:
0.35
, a:
1.0
})], )
run
batch.
attribute
(
BUTTON
,
Attribute
.
Role
{ value:
Role
.
Button
})
run
batch.
attribute
(
BUTTON
,
Attribute
.
TabIndex
{ value:
0
})
run
batch.
listen
(
BUTTON
,
LISTEN_CLICK
)
run
batch.
text
(
BUTTON
,
LABEL
,
"Click me"
)
run
tree.
apply
(
run
batch.
take
()) }
/// Called after input reached the tree: take its events and answer them.
effect
fn
pump
(self: &
mut
Self
, state: &
mut
Counter
, tree: &
mut
UiTree
<'_>) -> () !
TreeError
|
OutOfMemoryError
? &
mut
Allocator
{
let
mut
events =
List
<
Event
>.
make
()
run
tree.
takeEvents
(&
mut
events)
let
all = events.
asSlice
()
let
mut
i: usize =
0
while
i < all.length {
if
all[i].kind ==
EventKind
.
Click
&& all[i].target ==
BUTTON
{ state.clicks = state.clicks +
1
let
mut
batch =
Batch
.
make
()
run
batch.
setText
(
LABEL
,
if
state.clicks ==
1
{
"Clicked once"
}
else
{
"Clicked again"
}, )
run
tree.
apply
(
run
batch.
take
()) } i = i +
1
} } }
And
main
hands it to the host:
examples/apps/tree-window/src/main.silk
Copy
//! The tree counter in a native window. The client is `examples/window/src/client.silk`:
//! this file hands it to the window host.
import
window_client.client {
Client
,
Counter
}
import
ui_fixtures.fixtures
as
fixtures
import
ui_inspect_server.lib {
Inspector
}
import
ui_tree.legacy {
Mutation
,
TreeError
}
import
ui_tree_window.console {
StderrWriter
}
import
ui_tree_window.host {
RunOptions
, runWindow,
StartedGpu
, startGpu }
import
ui_tree_window.platform_thread {
Launched
, launch }
import
atlas.atlas {
AtlasError
}
import
vector.geometry_error {
GeometryError
}
import
webgpu.runtime {
GpuError
}
import
window.types {
WindowError
}
import
silk.collections.list {
List
}
import
silk.core.
effect
{
Effect
}
import
silk.core.option {
Option
}
import
silk.mem.allocator {
Allocator
,
OutOfMemoryError
}
effect
fn
counter
(platform:
Option
<
Launched
>) -> i32 !
TreeError
|
WindowError
|
GpuError
|
AtlasError
|
GeometryError
|
OutOfMemoryError
? &
mut
Allocator
{
// The GPU first: it takes longer than everything else up to the first frame.
let
gpu =
run
startGpu
(
false
)
let
fonts =
run
fixtures.
fonts
()
let
mut
state =
Counter
{ clicks:
0
}
let
mut
client =
Client
{}
let
options =
RunOptions
{ title:
"Tree counter"
, width:
400.0
, height:
250.0
, check:
false
, checkClick:
""
, checkExpect:
""
, lowestLatency:
false
, platform:
move
platform, gpu:
Option
<
StartedGpu
>.
some
(
move
gpu), }
return
run
runWindow
( &
mut
state, &fonts,
List
<
Mutation
>.
make
(),
move
options,
Inspector
.
off
(), &
mut
client, ) }
effect
fn
failed<
E
>(error:
E
) -> i32 {
drop
error
StderrWriter
.
print
(
"tree-window: the window could not be shown\n"
, &())
return
2
}
fn
app
(platform:
Option
<
Launched
>) -> i32 {
let
mut
allocator =
Allocator
.
systemAllocatorProvider
()
return
run
Effect
.
catchAll
(
counter
(
move
platform), failed) |>
Effect
.provideMut<
Allocator
>(&
mut
allocator) }
pub
fn
main
() -> i32 {
let
mut
allocator =
Allocator
.
systemAllocatorProvider
()
// The main thread keeps the system's event loop. The client runs on a thread of its own.
return
run
Effect
.
catchAll
(
launch
( |platform:
Option
<
Launched
>| -> i32 {
return
app
(
move
platform) }, ), failed, ) |>
Effect
.provideMut<
Allocator
>(&
mut
allocator) }
examples/apps/tree-window/silk.toml
Copy
[package]
name
=
"tree-window"
description
=
"A UI tree in a native window, with no framework"
version
=
"0.1.0"
kind
=
"executable"
[dependencies]
window_client
= { path =
"../../window"
, package =
"ui-docs-window-client"
}
ui_tree
= { path =
"../../../../modules/tree"
, package =
"ui-tree"
}
ui_tree_window
= { path =
"../../../../modules/tree/window"
, package =
"ui-tree-window"
}
ui_fixtures
= { path =
"../../../../modules/tree/fixtures"
, package =
"ui-tree-fixtures"
}
ui_inspect_server
= { path =
"../../../../modules/inspect/server"
, package =
"ui-inspect-server"
}
window
= { git =
"https://github.com/silk-lang/lib-native.git"
, rev =
"3386d71470b7e45007396591ff231448e98f992f"
, path =
"modules/platform/window"
, package =
"platform-window"
}
webgpu
= { git =
"https://github.com/silk-lang/lib-native.git"
, rev =
"3386d71470b7e45007396591ff231448e98f992f"
, path =
"modules/gpu/webgpu"
, package =
"gpu-webgpu"
}
atlas
= { git =
"https://github.com/silk-lang/lib-graphics.git"
, rev =
"08ca82abc883aca7c1742b665a22453fb8995f50"
, path =
"modules/atlas"
, package =
"graphics-atlas"
}
vector
= { git =
"https://github.com/silk-lang/lib-graphics.git"
, rev =
"08ca82abc883aca7c1742b665a22453fb8995f50"
, path =
"modules/vector"
, package =
"graphics-vector"
}
Shell
Copy
silk task native demo docs/examples/apps/tree-window
The client
examples/window/src/client.silk
Copy
impl
WindowClient
<
Counter
,
never
,
never
>
for
Client
{
/// Called once, before the first frame.
effect
fn
start
(self: &
mut
Self
, state: &
mut
Counter
, tree: &
mut
UiTree
<'_>) -> () !
TreeError
|
OutOfMemoryError
? &
mut
Allocator
{
let
mut
batch =
Batch
.
make
()
run
batch.
fontFamily
(
1
,
"Noto Sans, sans-serif"
)
run
batch.
create
(
ROOT
,
NodeKind
.
Box
)
run
batch.
style
(
ROOT
, &[ css.
display
(
Keyword
.
Flex
), css.
height
(
percent
(
100.0
)), css.
alignItems
(
Align
.
Center
), css.
justifyContent
(
Align
.
Center
), css.
fontFamily
(
1
), css.
fontSize
(
px
(
18.0
)), ], )
run
batch.
root
(
ROOT
)
run
batch.
box
(
ROOT
,
BUTTON
, &[ css.
padding
(
px
(
12.0
)), css.
borderRadius
(
px
(
10.0
)), css.
color
(
Rgba
<f32> { r:
1.0
, g:
1.0
, b:
1.0
, a:
1.0
}), css.
backgroundColor
(
Rgba
<f32> { r:
0.06
, g:
0.46
, b:
0.43
, a:
1.0
}), ], )
run
batch.
styleFor
(
BUTTON
,
StyleState
.
Hover
, &[css.
backgroundColor
(
Rgba
<f32> { r:
0.05
, g:
0.37
, b:
0.35
, a:
1.0
})], )
run
batch.
attribute
(
BUTTON
,
Attribute
.
Role
{ value:
Role
.
Button
})
run
batch.
attribute
(
BUTTON
,
Attribute
.
TabIndex
{ value:
0
})
run
batch.
listen
(
BUTTON
,
LISTEN_CLICK
)
run
batch.
text
(
BUTTON
,
LABEL
,
"Click me"
)
run
tree.
apply
(
run
batch.
take
()) }
/// Called after input reached the tree: take its events and answer them.
effect
fn
pump
(self: &
mut
Self
, state: &
mut
Counter
, tree: &
mut
UiTree
<'_>) -> () !
TreeError
|
OutOfMemoryError
? &
mut
Allocator
{
let
mut
events =
List
<
Event
>.
make
()
run
tree.
takeEvents
(&
mut
events)
let
all = events.
asSlice
()
let
mut
i: usize =
0
while
i < all.length {
if
all[i].kind ==
EventKind
.
Click
&& all[i].target ==
BUTTON
{ state.clicks = state.clicks +
1
let
mut
batch =
Batch
.
make
()
run
batch.
setText
(
LABEL
,
if
state.clicks ==
1
{
"Clicked once"
}
else
{
"Clicked again"
}, )
run
tree.
apply
(
run
batch.
take
()) } i = i +
1
} } }
A
WindowClient
has two functions that matter:
Function
Called
Your job
start
Once, before the first frame
Build the tree
pump
After input reached the tree
Take the events and answer them
Counter
is the client's own state, which the host keeps and hands back on every call.
The host does the rest. It turns the system's events into
Input
, dispatches them, asks for frames when the tree needs one, copies glyphs to the GPU, draws the scene, blinks the caret, serves the clipboard and input methods, and publishes the tree to screen readers.
Note:
The host hands the client the tree's engine,
UiTree
, and not the
Tree
of the earlier pages. So this client builds its nodes as a
Batch
of mutations with node numbers of its own, and reads events as flat records with a
kind
and a
target
. That is the same data as the direct calls, in its protocol form. Moving the host to the
Tree
API is planned.
Running it
examples/apps/tree-window/src/main.silk
Copy
effect
fn
counter
(platform:
Option
<
Launched
>) -> i32 !
TreeError
|
WindowError
|
GpuError
|
AtlasError
|
GeometryError
|
OutOfMemoryError
? &
mut
Allocator
{
// The GPU first: it takes longer than everything else up to the first frame.
let
gpu =
run
startGpu
(
false
)
let
fonts =
run
fixtures.
fonts
()
let
mut
state =
Counter
{ clicks:
0
}
let
mut
client =
Client
{}
let
options =
RunOptions
{ title:
"Tree counter"
, width:
400.0
, height:
250.0
, check:
false
, checkClick:
""
, checkExpect:
""
, lowestLatency:
false
, platform:
move
platform, gpu:
Option
<
StartedGpu
>.
some
(
move
gpu), }
return
run
runWindow
( &
mut
state, &fonts,
List
<
Mutation
>.
make
(),
move
options,
Inspector
.
off
(), &
mut
client, ) }
runWindow
takes the client's state, the fonts, mutations to apply before anything else, the window's options, an inspector, and the client. It returns when the window closes.
Inspector.off()
runs without the inspector. A client that wants the devtools passes a started one instead, and they work on a bare tree exactly as they do on a Silk UI app, because they only ever look at the tree.
examples/apps/tree-window/src/main.silk
Copy
pub
fn
main
() -> i32 {
let
mut
allocator =
Allocator
.
systemAllocatorProvider
()
// The main thread keeps the system's event loop. The client runs on a thread of its own.
return
run
Effect
.
catchAll
(
launch
( |platform:
Option
<
Launched
>| -> i32 {
return
app
(
move
platform) }, ), failed, ) |>
Effect
.provideMut<
Allocator
>(&
mut
allocator) }
As in a Silk UI app, the main thread keeps the system's event loop and the client runs on its own.
Other hooks
The two functions above are enough for most clients. The interface has more, each with a default:
Hook
For
setup
,
configure
,
opened
Preparing resources and window options before the window shows
event
Seeing native events before the tree does: menus, dialogs
closing
Keeping the window open, to ask about unsaved work
poll
,
wakeAfter
,
nextDeadline
Background work and timers
layout
,
frame
,
scene
,
draw
Custom layout passes and drawing your own GPU content with the tree's
More than one window
A tree can hold several
roots
: separate top-level trees that share fonts, the glyph atlas and one space of node ids. The host shows every root in an OS window of its own.
Silk
Copy
let
mut
options =
RootOptions
.
default
() options.title =
run
String
.
copy
(
"Second"
)
run
batch.
openRoot
(
2
,
40
,
move
options)
The client opens a root with a number it chooses, a node, and options such as a title and a size. What happens to that window comes back as root events: resized, focused, a request to close. The host never closes a root on its own. The client answers a close request with
batch.closeRoot(2)
, or does not.
This is what Silk UI's windows are built on.
Page zoom
In a window, Ctrl (Cmd on macOS) with
+
,
-
and
0
zoom the page as a browser does: lengths and text grow, and the page reflows, with media queries and viewport units reading a viewport that many times smaller. A key handler that prevents the key keeps it from zooming. To set the zoom from code, for a setting the app remembers, call
tree.setPageZoom(1.5)
. In a browser tab the browser zooms the page itself.
In a browser tab
The same host runs in a browser: the program is built for WebAssembly and draws on a canvas with WebGPU. That is a different thing from Silk UI's document backend, which uses real DOM nodes. On a canvas the tree does its own layout and text, pixel for pixel as it does natively.
The repository's small window counter runs this way. This serves it and prints the address to open:
Shell
Copy
silk task browser serve