remedy-ui 1.1.2

RemedyUI for apps: open windows, run them on the platform's bridge with the GPU renderer (CPU fallback), persist window geometry, timers and the clipboard.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# Moving from Qt or PyQt

RemedyPDF is the PyQt app moving over. This guide is for anyone coming from Qt Widgets,
PyQt or PySide.

Qt and RemedyUI are both retained: a tree of objects that lives between frames, with layouts
and a style sheet. The differences are in how state flows. Qt connects signals to slots by
hand and the widget keeps its own copy of the value. In RemedyUI the value lives in a
reactive signal the app owns, and the widget shows it.

## Concept map

| Qt / PyQt | RemedyUI |
|---|---|
| `QApplication`, `app.exec()` | [`crate::run`] with a setup closure |
| `QMainWindow`, `QWidget` as a window | [`crate::App::open`] with a [`crate::WindowSpec`] |
| A `QWidget` subclass | A function returning an [`crate::El`] |
| A widget tree with parents | Elements joined with `.child(..)` |
| `QHBoxLayout`, `QVBoxLayout`, `QGridLayout` | [`crate::widgets::row`], [`crate::widgets::column`], grid tracks in [`crate::El::layout`] |
| `addStretch`, `setSizePolicy` | [`crate::widgets::spacer`], [`crate::El::grow`], [`crate::El::fill_width`] |
| `Q_PROPERTY` with a `NOTIFY` signal | A [`crate::reactive::Signal`] |
| `connect(sender, signal, receiver, slot)` | A closure: `on_click(move \|\| ..)`, or a binding that reads a signal |
| `QAction` with a shortcut | A command in [`crate::nav::Commands`] |
| `QToolBar`, `QMenu` | [`crate::nav::toolbar`], [`crate::widgets::menu`], [`crate::widgets::context_menu`] |
| `QMenuBar` (`menuBar().addMenu("&File")`) | [`crate::widgets::menu_bar`] of [`crate::widgets::MenuItem::submenu`] with `.mnemonic('f')` |
| `QShortcut` | A command's chords; people can rebind them ([`crate::nav::Commands::rebind`]) |
| QSS (Qt style sheets) | A [`crate::style::StyleSheet`] over theme tokens; typed tokens (`Space::S3`, `Radius::R2`) in code |
| `QPalette` | A [`crate::style::Theme`] colors by role, type scale, spacing, radii |
| `QAbstractItemModel` + `QTableView` | [`crate::data::table`] over a `Signal<Vec<T>>` with [`crate::data::Column`]s |
| `QTreeView` | [`crate::data::tree_view`] |
| `QListView` | [`crate::widgets::list`], [`crate::widgets::virtual_list`] |
| `paintEvent` and `QPainter` | [`crate::El::paint`] or [`crate::widgets::canvas`] |
| `QThread`, `QRunnable` | [`crate::App::background`]; [`crate::App::background_cancellable`] for work that checks `isInterruptionRequested` |
| `QTimer::singleShot` | [`crate::App::after`], [`crate::Ui::after`] |
| `QSettings` for window geometry | [`crate::WindowSpec::persist_geometry`] |
| `QSettings` for other state | [`crate::reactive::Signal::persist`]: saved on change, restored at start |
| `setVisible`, `QStackedWidget` | [`crate::show`] with a fallback, [`crate::El::hidden`], [`crate::nav::NavStack`] |
| `QPropertyAnimation` | [`crate::El::animate`] with a spring or a curve from [`crate::motion`] |
| A slot wrapped in `try`/`except` that shows an error | [`crate::catch_errors`] around the content |
| `QDialog::exec()` | [`crate::widgets::dialog`] or [`crate::widgets::modal`]; they do not block |
| `QFileDialog` | [`crate::App::file_dialog`] |
| `QSystemTrayIcon` | [`crate::App::set_tray`] and [`crate::App::on_tray`] |
| `QGraphicsView` of pages | [`crate::pages`]: a page view over pages the app renders |

## Widgets and slots

```python
class Counter(QWidget):
    def __init__(self):
        super().__init__()
        self.count = 0
        self.label = QLabel("Clicked 0 times")
        button = QPushButton("Add one")
        button.clicked.connect(self.add)
        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(button)

    def add(self):
        self.count += 1
        self.label.setText(f"Clicked {self.count} times")
```

The slot has to remember to update the label. In RemedyUI the label is bound to the count,
so every change to the count shows, wherever it came from:

```
# use remedy_ui::testing::TestApp;
# use remedy_ui::AppConfig;
# fn names(ui: &Ui) -> Vec<String> {
#     ui.reader_view().into_iter().filter_map(|n| n.name).collect()
# }
# use remedy_ui::a11y::Role;
use remedy_ui::reactive::Signal;
use remedy_ui::widgets as w;
use remedy_ui::{App, El, Ui, WindowSpec};

fn counter(ui: &Ui) -> El {
    let count = Signal::new(0u32);
    w::column(ui)
        .gap(8.0)
        .child(w::text(ui, move || format!("Clicked {} times", count.get())))
        .child(w::button(ui, "Add one", move || count.update(|c| *c += 1)))
}

fn app(app: &App) {
    app.open(WindowSpec::new("Counter"), |ui, _| {
        ui.root().child(counter(ui));
    });
}

let mut app = TestApp::new(AppConfig::new("Counter"), app);
let Some(ui) = app.ui() else {
    unreachable!("the window is open")
};
assert!(app.press(Role::Button, "Add one"));
assert!(names(&ui).contains(&"Clicked 1 times".to_string()));
```

A Qt signal with several receivers becomes a signal several bindings read. A signal you
emit to tell other objects something happened becomes a closure argument (`on_open: impl
Fn(&Key)`), as the widgets themselves take them.

## Actions and shortcuts

`QAction` bundles a label, an icon, a shortcut and a slot. [`crate::nav::Commands`] does
the same, and also gives people a command palette, toolbars they arrange and shortcuts they
rebind. Agents run commands through the same table, with the person's consent.

```python
save = QAction(QIcon("save.png"), "Save", self)
save.setShortcut(QKeySequence.Save)
save.triggered.connect(self.save)
toolbar.addAction(save)
```

```
# use remedy_ui::testing::TestApp;
# use remedy_ui::AppConfig;
use std::cell::Cell;
use std::rc::Rc;

use remedy_ui::host::HostEvent;
use remedy_ui::input::{Chord, Key, KeyEvent, Modifiers, Platform};
use remedy_ui::nav::{Commands, toolbar};
use remedy_ui::reactive::Signal;
use remedy_ui::widgets::Icon;
use remedy_ui::{App, WindowSpec};

let saved = Rc::new(Cell::new(0));
let s = saved.clone();
let app = move |app: &App| {
    app.open(WindowSpec::new("Editor"), move |ui, _| {
        let commands = Commands::new();
        let ctrl_s = Chord::primary(ui.platform(), Key::Character("s".into()));
        commands.add("save", "Save", vec![ctrl_s], Some(Icon::Check), move || s.set(s.get() + 1));
        commands.attach(ui); // the window's keys reach the commands
        let slot = Signal::new(vec!["save".to_string()]);
        ui.root().child(toolbar(ui, "Document", &commands, slot));
    });
};
let mut app = TestApp::new(AppConfig::new("Editor"), app);
let Some(id) = app.host().window_ids().first().copied() else { unreachable!("the window is open") };
let key = KeyEvent::press(Key::Character("s".into()))
    .with_physical("KeyS")
    .with_modifiers(Modifiers::primary(Platform::default()));
app.host().send(HostEvent::Key(id, key));
app.frames(1);
assert_eq!(saved.get(), 1);
```

The toolbar shows the command's icon, with its label and shortcut in the tooltip.
`set_enabled` replaces `QAction::setEnabled`.

## Menus and the menu bar

`QMenuBar`, `QMenu` and `QAction` map to [`crate::widgets::menu_bar`] and
[`crate::widgets::MenuItem`]: a menu is a [`MenuItem::submenu`](crate::widgets::MenuItem::submenu)
of entries. Qt's `&` in a label becomes `.mnemonic(..)` with the letter, `addSeparator()`
becomes [`MenuItem::separator`](crate::widgets::MenuItem::separator), a checkable action
becomes a check entry, and an `QActionGroup` of exclusive actions becomes radio entries on one
signal. An action that is also a shortcut is a command, and
[`Commands::menu_item`](crate::nav::Commands::menu_item) makes its entry, so the menu shows the
command's label, shortcut and enabled state.

```python
bar = self.menuBar()
view = bar.addMenu("&View")
book = QAction("&Book Mode", self, checkable=True)
book.setShortcut("Ctrl+B")
view.addAction(book)
view.addSeparator()
scale = view.addMenu("&UI Scale")
group = QActionGroup(self)
for p in (100, 125):
    a = QAction(f"{p}%", self, checkable=True)
    group.addAction(a)
    scale.addAction(a)
```

```
use remedy_ui::input::Chord;
use remedy_ui::reactive::Signal;
use remedy_ui::testing::TestApp;
use remedy_ui::widgets::{MenuItem, menu_bar};

let book = Signal::new(false);
let scale = Signal::new(100);
let mut app = TestApp::window(move |ui, _| {
    let ctrl_b: Chord = "Ctrl+B".parse().unwrap_or_else(|e| unreachable!("{e}"));
    let view = MenuItem::submenu(
        "View",
        vec![
            MenuItem::check_signal("Book Mode", book).chord(ctrl_b).mnemonic('b'),
            MenuItem::separator(),
            MenuItem::submenu(
                "UI Scale",
                [100, 125].into_iter().map(|p| MenuItem::radio_signal(format!("{p}%"), scale, p)).collect(),
            )
            .mnemonic('u'),
        ],
    )
    .mnemonic('v');
    ui.root().child(menu_bar(ui, vec![view]));
});
assert!(app.press(remedy_ui::a11y::Role::MenuItem, "View"));
assert!(app.press(remedy_ui::a11y::Role::MenuItemCheckBox, "Book Mode"));
assert_eq!(book.get_untracked().ok(), Some(true));
```

Keyboard handling comes with it: Alt+V opens View wherever focus is, F10 or Alt alone reaches
the bar, arrows walk menus and submenus, and letters choose. In a narrow window the menus
that do not fit move into a "…" menu instead of being cut off.

## Layouts

```python
row = QHBoxLayout()
row.addWidget(QLabel("Zoom"))
row.addStretch()
row.addWidget(zoom_box)
```

```
# use remedy_ui::testing::TestApp;
# use remedy_ui::AppConfig;
use remedy_ui::layout::Align;
use remedy_ui::reactive::Signal;
use remedy_ui::widgets as w;
use remedy_ui::{App, WindowSpec};

fn app(app: &App) {
    app.open(WindowSpec::new("Zoom"), |ui, _| {
        let zoom = Signal::new(100.0f64);
        let row = w::row(ui)
            .align(Align::Center)
            .fill_width()
            .child(w::text(ui, "Zoom"))
            .child(w::spacer(ui))
            .child(w::stepper(ui, "Zoom", zoom, 1.0, 6400.0, 10.0));
        ui.root().child(row);
    });
}
# let mut app = TestApp::new(AppConfig::new("Zoom"), app);
# let Some(ui) = app.ui() else { unreachable!("open") };
# assert!(ui.reader_view().iter().any(|n| n.value.as_deref() == Some("100")));
```

Layout is flex and grid (as on the web), run before paint in the same frame. Sizes are in
logical pixels; the engine snaps to device pixels. See [`crate::guide::layout`].

## Animations

`QPropertyAnimation` animates a property between values over a duration. [`crate::El::animate`]
moves an element's opacity, colors, padding, radius, offset or scale to a target with a spring
or an easing curve, on the window's frame clock. A new target starts from where the value is,
with its speed, so an interrupted animation never jumps, and under reduced motion it is
instant:

```python
anim = QPropertyAnimation(effect, b"opacity")
anim.setDuration(200)
anim.setEndValue(0.0)
anim.start()
```

```
# use remedy_ui::testing::TestApp;
use remedy_ui::motion::{Motion, Pace, Target};
use remedy_ui::widgets as w;
use std::cell::OnceCell;
use std::rc::Rc;

let slot = Rc::new(OnceCell::new());
let s = slot.clone();
let mut app = TestApp::window(move |ui, _| {
    let banner = w::text(ui, "Saved");
    let _ = s.set(banner.clone());
    ui.root().child(banner);
});
let (Some(banner), Some(ui)) = (slot.get(), app.ui()) else { unreachable!("built") };
// The theme's standard motion, as a spring.
banner.animate(Target::Opacity(0.0), ui.motion(Pace::Standard));
app.frames(1);
assert!(banner.is_animating());
app.frames(120);
assert!(!banner.is_animating(), "it settled, and asks for no more frames");
# let _ = Motion::Instant;
```

## QSS becomes rules over tokens

QSS names colors directly, so a dark mode or a high-contrast mode means another sheet.
RemedyUI rules name theme tokens, so the same sheet works for all twelve Remedy palettes,
light and dark, and high contrast ([`crate::themes::high_contrast`]).

```css
QPushButton#danger { background: #c0392b; border-radius: 6px; }
QPushButton#danger:hover { background: #e74c3c; }
```

```
use remedy_ui::style::{States, StylePatch};
use remedy_ui::{AppConfig, themes};

let config = AppConfig::new("Reader").theme(themes::ThemeChoice::default().resolve(None, false), |t| {
    let mut sheet = themes::sheet(t);
    let patch = StylePatch::new().background(t.colors.danger).radius(t.radii.medium);
    sheet.rule("button", Some("remove"), States::NONE, patch);
    let hover = StylePatch::new().background(t.colors.danger.mix_oklab(t.colors.text, 0.15));
    sheet.rule("button", Some("remove"), States::HOVER, hover);
    sheet
});
# let _ = config;
```

`QPushButton#danger` becomes `button(..).variant("remove")`. Pseudo-states (`:hover`,
`:pressed`, `:focus`, `:disabled`, `:checked`) are [`crate::style::States`].

## Models and views

Qt separates the model (`QAbstractTableModel`: `rowCount`, `data`, `dataChanged`) from the
view. In RemedyUI the model is a `Signal<Vec<T>>` and a key per row; columns say how to show
and sort a row. Setting the signal is `dataChanged`, `rowsInserted` and `layoutChanged` at
once. The table is virtual: only rows near the viewport exist, and large sorts run off the UI
thread.

```
# use remedy_ui::testing::TestApp;
# use remedy_ui::AppConfig;
use remedy_ui::data::{Column, SortValue, TableState, table};
use remedy_ui::reactive::Signal;
use remedy_ui::widgets as w;
use remedy_ui::{App, WindowSpec};

#[derive(Clone)]
struct Page { number: u32, title: String }

let pages = Signal::new(vec![
    Page { number: 1, title: "Cover".into() },
    Page { number: 2, title: "Contents".into() },
]);
let app = move |app: &App| {
    app.open(WindowSpec::new("Outline").size(480.0, 320.0), move |ui, _| {
        let state = TableState::new(false);
        let columns = vec![
            Column::new("page", "Page", 80.0, |ui, p: &Page| w::text(ui, p.number.to_string()).data_text())
                .sort_by(|p| SortValue::Number(f64::from(p.number)))
                .align_end(),
            Column::text("title", "Title", 240.0, |p: &Page| p.title.clone()),
        ];
        let outline = table(ui, "Outline", pages, |p: &Page| p.number, columns, &state, |_number| {});
        ui.root().child(outline.grow(1.0));
    });
};
let mut app = TestApp::new(AppConfig::new("Outline"), app);
app.frames(1);
let Some(ui) = app.ui() else { unreachable!("open") };
let reader = ui.reader_view();
assert!(reader.iter().any(|n| n.name.as_deref().is_some_and(|s| s.contains("Contents"))));
```

[`crate::data::TableState`] holds what the user changed (sort, column order and widths,
selection) for the app to save, like `QHeaderView::saveState`.

## QPainter becomes a painter

`paintEvent` runs whenever Qt decides to repaint. A RemedyUI painter runs when the element is
first drawn and again only when a signal it read changes (or its size or theme changes):

```
# use remedy_ui::testing::TestApp;
# use remedy_ui::AppConfig;
use remedy_ui::core::{Point, Vec2};
use remedy_ui::reactive::Signal;
use remedy_ui::view::TextPaint;
use remedy_ui::widgets as w;
use remedy_ui::{App, WindowSpec};

fn app(app: &App) {
    app.open(WindowSpec::new("Page number"), |ui, _| {
        let page = Signal::new(1u32);
        let badge = w::canvas(ui, move |cx| {
            let p = TextPaint { size: 13.0, weight: 600.0, color: cx.theme.colors.muted, mono: true };
            let corner = Point::new(cx.size.width - 8.0, cx.size.height - 8.0);
            cx.text(&format!("Page {}", page.get()), corner, Vec2::new(1.0, 1.0), p);
        });
        ui.root().child(badge.width(200.0).height(60.0));
    });
}
# let mut app = TestApp::new(AppConfig::new("Page"), app);
# assert!(!app.host().window_ids().is_empty());
```

Painters record into a display list ([`crate::PaintCx::list`]) in the element's own
coordinates: rectangles, rounded rectangles, borders, paths, gradients, images and clips.
Those types ([`crate::paint::Brush`], [`crate::paint::Item`], [`crate::paint::Path`]) are in
[`crate::paint`]. Text goes through [`crate::PaintCx::text`] and
[`crate::PaintCx::layout_text`], which use the window's fonts and hinting.

## Pages: RemedyPDF's viewer

RemedyPDF draws pages with MuPDF into a `QGraphicsView` or a scroll area of labels. With
[`crate::pages`], MuPDF stays in the app behind a [`crate::pages::PageSource`]: the page
count, each page's size in points, and a render callback that draws a region at a scale into
pixels. The engine does the rest: virtual layout, tiles on worker threads, deep zoom from 1%
to 6400%, rotation, spreads, selection, search, thumbnails, annotations and screen readers.

```
# use remedy_ui::testing::TestApp;
# use remedy_ui::AppConfig;
use std::sync::Arc;

use remedy_ui::core::Size;
use remedy_ui::pages::{Cancel, PageSource, PageView, RenderError, RenderRequest, page_view};
use remedy_ui::{App, WindowSpec};
use remedy_ui::paint::Image;

/// Stands in for the MuPDF document: blank A4 pages.
struct Blank;

impl PageSource for Blank {
    fn page_count(&self) -> usize {
        3
    }
    fn page_size(&self, _page: usize) -> Size {
        Size::new(595.0, 842.0)
    }
    fn render(&self, request: &RenderRequest, cancel: &Cancel) -> Result<Image, RenderError> {
        if cancel.is_cancelled() {
            return Err(RenderError::Cancelled);
        }
        let pixels = vec![255u8; request.width as usize * request.height as usize * 4];
        Ok(Image::new(request.width, request.height, pixels.into()))
    }
}

fn app(app: &App) {
    app.open(WindowSpec::new("Reader").size(800.0, 600.0), |ui, _| {
        let view = PageView::new(Arc::new(Blank));
        ui.root().child(page_view(ui, &view, "Report.pdf"));
        view.go_to_page(1);
    });
}
let mut app = TestApp::new(AppConfig::new("Reader"), app);
app.frames(1);
let Some(ui) = app.ui() else { unreachable!("open") };
assert!(ui.reader_view().iter().any(|n| n.name.as_deref() == Some("Report.pdf")));
```

The render callback runs on page workers, so the source is `Send + Sync`. Check
[`crate::pages::Cancel`] in long renders: tiles that scrolled away are cancelled. See
[`crate::guide::documents`].

## Pitfalls

- **No blocking dialogs.** `QDialog::exec()` runs a nested event loop. RemedyUI dialogs are
  overlays with handlers; continue the work in the button's closure.
- **No `setText` from slots.** Calling methods on elements from handlers works, but it is
  the Qt habit. Set the signal and let the bound property follow, so every path that changes
  the value updates the screen.
- **No object ownership tree to manage.** Removing an element removes its children and
  disposes its bindings. Nothing needs `deleteLater`.
- **Threads hand results back.** Do not touch elements or signals from a worker thread.
  Return the result from [`crate::App::background`]'s work closure and set signals in `done`.
- **No `QStyle` subclassing.** Look comes from the theme and the style sheet; drawing that
  is not a widget's look goes in a painter.