Immediate-mode graphical interfaces for MoonBit.
fn account(ui : @ctx.Ui, name : String, remember : Bool) -> (String, Bool) {
@widget.heading(ui, "Account") |> ignore
let name = @widget.edit(ui, name, hint="your name").0
let remember = @widget.checkbox(ui, "Remember me", remember).0
if @widget.button(ui, "Save").clicked {
println("saved " + name)
}
(name, remember)
}let name_tag : @view.Tag = @view.Tag::new(1)
let remember_tag : @view.Tag = @view.Tag::new(2)
let save_tag : @view.Tag = @view.Tag::new(3)
fn account_tree(name : String, remember : Bool) -> Array[@view.View] {
[
@view.heading("Account"),
@view.edit(name, name_tag, hint="your name"),
@view.checkbox("Remember me", remember_tag, remember),
@view.button("Save", save_tag),
]
}
fn account_step(
ui : @ctx.Ui,
interp : @view.Interp,
name : String,
remember : Bool,
) -> (String, Bool) {
interp.show(ui, account_tree(name, remember))
let mut name = name
let mut remember = remember
for event in interp.events() {
match event {
Edited(tag~, text~) => if tag == name_tag { name = text }
Toggled(tag~, value~) => if tag == remember_tag { remember = value }
Clicked(tag) => if tag == save_tag { println("saved " + name) }
_ => ()
}
}
(name, remember)
}| Package | What it does |
|---|---|
| emath | Vec2, Rect, Align, Margin, and the interpolation everything else is built from |
| id | Ids derived from a parent and a piece of salt, which is what connects a widget to the same widget last frame |
| input | The events a host hands over, folded once a frame into the state widgets ask questions of |
| mem | What survives a frame: focus, the widget being dragged, and a value per widget, tidied at the end of every frame |
| paint | Colours, strokes, fonts, and the five shapes a frame is made of |
| layout | Where things go: placer cursors in four directions, and the flows and bounds a tree is measured against |
| hit | What the pointer is over, and the Response a widget answers with |
| text | Breaking, aligning, eliding, and turning a click into a caret position |
| ctx | The frame itself, Ui — the value you spend the frame with — and the style everything is drawn in |
| widget | The controls, from a label to a table |
| view | The tree: what a node is, how big it is, and the interpreter that draws a tree and remembers what it produced |
| back | The backend contract: three ways to measure, nine to draw, and a recorder that writes them down |
| Package | What it does |
|---|---|
| web | What both browser backends share: the surface, the text metrics, and the input coming the other way |
| canvas | A frame painted on a 2D canvas |
| html | The same frame as elements in the document, for pages that want selectable text and the rest of what H5 already has |
moon add moonbitstack/moonegui
moon add moonbitstack/moonpane # only for a program that draws into a pagelet canvas : @canvas.Canvas = @canvas.Canvas::new()
let context : @ctx.Ctx = @ctx.Ctx::new(canvas)
canvas.listen()
// then, once a frame:
let out = context.run(canvas.raw(now, dt), fn(ui) {
@widget.heading(ui, "moonegui") |> ignore
if @widget.button(ui, "Press me").clicked {
clicks = clicks + 1
}
@widget.label(ui, "clicked " + clicks.to_string() + " times") |> ignore
})
canvas.clear(context.style().background)
@back.play(out.shapes, canvas)moon build --target js
python -m http.server 8741
# http://127.0.0.1:8741/hosts/pane/src/examples/browser/index.html
# ?html for elements instead of a canvas, ?view for the tree, ?view&html for both| Group | Functions |
|---|---|
| Text | label, weak, heading, caption, hyperlink, badge |
| Buttons | button, accent_button, quiet_button |
| Choices | checkbox, radio, switch, slider, drag_value, combo, tabs |
| Fields | edit, text_area |
| Containers | panel, group, scroll_area, list, table, separator |
| Motion | progress, spinner |
Install
Download zipImmediate-mode graphical interfaces for MoonBit.