画布
宽 width、高 height 的画布,用 Canvas::paint 绘制。直接写长度表示固定尺寸,如 ui.canvas(Sizing::FILL, 40)。
ui.canvas基本用法
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); }});线、圆、图标和文字
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, );});接口
| 设置 | 说明 |
|---|---|
.focusable() | 可以获得键盘焦点:它是一个 Tab 停靠点,按下时获得焦点,由键盘获得焦点时在外框显示主题的焦点环。Response::focused 告诉你它是否有焦点。停用时不会获得焦点。 |
.keys(keys: &[Key]) | 画布聚焦时处理的按键,用 Ui::key_pressed_on 读取;这期间窗口其他地方通过 Ui::key_pressed 收不到它们。同时让画布可以获得焦点。 |
.ime() | 聚焦时接收文字:键入、粘贴以及用输入法组字,用 Ui::ime_on 读取,绘制时也可以用 Painter::ime。画布自己绘制文字和组字内容,并用 Painter::ime_caret 告诉输入法光标在哪里,候选窗口就在那里打开。 |
.aria_role(role: accesskit::Role) | 告诉辅助技术这块画布是什么:滑块、仪表、按钮。不设时,给了名字或值的画布被当作图片。 |
.aria_label(text: impl Display) | 辅助技术读出的名字。 |
.aria_value(text: impl Display) | 辅助技术读出的值,用文字表示,比如 "3 of 5 stars"。 |
.aria_range(value: f64, min: f64, max: f64) | 介于 min 和 max 之间的数值,用于滑块、仪表或进度。辅助技术用方向键调节滑块,所以要用 keys 接住方向键。 |
.rounded(radius: impl Length) | 外框的圆角,焦点环跟着它走。 |
.paint(draw: impl FnOnce(&mut Painter<'_>)) | 放入画布,返回交互结果。draw 立即执行,用 Painter 记录绘制命令;这些命令在布局之后画到画布所在的位置。 |