Canvas
A canvas width by height, drawn with Canvas::paint. A plain length is a fixed size, as in ui.canvas(Sizing::FILL, 40).
ui.canvasBasic
ui.canvas(400, 56).paint(|p| { let (w, h) = p.size(); let accent = p.theme().accent; let values = [0.42, 0.7, 0.55, 0.9, 0.35, 0.62, 0.8, 0.48]; // Bars with gaps between them, touching both edges of the canvas. let gap = 8.0; let bar = (w + gap) / values.len() as f32 - gap; for (i, v) in values.iter().enumerate() { let rect = Rect::new(i as f32 * (bar + gap), h - v * h, bar, v * h); p.rounded(rect, 4.0, accent); }});Lines, circles, icons and text
ui.canvas(400, 56).paint(|p| { let t = *p.theme(); p.line((4.0, 28.0), (120.0, 28.0), 2.0, t.border_strong); p.circle((164.0, 28.0), 20.0, 2.0, t.accent); p.icon( IconName::Star, Rect::new(212.0, 16.0, 24.0, 24.0), t.foreground, ); p.text( Font::ui(14.0), (260.0, 19.0), "Drawn text", t.muted_foreground, );});API reference
| Setting | Description |
|---|---|
.focusable() | Takes keyboard focus: it is a Tab stop, a press focuses it, and while focused from the keyboard it shows the theme's focus ring around its box. Response::focused tells whether it has focus. Disabled, it takes none. |
.keys(keys: &[Key]) | Keys the canvas handles while it has focus, read with Ui::key_pressed_on; the rest of the window, through Ui::key_pressed, no longer sees them then. Also makes it focusable. |
.ime() | Takes text while it has focus: typed, pasted and composed with an input method, read with Ui::ime_on or, while drawing, Painter::ime. The canvas draws the text and the composition itself, and tells the input method where the caret is with Painter::ime_caret, so the candidate window opens there. |
.aria_role(role: accesskit::Role) | What assistive technology is told the canvas is: a slider, a meter, a button. Without one, a canvas given a name or a value is an image. |
.aria_label(text: impl Display) | The name assistive technology reads. |
.aria_value(text: impl Display) | The value assistive technology reads, as text: "3 of 5 stars". |
.aria_range(value: f64, min: f64, max: f64) | A numeric value between min and max, for a slider, meter or progress. Assistive technology steps a slider with the arrow keys, so take those with keys. |
.rounded(radius: impl Length) | The corner radius of the box, which its focus ring follows. |
.paint(draw: impl FnOnce(&mut Painter<'_>)) | Places the canvas and returns its interaction result. draw runs immediately and records drawing commands with a Painter, which are drawn at the canvas's position after layout. |