Skip to main content

sui_widgets/composites/
painting.rs

1use crate::DefaultTheme;
2use crate::IconGlyph;
3use crate::SemanticTone;
4use crate::ThemeTextToken;
5use crate::composites::indicators::{numeric_text_style_if_numeric, text_token_style};
6use crate::composites::status::{StatusBadge, paint_status_badge};
7use crate::controls::draw_icon_glyph;
8use crate::draw::{measure_text, mix_color};
9use crate::frame::{draw_control_shape, physical_pixels, snap_to_pixels, stroke_border};
10use crate::text_align::paint_text;
11use crate::text_align::paint_text_line;
12use crate::text_align::{TextOverflow, paint_text_line_elided};
13use sui_core::Color;
14use sui_core::Path;
15use sui_core::Rect;
16use sui_core::SemanticsNode;
17use sui_core::SemanticsRole;
18use sui_core::SemanticsValue;
19use sui_core::Size;
20use sui_layout::Constraints;
21use sui_layout::Padding as Insets;
22use sui_runtime::MeasureCtx;
23use sui_runtime::PaintCtx;
24use sui_runtime::SemanticsCtx;
25use sui_runtime::Widget;
26use sui_text::FontWeight;
27use sui_text::TextAlign;
28use sui_text::TextDocument;
29use sui_text::TextLayoutRequest;
30use sui_text::TextStyle;
31use sui_text::TextWrap;
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum HairlineEdge {
35    Top,
36    Right,
37    Bottom,
38    Left,
39}
40
41pub fn paint_rounded_rect(ctx: &mut PaintCtx, rect: Rect, color: Color, radius: f32) {
42    if rect.width() <= 0.0 || rect.height() <= 0.0 {
43        return;
44    }
45    let radius = radius
46        .min(rect.width() * 0.5)
47        .min(rect.height() * 0.5)
48        .max(0.0);
49    if radius <= 0.5 {
50        ctx.fill_rect(rect, color);
51    } else {
52        ctx.fill(Path::rounded_rect(rect, radius), color);
53    }
54}
55
56pub fn paint_rounded_panel(
57    ctx: &mut PaintCtx,
58    rect: Rect,
59    fill: Color,
60    border: Color,
61    radius: f32,
62) {
63    paint_rounded_rect(ctx, rect, border, radius);
64    paint_rounded_rect(ctx, rect.inflate(-1.0, -1.0), fill, (radius - 1.0).max(0.0));
65}
66
67pub fn paint_hairline(ctx: &mut PaintCtx, rect: Rect, edge: HairlineEdge, color: Color) {
68    let line = match edge {
69        HairlineEdge::Top => Rect::new(rect.x(), rect.y(), rect.width(), 1.0),
70        HairlineEdge::Right => Rect::new(rect.max_x() - 1.0, rect.y(), 1.0, rect.height()),
71        HairlineEdge::Bottom => Rect::new(rect.x(), rect.max_y() - 1.0, rect.width(), 1.0),
72        HairlineEdge::Left => Rect::new(rect.x(), rect.y(), 1.0, rect.height()),
73    };
74    ctx.fill_rect(line, color);
75}
76
77pub fn paint_border(ctx: &mut PaintCtx, rect: Rect, color: Color) {
78    paint_hairline(ctx, rect, HairlineEdge::Top, color);
79    paint_hairline(ctx, rect, HairlineEdge::Right, color);
80    paint_hairline(ctx, rect, HairlineEdge::Bottom, color);
81    paint_hairline(ctx, rect, HairlineEdge::Left, color);
82}
83
84pub(super) fn centered_text_slot(bounds: Rect, center_y: f32, line_height: f32) -> Rect {
85    let height = line_height.max(1.0) * 2.0;
86    Rect::new(bounds.x(), center_y - height * 0.5, bounds.width(), height)
87}
88
89#[derive(Clone, Copy)]
90pub struct EmptyStatePaint<'a> {
91    pub(super) icon: Option<IconGlyph>,
92    pub(super) title: &'a str,
93    pub(super) description: &'a str,
94    pub(super) detail: Option<&'a str>,
95    pub(super) background: Option<Color>,
96    pub(super) center_offset_y: f32,
97    pub(super) reserve_action_space: bool,
98}
99
100impl<'a> EmptyStatePaint<'a> {
101    pub const fn new(title: &'a str, description: &'a str) -> Self {
102        Self {
103            icon: None,
104            title,
105            description,
106            detail: None,
107            background: None,
108            center_offset_y: 0.0,
109            reserve_action_space: false,
110        }
111    }
112
113    pub const fn icon(mut self, icon: IconGlyph) -> Self {
114        self.icon = Some(icon);
115        self
116    }
117
118    pub const fn detail(mut self, detail: &'a str) -> Self {
119        self.detail = Some(detail);
120        self
121    }
122
123    pub const fn background(mut self, color: Color) -> Self {
124        self.background = Some(color);
125        self
126    }
127
128    pub const fn center_offset_y(mut self, offset: f32) -> Self {
129        self.center_offset_y = offset;
130        self
131    }
132
133    pub const fn reserve_action_space(mut self, reserve: bool) -> Self {
134        self.reserve_action_space = reserve;
135        self
136    }
137}
138
139pub fn paint_empty_state(
140    ctx: &mut PaintCtx,
141    theme: &DefaultTheme,
142    bounds: Rect,
143    paint: EmptyStatePaint<'_>,
144) {
145    if let Some(background) = paint.background {
146        ctx.fill_rect(bounds, background);
147    }
148
149    let cy = bounds.y() + bounds.height() * 0.5
150        - if paint.reserve_action_space {
151            18.0
152        } else {
153            0.0
154        }
155        + paint.center_offset_y;
156    let icon_color = theme.surfaces.text_faint;
157    if let Some(icon) = paint.icon {
158        let side = 40.0;
159        let cx = bounds.x() + bounds.width() * 0.5;
160        draw_icon_glyph(
161            ctx,
162            icon,
163            Rect::new(cx - side * 0.5, cy - 46.0 - side * 0.5, side, side),
164            icon_color,
165        );
166    }
167
168    let mut title_style = text_token_style(theme, theme.text.lg, theme.surfaces.text_muted);
169    title_style.weight = FontWeight::SEMIBOLD;
170    paint_text_line(
171        ctx,
172        centered_text_slot(bounds, cy + 4.0, title_style.line_height),
173        paint.title,
174        &title_style,
175        TextAlign::Center,
176    );
177
178    let description_style = text_token_style(theme, theme.text.sm, theme.surfaces.text_faint);
179    paint_text_line(
180        ctx,
181        centered_text_slot(bounds, cy + 30.0, description_style.line_height),
182        paint.description,
183        &description_style,
184        TextAlign::Center,
185    );
186
187    if let Some(detail) = paint.detail {
188        let mut detail_style = text_token_style(theme, theme.text.xs, theme.surfaces.text_muted);
189        detail_style.weight = FontWeight::MEDIUM;
190        paint_text_line(
191            ctx,
192            centered_text_slot(bounds, cy + 48.0, detail_style.line_height),
193            detail,
194            &detail_style,
195            TextAlign::Center,
196        );
197    }
198}
199
200#[derive(Clone, Copy, Debug, PartialEq, Eq)]
201pub enum CommandButtonFill {
202    Surface,
203    Filled,
204}
205
206#[derive(Clone, Copy, Debug, PartialEq, Eq)]
207pub struct CommandButtonPaint {
208    pub tone: SemanticTone,
209    pub icon_tone: Option<SemanticTone>,
210    pub fill: CommandButtonFill,
211    pub hovered: bool,
212    pub pressed: bool,
213}
214
215impl CommandButtonPaint {
216    pub const fn neutral() -> Self {
217        Self {
218            tone: SemanticTone::Neutral,
219            icon_tone: None,
220            fill: CommandButtonFill::Surface,
221            hovered: false,
222            pressed: false,
223        }
224    }
225
226    pub const fn tonal(tone: SemanticTone) -> Self {
227        Self {
228            tone,
229            icon_tone: None,
230            fill: CommandButtonFill::Surface,
231            hovered: false,
232            pressed: false,
233        }
234    }
235
236    pub const fn filled(tone: SemanticTone) -> Self {
237        Self {
238            tone,
239            icon_tone: None,
240            fill: CommandButtonFill::Filled,
241            hovered: false,
242            pressed: false,
243        }
244    }
245
246    pub const fn icon_tone(mut self, tone: SemanticTone) -> Self {
247        self.icon_tone = Some(tone);
248        self
249    }
250
251    pub const fn hovered(mut self, hovered: bool) -> Self {
252        self.hovered = hovered;
253        self
254    }
255
256    pub const fn pressed(mut self, pressed: bool) -> Self {
257        self.pressed = pressed;
258        self
259    }
260}
261
262impl Default for CommandButtonPaint {
263    fn default() -> Self {
264        Self::neutral()
265    }
266}
267
268#[derive(Clone, Copy, Debug, PartialEq, Eq)]
269pub struct DisclosureButtonPaint {
270    pub command: CommandButtonPaint,
271}
272
273impl DisclosureButtonPaint {
274    pub const fn new() -> Self {
275        Self {
276            command: CommandButtonPaint::tonal(SemanticTone::Accent)
277                .icon_tone(SemanticTone::Accent),
278        }
279    }
280
281    pub const fn command(mut self, command: CommandButtonPaint) -> Self {
282        self.command = command;
283        self
284    }
285
286    pub const fn tone(mut self, tone: SemanticTone) -> Self {
287        self.command = CommandButtonPaint::tonal(tone).icon_tone(tone);
288        self
289    }
290
291    pub const fn hovered(mut self, hovered: bool) -> Self {
292        self.command = self.command.hovered(hovered);
293        self
294    }
295
296    pub const fn pressed(mut self, pressed: bool) -> Self {
297        self.command = self.command.pressed(pressed);
298        self
299    }
300}
301
302impl Default for DisclosureButtonPaint {
303    fn default() -> Self {
304        Self::new()
305    }
306}
307
308pub fn paint_command_button(
309    ctx: &mut PaintCtx,
310    theme: &DefaultTheme,
311    rect: Rect,
312    label: &str,
313    icon: Option<IconGlyph>,
314    style: CommandButtonPaint,
315) {
316    if rect.width() <= 0.0 || rect.height() <= 0.0 {
317        return;
318    }
319
320    let (tone_color, tone_text) = theme.semantic_tone_colors(style.tone);
321    let (base_fill, border, label_color) = match style.fill {
322        CommandButtonFill::Surface => {
323            let label_color = if style.tone == SemanticTone::Neutral {
324                theme.palette.text
325            } else {
326                tone_color
327            };
328            let border = if style.tone == SemanticTone::Neutral {
329                theme.palette.border
330            } else {
331                tone_color.with_alpha(0.72)
332            };
333            (theme.surfaces.field, border, label_color)
334        }
335        CommandButtonFill::Filled => (tone_color, tone_color, tone_text),
336    };
337    let fill = match style.fill {
338        CommandButtonFill::Surface if style.pressed => theme.palette.control_active,
339        CommandButtonFill::Surface if style.hovered => theme.palette.control_hover,
340        CommandButtonFill::Filled
341            if style.pressed && matches!(style.tone, SemanticTone::Accent) =>
342        {
343            theme.palette.accent_pressed
344        }
345        CommandButtonFill::Filled
346            if style.hovered && matches!(style.tone, SemanticTone::Accent) =>
347        {
348            theme.palette.accent_hover
349        }
350        _ => base_fill,
351    };
352    let icon_color = style
353        .icon_tone
354        .map(|tone| {
355            if style.fill == CommandButtonFill::Filled && tone == style.tone {
356                theme.semantic_tone_text_color(tone)
357            } else if tone == SemanticTone::Neutral {
358                theme.palette.text_muted
359            } else {
360                theme.semantic_tone_color(tone)
361            }
362        })
363        .unwrap_or_else(|| match style.fill {
364            CommandButtonFill::Surface => {
365                if style.tone == SemanticTone::Neutral {
366                    theme.palette.text_muted
367                } else {
368                    tone_color
369                }
370            }
371            CommandButtonFill::Filled => tone_text,
372        });
373
374    let radius = theme
375        .metrics
376        .corner_radius
377        .min(rect.height() * 0.35)
378        .max(0.0);
379    draw_control_shape(
380        ctx,
381        rect,
382        radius,
383        physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
384        fill,
385        border,
386    );
387
388    let icon_size = (rect.height() - 14.0).clamp(12.0, 16.0);
389    let padding = theme
390        .metrics
391        .button_padding
392        .left
393        .max(8.0)
394        .min(rect.width() * 0.4);
395    let gap = theme.metrics.icon_label_gap.max(5.0);
396    let mut text_x = rect.x() + padding;
397    if let Some(icon) = icon {
398        let icon_rect = Rect::new(
399            rect.x() + padding,
400            rect.y() + (rect.height() - icon_size) * 0.5,
401            icon_size,
402            icon_size,
403        );
404        draw_icon_glyph(ctx, icon, icon_rect, icon_color);
405        text_x = icon_rect.max_x() + gap;
406    }
407
408    let label_rect = Rect::new(
409        text_x,
410        rect.y(),
411        (rect.max_x() - text_x - padding * 0.75).max(0.0),
412        rect.height(),
413    );
414    if label_rect.width() <= 0.0 {
415        return;
416    }
417
418    let mut text_style = text_token_style(theme, theme.text.sm, label_color);
419    text_style.weight = FontWeight::SEMIBOLD;
420    let text_style = numeric_text_style_if_numeric(label, text_style);
421    ctx.push_clip_rect(label_rect);
422    paint_text_line(ctx, label_rect, label, &text_style, TextAlign::Start);
423    ctx.pop_clip();
424}
425
426pub fn paint_disclosure_button(
427    ctx: &mut PaintCtx,
428    theme: &DefaultTheme,
429    rect: Rect,
430    label: &str,
431    expanded: bool,
432    paint: DisclosureButtonPaint,
433) {
434    paint_command_button(
435        ctx,
436        theme,
437        rect,
438        label,
439        Some(if expanded {
440            IconGlyph::ChevronUp
441        } else {
442            IconGlyph::ChevronDown
443        }),
444        paint.command,
445    );
446}
447
448#[derive(Clone, Copy, Debug, PartialEq)]
449pub struct ActionTilePaint {
450    pub tone: SemanticTone,
451    pub highlighted: bool,
452    pub hovered: bool,
453    pub pressed: bool,
454    pub enabled: bool,
455    pub background: Option<Color>,
456    pub border: Option<Color>,
457    pub title_color: Option<Color>,
458    pub subtitle_color: Option<Color>,
459    pub icon_color: Option<Color>,
460    pub leading_tone_dot: Option<SemanticTone>,
461    pub radius: Option<f32>,
462    pub padding_x: Option<f32>,
463    pub leading_width: f32,
464    pub trailing_width: f32,
465    /// What a title or subtitle too wide for the tile does. Clipped by
466    /// default.
467    pub text_overflow: TextOverflow,
468}
469
470impl ActionTilePaint {
471    pub const fn neutral() -> Self {
472        Self {
473            tone: SemanticTone::Neutral,
474            highlighted: false,
475            hovered: false,
476            pressed: false,
477            enabled: true,
478            background: None,
479            border: None,
480            title_color: None,
481            subtitle_color: None,
482            icon_color: None,
483            leading_tone_dot: None,
484            radius: None,
485            padding_x: None,
486            leading_width: 0.0,
487            trailing_width: 0.0,
488            text_overflow: TextOverflow::Clip,
489        }
490    }
491
492    pub const fn tonal(tone: SemanticTone) -> Self {
493        Self {
494            tone,
495            highlighted: true,
496            hovered: false,
497            pressed: false,
498            enabled: true,
499            background: None,
500            border: None,
501            title_color: None,
502            subtitle_color: None,
503            icon_color: None,
504            leading_tone_dot: None,
505            radius: None,
506            padding_x: None,
507            leading_width: 0.0,
508            trailing_width: 0.0,
509            text_overflow: TextOverflow::Clip,
510        }
511    }
512
513    pub const fn highlighted(mut self, highlighted: bool) -> Self {
514        self.highlighted = highlighted;
515        self
516    }
517
518    pub const fn hovered(mut self, hovered: bool) -> Self {
519        self.hovered = hovered;
520        self
521    }
522
523    pub const fn pressed(mut self, pressed: bool) -> Self {
524        self.pressed = pressed;
525        self
526    }
527
528    pub const fn enabled(mut self, enabled: bool) -> Self {
529        self.enabled = enabled;
530        self
531    }
532
533    pub const fn background(mut self, background: Color) -> Self {
534        self.background = Some(background);
535        self
536    }
537
538    pub const fn border(mut self, border: Color) -> Self {
539        self.border = Some(border);
540        self
541    }
542
543    pub const fn title_color(mut self, title_color: Color) -> Self {
544        self.title_color = Some(title_color);
545        self
546    }
547
548    pub const fn subtitle_color(mut self, subtitle_color: Color) -> Self {
549        self.subtitle_color = Some(subtitle_color);
550        self
551    }
552
553    pub const fn icon_color(mut self, icon_color: Color) -> Self {
554        self.icon_color = Some(icon_color);
555        self
556    }
557
558    pub const fn leading_tone_dot(mut self, tone: SemanticTone) -> Self {
559        self.leading_tone_dot = Some(tone);
560        self
561    }
562
563    pub const fn corner_radius(mut self, corner_radius: f32) -> Self {
564        self.radius = Some(corner_radius);
565        self
566    }
567
568    pub const fn padding_x(mut self, padding_x: f32) -> Self {
569        self.padding_x = Some(padding_x);
570        self
571    }
572
573    pub const fn leading_width(mut self, leading_width: f32) -> Self {
574        self.leading_width = leading_width;
575        self
576    }
577
578    pub const fn trailing_width(mut self, trailing_width: f32) -> Self {
579        self.trailing_width = trailing_width;
580        self
581    }
582
583    /// Set what a title or subtitle too wide for the tile does.
584    /// [`TextOverflow::Ellipsis`] ends it in "…" instead of cutting a glyph.
585    pub const fn text_overflow(mut self, text_overflow: TextOverflow) -> Self {
586        self.text_overflow = text_overflow;
587        self
588    }
589}
590
591impl Default for ActionTilePaint {
592    fn default() -> Self {
593        Self::neutral()
594    }
595}
596
597#[derive(Clone, Copy, Debug, PartialEq)]
598pub struct CalloutPaint {
599    pub tone: SemanticTone,
600    pub fill: Option<Color>,
601    pub border: Option<Color>,
602    pub rail_color: Option<Color>,
603    pub icon_color: Option<Color>,
604    pub title_color: Option<Color>,
605    pub body_color: Option<Color>,
606    pub radius: Option<f32>,
607    pub padding: Insets,
608    pub icon_size: f32,
609    pub icon_gap: f32,
610    pub rail_width: f32,
611    pub reserved_bottom: f32,
612}
613
614impl CalloutPaint {
615    pub const fn new(tone: SemanticTone) -> Self {
616        Self {
617            tone,
618            fill: None,
619            border: None,
620            rail_color: None,
621            icon_color: None,
622            title_color: None,
623            body_color: None,
624            radius: None,
625            padding: Insets {
626                left: 12.0,
627                top: 10.0,
628                right: 12.0,
629                bottom: 10.0,
630            },
631            icon_size: 15.0,
632            icon_gap: 9.0,
633            rail_width: 2.0,
634            reserved_bottom: 0.0,
635        }
636    }
637
638    pub const fn fill(mut self, fill: Color) -> Self {
639        self.fill = Some(fill);
640        self
641    }
642
643    pub const fn border(mut self, border: Color) -> Self {
644        self.border = Some(border);
645        self
646    }
647
648    pub const fn rail_color(mut self, rail_color: Color) -> Self {
649        self.rail_color = Some(rail_color);
650        self
651    }
652
653    pub const fn icon_color(mut self, icon_color: Color) -> Self {
654        self.icon_color = Some(icon_color);
655        self
656    }
657
658    pub const fn title_color(mut self, title_color: Color) -> Self {
659        self.title_color = Some(title_color);
660        self
661    }
662
663    pub const fn body_color(mut self, body_color: Color) -> Self {
664        self.body_color = Some(body_color);
665        self
666    }
667
668    pub const fn corner_radius(mut self, corner_radius: f32) -> Self {
669        self.radius = Some(corner_radius);
670        self
671    }
672
673    pub const fn padding(mut self, padding: Insets) -> Self {
674        self.padding = padding;
675        self
676    }
677
678    pub const fn icon_size(mut self, icon_size: f32) -> Self {
679        self.icon_size = icon_size;
680        self
681    }
682
683    pub const fn icon_gap(mut self, icon_gap: f32) -> Self {
684        self.icon_gap = icon_gap;
685        self
686    }
687
688    pub const fn rail_width(mut self, rail_width: f32) -> Self {
689        self.rail_width = rail_width;
690        self
691    }
692
693    pub const fn reserved_bottom(mut self, reserved_bottom: f32) -> Self {
694        self.reserved_bottom = reserved_bottom;
695        self
696    }
697}
698
699impl Default for CalloutPaint {
700    fn default() -> Self {
701        Self::new(SemanticTone::Info)
702    }
703}
704
705pub fn paint_callout(
706    ctx: &mut PaintCtx,
707    theme: &DefaultTheme,
708    rect: Rect,
709    icon: Option<IconGlyph>,
710    title: Option<&str>,
711    body: &str,
712    style: CalloutPaint,
713) {
714    if rect.width() <= 0.0 || rect.height() <= 0.0 {
715        return;
716    }
717
718    let palette = theme.palette;
719    let (tone_color, _) = theme.semantic_tone_colors(style.tone);
720    // Mesh callout: quiet soft wash, hairline border, a 2px status rail, and
721    // the status-hued ink (not the on-solid content color) for the icon.
722    let (tone_soft, tone_ink) = theme.semantic_tone_soft_colors(style.tone);
723    let fill = style.fill.unwrap_or(tone_soft);
724    let border = style.border.unwrap_or(palette.border);
725    let rail = style.rail_color.unwrap_or(tone_color);
726    let radius = style.radius.unwrap_or(theme.radius.md).max(0.0);
727
728    draw_control_shape(
729        ctx,
730        rect,
731        radius,
732        physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
733        fill,
734        border,
735    );
736
737    let rail_width = style.rail_width.max(0.0).min(rect.width());
738    if rail_width > 0.0 {
739        let rail_rect = Rect::new(rect.x(), rect.y(), rail_width, rect.height());
740        ctx.fill(Path::rounded_rect(rail_rect, rail_width * 0.5), rail);
741    }
742
743    let padding = style.padding;
744    let content_bottom = (rect.max_y() - padding.bottom.max(0.0) - style.reserved_bottom.max(0.0))
745        .max(rect.y() + padding.top.max(0.0));
746    let content = Rect::new(
747        rect.x() + padding.left.max(0.0),
748        rect.y() + padding.top.max(0.0),
749        (rect.width() - padding.left.max(0.0) - padding.right.max(0.0)).max(0.0),
750        (content_bottom - rect.y() - padding.top.max(0.0)).max(0.0),
751    );
752    if content.width() <= 0.0 || content.height() <= 0.0 {
753        return;
754    }
755
756    let icon_size = style
757        .icon_size
758        .max(0.0)
759        .min(content.height())
760        .min(content.width());
761    let mut text_x = content.x();
762    if let Some(icon) = icon.filter(|_| icon_size > 0.0) {
763        let icon_rect = Rect::new(
764            content.x(),
765            content.y() + 2.0_f32.min((content.height() - icon_size).max(0.0)),
766            icon_size,
767            icon_size,
768        );
769        draw_icon_glyph(ctx, icon, icon_rect, style.icon_color.unwrap_or(tone_ink));
770        text_x = icon_rect.max_x() + style.icon_gap.max(0.0);
771    }
772
773    let text_rect = Rect::new(
774        text_x,
775        content.y(),
776        (content.max_x() - text_x).max(0.0),
777        content.height(),
778    );
779    if text_rect.width() <= 0.0 || text_rect.height() <= 0.0 {
780        return;
781    }
782
783    let title_line = if title.is_some() {
784        theme.text.sm.line_height
785    } else {
786        0.0
787    };
788    if let Some(title) = title {
789        let mut title_style = text_token_style(
790            theme,
791            theme.text.sm,
792            style.title_color.unwrap_or(palette.text),
793        );
794        title_style.weight = FontWeight::SEMIBOLD;
795        let title_rect = Rect::new(text_rect.x(), text_rect.y(), text_rect.width(), title_line);
796        ctx.push_clip_rect(title_rect);
797        paint_text_line(ctx, title_rect, title, &title_style, TextAlign::Start);
798        ctx.pop_clip();
799    }
800
801    if body.trim().is_empty() {
802        return;
803    }
804    let body_y = text_rect.y() + title_line + if title.is_some() { 4.0 } else { 0.0 };
805    let body_rect = Rect::new(
806        text_rect.x(),
807        body_y,
808        text_rect.width(),
809        (text_rect.max_y() - body_y).max(0.0),
810    );
811    if body_rect.width() <= 0.0 || body_rect.height() <= 0.0 {
812        return;
813    }
814
815    let color = style.body_color.unwrap_or(palette.text_muted);
816    let mut layout_style = text_token_style(theme, theme.text.sm, color);
817    layout_style.color = Color::WHITE;
818    let mut document = TextDocument::from_plain_text(body.to_string(), layout_style);
819    for paragraph in &mut document.paragraphs {
820        paragraph.style.align = TextAlign::Start;
821        paragraph.style.wrap = TextWrap::Word;
822    }
823
824    ctx.push_clip_rect(body_rect);
825    if let Ok(layout) = ctx.layout_text_document(TextLayoutRequest::new(document).with_box_size(
826        Size::new(body_rect.width().max(1.0), body_rect.height().max(1.0)),
827    )) {
828        ctx.draw_text_layout_with_color(body_rect.origin, &layout, color);
829    } else {
830        let fallback_style = text_token_style(theme, theme.text.sm, color);
831        ctx.draw_text(body_rect, body.to_string(), fallback_style);
832    }
833    ctx.pop_clip();
834}
835
836/// A tile this tall or shorter shows only its title.
837const ACTION_TILE_COMPACT_HEIGHT: f32 = 46.0;
838/// The space above a tile's title and below its subtitle.
839const ACTION_TILE_TEXT_INSET_Y: f32 = 8.0;
840
841/// Where [`paint_action_tile`] puts each part of a tile. Build it to size a
842/// tile for the theme's text, or to place a control in the slot that
843/// [`ActionTilePaint::trailing_width`] keeps clear; painting uses the same
844/// geometry.
845#[derive(Clone, Copy, Debug, PartialEq)]
846pub struct ActionTileGeometry {
847    /// Whether the title is alone, centered vertically, without the subtitle.
848    /// A tile without a subtitle, or 46 or less tall, is compact.
849    pub compact: bool,
850    /// The icon, or the leading tone dot.
851    pub leading_rect: Option<Rect>,
852    pub title_rect: Rect,
853    /// Where the subtitle goes, unless the tile is compact.
854    pub subtitle_rect: Option<Rect>,
855    /// The trailing slot, inside the tile's padding and its full height.
856    pub trailing_rect: Rect,
857}
858
859impl ActionTileGeometry {
860    pub fn new(
861        theme: &DefaultTheme,
862        rect: Rect,
863        has_icon: bool,
864        has_subtitle: bool,
865        style: ActionTilePaint,
866    ) -> Self {
867        let padding_x = style
868            .padding_x
869            .unwrap_or_else(|| theme.metrics.button_padding.left.max(10.0))
870            .max(0.0)
871            .min(rect.width() * 0.45);
872        let compact = rect.height() <= ACTION_TILE_COMPACT_HEIGHT || !has_subtitle;
873        let icon_label_gap = theme.metrics.icon_label_gap.max(7.0);
874        let mut text_x = rect.x() + padding_x;
875        let leading_rect = if has_icon {
876            let base_icon_side: f32 = if compact { 14.0 } else { 17.0 };
877            let icon_side = base_icon_side
878                .min((rect.height() - 14.0).max(10.0))
879                .max(0.0);
880            let icon_y = if compact {
881                rect.y() + (rect.height() - icon_side) * 0.5
882            } else {
883                rect.y() + 12.0
884            };
885            let icon_rect = Rect::new(text_x, icon_y, icon_side, icon_side);
886            text_x = icon_rect.max_x() + icon_label_gap;
887            Some(icon_rect)
888        } else if style.leading_tone_dot.is_some() {
889            let dot_side = 8.0_f32.min((rect.height() - 12.0).max(4.0)).max(4.0);
890            let dot_rect = Rect::new(
891                text_x,
892                if compact {
893                    rect.y() + (rect.height() - dot_side) * 0.5
894                } else {
895                    rect.y() + 15.0_f32.min((rect.height() - dot_side).max(0.0))
896                },
897                dot_side,
898                dot_side,
899            );
900            text_x += style.leading_width.max(dot_side + icon_label_gap);
901            Some(dot_rect)
902        } else {
903            text_x += style.leading_width.max(0.0);
904            None
905        };
906
907        let trailing_width = style.trailing_width.max(0.0);
908        let text_width = (rect.max_x() - text_x - padding_x - trailing_width).max(0.0);
909        let title_rect = if compact {
910            Rect::new(text_x, rect.y(), text_width, rect.height())
911        } else {
912            Rect::new(
913                text_x,
914                rect.y() + ACTION_TILE_TEXT_INSET_Y,
915                text_width,
916                theme.text.sm.line_height.max(1.0),
917            )
918        };
919        let subtitle_rect = (!compact).then(|| {
920            Rect::new(
921                text_x,
922                title_rect.max_y() + theme.metrics.action_card_text_gap.min(3.0),
923                text_width,
924                theme.text.xs.line_height.max(1.0),
925            )
926        });
927        Self {
928            compact,
929            leading_rect,
930            title_rect,
931            subtitle_rect,
932            trailing_rect: Rect::new(
933                rect.max_x() - padding_x - trailing_width,
934                rect.y(),
935                trailing_width,
936                rect.height(),
937            ),
938        }
939    }
940
941    /// The height a tile needs to fit its title, and its subtitle when it has
942    /// one, at the theme's text sizes.
943    pub fn height(theme: &DefaultTheme, has_subtitle: bool) -> f32 {
944        let title = theme.text.sm.line_height.max(1.0);
945        if !has_subtitle {
946            return title + ACTION_TILE_TEXT_INSET_Y * 2.0;
947        }
948        let height = ACTION_TILE_TEXT_INSET_Y * 2.0
949            + title
950            + theme.metrics.action_card_text_gap.min(3.0)
951            + theme.text.xs.line_height.max(1.0);
952        // Taller than a compact tile, so the subtitle shows.
953        height.max(ACTION_TILE_COMPACT_HEIGHT + 1.0)
954    }
955}
956
957/// Paint a tile with a title, an optional subtitle and an optional leading
958/// icon, and return where its parts went.
959pub fn paint_action_tile(
960    ctx: &mut PaintCtx,
961    theme: &DefaultTheme,
962    rect: Rect,
963    title: &str,
964    subtitle: Option<&str>,
965    icon: Option<IconGlyph>,
966    style: ActionTilePaint,
967) -> ActionTileGeometry {
968    let geometry = ActionTileGeometry::new(theme, rect, icon.is_some(), subtitle.is_some(), style);
969    if rect.width() <= 0.0 || rect.height() <= 0.0 {
970        return geometry;
971    }
972
973    let palette = theme.palette;
974    let tone_color = theme.semantic_tone_color(style.tone);
975    let effective_tone = if style.tone == SemanticTone::Neutral {
976        palette.text_muted
977    } else {
978        tone_color
979    };
980    let base_background = if !style.enabled {
981        mix_color(palette.control, palette.surface, 0.68).with_alpha(0.82)
982    } else if style.pressed {
983        palette.control_active
984    } else if style.hovered {
985        palette.control_hover
986    } else {
987        palette.control
988    };
989    let background = style.background.unwrap_or(base_background);
990    let base_border = if !style.enabled {
991        palette.border.with_alpha(0.55)
992    } else if style.highlighted {
993        effective_tone.with_alpha(0.84)
994    } else if style.hovered {
995        palette.border_hover
996    } else {
997        palette.border
998    };
999    let border = style.border.unwrap_or(base_border);
1000    let radius = theme
1001        .metrics
1002        .corner_radius
1003        .min(rect.height() * 0.28)
1004        .max(0.0);
1005    let radius = style.radius.unwrap_or(radius).max(0.0);
1006    draw_control_shape(
1007        ctx,
1008        rect,
1009        radius,
1010        physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
1011        background,
1012        border,
1013    );
1014
1015    if let Some(leading_rect) = geometry.leading_rect {
1016        if let Some(icon) = icon {
1017            let icon_color = style.icon_color.unwrap_or_else(|| {
1018                if style.enabled {
1019                    if style.highlighted || style.hovered {
1020                        effective_tone
1021                    } else {
1022                        palette.text_muted
1023                    }
1024                } else {
1025                    palette.text.with_alpha(0.34)
1026                }
1027            });
1028            draw_icon_glyph(ctx, icon, leading_rect, icon_color);
1029        } else if let Some(dot_tone) = style.leading_tone_dot {
1030            ctx.fill(
1031                Path::rounded_rect(leading_rect, leading_rect.width() * 0.5),
1032                theme.semantic_tone_color(dot_tone),
1033            );
1034        }
1035    }
1036
1037    if geometry.title_rect.width() <= 0.0 {
1038        return geometry;
1039    }
1040
1041    let title_color = style.title_color.unwrap_or_else(|| {
1042        if !style.enabled {
1043            palette.text.with_alpha(0.42)
1044        } else if style.highlighted {
1045            palette.text
1046        } else {
1047            palette.text_muted
1048        }
1049    });
1050    let subtitle_color = style.subtitle_color.unwrap_or_else(|| {
1051        if !style.enabled {
1052            palette.text.with_alpha(0.32)
1053        } else {
1054            palette.placeholder
1055        }
1056    });
1057    let mut title_style = text_token_style(theme, theme.text.sm, title_color);
1058    title_style.weight = FontWeight::SEMIBOLD;
1059    let title_style = numeric_text_style_if_numeric(title, title_style);
1060    let subtitle_style = text_token_style(theme, theme.text.xs, subtitle_color);
1061    let elide = style.text_overflow == TextOverflow::Ellipsis;
1062
1063    let title_rect = geometry.title_rect;
1064    if elide {
1065        paint_text_line_elided(ctx, title_rect, title, &title_style, TextAlign::Start);
1066    } else {
1067        ctx.push_clip_rect(title_rect);
1068        if geometry.compact {
1069            paint_text(ctx, title_rect, title, &title_style, TextAlign::Start);
1070        } else {
1071            paint_text_line(ctx, title_rect, title, &title_style, TextAlign::Start);
1072        }
1073        ctx.pop_clip();
1074    }
1075
1076    if let (Some(subtitle), Some(subtitle_rect)) = (subtitle, geometry.subtitle_rect) {
1077        if elide {
1078            paint_text_line_elided(
1079                ctx,
1080                subtitle_rect,
1081                subtitle,
1082                &subtitle_style,
1083                TextAlign::Start,
1084            );
1085        } else {
1086            ctx.push_clip_rect(subtitle_rect);
1087            paint_text_line(
1088                ctx,
1089                subtitle_rect,
1090                subtitle,
1091                &subtitle_style,
1092                TextAlign::Start,
1093            );
1094            ctx.pop_clip();
1095        }
1096    }
1097    geometry
1098}
1099
1100#[derive(Clone, Copy, Debug, PartialEq)]
1101pub struct CodePanelPaint {
1102    pub fill: Option<Color>,
1103    pub border: Option<Color>,
1104    pub header_fill: Option<Color>,
1105    pub label_color: Option<Color>,
1106    pub radius: Option<f32>,
1107    pub header_height: f32,
1108    pub content_padding: Insets,
1109    pub label_inset_x: f32,
1110}
1111
1112impl CodePanelPaint {
1113    pub const fn new() -> Self {
1114        Self {
1115            fill: None,
1116            border: None,
1117            header_fill: None,
1118            label_color: None,
1119            radius: None,
1120            header_height: 24.0,
1121            content_padding: Insets {
1122                left: 8.0,
1123                top: 6.0,
1124                right: 8.0,
1125                bottom: 4.0,
1126            },
1127            label_inset_x: 10.0,
1128        }
1129    }
1130
1131    pub const fn fill(mut self, fill: Color) -> Self {
1132        self.fill = Some(fill);
1133        self
1134    }
1135
1136    pub const fn border(mut self, border: Color) -> Self {
1137        self.border = Some(border);
1138        self
1139    }
1140
1141    pub const fn header_fill(mut self, header_fill: Color) -> Self {
1142        self.header_fill = Some(header_fill);
1143        self
1144    }
1145
1146    pub const fn label_color(mut self, label_color: Color) -> Self {
1147        self.label_color = Some(label_color);
1148        self
1149    }
1150
1151    pub const fn corner_radius(mut self, corner_radius: f32) -> Self {
1152        self.radius = Some(corner_radius);
1153        self
1154    }
1155
1156    pub const fn header_height(mut self, header_height: f32) -> Self {
1157        self.header_height = header_height;
1158        self
1159    }
1160
1161    pub const fn content_padding(mut self, content_padding: Insets) -> Self {
1162        self.content_padding = content_padding;
1163        self
1164    }
1165
1166    pub const fn label_inset_x(mut self, label_inset_x: f32) -> Self {
1167        self.label_inset_x = label_inset_x;
1168        self
1169    }
1170}
1171
1172impl Default for CodePanelPaint {
1173    fn default() -> Self {
1174        Self::new()
1175    }
1176}
1177
1178#[derive(Clone, Copy, Debug, PartialEq)]
1179pub struct CodeTextSpan<'a> {
1180    pub text: &'a str,
1181    pub color: Option<Color>,
1182}
1183
1184impl<'a> CodeTextSpan<'a> {
1185    pub const fn new(text: &'a str) -> Self {
1186        Self { text, color: None }
1187    }
1188
1189    pub const fn color(mut self, color: Color) -> Self {
1190        self.color = Some(color);
1191        self
1192    }
1193}
1194
1195#[derive(Clone, Copy, Debug, PartialEq)]
1196pub struct CodeTextLine<'a> {
1197    pub spans: &'a [CodeTextSpan<'a>],
1198    pub background: Option<Color>,
1199    pub fallback_color: Option<Color>,
1200}
1201
1202impl<'a> CodeTextLine<'a> {
1203    pub const fn new(spans: &'a [CodeTextSpan<'a>]) -> Self {
1204        Self {
1205            spans,
1206            background: None,
1207            fallback_color: None,
1208        }
1209    }
1210
1211    pub const fn background(mut self, background: Color) -> Self {
1212        self.background = Some(background);
1213        self
1214    }
1215
1216    pub const fn fallback_color(mut self, color: Color) -> Self {
1217        self.fallback_color = Some(color);
1218        self
1219    }
1220}
1221
1222#[derive(Clone, Copy, Debug, PartialEq)]
1223pub struct CodeTextPaint {
1224    pub color: Option<Color>,
1225    /// Font size override. Non-positive values resolve to the active theme's
1226    /// `xs` text token when painted.
1227    pub font_size: f32,
1228    /// Line-height override. Non-positive values resolve to the active theme's
1229    /// `xs` text token when painted.
1230    pub line_height: f32,
1231    pub x_padding: f32,
1232    pub weight: FontWeight,
1233}
1234
1235impl CodeTextPaint {
1236    pub const fn new() -> Self {
1237        Self {
1238            color: None,
1239            font_size: 0.0,
1240            line_height: 0.0,
1241            x_padding: 2.0,
1242            weight: FontWeight::NORMAL,
1243        }
1244    }
1245
1246    pub const fn color(mut self, color: Color) -> Self {
1247        self.color = Some(color);
1248        self
1249    }
1250
1251    pub const fn font_size(mut self, font_size: f32) -> Self {
1252        self.font_size = font_size;
1253        self
1254    }
1255
1256    pub const fn line_height(mut self, line_height: f32) -> Self {
1257        self.line_height = line_height;
1258        self
1259    }
1260
1261    pub const fn x_padding(mut self, x_padding: f32) -> Self {
1262        self.x_padding = x_padding;
1263        self
1264    }
1265
1266    pub const fn weight(mut self, weight: FontWeight) -> Self {
1267        self.weight = weight;
1268        self
1269    }
1270}
1271
1272impl Default for CodeTextPaint {
1273    fn default() -> Self {
1274        Self::new()
1275    }
1276}
1277
1278pub fn paint_code_lines(
1279    ctx: &mut PaintCtx,
1280    theme: &DefaultTheme,
1281    rect: Rect,
1282    lines: &[CodeTextLine<'_>],
1283    style: CodeTextPaint,
1284) {
1285    if rect.width() <= 0.0 || rect.height() <= 0.0 || lines.is_empty() {
1286        return;
1287    }
1288
1289    let token = theme.text.xs;
1290    let font_size = if style.font_size > 0.0 {
1291        style.font_size
1292    } else {
1293        token.size
1294    };
1295    let line_height = if style.line_height > 0.0 {
1296        style.line_height
1297    } else {
1298        token.line_height
1299    };
1300    let mut base_style = TextStyle {
1301        font_size: font_size.max(1.0),
1302        line_height: line_height.max(1.0),
1303        color: style.color.unwrap_or(theme.palette.text),
1304        ..theme.mono_text_style(theme.palette.text)
1305    };
1306    base_style.weight = style.weight;
1307
1308    let line_height = base_style.line_height;
1309    let mut y = rect.y();
1310    ctx.push_clip_rect(rect);
1311    for line in lines {
1312        if y + line_height > rect.max_y() {
1313            break;
1314        }
1315        if let Some(background) = line.background {
1316            ctx.fill_rect(
1317                Rect::new(rect.x(), y, rect.width(), line_height),
1318                background,
1319            );
1320        }
1321
1322        let mut x = rect.x() + style.x_padding.max(0.0);
1323        for span in line.spans {
1324            if span.text.is_empty() || x > rect.max_x() {
1325                continue;
1326            }
1327            let mut span_style = base_style.clone();
1328            span_style.color = span
1329                .color
1330                .or(line.fallback_color)
1331                .unwrap_or(base_style.color);
1332            let width = ctx
1333                .measure_text(span.text.to_string(), span_style.clone())
1334                .ok()
1335                .map(|measurement| measurement.width)
1336                .unwrap_or(0.0);
1337            ctx.draw_text(
1338                Rect::new(x, y, (rect.max_x() - x).max(0.0), line_height),
1339                span.text,
1340                span_style,
1341            );
1342            x += width;
1343        }
1344        y += line_height;
1345    }
1346    ctx.pop_clip();
1347}
1348
1349pub fn paint_code_panel(
1350    ctx: &mut PaintCtx,
1351    theme: &DefaultTheme,
1352    rect: Rect,
1353    label: &str,
1354    style: CodePanelPaint,
1355) -> Rect {
1356    if rect.width() <= 0.0 || rect.height() <= 0.0 {
1357        return Rect::ZERO;
1358    }
1359
1360    let fill = style.fill.unwrap_or(theme.surfaces.field);
1361    let border = style.border.unwrap_or(theme.surfaces.border);
1362    let header_fill = style.header_fill.unwrap_or(theme.surfaces.titlebar);
1363    let label_color = style.label_color.unwrap_or(theme.surfaces.text_faint);
1364    let radius = style
1365        .radius
1366        .unwrap_or(theme.radius.xl)
1367        .min(rect.width().min(rect.height()) * 0.5)
1368        .max(0.0);
1369    let border_width = physical_pixels(ctx, theme.metrics.border_width.max(1.0));
1370
1371    let frame = snap_to_pixels(ctx, rect);
1372    ctx.fill(Path::rounded_rect(frame, radius), fill);
1373
1374    let header_height = style.header_height.clamp(0.0, rect.height());
1375    if header_height > 0.0 {
1376        let header_rect = Rect::new(frame.x(), frame.y(), frame.width(), header_height);
1377        let header_radius = radius.min(header_height * 0.5);
1378        ctx.fill(Path::rounded_rect(header_rect, header_radius), header_fill);
1379        if header_height > header_radius {
1380            ctx.fill_rect(
1381                Rect::new(
1382                    header_rect.x(),
1383                    (header_rect.max_y() - header_radius).max(header_rect.y()),
1384                    header_rect.width(),
1385                    header_radius,
1386                ),
1387                header_fill,
1388            );
1389        }
1390
1391        let mut label_style = text_token_style(theme, theme.text.xs, label_color);
1392        label_style.weight = FontWeight::SEMIBOLD;
1393        let label_x = rect.x() + style.label_inset_x.max(0.0);
1394        let label_rect = Rect::new(
1395            label_x,
1396            rect.y() + ((header_height - label_style.line_height) * 0.5).max(0.0),
1397            (rect.max_x() - label_x - style.label_inset_x.max(0.0)).max(0.0),
1398            label_style.line_height,
1399        );
1400        if label_rect.width() > 0.0 {
1401            ctx.push_clip_rect(label_rect);
1402            paint_text_line(ctx, label_rect, label, &label_style, TextAlign::Start);
1403            ctx.pop_clip();
1404        }
1405    }
1406
1407    stroke_border(ctx, frame, radius, border_width, border);
1408
1409    Rect::new(
1410        rect.x() + style.content_padding.left,
1411        rect.y() + header_height + style.content_padding.top,
1412        (rect.width() - style.content_padding.left - style.content_padding.right).max(0.0),
1413        (rect.height() - header_height - style.content_padding.top - style.content_padding.bottom)
1414            .max(0.0),
1415    )
1416}
1417
1418#[derive(Clone, Copy, Debug, PartialEq)]
1419pub struct SectionPanelPaint {
1420    pub fill: Option<Color>,
1421    pub border: Option<Color>,
1422    pub title_color: Option<Color>,
1423    pub radius: Option<f32>,
1424    pub header_height: f32,
1425    pub content_padding: Insets,
1426    pub title_inset_x: f32,
1427    pub trailing_width: f32,
1428    pub title_token: Option<ThemeTextToken>,
1429    pub title_weight: FontWeight,
1430}
1431
1432#[derive(Clone, Copy, Debug, PartialEq)]
1433pub struct SectionPanelGeometry {
1434    pub panel_rect: Rect,
1435    pub header_rect: Rect,
1436    pub title_rect: Rect,
1437    pub content_rect: Rect,
1438}
1439
1440impl SectionPanelPaint {
1441    pub const fn new() -> Self {
1442        Self {
1443            fill: None,
1444            border: None,
1445            title_color: None,
1446            radius: None,
1447            header_height: 34.0,
1448            content_padding: Insets {
1449                left: 12.0,
1450                top: 0.0,
1451                right: 12.0,
1452                bottom: 8.0,
1453            },
1454            title_inset_x: 12.0,
1455            trailing_width: 0.0,
1456            title_token: None,
1457            title_weight: FontWeight::SEMIBOLD,
1458        }
1459    }
1460
1461    pub const fn fill(mut self, fill: Color) -> Self {
1462        self.fill = Some(fill);
1463        self
1464    }
1465
1466    pub const fn border(mut self, border: Color) -> Self {
1467        self.border = Some(border);
1468        self
1469    }
1470
1471    pub const fn title_color(mut self, title_color: Color) -> Self {
1472        self.title_color = Some(title_color);
1473        self
1474    }
1475
1476    pub const fn corner_radius(mut self, corner_radius: f32) -> Self {
1477        self.radius = Some(corner_radius);
1478        self
1479    }
1480
1481    pub const fn header_height(mut self, header_height: f32) -> Self {
1482        self.header_height = header_height;
1483        self
1484    }
1485
1486    pub const fn content_padding(mut self, content_padding: Insets) -> Self {
1487        self.content_padding = content_padding;
1488        self
1489    }
1490
1491    pub const fn title_inset_x(mut self, title_inset_x: f32) -> Self {
1492        self.title_inset_x = title_inset_x;
1493        self
1494    }
1495
1496    pub const fn trailing_width(mut self, trailing_width: f32) -> Self {
1497        self.trailing_width = trailing_width;
1498        self
1499    }
1500
1501    pub const fn title_token(mut self, title_token: ThemeTextToken) -> Self {
1502        self.title_token = Some(title_token);
1503        self
1504    }
1505
1506    pub const fn title_weight(mut self, title_weight: FontWeight) -> Self {
1507        self.title_weight = title_weight;
1508        self
1509    }
1510}
1511
1512impl Default for SectionPanelPaint {
1513    fn default() -> Self {
1514        Self::new()
1515    }
1516}
1517
1518pub fn paint_section_panel(
1519    ctx: &mut PaintCtx,
1520    theme: &DefaultTheme,
1521    rect: Rect,
1522    title: &str,
1523    style: SectionPanelPaint,
1524) -> SectionPanelGeometry {
1525    if rect.width() <= 0.0 || rect.height() <= 0.0 {
1526        return SectionPanelGeometry {
1527            panel_rect: Rect::ZERO,
1528            header_rect: Rect::ZERO,
1529            title_rect: Rect::ZERO,
1530            content_rect: Rect::ZERO,
1531        };
1532    }
1533
1534    let fill = style.fill.unwrap_or(theme.surfaces.panel);
1535    let border = style.border.unwrap_or(theme.surfaces.border);
1536    let title_color = style.title_color.unwrap_or(theme.surfaces.text);
1537    let radius = style
1538        .radius
1539        .unwrap_or(theme.radius.lg)
1540        .min(rect.width().min(rect.height()) * 0.5)
1541        .max(0.0);
1542    let header_height = style.header_height.clamp(0.0, rect.height());
1543    draw_control_shape(
1544        ctx,
1545        rect,
1546        radius,
1547        physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
1548        fill,
1549        border,
1550    );
1551
1552    let header_rect = Rect::new(rect.x(), rect.y(), rect.width(), header_height);
1553    let mut title_style = text_token_style(
1554        theme,
1555        style.title_token.unwrap_or(theme.text.sm),
1556        title_color,
1557    );
1558    title_style.weight = style.title_weight;
1559    let title_x = rect.x() + style.title_inset_x.max(0.0);
1560    let title_rect = Rect::new(
1561        title_x,
1562        rect.y() + ((header_height - title_style.line_height) * 0.5).max(0.0),
1563        (rect.max_x() - title_x - style.title_inset_x.max(0.0) - style.trailing_width.max(0.0))
1564            .max(0.0),
1565        title_style.line_height,
1566    );
1567    if title_rect.width() > 0.0 && !title.is_empty() {
1568        ctx.push_clip_rect(title_rect);
1569        paint_text_line(ctx, title_rect, title, &title_style, TextAlign::Start);
1570        ctx.pop_clip();
1571    }
1572
1573    let content_rect = Rect::new(
1574        rect.x() + style.content_padding.left,
1575        rect.y() + header_height + style.content_padding.top,
1576        (rect.width() - style.content_padding.left - style.content_padding.right).max(0.0),
1577        (rect.height() - header_height - style.content_padding.top - style.content_padding.bottom)
1578            .max(0.0),
1579    );
1580
1581    SectionPanelGeometry {
1582        panel_rect: rect,
1583        header_rect,
1584        title_rect,
1585        content_rect,
1586    }
1587}
1588
1589impl Widget for StatusBadge {
1590    fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
1591        self.label.observe(|label| {
1592            ctx.observe::<String, _>(label);
1593        });
1594        let theme = self.resolved_theme();
1595        let (height, icon_size, gap, padding) = self.metrics(&theme);
1596        let label = self.label();
1597        let tone = self.resolved_tone();
1598        let text = measure_text(ctx, &label, &self.text_style(&theme, &label, tone));
1599        let icon_w = self.icon.map(|_| icon_size + gap).unwrap_or(0.0);
1600        let natural_w = text.width.ceil() + icon_w + padding * 2.0;
1601        constraints.clamp(Size::new(
1602            self.min_width.unwrap_or(0.0).max(natural_w),
1603            height,
1604        ))
1605    }
1606
1607    fn paint(&self, ctx: &mut PaintCtx) {
1608        let theme = self.resolved_theme();
1609        let label = self.label();
1610        let tone = self.resolved_tone();
1611        paint_status_badge(ctx, ctx.bounds(), &theme, &label, self.icon, tone);
1612    }
1613
1614    fn semantics(&self, ctx: &mut SemanticsCtx) {
1615        let label = self.label();
1616        let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Text, ctx.bounds());
1617        node.name = Some(label.clone());
1618        node.value = Some(SemanticsValue::Text(label));
1619        ctx.push(node);
1620    }
1621}