1use crate::ControlMetrics;
2use crate::DefaultTheme;
3use crate::GlowTone;
4use crate::IconGlyph;
5use crate::Interpolate;
6use crate::Progress;
7use crate::ResolvedEffectStyle;
8use crate::SemanticTone;
9use crate::ThemeTextToken;
10use crate::binding::Binding;
11use crate::composites::popups::TooltipPlacement;
12use crate::composites::status::{StatusBadge, paint_status_badge};
13use crate::controls::apply_hdr_policy_cap;
14use crate::draw::{inset_rect, measure_text, mix_color, rect_center};
15use crate::frame::{draw_control_shape, physical_pixels, stroke_border};
16use crate::paint_theme_glow;
17use crate::text_align::paint_text;
18use crate::text_align::paint_text_line;
19use sui_core::Color;
20use sui_core::Path;
21use sui_core::PathBuilder;
22use sui_core::Point;
23use sui_core::Rect;
24use sui_core::SemanticsNode;
25use sui_core::SemanticsRole;
26use sui_core::SemanticsValue;
27use sui_core::Size;
28use sui_core::Vector;
29use sui_layout::Constraints;
30use sui_layout::Padding as Insets;
31use sui_reactive::Observable;
32use sui_runtime::MeasureCtx;
33use sui_runtime::PaintCtx;
34use sui_runtime::SemanticsCtx;
35use sui_runtime::Widget;
36use sui_text::FontWeight;
37use sui_text::TextStyle;
38use sui_text::{FontFeature, TextAlign};
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub struct CoverageDotsConfig {
42 pub current: usize,
43 pub target: usize,
44 pub tone: SemanticTone,
45 pub max_dots: usize,
46 pub show_label: bool,
47}
48
49impl Default for CoverageDotsConfig {
50 fn default() -> Self {
51 Self::new(0, 0)
52 }
53}
54
55impl CoverageDotsConfig {
56 pub fn new(current: usize, target: usize) -> Self {
57 Self {
58 current,
59 target,
60 tone: SemanticTone::Neutral,
61 max_dots: 4,
62 show_label: true,
63 }
64 }
65
66 pub(super) fn normalized_target(self) -> usize {
67 self.target.max(self.current)
68 }
69
70 pub(super) fn normalized_max_dots(self) -> usize {
71 self.max_dots.max(1)
72 }
73
74 pub(super) fn label(self) -> String {
75 format!("{}/{}", self.current, self.normalized_target())
76 }
77}
78
79pub fn paint_coverage_dots(
80 ctx: &mut PaintCtx,
81 theme: &DefaultTheme,
82 rect: Rect,
83 current: usize,
84 target: usize,
85 tone: SemanticTone,
86) {
87 let mut config = CoverageDotsConfig::new(current, target);
88 config.tone = tone;
89 paint_coverage_dots_with_config(ctx, theme, rect, config);
90}
91
92pub fn paint_coverage_dots_with_config(
93 ctx: &mut PaintCtx,
94 theme: &DefaultTheme,
95 rect: Rect,
96 config: CoverageDotsConfig,
97) {
98 let target = config.normalized_target();
99 if target == 0 || rect.width() <= 0.0 || rect.height() <= 0.0 {
100 return;
101 }
102
103 let (dot, gap, label_gap) = coverage_dot_metrics(theme);
104 let label = config.label();
105 let text_style = numeric_text_style(text_token_style(
106 theme,
107 theme.text.xs,
108 theme.palette.text_muted,
109 ));
110 let label_width = if config.show_label {
111 ctx.measure_text(label.clone(), text_style.clone())
112 .map(|measurement| measurement.width)
113 .unwrap_or(0.0)
114 } else {
115 0.0
116 };
117 let shown = target.min(config.normalized_max_dots());
120 let dots_width = shown as f32 * dot + shown.saturating_sub(1) as f32 * gap;
121 let separation = if config.show_label { label_gap } else { 0.0 };
122 let show_dots = dots_width + separation + label_width <= rect.width();
123 let mut x = rect.x();
124 ctx.push_clip_rect(rect);
125 if show_dots {
126 let y = rect.y() + (rect.height() - dot) * 0.5;
127 let (tone_color, _) = theme.semantic_tone_colors(config.tone);
128 for index in 0..shown {
129 let dot_rect = Rect::new(x, y, dot, dot);
130 if index < config.current.min(shown) {
131 ctx.fill(Path::rounded_rect(dot_rect, dot * 0.5), tone_color);
132 } else {
133 stroke_border(
134 ctx,
135 dot_rect,
136 dot * 0.5,
137 physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
138 theme.palette.border,
139 );
140 }
141 x += dot + gap;
142 }
143 x += separation - gap;
144 }
145 if config.show_label {
146 let label_rect = Rect::new(x, rect.y(), (rect.max_x() - x).max(0.0), rect.height());
147 paint_text_line(ctx, label_rect, &label, &text_style, TextAlign::Start);
148 }
149 ctx.pop_clip();
150}
151
152pub(super) fn coverage_dot_metrics(theme: &DefaultTheme) -> (f32, f32, f32) {
153 let dot = (theme.text.xs.size * 0.42).clamp(4.0, 6.0);
154 let gap = (dot * 0.65).clamp(2.0, 4.0);
155 let label_gap = (theme.metrics.icon_label_gap * 0.7).max(4.0);
156 (dot, gap, label_gap)
157}
158
159pub struct CoverageDots {
160 pub(super) theme: Box<DefaultTheme>,
161 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
162 pub(super) name: String,
163 pub(super) config: CoverageDotsConfig,
164 pub(super) min_width: Option<f32>,
165}
166
167impl CoverageDots {
168 pub fn new(name: impl Into<String>, current: usize, target: usize) -> Self {
169 Self {
170 theme: Box::new(DefaultTheme::default()),
171 theme_reader: None,
172 name: name.into(),
173 config: CoverageDotsConfig::new(current, target),
174 min_width: None,
175 }
176 }
177
178 pub fn theme(mut self, theme: DefaultTheme) -> Self {
179 self.theme = Box::new(theme);
180 self.theme_reader = None;
181 self
182 }
183
184 pub fn theme_when<F>(mut self, theme: F) -> Self
185 where
186 F: Fn() -> DefaultTheme + 'static,
187 {
188 self.theme_reader = Some(Box::new(theme));
189 self
190 }
191
192 pub fn tone(mut self, tone: SemanticTone) -> Self {
193 self.config.tone = tone;
194 self
195 }
196
197 pub fn max_dots(mut self, max_dots: usize) -> Self {
198 self.config.max_dots = max_dots;
199 self
200 }
201
202 pub fn show_label(mut self, show_label: bool) -> Self {
203 self.config.show_label = show_label;
204 self
205 }
206
207 pub fn min_width(mut self, min_width: f32) -> Self {
208 self.min_width = Some(min_width.max(0.0));
209 self
210 }
211
212 pub(super) fn resolved_theme(&self) -> DefaultTheme {
213 self.theme_reader
214 .as_ref()
215 .map(|theme| theme())
216 .unwrap_or_else(|| *self.theme)
217 }
218}
219
220impl Widget for CoverageDots {
221 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
222 let theme = self.resolved_theme();
223 let target = self.config.normalized_target();
224 if target == 0 {
225 return constraints.clamp(Size::ZERO);
226 }
227 let (dot, gap, label_gap) = coverage_dot_metrics(&theme);
228 let shown = target.min(self.config.normalized_max_dots());
229 let dots_width = shown as f32 * dot + shown.saturating_sub(1) as f32 * gap;
230 let label_width = if self.config.show_label {
231 measure_text(
232 ctx,
233 &self.config.label(),
234 &numeric_text_style(text_token_style(
235 &theme,
236 theme.text.xs,
237 theme.palette.text_muted,
238 )),
239 )
240 .width
241 + label_gap
242 } else {
243 0.0
244 };
245 constraints.clamp(Size::new(
246 self.min_width.unwrap_or(0.0).max(dots_width + label_width),
247 theme
248 .text
249 .xs
250 .line_height
251 .max(dot + 2.0)
252 .max(theme.metrics.min_height * 0.55),
253 ))
254 }
255
256 fn paint(&self, ctx: &mut PaintCtx) {
257 paint_coverage_dots_with_config(ctx, &self.resolved_theme(), ctx.bounds(), self.config);
258 }
259
260 fn semantics(&self, ctx: &mut SemanticsCtx) {
261 let target = self.config.normalized_target();
262 let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Text, ctx.bounds());
263 node.name = Some(self.name.clone());
264 node.value = Some(SemanticsValue::Text(self.config.label()));
265 node.description = Some(format!(
266 "{} of {} covered",
267 self.config.current.min(target),
268 target
269 ));
270 ctx.push(node);
271 }
272}
273
274pub struct PlacementBadge {
275 pub(super) theme: Box<DefaultTheme>,
276 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
277 pub(super) label: Binding<String>,
278 pub(super) icon: Option<IconGlyph>,
279 pub(super) tone: SemanticTone,
280 pub(super) tone_reader: Option<Box<dyn Fn() -> SemanticTone>>,
281 pub(super) coverage: Option<(usize, usize)>,
282 pub(super) coverage_reader: Option<Box<dyn Fn() -> Option<(usize, usize)>>>,
283 pub(super) min_width: Option<f32>,
284}
285
286impl PlacementBadge {
287 pub fn new(label: impl Into<String>) -> Self {
288 Self {
289 theme: Box::new(DefaultTheme::default()),
290 theme_reader: None,
291 label: Binding::new(label.into()),
292 icon: None,
293 tone: SemanticTone::Neutral,
294 tone_reader: None,
295 coverage: None,
296 coverage_reader: None,
297 min_width: None,
298 }
299 }
300
301 pub fn label_when<F>(mut self, label: F) -> Self
303 where
304 F: Fn() -> String + 'static,
305 {
306 self.label.set_when(label);
307 self
308 }
309
310 pub fn label_from<O>(mut self, label: O) -> Self
313 where
314 O: Observable<String> + 'static,
315 {
316 self.label.set_from(label);
317 self
318 }
319
320 pub fn theme(mut self, theme: DefaultTheme) -> Self {
321 self.theme = Box::new(theme);
322 self.theme_reader = None;
323 self
324 }
325
326 pub fn theme_when<F>(mut self, theme: F) -> Self
327 where
328 F: Fn() -> DefaultTheme + 'static,
329 {
330 self.theme_reader = Some(Box::new(theme));
331 self
332 }
333
334 pub fn icon(mut self, icon: IconGlyph) -> Self {
335 self.icon = Some(icon);
336 self
337 }
338
339 pub fn tone(mut self, tone: SemanticTone) -> Self {
340 self.tone = tone;
341 self.tone_reader = None;
342 self
343 }
344
345 pub fn tone_when<F>(mut self, tone: F) -> Self
346 where
347 F: Fn() -> SemanticTone + 'static,
348 {
349 self.tone_reader = Some(Box::new(tone));
350 self
351 }
352
353 pub fn coverage(mut self, current: usize, target: usize) -> Self {
354 self.coverage = Some((current, target));
355 self.coverage_reader = None;
356 self
357 }
358
359 pub fn coverage_when<F>(mut self, coverage: F) -> Self
360 where
361 F: Fn() -> Option<(usize, usize)> + 'static,
362 {
363 self.coverage_reader = Some(Box::new(coverage));
364 self
365 }
366
367 pub fn min_width(mut self, min_width: f32) -> Self {
368 self.min_width = Some(min_width.max(0.0));
369 self
370 }
371
372 pub(super) fn resolved_theme(&self) -> DefaultTheme {
373 self.theme_reader
374 .as_ref()
375 .map(|theme| theme())
376 .unwrap_or_else(|| *self.theme)
377 }
378
379 pub(super) fn label(&self) -> String {
380 self.label.get()
381 }
382
383 pub(super) fn resolved_tone(&self) -> SemanticTone {
384 self.tone_reader
385 .as_ref()
386 .map(|reader| reader())
387 .unwrap_or(self.tone)
388 }
389
390 pub(super) fn resolved_coverage(&self) -> Option<(usize, usize)> {
391 self.coverage_reader
392 .as_ref()
393 .map(|reader| reader())
394 .unwrap_or(self.coverage)
395 .filter(|(_, target)| *target > 0)
396 }
397
398 pub(super) fn metrics(theme: &DefaultTheme) -> (f32, f32, f32) {
399 let height = (theme.metrics.min_height - 2.0).max(22.0);
400 let coverage_width = 50.0;
401 let gap = theme.metrics.icon_label_gap.max(6.0);
402 (height, coverage_width, gap)
403 }
404}
405
406#[derive(Clone, Copy, Debug, PartialEq)]
407pub struct PlacementBadgePaint {
408 pub padding: Insets,
409}
410
411impl PlacementBadgePaint {
412 pub const fn new() -> Self {
413 Self {
414 padding: Insets::ZERO,
415 }
416 }
417
418 pub const fn padding(mut self, left: f32, top: f32, right: f32, bottom: f32) -> Self {
419 self.padding = Insets {
420 left,
421 top,
422 right,
423 bottom,
424 };
425 self
426 }
427
428 pub(super) fn content_rect(self, rect: Rect) -> Rect {
429 Rect::new(
430 rect.x() + self.padding.left,
431 rect.y() + self.padding.top,
432 (rect.width() - self.padding.left - self.padding.right).max(0.0),
433 (rect.height() - self.padding.top - self.padding.bottom).max(0.0),
434 )
435 }
436}
437
438impl Default for PlacementBadgePaint {
439 fn default() -> Self {
440 Self::new()
441 }
442}
443
444pub fn paint_placement_badge(
445 ctx: &mut PaintCtx,
446 theme: &DefaultTheme,
447 rect: Rect,
448 label: &str,
449 icon: Option<IconGlyph>,
450 tone: SemanticTone,
451 coverage: Option<(usize, usize)>,
452) {
453 paint_placement_badge_with(
454 ctx,
455 theme,
456 rect,
457 label,
458 icon,
459 tone,
460 coverage,
461 PlacementBadgePaint::new(),
462 );
463}
464
465pub fn paint_placement_badge_with(
466 ctx: &mut PaintCtx,
467 theme: &DefaultTheme,
468 rect: Rect,
469 label: &str,
470 icon: Option<IconGlyph>,
471 tone: SemanticTone,
472 coverage: Option<(usize, usize)>,
473 paint: PlacementBadgePaint,
474) {
475 let rect = paint.content_rect(rect);
476 if rect.width() <= 0.0 || rect.height() <= 0.0 {
477 return;
478 }
479
480 let (_, coverage_width, gap) = PlacementBadge::metrics(theme);
481 let show_coverage = coverage.is_some() && rect.width() >= 118.0;
482 let coverage_slot = if show_coverage { coverage_width } else { 0.0 };
483 let slot_gap = if show_coverage { gap } else { 0.0 };
484 let badge_rect = Rect::new(
485 rect.x(),
486 rect.y(),
487 (rect.width() - coverage_slot - slot_gap).clamp(48.0, 86.0),
488 rect.height(),
489 );
490 let label_width = ctx
491 .measure_text(
492 label.to_string(),
493 semibold_control_text_style(theme, theme.palette.text),
494 )
495 .map(|measurement| measurement.width)
496 .unwrap_or(0.0);
497 let icon_width =
498 (rect.height() - 13.0).clamp(11.0, 15.0) + theme.metrics.icon_label_gap.max(4.0);
499 let padding = theme.metrics.button_padding.left.max(6.0) * 1.5;
500 let icon = icon.filter(|_| label_width + icon_width + padding <= badge_rect.width());
501 paint_status_badge(ctx, badge_rect, theme, label, icon, tone);
502
503 if show_coverage && let Some((current, target)) = coverage {
504 let dots_rect = Rect::new(
505 badge_rect.max_x() + slot_gap,
506 rect.y(),
507 (rect.max_x() - badge_rect.max_x() - slot_gap).max(0.0),
508 rect.height(),
509 );
510 paint_coverage_dots(ctx, theme, dots_rect, current, target, tone);
511 }
512}
513
514impl Widget for PlacementBadge {
515 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
516 self.label.observe(|label| {
517 ctx.observe::<String, _>(label);
518 });
519 let theme = self.resolved_theme();
520 let (height, coverage_width, gap) = Self::metrics(&theme);
521 let label = self.label();
522 let tone = self.resolved_tone();
523 let icon_w = self
524 .icon
525 .map(|_| (height - 13.0).clamp(11.0, 15.0) + theme.metrics.icon_label_gap.max(4.0))
526 .unwrap_or(0.0);
527 let text = measure_text(
528 ctx,
529 &label,
530 &StatusBadge::new(&label).text_style(&theme, &label, tone),
531 );
532 let badge_width =
533 (text.width.ceil() + icon_w + theme.metrics.button_padding.left.max(8.0) * 2.0)
534 .clamp(48.0, 86.0);
535 let coverage_width = if self.resolved_coverage().is_some() {
536 coverage_width + gap
537 } else {
538 0.0
539 };
540 constraints.clamp(Size::new(
541 self.min_width
542 .unwrap_or(0.0)
543 .max(badge_width + coverage_width),
544 height,
545 ))
546 }
547
548 fn paint(&self, ctx: &mut PaintCtx) {
549 let theme = self.resolved_theme();
550 paint_placement_badge(
551 ctx,
552 &theme,
553 ctx.bounds(),
554 &self.label(),
555 self.icon,
556 self.resolved_tone(),
557 self.resolved_coverage(),
558 );
559 }
560
561 fn semantics(&self, ctx: &mut SemanticsCtx) {
562 let label = self.label();
563 let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Text, ctx.bounds());
564 node.name = Some(label.clone());
565 node.value = Some(SemanticsValue::Text(label.clone()));
566 if let Some((current, target)) = self.resolved_coverage() {
567 let target = target.max(current);
568 node.description = Some(format!("{current} of {target} replicas available"));
569 }
570 ctx.push(node);
571 }
572}
573
574pub struct ProgressBar {
575 pub(super) theme: Box<DefaultTheme>,
576 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
577 pub(super) name: String,
578 pub(super) min: f64,
579 pub(super) max: f64,
580 pub(super) value: f64,
581 pub(super) tone: SemanticTone,
582 pub(super) min_width: Option<f32>,
583 pub(super) height: Option<f32>,
584 pub(super) show_value: bool,
585}
586
587impl ProgressBar {
588 pub fn new(name: impl Into<String>) -> Self {
589 Self {
590 theme: Box::new(DefaultTheme::default()),
591 theme_reader: None,
592 name: name.into(),
593 min: 0.0,
594 max: 1.0,
595 value: 0.0,
596 tone: SemanticTone::Accent,
597 min_width: None,
598 height: None,
599 show_value: false,
600 }
601 }
602
603 pub fn theme(mut self, theme: DefaultTheme) -> Self {
604 self.theme = Box::new(theme);
605 self.theme_reader = None;
606 self
607 }
608
609 pub fn theme_when<F>(mut self, theme: F) -> Self
610 where
611 F: Fn() -> DefaultTheme + 'static,
612 {
613 self.theme_reader = Some(Box::new(theme));
614 self
615 }
616
617 pub fn range(mut self, min: f64, max: f64) -> Self {
618 self.min = min.min(max);
619 self.max = max.max(min);
620 self.value = self.value.clamp(self.min, self.max);
621 self
622 }
623
624 pub fn value(mut self, value: f64) -> Self {
625 self.value = value.clamp(self.min, self.max);
626 self
627 }
628
629 pub fn tone(mut self, tone: SemanticTone) -> Self {
630 self.tone = tone;
631 self
632 }
633
634 pub fn min_width(mut self, min_width: f32) -> Self {
635 self.min_width = Some(min_width.max(0.0));
636 self
637 }
638
639 pub fn height(mut self, height: f32) -> Self {
640 self.height = Some(height.max(1.0));
641 self
642 }
643
644 pub fn show_value(mut self, show_value: bool) -> Self {
645 self.show_value = show_value;
646 self
647 }
648
649 pub(super) fn fraction(&self) -> f32 {
650 if (self.max - self.min).abs() <= f64::EPSILON {
651 0.0
652 } else {
653 ((self.value - self.min) / (self.max - self.min)).clamp(0.0, 1.0) as f32
654 }
655 }
656
657 pub(super) fn resolved_theme(&self) -> DefaultTheme {
658 self.theme_reader
659 .as_ref()
660 .map(|theme| theme())
661 .unwrap_or_else(|| *self.theme)
662 }
663}
664
665pub fn paint_progress_bar(
666 ctx: &mut PaintCtx,
667 rect: Rect,
668 theme: &DefaultTheme,
669 fraction: f32,
670 tone: SemanticTone,
671) {
672 if rect.width() <= 0.0 || rect.height() <= 0.0 {
673 return;
674 }
675
676 let metrics = theme.metrics;
677 let palette = theme.palette;
678 let (tone, _) = theme.semantic_tone_colors(tone);
679 ctx.fill(
681 Path::rounded_rect(rect, metrics.corner_radius),
682 palette.control_hover,
683 );
684
685 let fill = Rect::new(
686 rect.x(),
687 rect.y(),
688 rect.width() * fraction.clamp(0.0, 1.0),
689 rect.height(),
690 );
691 if fill.width() > 0.0 {
692 ctx.fill(Path::rounded_rect(fill, metrics.corner_radius), tone);
693 }
694}
695
696impl Widget for ProgressBar {
697 fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
698 let theme = self.resolved_theme();
699 let metrics = theme.metrics;
700 let min_height = if let Some(height) = self.height {
701 height
702 } else if self.show_value {
703 metrics
704 .progress_bar_value_height
705 .max(text_token_style(&theme, theme.text.sm, theme.palette.text).line_height)
706 } else {
707 metrics.progress_bar_height
708 };
709 constraints.clamp(Size::new(
710 self.min_width.unwrap_or(metrics.progress_bar_min_width),
711 min_height,
712 ))
713 }
714
715 fn paint(&self, ctx: &mut PaintCtx) {
716 let theme = self.resolved_theme();
717 let metrics = theme.metrics;
718 let (_, tone_text) = theme.semantic_tone_colors(self.tone);
719 let bounds = ctx.bounds();
720 paint_progress_bar(ctx, bounds, &theme, self.fraction(), self.tone);
721 if self.show_value {
722 let label = format!("{:.0}%", self.fraction() * 100.0);
723 let label_padding = Insets {
724 top: 0.0,
725 bottom: 0.0,
726 ..metrics.progress_bar_label_padding
727 };
728 let label_slot = inset_rect(bounds, label_padding);
729 let fill_edge = bounds.x() + bounds.width() * self.fraction();
733 let fill_part = Rect::new(
734 bounds.x(),
735 bounds.y(),
736 (fill_edge - bounds.x()).max(0.0),
737 bounds.height(),
738 );
739 let track_part = Rect::new(
740 fill_edge,
741 bounds.y(),
742 (bounds.max_x() - fill_edge).max(0.0),
743 bounds.height(),
744 );
745 for (part, color) in [(fill_part, tone_text), (track_part, theme.palette.text)] {
746 let Some(clip) = part.intersection(label_slot) else {
747 continue;
748 };
749 let text_style = numeric_text_style(text_token_style(&theme, theme.text.sm, color));
750 ctx.push_clip_rect(clip);
751 paint_text(ctx, label_slot, &label, &text_style, TextAlign::Center);
752 ctx.pop_clip();
753 }
754 }
755 }
756
757 fn semantics(&self, ctx: &mut SemanticsCtx) {
758 let mut node =
759 SemanticsNode::new(ctx.widget_id(), SemanticsRole::ProgressBar, ctx.bounds());
760 node.name = Some(self.name.clone());
761 node.value = Some(SemanticsValue::Range {
762 value: self.value,
763 min: self.min,
764 max: self.max,
765 });
766 ctx.push(node);
767 }
768}
769
770#[doc(alias = "BusyIndicator")]
771pub struct Spinner {
772 pub(super) theme: Box<DefaultTheme>,
773 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
774 pub(super) name: String,
775 pub(super) size: f32,
776 pub(super) label: Option<String>,
777}
778
779impl Spinner {
780 pub fn new(name: impl Into<String>) -> Self {
781 Self {
782 theme: Box::new(DefaultTheme::default()),
783 theme_reader: None,
784 name: name.into(),
785 size: 20.0,
786 label: None,
787 }
788 }
789
790 pub fn theme(mut self, theme: DefaultTheme) -> Self {
791 self.theme = Box::new(theme);
792 self.theme_reader = None;
793 self
794 }
795
796 pub fn theme_when<F>(mut self, theme: F) -> Self
797 where
798 F: Fn() -> DefaultTheme + 'static,
799 {
800 self.theme_reader = Some(Box::new(theme));
801 self
802 }
803
804 pub fn size(mut self, size: f32) -> Self {
805 self.size = size.max(8.0);
806 self
807 }
808
809 pub fn label(mut self, label: impl Into<String>) -> Self {
810 self.label = Some(label.into());
811 self
812 }
813
814 pub(super) fn indicator_rect(&self, bounds: Rect) -> Rect {
815 Rect::new(
816 bounds.x(),
817 bounds.y() + ((bounds.height() - self.size) * 0.5),
818 self.size,
819 self.size,
820 )
821 }
822
823 pub(super) fn resolved_theme(&self) -> DefaultTheme {
824 self.theme_reader
825 .as_ref()
826 .map(|theme| theme())
827 .unwrap_or_else(|| *self.theme)
828 }
829}
830
831impl Widget for Spinner {
832 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
833 let theme = self.resolved_theme();
834 let text_style = text_token_style(&theme, theme.text.sm, theme.palette.text);
835 let label_measurement = self
836 .label
837 .as_ref()
838 .map(|label| measure_text(ctx, label, &text_style));
839 let label_width = label_measurement
840 .map(|measurement| measurement.width + 12.0)
841 .unwrap_or(0.0);
842 let label_height = label_measurement
843 .map(|measurement| measurement.height.max(text_style.line_height))
844 .unwrap_or(0.0);
845 constraints.clamp(Size::new(
846 self.size + label_width,
847 self.size.max(20.0).max(label_height),
848 ))
849 }
850
851 fn paint(&self, ctx: &mut PaintCtx) {
852 let theme = self.resolved_theme();
853 let palette = theme.palette;
854 let indicator = self.indicator_rect(ctx.bounds());
855 let glow = theme.glow_for_output(GlowTone::Accent, ctx.output_color_range());
857 paint_theme_glow(ctx, indicator, [indicator.width() * 0.5; 4], &glow);
858 let center = rect_center(indicator);
859 let radius = indicator.width().min(indicator.height()) * 0.4;
860 let dot_radius = (indicator.width() * 0.09).max(1.5);
861 for index in 0..10 {
862 let angle = (index as f32 / 10.0) * std::f32::consts::TAU;
863 let alpha = 0.22 + ((index as f32) / 10.0) * 0.72;
864 let color = Color::rgba(
865 palette.accent.red,
866 palette.accent.green,
867 palette.accent.blue,
868 alpha,
869 );
870 let dot = Point::new(
871 center.x + angle.cos() * radius,
872 center.y + angle.sin() * radius,
873 );
874 ctx.fill(Path::circle(dot, dot_radius), color);
875 }
876
877 if let Some(label) = &self.label {
878 let text_style = text_token_style(&theme, theme.text.sm, palette.text);
879 let text_slot = Rect::new(
880 indicator.max_x() + 12.0,
881 ctx.bounds().y(),
882 ctx.bounds().width() - indicator.width() - 12.0,
883 ctx.bounds().height(),
884 );
885 ctx.push_clip_rect(text_slot);
886 paint_text(ctx, text_slot, label, &text_style, TextAlign::Start);
887 ctx.pop_clip();
888 }
889 }
890
891 fn semantics(&self, ctx: &mut SemanticsCtx) {
892 let mut node =
893 SemanticsNode::new(ctx.widget_id(), SemanticsRole::BusyIndicator, ctx.bounds());
894 node.name = Some(self.name.clone());
895 node.description = self.label.clone();
896 node.state.busy = true;
897 ctx.push(node);
898 }
899}
900
901pub(super) fn text_token_style(
902 theme: &DefaultTheme,
903 token: ThemeTextToken,
904 color: Color,
905) -> TextStyle {
906 TextStyle {
907 font_size: token.size.max(1.0),
908 line_height: token.line_height.max(1.0),
909 color,
910 ..theme.body_text_style()
911 }
912}
913
914pub(super) fn semibold_control_text_style(theme: &DefaultTheme, color: Color) -> TextStyle {
915 let mut style = theme.text_style(color);
916 style.weight = FontWeight::SEMIBOLD;
917 style
918}
919
920pub(super) fn numeric_text_style(mut style: TextStyle) -> TextStyle {
921 style.features.enable(FontFeature::TABULAR_FIGURES);
922 style
923}
924
925pub(super) fn numeric_text_style_if_numeric(text: &str, style: TextStyle) -> TextStyle {
926 if text_contains_ascii_digit(text) {
927 numeric_text_style(style)
928 } else {
929 style
930 }
931}
932
933pub(super) fn text_contains_ascii_digit(text: &str) -> bool {
934 text.chars().any(|c| c.is_ascii_digit())
935}
936
937pub(super) fn tab_indicator_rect<F>(
938 mut tab_rect: F,
939 from_index: usize,
940 selected_index: usize,
941 progress: f32,
942 padding: Insets,
943 thickness: f32,
944) -> Option<Rect>
945where
946 F: FnMut(usize) -> Option<Rect>,
947{
948 let to = tab_indicator_from_tab_rect(tab_rect(selected_index)?, padding, thickness);
949 let from = tab_rect(from_index)
950 .map(|rect| tab_indicator_from_tab_rect(rect, padding, thickness))
951 .unwrap_or(to);
952 Some(lerp_rect(from, to, progress))
953}
954
955pub(super) fn tab_indicator_from_tab_rect(rect: Rect, padding: Insets, thickness: f32) -> Rect {
956 Rect::new(
957 rect.x() + padding.left,
958 rect.max_y() - thickness,
959 (rect.width() - padding.left - padding.right).max(0.0),
960 thickness,
961 )
962}
963
964pub(super) fn sliding_inset_rect<F>(
965 mut item_rect: F,
966 from_index: usize,
967 selected_index: usize,
968 progress: f32,
969 insets: Insets,
970) -> Option<Rect>
971where
972 F: FnMut(usize) -> Option<Rect>,
973{
974 let to = inset_rect(item_rect(selected_index)?, insets);
975 let from = item_rect(from_index)
976 .map(|rect| inset_rect(rect, insets))
977 .unwrap_or(to);
978 Some(lerp_rect(from, to, progress))
979}
980
981pub(super) fn tab_panel_transition_translation(
982 from_index: usize,
983 selected_index: usize,
984 progress: f32,
985 metrics: ControlMetrics,
986) -> Vector {
987 if from_index == selected_index {
988 return Vector::ZERO;
989 }
990
991 let remaining = 1.0 - progress.clamp(0.0, 1.0);
992 if remaining <= Progress::EPSILON {
993 return Vector::ZERO;
994 }
995
996 let direction = if selected_index > from_index {
997 1.0
998 } else {
999 -1.0
1000 };
1001 Vector::new(direction * metrics.tab_panel_gap * remaining, 0.0)
1002}
1003
1004pub(super) fn lerp_rect(from: Rect, to: Rect, progress: f32) -> Rect {
1005 let progress = progress.clamp(0.0, 1.0);
1006 Rect::new(
1007 f32::interpolate(from.x(), to.x(), progress),
1008 f32::interpolate(from.y(), to.y(), progress),
1009 f32::interpolate(from.width(), to.width(), progress),
1010 f32::interpolate(from.height(), to.height(), progress),
1011 )
1012}
1013
1014pub(super) fn tab_state_visuals(
1019 theme: &DefaultTheme,
1020 selected: bool,
1021 hovered: bool,
1022 pressed: bool,
1023 hover_amount: f32,
1024 press_amount: f32,
1025) -> Option<(Color, Color)> {
1026 let palette = theme.palette;
1027 let interaction = theme.interaction;
1028 if selected {
1029 return Some((palette.surface_raised, palette.border));
1030 }
1031 if !(hovered || pressed || hover_amount > 0.0 || press_amount > 0.0) {
1032 return None;
1033 }
1034 let hovered_fill = mix_color(
1035 palette.control.with_alpha(0.0),
1036 palette.control,
1037 interaction.hover_blend * hover_amount,
1038 );
1039 let fill = mix_color(
1040 hovered_fill,
1041 palette.control_hover,
1042 interaction.pressed_blend * press_amount,
1043 );
1044 Some((fill, Color::TRANSPARENT))
1045}
1046
1047pub(super) fn draw_popover_arrival_overlay(
1048 ctx: &mut PaintCtx,
1049 rect: Rect,
1050 metrics: ControlMetrics,
1051 background: Color,
1052 border: Color,
1053 arrival_effect: ResolvedEffectStyle,
1054) {
1055 let overlay_inset = physical_pixels(ctx, 1.0);
1056 let overlay_rect = rect.inflate(-overlay_inset, -overlay_inset);
1057 let overlay_radius = (metrics.corner_radius + 2.0 - overlay_inset).max(0.0);
1058 let overlay_fill = mix_color(background, arrival_effect.color, 0.35)
1059 .with_alpha((0.10 + (arrival_effect.intensity * 0.12)).clamp(0.0, 0.22));
1060 let stroke_color = apply_hdr_policy_cap(
1061 mix_color(border, arrival_effect.color, 0.55),
1062 arrival_effect
1063 .color
1064 .red
1065 .max(arrival_effect.color.green.max(arrival_effect.color.blue)),
1066 )
1067 .with_alpha((0.16 + (arrival_effect.intensity * 0.12)).clamp(0.0, 0.30));
1068
1069 draw_control_shape(
1070 ctx,
1071 overlay_rect,
1072 overlay_radius,
1073 physical_pixels(ctx, 1.0),
1074 overlay_fill,
1075 stroke_color,
1076 );
1077}
1078
1079pub(super) fn tooltip_tail(
1084 trigger: Rect,
1085 bubble: Rect,
1086 placement: TooltipPlacement,
1087 base_inset: f32,
1088 side_outset: f32,
1089) -> Path {
1090 const HALF_WIDTH: f32 = 6.0;
1091 const HEIGHT: f32 = 8.0;
1092 let side_length = HALF_WIDTH.hypot(HEIGHT);
1093 let center_x = rect_center(trigger)
1094 .x
1095 .clamp(bubble.x() + 12.0, bubble.max_x() - 12.0);
1096 let (edge, direction) = match placement {
1097 TooltipPlacement::Above => (bubble.max_y(), 1.0),
1098 TooltipPlacement::Below => (bubble.y(), -1.0),
1099 };
1100 let base_y = edge - direction * base_inset;
1101 let half_width =
1102 HALF_WIDTH + side_outset * side_length / HEIGHT + base_inset * HALF_WIDTH / HEIGHT;
1103 let apex_y = edge + direction * (HEIGHT + side_outset * side_length / HALF_WIDTH);
1104 let mut builder = PathBuilder::new();
1105 builder
1106 .move_to(Point::new(center_x - half_width, base_y))
1107 .line_to(Point::new(center_x + half_width, base_y))
1108 .line_to(Point::new(center_x, apex_y));
1109 builder.build()
1110}