1use crate::DefaultTheme;
2use crate::Progress;
3use crate::SemanticTone;
4use crate::composites::forms::set_hover_animation_target;
5use crate::draw::{inset_rect, mix_color};
6use crate::frame::{
7 draw_control_shape, draw_focus_ring, physical_pixels, snap_to_pixels, snap_width_to_pixels,
8 stroke_border,
9};
10use crate::{
11 GlowTone, ThemeShadow, paint_theme_glow, paint_theme_inset_shadow, paint_theme_shadow,
12};
13use sui_core::Color;
14use sui_core::Event;
15use sui_core::Path;
16use sui_core::PointerEventKind;
17use sui_core::Rect;
18use sui_core::SemanticsNode;
19use sui_core::SemanticsRole;
20use sui_core::Size;
21use sui_core::WidgetId;
22use sui_layout::Constraints;
23use sui_layout::Padding as Insets;
24use sui_runtime::ArrangeCtx;
25use sui_runtime::EventCtx;
26use sui_runtime::MeasureCtx;
27use sui_runtime::PaintCtx;
28use sui_runtime::SemanticsCtx;
29use sui_runtime::SingleChild;
30use sui_runtime::Widget;
31use sui_runtime::WidgetPod;
32use sui_runtime::WidgetPodMutVisitor;
33use sui_runtime::WidgetPodVisitor;
34
35type ShadowToken = Box<dyn Fn(&DefaultTheme) -> ThemeShadow>;
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum SurfaceRole {
40 Window,
41 Sidebar,
42 Panel,
43 Titlebar,
44 Field,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum SurfaceBorder {
49 None,
50 All,
51 Top,
52 Right,
53 Bottom,
54 Left,
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub enum SurfaceElevation {
59 None,
60 Small,
61 Medium,
62 Large,
63}
64
65#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
66pub enum SurfaceAppearance {
67 #[default]
69 Standard,
70 Raised,
72 Soft,
74 Filled,
76}
77
78pub struct Surface {
79 pub(super) theme: Box<DefaultTheme>,
80 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
81 pub(super) name: Option<String>,
82 pub(super) role: SurfaceRole,
83 pub(super) appearance: SurfaceAppearance,
84 pub(super) tone: SemanticTone,
85 pub(super) border: SurfaceBorder,
86 pub(super) elevation: SurfaceElevation,
87 pub(super) shadow: Option<ShadowToken>,
88 pub(super) glow: Option<GlowTone>,
89 pub(super) radius: f32,
90 pub(super) padding: Insets,
91 pub(super) fill_width: bool,
92 pub(super) fill_height: bool,
93 pub(super) child: SingleChild,
94}
95
96impl Surface {
97 pub fn new<W>(role: SurfaceRole, child: W) -> Self
98 where
99 W: Widget + 'static,
100 {
101 Self {
102 theme: Box::new(DefaultTheme::default()),
103 theme_reader: None,
104 name: None,
105 role,
106 appearance: SurfaceAppearance::Standard,
107 tone: SemanticTone::Neutral,
108 border: SurfaceBorder::None,
109 elevation: SurfaceElevation::None,
110 shadow: None,
111 glow: None,
112 radius: 0.0,
113 padding: Insets::ZERO,
114 fill_width: false,
115 fill_height: false,
116 child: SingleChild::new(child),
117 }
118 }
119
120 pub fn window<W>(child: W) -> Self
121 where
122 W: Widget + 'static,
123 {
124 Self::new(SurfaceRole::Window, child)
125 }
126
127 pub fn sidebar<W>(child: W) -> Self
128 where
129 W: Widget + 'static,
130 {
131 Self::new(SurfaceRole::Sidebar, child).border(SurfaceBorder::Right)
132 }
133
134 pub fn panel<W>(child: W) -> Self
135 where
136 W: Widget + 'static,
137 {
138 Self::new(SurfaceRole::Panel, child)
139 .border(SurfaceBorder::All)
140 .corner_radius(8.0)
141 }
142
143 pub fn titlebar<W>(child: W) -> Self
144 where
145 W: Widget + 'static,
146 {
147 Self::new(SurfaceRole::Titlebar, child).border(SurfaceBorder::Bottom)
148 }
149
150 pub fn field<W>(child: W) -> Self
151 where
152 W: Widget + 'static,
153 {
154 Self::new(SurfaceRole::Field, child)
155 .border(SurfaceBorder::All)
156 .corner_radius(6.0)
157 }
158
159 pub fn theme(mut self, theme: DefaultTheme) -> Self {
160 self.theme = Box::new(theme);
161 self.theme_reader = None;
162 self
163 }
164
165 pub fn theme_when<F>(mut self, theme: F) -> Self
166 where
167 F: Fn() -> DefaultTheme + 'static,
168 {
169 self.theme_reader = Some(Box::new(theme));
170 self
171 }
172
173 pub fn name(mut self, name: impl Into<String>) -> Self {
174 self.name = Some(name.into());
175 self
176 }
177
178 pub fn border(mut self, border: SurfaceBorder) -> Self {
179 self.border = border;
180 self
181 }
182
183 pub fn appearance(mut self, appearance: SurfaceAppearance) -> Self {
184 self.appearance = appearance;
185 self
186 }
187
188 pub fn tone(mut self, tone: SemanticTone) -> Self {
189 self.tone = tone;
190 self
191 }
192
193 pub fn elevation(mut self, elevation: SurfaceElevation) -> Self {
194 self.elevation = elevation;
195 self
196 }
197
198 pub fn shadow<F>(mut self, shadow: F) -> Self
206 where
207 F: Fn(&DefaultTheme) -> ThemeShadow + 'static,
208 {
209 self.shadow = Some(Box::new(shadow));
210 self
211 }
212
213 pub fn glow(mut self, tone: GlowTone) -> Self {
216 self.glow = Some(tone);
217 self
218 }
219
220 pub fn corner_radius(mut self, corner_radius: f32) -> Self {
221 self.radius = corner_radius.max(0.0);
222 self
223 }
224
225 pub fn padding(mut self, padding: Insets) -> Self {
226 self.padding = padding;
227 self
228 }
229
230 pub fn fill(mut self) -> Self {
231 self.fill_width = true;
232 self.fill_height = true;
233 self
234 }
235
236 pub fn fill_width(mut self, fill_width: bool) -> Self {
237 self.fill_width = fill_width;
238 self
239 }
240
241 pub fn fill_height(mut self, fill_height: bool) -> Self {
242 self.fill_height = fill_height;
243 self
244 }
245
246 pub(super) fn resolved_theme(&self) -> DefaultTheme {
247 self.theme_reader
248 .as_ref()
249 .map(|theme| theme())
250 .unwrap_or_else(|| *self.theme)
251 }
252
253 pub(super) fn background_for_role(theme: &DefaultTheme, role: SurfaceRole) -> Color {
254 match role {
255 SurfaceRole::Window => theme.surfaces.window,
256 SurfaceRole::Sidebar => theme.surfaces.sidebar,
257 SurfaceRole::Panel => theme.surfaces.panel,
258 SurfaceRole::Titlebar => theme.surfaces.titlebar,
259 SurfaceRole::Field => theme.surfaces.field,
260 }
261 }
262
263 pub(super) fn resolved_colors(&self, theme: &DefaultTheme) -> (Color, Color) {
264 match self.appearance {
265 SurfaceAppearance::Standard => (
266 Self::background_for_role(theme, self.role),
267 theme.surfaces.border,
268 ),
269 SurfaceAppearance::Raised => (theme.palette.surface_raised, theme.surfaces.border),
270 SurfaceAppearance::Soft => {
271 let (fill, _) = theme.semantic_tone_soft_colors(self.tone);
272 let border = if self.tone == SemanticTone::Neutral {
273 theme.surfaces.border
274 } else {
275 theme.semantic_tone_color(self.tone).with_alpha(0.36)
276 };
277 (fill, border)
278 }
279 SurfaceAppearance::Filled => {
280 let (fill, _) = theme.semantic_tone_colors(self.tone);
281 (fill, fill)
282 }
283 }
284 }
285
286 pub(super) fn content_rect(&self, bounds: Rect) -> Rect {
287 inset_rect(bounds, self.padding)
288 }
289}
290
291impl Widget for Surface {
292 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
293 let max_child = Size::new(
294 if constraints.max.width.is_finite() {
295 (constraints.max.width - self.padding.left - self.padding.right).max(0.0)
296 } else {
297 f32::INFINITY
298 },
299 if constraints.max.height.is_finite() {
300 (constraints.max.height - self.padding.top - self.padding.bottom).max(0.0)
301 } else {
302 f32::INFINITY
303 },
304 );
305 let min_child = Size::new(
312 if self.fill_width && max_child.width.is_finite() {
313 max_child.width
314 } else {
315 (constraints.min.width - self.padding.left - self.padding.right)
316 .max(0.0)
317 .min(max_child.width)
318 },
319 if self.fill_height && max_child.height.is_finite() {
320 max_child.height
321 } else {
322 (constraints.min.height - self.padding.top - self.padding.bottom)
323 .max(0.0)
324 .min(max_child.height)
325 },
326 );
327 let child_size = self
328 .child
329 .measure(ctx, Constraints::new(min_child, max_child));
330 let mut size = Size::new(
331 child_size.width + self.padding.left + self.padding.right,
332 child_size.height + self.padding.top + self.padding.bottom,
333 );
334 if self.fill_width && constraints.max.width.is_finite() {
335 size.width = constraints.max.width;
336 }
337 if self.fill_height && constraints.max.height.is_finite() {
338 size.height = constraints.max.height;
339 }
340 constraints.clamp(size)
341 }
342
343 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
344 self.child.arrange(ctx, self.content_rect(bounds));
345 }
346
347 fn paint(&self, ctx: &mut PaintCtx) {
348 let theme = self.resolved_theme();
349 let bounds = snap_to_pixels(ctx, ctx.bounds());
350 let radius = self.radius.min(bounds.width().min(bounds.height()) * 0.5);
351
352 let shadow = match (&self.shadow, self.elevation) {
353 (Some(shadow), _) => Some(shadow(&theme)),
354 (None, SurfaceElevation::None) => None,
355 (None, SurfaceElevation::Small) => Some(theme.shadows.box_shadow.sm),
356 (None, SurfaceElevation::Medium) => Some(theme.shadows.box_shadow.md),
357 (None, SurfaceElevation::Large) => Some(theme.shadows.box_shadow.lg),
358 };
359 if let Some(shadow) = &shadow {
360 paint_theme_shadow(ctx, bounds, [radius; 4], shadow);
361 }
362 if let Some(tone) = self.glow {
363 let glow = theme.glow_for_output(tone, ctx.output_color_range());
364 paint_theme_glow(ctx, bounds, [radius; 4], &glow);
365 }
366
367 let (background, border) = self.resolved_colors(&theme);
368 if radius > 0.0 {
369 ctx.fill(Path::rounded_rect(bounds, radius), background);
370 } else {
371 ctx.fill_rect(bounds, background);
372 }
373 let stroke_width = snap_width_to_pixels(
374 ctx,
375 physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
376 );
377 if let Some(shadow) = &shadow {
378 let (inside, inside_radius) = inside_border(bounds, radius, self.border, stroke_width);
379 paint_theme_inset_shadow(ctx, inside, [inside_radius; 4], shadow);
380 }
381
382 match self.border {
383 SurfaceBorder::None => {}
384 SurfaceBorder::All => stroke_border(ctx, bounds, radius, stroke_width, border),
385 SurfaceBorder::Top => ctx.fill_rect(
386 Rect::new(bounds.x(), bounds.y(), bounds.width(), stroke_width),
387 border,
388 ),
389 SurfaceBorder::Right => ctx.fill_rect(
390 Rect::new(
391 bounds.max_x() - stroke_width,
392 bounds.y(),
393 stroke_width,
394 bounds.height(),
395 ),
396 border,
397 ),
398 SurfaceBorder::Bottom => ctx.fill_rect(
399 Rect::new(
400 bounds.x(),
401 bounds.max_y() - stroke_width,
402 bounds.width(),
403 stroke_width,
404 ),
405 border,
406 ),
407 SurfaceBorder::Left => ctx.fill_rect(
408 Rect::new(bounds.x(), bounds.y(), stroke_width, bounds.height()),
409 border,
410 ),
411 }
412
413 self.child.paint(ctx);
414 }
415
416 fn semantics(&self, ctx: &mut SemanticsCtx) {
417 if let Some(name) = &self.name {
418 let mut node = SemanticsNode::new(
419 ctx.widget_id(),
420 SemanticsRole::GenericContainer,
421 ctx.bounds(),
422 );
423 node.name = Some(name.clone());
424 ctx.push(node);
425 }
426 self.child.semantics(ctx);
427 }
428
429 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
430 self.child.visit_children(visitor);
431 }
432
433 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
434 self.child.visit_children_mut(visitor);
435 }
436}
437
438pub struct ShadowBox {
444 theme: Box<DefaultTheme>,
445 theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
446 radius: f32,
447 shadow: Option<ShadowToken>,
448 glow: Option<GlowTone>,
449 child: SingleChild,
450}
451
452impl ShadowBox {
453 pub fn new<W>(child: W) -> Self
454 where
455 W: Widget + 'static,
456 {
457 Self {
458 theme: Box::new(DefaultTheme::default()),
459 theme_reader: None,
460 radius: 0.0,
461 shadow: None,
462 glow: None,
463 child: SingleChild::new(child),
464 }
465 }
466
467 pub fn theme(mut self, theme: DefaultTheme) -> Self {
468 self.theme = Box::new(theme);
469 self.theme_reader = None;
470 self
471 }
472
473 pub fn theme_when<F>(mut self, theme: F) -> Self
474 where
475 F: Fn() -> DefaultTheme + 'static,
476 {
477 self.theme_reader = Some(Box::new(theme));
478 self
479 }
480
481 pub fn corner_radius(mut self, corner_radius: f32) -> Self {
483 self.radius = corner_radius.max(0.0);
484 self
485 }
486
487 pub fn shadow<F>(mut self, shadow: F) -> Self
490 where
491 F: Fn(&DefaultTheme) -> ThemeShadow + 'static,
492 {
493 self.shadow = Some(Box::new(shadow));
494 self
495 }
496
497 pub fn glow(mut self, tone: GlowTone) -> Self {
500 self.glow = Some(tone);
501 self
502 }
503
504 fn resolved_theme(&self) -> DefaultTheme {
505 self.theme_reader
506 .as_ref()
507 .map(|theme| theme())
508 .unwrap_or_else(|| *self.theme)
509 }
510}
511
512fn inside_border(
516 bounds: Rect,
517 radius: f32,
518 border: SurfaceBorder,
519 stroke_width: f32,
520) -> (Rect, f32) {
521 let (left, top, right, bottom) = match border {
522 SurfaceBorder::None => (0.0, 0.0, 0.0, 0.0),
523 SurfaceBorder::All => (stroke_width, stroke_width, stroke_width, stroke_width),
524 SurfaceBorder::Top => (0.0, stroke_width, 0.0, 0.0),
525 SurfaceBorder::Right => (0.0, 0.0, stroke_width, 0.0),
526 SurfaceBorder::Bottom => (0.0, 0.0, 0.0, stroke_width),
527 SurfaceBorder::Left => (stroke_width, 0.0, 0.0, 0.0),
528 };
529 let inside = Rect::new(
530 bounds.x() + left,
531 bounds.y() + top,
532 (bounds.width() - left - right).max(0.0),
533 (bounds.height() - top - bottom).max(0.0),
534 );
535 let widest = left.max(top).max(right).max(bottom);
536 (inside, (radius - widest).max(0.0))
537}
538
539impl Widget for ShadowBox {
540 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
541 self.child.measure(ctx, constraints)
542 }
543
544 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
545 self.child.arrange(ctx, bounds);
546 }
547
548 fn paint(&self, ctx: &mut PaintCtx) {
549 let theme = self.resolved_theme();
550 let bounds = ctx.bounds();
551 let radii = [self.radius.min(bounds.width().min(bounds.height()) * 0.5); 4];
552 let shadow = self.shadow.as_ref().map(|shadow| shadow(&theme));
553 if let Some(shadow) = &shadow {
554 paint_theme_shadow(ctx, bounds, radii, shadow);
555 }
556 if let Some(tone) = self.glow {
557 let glow = theme.glow_for_output(tone, ctx.output_color_range());
558 paint_theme_glow(ctx, bounds, radii, &glow);
559 }
560 self.child.paint(ctx);
561 if let Some(shadow) = &shadow {
562 paint_theme_inset_shadow(ctx, bounds, radii, shadow);
563 }
564 }
565
566 fn semantics(&self, ctx: &mut SemanticsCtx) {
567 self.child.semantics(ctx);
568 }
569
570 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
571 self.child.visit_children(visitor);
572 }
573
574 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
575 self.child.visit_children_mut(visitor);
576 }
577}
578
579pub struct FramedField {
586 pub(super) theme: Box<DefaultTheme>,
587 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
588 pub(super) name: Option<String>,
589 pub(super) description: Option<String>,
590 pub(super) padding: Insets,
591 pub(super) min_height: Option<f32>,
592 pub(super) fill_width: bool,
593 pub(super) focused: Option<bool>,
594 pub(super) focused_reader: Option<Box<dyn Fn() -> bool>>,
595 pub(super) invalid: bool,
596 pub(super) invalid_reader: Option<Box<dyn Fn() -> bool>>,
597 pub(super) hovered: bool,
598 pub(super) hover_animation: Progress,
599 pub(super) child: SingleChild,
600}
601
602impl FramedField {
603 pub fn new<W>(child: W) -> Self
604 where
605 W: Widget + 'static,
606 {
607 Self {
608 theme: Box::new(DefaultTheme::default()),
609 theme_reader: None,
610 name: None,
611 description: None,
612 padding: Insets::ZERO,
613 min_height: None,
614 fill_width: false,
615 focused: None,
616 focused_reader: None,
617 invalid: false,
618 invalid_reader: None,
619 hovered: false,
620 hover_animation: Progress::new(0.0),
621 child: SingleChild::new(child),
622 }
623 }
624
625 pub fn theme(mut self, theme: DefaultTheme) -> Self {
626 self.theme = Box::new(theme);
627 self.theme_reader = None;
628 self
629 }
630
631 pub fn theme_when<F>(mut self, theme: F) -> Self
632 where
633 F: Fn() -> DefaultTheme + 'static,
634 {
635 self.theme_reader = Some(Box::new(theme));
636 self
637 }
638
639 pub fn name(mut self, name: impl Into<String>) -> Self {
640 self.name = Some(name.into());
641 self
642 }
643
644 pub fn description(mut self, description: impl Into<String>) -> Self {
645 self.description = Some(description.into());
646 self
647 }
648
649 pub fn padding(mut self, padding: Insets) -> Self {
650 self.padding = padding;
651 self
652 }
653
654 pub fn min_height(mut self, min_height: f32) -> Self {
655 self.min_height = Some(min_height.max(0.0));
656 self
657 }
658
659 pub fn fill_width(mut self, fill_width: bool) -> Self {
660 self.fill_width = fill_width;
661 self
662 }
663
664 pub fn focused(mut self, focused: bool) -> Self {
665 self.focused = Some(focused);
666 self.focused_reader = None;
667 self
668 }
669
670 pub fn focused_when<F>(mut self, focused: F) -> Self
671 where
672 F: Fn() -> bool + 'static,
673 {
674 self.focused_reader = Some(Box::new(focused));
675 self
676 }
677
678 pub fn invalid(mut self, invalid: bool) -> Self {
679 self.invalid = invalid;
680 self.invalid_reader = None;
681 self
682 }
683
684 pub fn invalid_when<F>(mut self, invalid: F) -> Self
685 where
686 F: Fn() -> bool + 'static,
687 {
688 self.invalid_reader = Some(Box::new(invalid));
689 self
690 }
691
692 pub(super) fn resolved_theme(&self) -> DefaultTheme {
693 self.theme_reader
694 .as_ref()
695 .map(|theme| theme())
696 .unwrap_or_else(|| *self.theme)
697 }
698
699 pub(super) fn child_contains(&self, target: WidgetId) -> bool {
700 if self.child.child().id() == target {
701 return true;
702 }
703 struct Finder {
704 target: WidgetId,
705 found: bool,
706 }
707 impl WidgetPodVisitor for Finder {
708 fn visit(&mut self, child: &WidgetPod) {
709 if self.found {
710 return;
711 }
712 if child.id() == self.target {
713 self.found = true;
714 } else {
715 child.visit_children(self);
716 }
717 }
718 }
719 let mut finder = Finder {
720 target,
721 found: false,
722 };
723 self.child.child().visit_children(&mut finder);
724 finder.found
725 }
726
727 pub(super) fn is_focused(&self, focused_widget_id: Option<WidgetId>) -> bool {
728 if let Some(focused) = &self.focused_reader {
729 return focused();
730 }
731 if let Some(focused) = self.focused {
732 return focused;
733 }
734 focused_widget_id
735 .map(|focused| self.child_contains(focused))
736 .unwrap_or(false)
737 }
738
739 pub(super) fn is_invalid(&self) -> bool {
740 self.invalid_reader
741 .as_ref()
742 .map(|invalid| invalid())
743 .unwrap_or(self.invalid)
744 }
745
746 pub(super) fn content_rect(&self, bounds: Rect) -> Rect {
747 inset_rect(bounds, self.padding)
748 }
749
750 pub(super) fn set_hovered(&mut self, hovered: bool, ctx: &mut EventCtx) {
751 if self.hovered == hovered {
752 return;
753 }
754 self.hovered = hovered;
755 let theme = self.resolved_theme();
756 set_hover_animation_target(&mut self.hover_animation, hovered as u8 as f32, &theme, ctx);
757 ctx.request_paint();
758 }
759}
760
761impl Widget for FramedField {
762 fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
763 match event {
764 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
765 self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
766 }
767 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Enter => {
768 self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
769 }
770 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Leave => {
771 self.set_hovered(false, ctx);
772 }
773 _ => {}
774 }
775 }
776
777 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
778 let max_child = Size::new(
779 if constraints.max.width.is_finite() {
780 (constraints.max.width - self.padding.left - self.padding.right).max(0.0)
781 } else {
782 f32::INFINITY
783 },
784 if constraints.max.height.is_finite() {
785 (constraints.max.height - self.padding.top - self.padding.bottom).max(0.0)
786 } else {
787 f32::INFINITY
788 },
789 );
790 let child = self
791 .child
792 .measure(ctx, Constraints::new(Size::ZERO, max_child));
793 let theme = self.resolved_theme();
794 let mut size = Size::new(
795 child.width + self.padding.left + self.padding.right,
796 (child.height + self.padding.top + self.padding.bottom)
797 .max(self.min_height.unwrap_or(theme.metrics.min_height)),
798 );
799 if self.fill_width && constraints.max.width.is_finite() {
800 size.width = constraints.max.width;
801 }
802 constraints.clamp(size)
803 }
804
805 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
806 self.child.arrange(ctx, self.content_rect(bounds));
807 }
808
809 fn paint(&self, ctx: &mut PaintCtx) {
810 let theme = self.resolved_theme();
811 let bounds = ctx.bounds();
812 let radius = theme
813 .metrics
814 .corner_radius
815 .min(bounds.width().min(bounds.height()) * 0.5);
816 let invalid = self.is_invalid();
817 let focused = self.is_focused(ctx.focused_widget_id());
818 let interaction_border = mix_color(
819 theme.palette.button_border,
820 theme.palette.border_hover,
821 self.hover_animation.get(ctx),
822 );
823 let border = if invalid {
824 theme.semantic_tone_color(SemanticTone::Danger)
825 } else if focused {
826 theme.palette.border_focus
827 } else {
828 interaction_border
829 };
830 let background = mix_color(
831 theme.surfaces.field,
832 theme.palette.surface_focus,
833 focused as u8 as f32,
834 );
835 draw_control_shape(
836 ctx,
837 bounds,
838 radius,
839 physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
840 background,
841 border,
842 );
843 if focused {
844 draw_focus_ring(
845 ctx,
846 bounds,
847 radius,
848 theme.metrics,
849 if invalid {
850 theme.semantic_tone_color(SemanticTone::Danger)
851 } else {
852 theme.palette.focus_ring
853 },
854 );
855 }
856 self.child.paint(ctx);
857 }
858
859 fn semantics(&self, ctx: &mut SemanticsCtx) {
860 if self.name.is_some() || self.description.is_some() {
861 let mut node = SemanticsNode::new(
862 ctx.widget_id(),
863 SemanticsRole::GenericContainer,
864 ctx.bounds(),
865 );
866 node.name = self.name.clone();
867 node.description = self.description.clone();
868 node.state.focused = self.is_focused(ctx.focused_widget_id());
869 node.state.hovered = self.hovered;
870 ctx.push(node);
871 }
872 self.child.semantics(ctx);
873 }
874
875 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
876 self.child.visit_children(visitor);
877 }
878
879 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
880 self.child.visit_children_mut(visitor);
881 }
882}