Coming from iced¶
This page is for people who have written iced apps in Rust. yugui uses iced from Python: your view builds a tree of plain Python values, which yugui turns into iced widgets with iced's own functions, and your update runs when iced delivers a message. So the widgets, the layout rules, the themes and the look are iced's, and most of what you know carries over. How yugui works has the details.
The application¶
| iced | yugui |
|---|---|
iced::application(App::new, App::update, App::view) |
a subclass of ui.App[Model] with init, update and view, run with ui.run(App()) |
update(&mut self, message) |
update(self, model, message): return a new model, or change it in place and return nothing |
enum Message { ... } |
any Python objects: strings, dataclasses, tuples; match takes the place of the enum's match |
.title(...), .theme(...), .subscription(...) |
title, theme and subscription, each a class attribute, or a method that takes the model |
Theme::TokyoNight |
theme = "Tokyo Night" (the names are in ui.THEMES) |
iced::daemon and window::open |
windows(model) yields ui.window(key, content, ...) for each secondary window that should be open |
The model is passed to view and update instead of being self: the app object holds configuration, not state.
Widgets¶
Builders become functions, and chained methods become keyword arguments:
# iced: button(text("Save")).on_press(Message::Save).padding([5, 10]).style(button::danger)
ui.button("Save", on_press="save", padding=(5, 10), style="danger")
# iced: column![a, b, c].spacing(10).align_x(Center).width(Fill)
ui.column(a, b, c, spacing=10, align_x="center", width="fill")
- Lengths:
Fillis"fill",Shrinkis"shrink",FillPortion(2)is("fill", 2), and pixels are numbers. - Padding:
padding(10),padding([5, 10])and the four-sided form are10,(5, 10)and(top, right, bottom, left). - Style functions are named:
style="danger"forbutton::danger,style="bordered"forcontainer::bordered_box, and so on. The reference lists the names each widget takes. - A widget is disabled the same way: without
on_press(oron_input,on_toggle...), it doesn't react. - Strings are text widgets:
ui.column("Hello", ui.button(...))works likecolumn!["Hello", button(...)].NoneandFalsechildren are skipped, socondition and widgetplays the part ofOption<Element>. - Helpers:
space::horizontal()isui.space(width="fill"),rule::horizontal(1)isui.rule(thickness=1),center(x)isui.center(x),.explain(color)isui.explain(x, color).
Widgets with a value send their handler's result: on_input=Message::NameChanged becomes on_input=NameChanged with a one-field dataclass, or on_input=ui.set("name"), which stores the value in the model's name field without a message type of your own.
Subscriptions and tasks¶
| iced | yugui |
|---|---|
time::every(Duration::from_millis(500)).map(...) |
ui.every(0.5, "tick") |
keyboard::listen().filter_map(...) |
ui.on_key(handler): the handler gets a KeyEvent and returns a message, or None to send nothing |
window::events() |
ui.on_window(handler) |
Task::perform(future, Message::Done) |
ui.perform(fn, *args, then=Done, catch=Failed), which runs fn on a thread of its own |
operation::focus(id), focus_next(), focus_previous() |
ui.focus(id), ui.focus_next(), ui.focus_previous() |
operation::scroll_to(id, offset), snap_to(id, offset) |
ui.scroll_to(id, x=, y=), ui.snap_to(id, x=, y=) |
iced::exit() |
ui.exit() |
update returns (model, task), or the task alone to keep the model. Like iced, yugui leaves Tab to the app: map it to a message that returns ui.focus_next(), as examples/iced/modal.py does.
Keys that matter only while part of the view is shown don't need a subscription: ui.shortcuts(content, {"Escape": "close", "cmd+s": "save"}) sends those messages for keys the content didn't take, such as a focused text input's. ui.modal and ui.popup use it to close on Escape.
The canvas¶
There is no canvas::Program to implement. ui.canvas(shapes, ...) takes a list of shapes (circle, rect, line, path, label) and handlers (on_press, on_release, on_move, on_hover), and draws the shapes in Rust without calling back into Python. State that a Program would keep in its State lives in the model instead, updated by the handlers' messages: examples/iced/bezier_tool.py keeps the curve being drawn there.
Shapes with a key can be hit: a press reports the key of the shape under the pointer, and on_hover the key under the pointer when it changes. viewbox=(x, y, width, height) draws in the drawing's own coordinates, scaled to the canvas, where iced code would read bounds.size() in draw.
Testing¶
yugui.testing.Driver plays the part of iced_test's simulator, through the same widgets: d.click("Save") finds the text, d.click_at, d.type, d.key, d.scroll and d.snapshot(path) do what their names say. Unlike the simulator, the Driver also sends the messages to update, rebuilds the view, and keeps widget state between calls, as a running app would.
Text editors and combo boxes¶
In iced, the app owns a text_editor::Content and applies each Action to it, and owns a combo_box::State built from the options. In yugui, both live on the Rust side, kept by the widget's id, which these two widgets require:
ui.text_editor(model.body, id="body", on_change=ui.set("body"))
ui.combo_box(LANGUAGES, model.language, id="language", on_select=ui.set("language"))
yugui applies the editor's actions itself and calls on_change with the whole text after each edit; moving the cursor or selecting sends nothing. Passing a different text from the model, after an undo say, replaces the content, as Content::with_text would. The combo box's state is rebuilt when its options change.
What's missing¶
Widgets whose state the app keeps, other than the two above, and anything you would write by implementing a Rust trait aren't available yet: pane_grid, markdown, rich_text, responsive, lazy, shader, custom Widgets, and style closures (styles are named). The examples ported from iced say what each leaves out.