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