Skip to main content

rust_glass/
ui.rs

1//! Layout. A `Ui` is a region that hands out rectangles to widgets.
2
3use std::hash::Hash;
4use std::rc::Rc;
5
6use crate::accessibility::{Node, Role};
7use crate::color::Color;
8use crate::context::{Context, Id, Response, Sense};
9use crate::geometry::{Rect, Vec2, vec2};
10use crate::material::{Corner, Glass, Material};
11use crate::paint::Painter;
12use crate::text::{TextLayout, TextStyle, Weight};
13use crate::theme::Surface;
14
15/// How children line up across the direction of a layout.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum Align {
18    /// At the left, or the top in a row.
19    Start,
20    /// In the middle.
21    Center,
22    /// At the right, or the bottom in a row.
23    End,
24}
25
26#[derive(Clone, Copy, Debug, PartialEq)]
27enum Dir {
28    Vertical,
29    /// Row of the given height; children are centered in it.
30    Horizontal {
31        row: f32,
32    },
33}
34
35/// Anything that can be added with [`Ui::add`].
36pub trait Widget {
37    /// Lays out and draws the widget, and returns what happened to it.
38    fn ui(self, ui: &mut Ui) -> Response;
39}
40
41/// A region that places widgets one after another and hands each a
42/// rectangle.
43///
44/// A `Ui` stacks widgets vertically unless it was made by
45/// [`Ui::horizontal`]. Widgets are drawn on its layer and clipped to
46/// [`Ui::clip`].
47pub struct Ui<'a> {
48    /// The window state.
49    pub ctx: &'a mut Context,
50    /// The layer widgets are drawn on.
51    pub layer: usize,
52    /// Widgets are only drawn and only react inside this rectangle.
53    pub clip: Rect,
54    /// Mask applied to everything drawn. 0 is none.
55    pub mask: u32,
56    /// Opacity of everything drawn, from 0 to 1.
57    pub opacity: f32,
58    /// The space this `Ui` may use.
59    pub max_rect: Rect,
60    /// Where the next widget goes.
61    pub cursor: Vec2,
62    /// Gap between widgets, in points.
63    pub spacing: f32,
64    /// How widgets line up across the layout direction.
65    pub align: Align,
66    /// Widgets react to input. Disabled widgets are drawn faded.
67    pub enabled: bool,
68    /// What widgets are drawn on. It decides label and fill colors.
69    pub surface: Surface,
70    /// Id of this `Ui`. Widget ids are derived from it.
71    pub id: Id,
72    dir: Dir,
73    content: Rect,
74    auto: u64,
75    first: bool,
76}
77
78#[derive(Clone, Copy, Default)]
79struct SizeMem(Vec2);
80
81impl<'a> Ui<'a> {
82    /// Creates a vertical layout in `max_rect`. Most code gets a `Ui` from
83    /// [`region`](crate::prelude::region) or [`Panel`](crate::prelude::Panel)
84    /// instead.
85    pub fn new(ctx: &'a mut Context, layer: usize, id: Id, max: Rect) -> Self {
86        let spacing = ctx.theme.spacing;
87        let surface = Surface::Content { dark: ctx.theme.dark };
88        Ui {
89            ctx,
90            layer,
91            clip: Rect::EVERYTHING,
92            mask: 0,
93            opacity: 1.0,
94            max_rect: max,
95            cursor: max.min,
96            spacing,
97            align: Align::Start,
98            enabled: true,
99            surface,
100            id,
101            dir: Dir::Vertical,
102            content: Rect::from_min_size(max.min, Vec2::ZERO),
103            auto: 0,
104            first: true,
105        }
106    }
107
108    fn sub(&mut self, id: Id, max: Rect, dir: Dir) -> Ui<'_> {
109        Ui {
110            layer: self.layer,
111            clip: self.clip,
112            mask: self.mask,
113            opacity: self.opacity,
114            max_rect: max,
115            cursor: max.min,
116            spacing: self.spacing,
117            align: self.align,
118            enabled: self.enabled,
119            surface: self.surface,
120            id,
121            dir,
122            content: Rect::from_min_size(max.min, Vec2::ZERO),
123            auto: 0,
124            first: true,
125            ctx: self.ctx,
126        }
127    }
128
129    /// Returns a new id for the next widget, derived from its position in this
130    /// layout.
131    pub fn next_id(&mut self) -> Id {
132        self.auto += 1;
133        self.id.with(self.auto)
134    }
135    /// Derives an id from this `Ui`'s id and `source`.
136    pub fn make_id(&self, source: impl Hash) -> Id {
137        self.id.with(source)
138    }
139
140    /// Returns the width left in this layout.
141    pub fn available_width(&self) -> f32 {
142        (self.max_rect.max.x - self.cursor.x).max(0.0)
143    }
144    /// Returns the height left in this layout.
145    pub fn available_height(&self) -> f32 {
146        (self.max_rect.max.y - self.cursor.y).max(0.0)
147    }
148    /// Returns the space left in this layout.
149    pub fn available(&self) -> Rect {
150        Rect { min: self.cursor, max: self.max_rect.max.max(self.cursor) }
151    }
152    /// Returns the space used so far.
153    pub fn used(&self) -> Rect {
154        self.content
155    }
156    /// Returns `true` in a row.
157    pub fn is_horizontal(&self) -> bool {
158        matches!(self.dir, Dir::Horizontal { .. })
159    }
160
161    /// Takes space for a widget of `size` and returns where it goes.
162    pub fn allocate(&mut self, size: Vec2) -> Rect {
163        let gap = if self.first { 0.0 } else { self.spacing };
164        self.first = false;
165        let r = match self.dir {
166            Dir::Vertical => {
167                self.cursor.y += gap;
168                let free = (self.max_rect.width() - size.x).max(0.0);
169                let x = match self.align {
170                    Align::Start => self.max_rect.min.x,
171                    Align::Center => self.max_rect.min.x + free * 0.5,
172                    Align::End => self.max_rect.min.x + free,
173                };
174                let r = Rect::from_min_size(vec2(x, self.cursor.y), size);
175                self.cursor.y += size.y;
176                r
177            }
178            Dir::Horizontal { row } => {
179                self.cursor.x += gap;
180                let y = self.max_rect.min.y + ((row - size.y) * 0.5).max(0.0);
181                let r = Rect::from_min_size(vec2(self.cursor.x, y), size);
182                self.cursor.x += size.x;
183                r
184            }
185        };
186        self.content = self.content.union(r);
187        // Whole pixels keep edges and text crisp.
188        let p = self.ctx.pixels_per_point;
189        let snap = |v: f32| (v * p).round() / p;
190        Rect { min: vec2(snap(r.min.x), snap(r.min.y)), max: vec2(snap(r.max.x), snap(r.max.y)) }
191    }
192
193    /// Returns where the next widget will start.
194    pub fn next_pos(&self) -> Vec2 {
195        let gap = if self.first { 0.0 } else { self.spacing };
196        match self.dir {
197            Dir::Vertical => vec2(self.max_rect.min.x, self.cursor.y + gap),
198            Dir::Horizontal { .. } => vec2(self.cursor.x + gap, self.max_rect.min.y),
199        }
200    }
201
202    /// Takes space for a widget of `size` and senses input on it.
203    pub fn allocate_response(&mut self, size: Vec2, sense: Sense) -> Response {
204        let r = self.allocate(size);
205        let id = self.next_id();
206        self.interact(id, r, sense)
207    }
208
209    /// Senses input on `rect` without taking space.
210    pub fn interact(&mut self, id: Id, rect: Rect, sense: Sense) -> Response {
211        if !self.enabled {
212            let mut res = self.ctx.interact(id, rect, self.clip, self.layer, Sense::Hover);
213            res.hovered = false;
214            return res;
215        }
216        self.ctx.interact(id, rect, self.clip, self.layer, sense)
217    }
218
219    /// Makes the space used so far at least `size`, measured from the top left.
220    pub fn reserve(&mut self, size: Vec2) {
221        self.content.max = self.content.max.max(self.max_rect.min + size);
222        match self.dir {
223            Dir::Vertical => self.cursor.y = self.cursor.y.max(self.content.max.y),
224            Dir::Horizontal { .. } => self.cursor.x = self.cursor.x.max(self.content.max.x),
225        }
226    }
227
228    /// Adds an empty gap of `amount` points.
229    pub fn space(&mut self, amount: f32) {
230        match self.dir {
231            Dir::Vertical => self.cursor.y += amount,
232            Dir::Horizontal { .. } => self.cursor.x += amount,
233        }
234        self.content.max = self.content.max.max(vec2(
235            if self.is_horizontal() { self.cursor.x } else { self.content.max.x },
236            if self.is_horizontal() { self.content.max.y } else { self.cursor.y },
237        ));
238    }
239
240    /// Returns a painter on this `Ui`'s layer, with its clip, mask and opacity.
241    pub fn painter(&mut self) -> Painter<'_> {
242        let (l, c, m, o) = (self.layer, self.clip, self.mask, self.opacity);
243        let o = if self.enabled { o } else { o * 0.4 };
244        let mut p = self.ctx.painter(l, c);
245        p.mask = m;
246        p.opacity = o;
247        p
248    }
249
250    /// Lays out `text` in `style`. The result can be painted and measured.
251    pub fn layout_text(&mut self, text: &str, style: &TextStyle) -> Rc<TextLayout> {
252        self.ctx.text.layout(text, style)
253    }
254
255    /// Adds a widget, such as [`Button`](crate::prelude::Button) or
256    /// [`Slider`](crate::prelude::Slider).
257    pub fn add(&mut self, widget: impl Widget) -> Response {
258        widget.ui(self)
259    }
260
261    /// Describe an element for screen readers. `None` while none is listening.
262    ///
263    /// ```
264    /// # use rust_glass::prelude::*;
265    /// # use rust_glass::accessibility::Role;
266    /// # fn f(ui: &mut Ui, r: Response, on: bool) {
267    /// if let Some(n) = ui.access(r.id, Role::Switch, r.rect) {
268    ///     n.label("Airplane mode").toggled(on);
269    /// }
270    /// # }
271    /// ```
272    pub fn access(&mut self, id: Id, role: Role, rect: Rect) -> Option<&mut Node> {
273        let enabled = self.enabled;
274        let n = self.ctx.access_node(id, role, rect, self.layer)?;
275        n.disabled = !enabled;
276        Some(n)
277    }
278
279    /// Runs `f` with widgets enabled or disabled.
280    pub fn add_enabled<R>(&mut self, enabled: bool, f: impl FnOnce(&mut Ui) -> R) -> R {
281        let old = self.enabled;
282        self.enabled &= enabled;
283        let r = f(self);
284        self.enabled = old;
285        r
286    }
287
288    // ----- containers -----
289
290    pub(crate) fn size_of(&mut self, id: Id) -> Vec2 {
291        match self.ctx.try_get::<SizeMem>(id) {
292            Some(s) => s.0,
293            None => {
294                self.ctx.request_relayout();
295                Vec2::ZERO
296            }
297        }
298    }
299    pub(crate) fn remember(&mut self, id: Id, size: Vec2) {
300        let old = self.ctx.try_get::<SizeMem>(id).map(|s| s.0);
301        if old.is_none_or(|o| (o - size).length() > 0.5) {
302            if old.is_some() {
303                self.ctx.request_repaint();
304            }
305            self.ctx.set(id, SizeMem(size));
306        } else {
307            self.ctx.set(id, SizeMem(old.unwrap()));
308        }
309    }
310
311    /// Lays out the widgets added in `f` side by side, centered vertically.
312    pub fn horizontal<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
313        self.row(Align::Start, f)
314    }
315    /// Like [`Self::horizontal`], centered in the available width.
316    pub fn horizontal_centered<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
317        self.row(Align::Center, f)
318    }
319    /// Like [`Self::horizontal`], pushed to the right edge.
320    pub fn horizontal_right<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
321        self.row(Align::End, f)
322    }
323
324    fn row<R>(&mut self, align: Align, f: impl FnOnce(&mut Ui) -> R) -> R {
325        let id = self.next_id();
326        let prev = self.size_of(id);
327        let avail = self.available_width();
328        let gap = if self.first { 0.0 } else { self.spacing };
329        let start = self.cursor + if self.is_horizontal() { vec2(gap, 0.0) } else { vec2(0.0, gap) };
330        let x = match align {
331            Align::Start => start.x,
332            Align::Center => start.x + ((avail - prev.x) * 0.5).max(0.0),
333            Align::End => start.x + (avail - prev.x).max(0.0),
334        };
335        let row = prev.y;
336        // Nested in another row: center this one too.
337        let y = match self.dir {
338            Dir::Horizontal { row: outer } => self.max_rect.min.y + ((outer - row) * 0.5).max(0.0),
339            Dir::Vertical => start.y,
340        };
341        let max = Rect { min: vec2(x, y), max: vec2(self.max_rect.max.x.max(x), self.max_rect.max.y) };
342        let mut ui = self.sub(id, max, Dir::Horizontal { row });
343        ui.align = Align::Start;
344        let out = f(&mut ui);
345        let used = ui.content.size();
346        self.remember(id, used);
347        let taken = match (align, self.dir) {
348            (Align::Start, _) | (_, Dir::Horizontal { .. }) => used,
349            _ => vec2(avail, used.y),
350        };
351        self.allocate(taken);
352        out
353    }
354
355    /// Stacks the widgets added in `f` in a column of `width` points.
356    pub fn vertical<R>(&mut self, width: f32, f: impl FnOnce(&mut Ui) -> R) -> R {
357        let id = self.next_id();
358        let prev = self.size_of(id);
359        let gap = if self.first { 0.0 } else { self.spacing };
360        let start = match self.dir {
361            Dir::Horizontal { row } => {
362                vec2(self.cursor.x + gap, self.max_rect.min.y + ((row - prev.y) * 0.5).max(0.0))
363            }
364            Dir::Vertical => self.cursor + vec2(0.0, gap),
365        };
366        let max = Rect { min: start, max: vec2(start.x + width, self.max_rect.max.y) };
367        let mut ui = self.sub(id, max, Dir::Vertical);
368        let out = f(&mut ui);
369        let used = ui.content.size();
370        self.remember(id, used);
371        self.allocate(vec2(width, used.y));
372        out
373    }
374
375    /// Stacks the widgets added in `f` in a column as wide as they need, placed
376    /// like a single widget. Returns the result of `f` and the rectangle used.
377    pub fn scope<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> (R, Rect) {
378        let id = self.next_id();
379        self.scope_id(id, f)
380    }
381
382    /// Like [`Self::scope`] with an id of your own, so the state of the widgets
383    /// inside follows the content when it moves in the layout.
384    pub fn scope_id<R>(&mut self, id: Id, f: impl FnOnce(&mut Ui) -> R) -> (R, Rect) {
385        let prev = self.size_of(id);
386        let gap = if self.first { 0.0 } else { self.spacing };
387        let start = match self.dir {
388            Dir::Horizontal { row } => {
389                vec2(self.cursor.x + gap, self.max_rect.min.y + ((row - prev.y) * 0.5).max(0.0))
390            }
391            Dir::Vertical => self.cursor + vec2(0.0, gap),
392        };
393        let max = Rect { min: start, max: self.max_rect.max.max(start) };
394        let mut ui = self.sub(id, max, Dir::Vertical);
395        let out = f(&mut ui);
396        let used = ui.content.size();
397        self.remember(id, used);
398        let align = std::mem::replace(&mut self.align, Align::Start);
399        let r = self.allocate(used);
400        self.align = align;
401        (out, r)
402    }
403
404    /// Splits the width into `columns` columns of equal width with `gap` between
405    /// them. `f` is called once per column with its index.
406    pub fn grid(&mut self, columns: usize, gap: f32, mut f: impl FnMut(&mut Ui, usize)) {
407        let n = columns;
408        let id = self.next_id();
409        let w = ((self.available_width() - gap * (n as f32 - 1.0)) / n as f32).max(1.0);
410        let g = if self.first { 0.0 } else { self.spacing };
411        let top = self.cursor.y + g;
412        let left = self.cursor.x;
413        let mut h: f32 = 0.0;
414        for i in 0..n {
415            let min = vec2(left + i as f32 * (w + gap), top);
416            let max = Rect { min, max: vec2(min.x + w, self.max_rect.max.y) };
417            let mut ui = self.sub(id.with(i), max, Dir::Vertical);
418            f(&mut ui, i);
419            h = h.max(ui.content.height());
420        }
421        self.allocate(vec2(w * n as f32 + gap * (n as f32 - 1.0), h));
422    }
423
424    /// Insets the widgets added in `f` by `padding` on each side.
425    pub fn padded<R>(&mut self, padding: Vec2, f: impl FnOnce(&mut Ui) -> R) -> R {
426        let pad = padding;
427        let id = self.next_id();
428        let gap = if self.first || self.is_horizontal() { 0.0 } else { self.spacing };
429        let start = self.cursor + vec2(0.0, gap);
430        let max = Rect { min: start + pad, max: vec2(self.max_rect.max.x - pad.x, self.max_rect.max.y) };
431        let mut ui = self.sub(id, max, Dir::Vertical);
432        let out = f(&mut ui);
433        let used = ui.content.size();
434        self.allocate(vec2(self.available_width(), used.y + pad.y * 2.0));
435        out
436    }
437
438    /// Lays out the widgets added in `f` in `rect`, without taking space here.
439    pub fn in_rect<R>(&mut self, id: impl Hash, rect: Rect, f: impl FnOnce(&mut Ui) -> R) -> R {
440        let r = rect;
441        let id = self.id.with(id);
442        let mut ui = self.sub(id, r, Dir::Vertical);
443        f(&mut ui)
444    }
445
446    /// Like [`Self::in_rect`], and also returns the size used.
447    pub fn in_rect_sized<R>(&mut self, id: impl Hash, rect: Rect, f: impl FnOnce(&mut Ui) -> R) -> (R, Vec2) {
448        let r = rect;
449        let id = self.id.with(id);
450        let mut ui = self.sub(id, r, Dir::Vertical);
451        let out = f(&mut ui);
452        (out, ui.content.size())
453    }
454
455    /// Lays out the widgets added in `f` side by side in `rect`, centered on its
456    /// height.
457    pub fn row_in_rect<R>(&mut self, id: impl Hash, rect: Rect, f: impl FnOnce(&mut Ui) -> R) -> R {
458        let r = rect;
459        let id = self.id.with(id);
460        let mut ui = self.sub(id, r, Dir::Horizontal { row: r.height() });
461        f(&mut ui)
462    }
463
464    pub(crate) fn row_sized<R>(&mut self, id: Id, r: Rect, f: impl FnOnce(&mut Ui) -> R) -> (R, Vec2) {
465        let mut ui = self.sub(id, r, Dir::Horizontal { row: r.height() });
466        let out = f(&mut ui);
467        (out, ui.content.size())
468    }
469
470    /// Groups the widgets added in `f` on a rounded, nearly opaque card. Cards
471    /// are content, not glass: glass belongs on floating controls.
472    pub fn card<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
473        let id = self.next_id();
474        let w = self.available_width();
475        let gap = if self.first || self.is_horizontal() { 0.0 } else { self.spacing };
476        let start = self.cursor + vec2(0.0, gap);
477        let h = self.size_of(id.with("card")).y;
478        let r = Rect::from_min_size(start, vec2(w, h));
479        let fill =
480            if self.surface.darkness() > 0.5 { Color::gray(0.13).alpha(0.88) } else { Color::white(0.86) };
481        self.painter().fill(r, 20.0, fill);
482        let max = Rect { min: start + vec2(18.0, 16.0), max: vec2(start.x + w - 18.0, self.max_rect.max.y) };
483        let mut ui = self.sub(id, max, Dir::Vertical);
484        let out = f(&mut ui);
485        let used = ui.content.height() + 32.0;
486        self.remember(id.with("card"), vec2(w, used));
487        self.allocate(vec2(w, used));
488        out
489    }
490
491    /// Groups the widgets added in `f` on a glass panel that sizes itself to the
492    /// content.
493    pub fn glass_panel<R>(&mut self, material: Material, f: impl FnOnce(&mut Ui) -> R) -> R {
494        let id = self.next_id();
495        let w = self.available_width();
496        let gap = if self.first || self.is_horizontal() { 0.0 } else { self.spacing };
497        let start = self.cursor + vec2(0.0, gap);
498        let h = self.size_of(id.with("card")).y;
499        let r = Rect::from_min_size(start, vec2(w, h));
500        let surface = self.glass(id, r, Corner::Radius(24.0), material, false);
501        let max = Rect { min: start + vec2(18.0, 16.0), max: vec2(start.x + w - 18.0, self.max_rect.max.y) };
502        let mut ui = self.sub(id, max, Dir::Vertical);
503        ui.surface = surface;
504        let out = f(&mut ui);
505        let used = ui.content.height() + 32.0;
506        self.remember(id.with("card"), vec2(w, used));
507        self.allocate(vec2(w, used));
508        out
509    }
510
511    /// Paints an adaptive glass element and returns the surface for content on
512    /// it.
513    pub fn glass(
514        &mut self,
515        id: Id,
516        rect: Rect,
517        corner: impl Into<Corner>,
518        material: Material,
519        adaptive: bool,
520    ) -> Surface {
521        let mut g = Glass::new(rect, corner, material);
522        let a = self.ctx.prepare_glass(id, &mut g, adaptive);
523        self.painter().glass(&g);
524        a.surface()
525    }
526
527    // ----- text -----
528
529    /// Adds text in the body size.
530    pub fn label(&mut self, text: &str) -> Response {
531        let c = self.surface.text_color();
532        self.styled_text(text, TextStyle::new(self.ctx.theme.body_size), c)
533    }
534    /// Adds less important text in the body size.
535    pub fn secondary(&mut self, text: &str) -> Response {
536        let c = self.surface.secondary_color();
537        self.styled_text(text, TextStyle::new(self.ctx.theme.body_size), c)
538    }
539    /// Adds small, less important text.
540    pub fn caption(&mut self, text: &str) -> Response {
541        let c = self.surface.secondary_color();
542        self.styled_text(text, TextStyle::new(self.ctx.theme.caption_size), c)
543    }
544    /// Adds bold text in the body size.
545    pub fn strong(&mut self, text: &str) -> Response {
546        let c = self.surface.text_color();
547        self.styled_text(text, TextStyle::new(self.ctx.theme.body_size).weight(Weight::Semibold), c)
548    }
549    /// Adds a large title.
550    pub fn heading(&mut self, text: &str) -> Response {
551        let c = self.surface.text_color();
552        let st = TextStyle::new(self.ctx.theme.title_size).weight(Weight::Bold);
553        self.text_role(text, st, c, Role::Heading)
554    }
555    /// Adds a smaller title.
556    pub fn subheading(&mut self, text: &str) -> Response {
557        let c = self.surface.text_color();
558        self.styled_text(text, TextStyle::new(17.0).weight(Weight::Semibold), c)
559    }
560
561    /// Adds text in `style` and `color`.
562    pub fn styled_text(&mut self, text: &str, style: TextStyle, color: Color) -> Response {
563        self.text_role(text, style, color, Role::Label)
564    }
565
566    fn text_role(&mut self, text: &str, mut st: TextStyle, c: Color, role: Role) -> Response {
567        if !self.is_horizontal() && st.wrap.is_none() {
568            st.wrap = Some(self.max_rect.width().max(10.0));
569        }
570        let l = self.layout_text(text, &st);
571        let r = self.allocate(l.size);
572        self.painter().text(r.min, &l, c);
573        let id = self.next_id();
574        if let Some(n) = self.access(id, role, r) {
575            n.label(text);
576        }
577        Response {
578            id,
579            rect: r,
580            hovered: self.ctx.input.pointer.is_some_and(|p| r.contains(p)),
581            held: false,
582            pressed: false,
583            released: false,
584            clicked: false,
585            double_clicked: false,
586            secondary_clicked: false,
587            focused: false,
588            changed: false,
589            delta: Vec2::ZERO,
590            pointer: self.ctx.input.pointer,
591        }
592    }
593
594    /// Adds a thin line across the layout direction.
595    pub fn separator(&mut self) {
596        let c = self.surface.separator_color();
597        match self.dir {
598            Dir::Vertical => {
599                let w = self.available_width();
600                let r = self.allocate(vec2(w, 1.0));
601                self.painter().fill(r, 0.0, c);
602            }
603            Dir::Horizontal { row } => {
604                let h = row.max(20.0) * 0.6;
605                let r = self.allocate(vec2(1.0, h));
606                self.painter().fill(r, 0.0, c);
607            }
608        }
609    }
610
611    /// Draws the keyboard focus ring around `rect` while `id` has focus from the
612    /// keyboard.
613    pub fn focus_ring(&mut self, id: Id, rect: Rect, corner: impl Into<Corner>) {
614        if self.ctx.shows_focus(id) {
615            let c = self.ctx.theme.accent;
616            self.painter().stroke(rect.expand(3.5), corner, 2.5, c.alpha(0.85));
617        }
618    }
619}