1use crate::ControlMetrics;
2use crate::DecorativeHue;
3use crate::DefaultTheme;
4use crate::IconGlyph;
5use crate::SemanticTone;
6use crate::animation::{AnimationSpec, Progress, StateMotion};
7use crate::composites::indicators::text_token_style;
8use crate::composites::surfaces::SurfaceElevation;
9use crate::controls::draw_icon_glyph;
10use crate::draw::{inset_rect, measure_text, mix_color};
11use crate::frame::{draw_control_frame, draw_control_shape, draw_focus_ring, physical_pixels};
12use crate::paint_theme_shadow;
13use crate::text_align::paint_text;
14use crate::text_align::paint_text_line;
15use sui_core::Color;
16use sui_core::Event;
17use sui_core::KeyState;
18use sui_core::Path;
19use sui_core::PathBuilder;
20use sui_core::Point;
21use sui_core::PointerButton;
22use sui_core::PointerEventKind;
23use sui_core::Rect;
24use sui_core::SemanticsAction;
25use sui_core::SemanticsNode;
26use sui_core::SemanticsRole;
27use sui_core::SemanticsValue;
28use sui_core::Size;
29use sui_core::WidgetId;
30use sui_layout::Constraints;
31use sui_layout::Padding as Insets;
32use sui_runtime::ArrangeCtx;
33use sui_runtime::EventCtx;
34use sui_runtime::LayerOptions;
35use sui_runtime::MeasureCtx;
36use sui_runtime::PaintBoundaryMode;
37use sui_runtime::PaintCtx;
38use sui_runtime::SemanticsCtx;
39use sui_runtime::SingleChild;
40use sui_runtime::Widget;
41use sui_runtime::WidgetChildren;
42use sui_runtime::WidgetPodMutVisitor;
43use sui_runtime::WidgetPodVisitor;
44use sui_text::TextMeasurement;
45use sui_text::TextStyle;
46use sui_text::{FontWeight, TextAlign};
47
48pub struct ActionCard {
49 pub(super) theme: Box<DefaultTheme>,
50 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
51 pub(super) title: String,
52 pub(super) description: String,
53 pub(super) icon: Option<IconGlyph>,
54 pub(super) tone: SemanticTone,
55 pub(super) accent: Option<Color>,
56 pub(super) decorative: Option<DecorativeHue>,
57 pub(super) padding: Option<Insets>,
58 pub(super) min_width: Option<f32>,
59 pub(super) min_height: Option<f32>,
60 pub(super) hovered: bool,
61 pub(super) pressed: bool,
62 pub(super) hover_animation: Progress,
63 pub(super) press_animation: Progress,
64 pub(super) focus_animation: Progress,
65 pub(super) title_measurement: Option<TextMeasurement>,
66 pub(super) description_measurement: Option<TextMeasurement>,
67 pub(super) enabled: bool,
68 pub(super) enabled_reader: Option<Box<dyn Fn() -> bool>>,
69 pub(super) on_press: Option<Box<dyn FnMut()>>,
70 pub(super) on_press_with_ctx: Option<Box<dyn FnMut(&mut EventCtx)>>,
71}
72
73impl ActionCard {
74 pub fn new(title: impl Into<String>, description: impl Into<String>) -> Self {
75 Self {
76 theme: Box::new(DefaultTheme::default()),
77 theme_reader: None,
78 title: title.into(),
79 description: description.into(),
80 icon: None,
81 tone: SemanticTone::Accent,
82 accent: None,
83 decorative: None,
84 padding: None,
85 min_width: None,
86 min_height: None,
87 hovered: false,
88 pressed: false,
89 hover_animation: Progress::new(0.0),
90 press_animation: Progress::new(0.0),
91 focus_animation: Progress::new(0.0),
92 title_measurement: None,
93 description_measurement: None,
94 enabled: true,
95 enabled_reader: None,
96 on_press: None,
97 on_press_with_ctx: None,
98 }
99 }
100
101 pub fn theme(mut self, theme: DefaultTheme) -> Self {
102 self.theme = Box::new(theme);
103 self.theme_reader = None;
104 self
105 }
106
107 pub fn theme_when<F>(mut self, theme: F) -> Self
108 where
109 F: Fn() -> DefaultTheme + 'static,
110 {
111 self.theme_reader = Some(Box::new(theme));
112 self
113 }
114
115 pub fn icon(mut self, icon: IconGlyph) -> Self {
116 self.icon = Some(icon);
117 self
118 }
119
120 pub fn without_icon(mut self) -> Self {
121 self.icon = None;
122 self
123 }
124
125 pub fn accent(mut self, accent: Color) -> Self {
127 self.accent = Some(accent);
128 self.decorative = None;
129 self
130 }
131
132 pub fn decorative(mut self, hue: DecorativeHue) -> Self {
135 self.decorative = Some(hue);
136 self.accent = None;
137 self
138 }
139
140 pub fn tone(mut self, tone: SemanticTone) -> Self {
141 self.tone = tone;
142 self
143 }
144
145 pub fn padding(mut self, padding: Insets) -> Self {
146 self.padding = Some(padding);
147 self
148 }
149
150 pub fn min_width(mut self, width: f32) -> Self {
151 self.min_width = Some(width.max(0.0));
152 self
153 }
154
155 pub fn min_height(mut self, height: f32) -> Self {
156 self.min_height = Some(height.max(0.0));
157 self
158 }
159
160 pub fn enabled(mut self, enabled: bool) -> Self {
161 self.enabled = enabled;
162 self.enabled_reader = None;
163 self
164 }
165
166 pub fn enabled_when<F>(mut self, enabled: F) -> Self
167 where
168 F: Fn() -> bool + 'static,
169 {
170 self.enabled_reader = Some(Box::new(enabled));
171 self
172 }
173
174 pub fn on_press<F>(mut self, on_press: F) -> Self
175 where
176 F: FnMut() + 'static,
177 {
178 self.on_press = Some(Box::new(on_press));
179 self
180 }
181
182 pub fn on_press_with_ctx<F>(mut self, on_press: F) -> Self
183 where
184 F: FnMut(&mut EventCtx) + 'static,
185 {
186 self.on_press_with_ctx = Some(Box::new(on_press));
187 self
188 }
189
190 pub(super) fn is_enabled(&self) -> bool {
191 self.enabled_reader
192 .as_ref()
193 .map(|enabled| enabled())
194 .unwrap_or(self.enabled)
195 }
196
197 pub(super) fn resolved_theme(&self) -> DefaultTheme {
198 self.theme_reader
199 .as_ref()
200 .map(|theme| theme())
201 .unwrap_or_else(|| *self.theme)
202 }
203
204 pub(super) fn resolved_padding(&self, metrics: ControlMetrics) -> Insets {
205 self.padding.unwrap_or(metrics.action_card_padding)
206 }
207
208 pub(super) fn resolved_min_width(&self, metrics: ControlMetrics) -> f32 {
209 self.min_width.unwrap_or(metrics.action_card_min_width)
210 }
211
212 pub(super) fn resolved_min_height(&self, metrics: ControlMetrics) -> f32 {
213 self.min_height.unwrap_or(metrics.action_card_min_height)
214 }
215
216 pub(super) fn activate(&mut self, ctx: &mut EventCtx) {
217 if !self.is_enabled() {
218 return;
219 }
220 if let Some(on_press) = &mut self.on_press {
221 on_press();
222 }
223 if let Some(on_press) = &mut self.on_press_with_ctx {
224 on_press(ctx);
225 }
226 }
227
228 pub(super) fn set_hovered(&mut self, hovered: bool, ctx: &mut EventCtx) {
229 if self.hovered == hovered {
230 return;
231 }
232 let theme = self.resolved_theme();
233 self.hovered = hovered;
234 set_action_card_hover_animation_target(
235 &mut self.hover_animation,
236 hovered as u8 as f32,
237 &theme,
238 ctx,
239 );
240 ctx.request_paint();
241 ctx.request_semantics();
242 }
243
244 pub(super) fn clear_transient_state_for_hidden_bounds(&mut self, ctx: &mut ArrangeCtx) {
245 if !self.hovered && !self.pressed && !self.focus_animation.is_presented(ctx) {
246 return;
247 }
248
249 self.hovered = false;
250 self.pressed = false;
251 self.hover_animation = Progress::new(0.0);
252 self.press_animation = Progress::new(0.0);
253 self.focus_animation = Progress::new(0.0);
254 ctx.request_paint();
255 ctx.request_semantics();
256 }
257
258 pub(super) fn resolved_title_style(&self) -> TextStyle {
259 let theme = self.resolved_theme();
260 TextStyle {
261 weight: FontWeight::SEMIBOLD,
262 ..text_token_style(&theme, theme.text.base, theme.palette.text)
263 }
264 }
265
266 pub(super) fn resolved_description_style(&self) -> TextStyle {
267 let theme = self.resolved_theme();
268 text_token_style(&theme, theme.text.sm, theme.palette.text_muted)
269 }
270
271 pub(super) fn content_rect(&self, bounds: Rect, metrics: ControlMetrics) -> Rect {
272 inset_rect(bounds, self.resolved_padding(metrics))
273 }
274
275 pub(super) fn text_bounds(&self, bounds: Rect, metrics: ControlMetrics) -> Rect {
276 let content = self.content_rect(bounds, metrics);
277 let icon_extent = self
278 .icon
279 .map(|_| metrics.action_card_icon_box_size + metrics.action_card_icon_gap)
280 .unwrap_or(0.0);
281 let trailing = metrics.action_card_trailing_gap;
282 Rect::new(
283 content.x() + icon_extent,
284 content.y(),
285 (content.width() - icon_extent - trailing).max(0.0),
286 content.height(),
287 )
288 }
289}
290
291impl Widget for ActionCard {
292 fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
293 if !self.is_enabled() {
294 if self.hovered || self.pressed {
295 let theme = self.resolved_theme();
296 self.hovered = false;
297 self.pressed = false;
298 set_action_card_hover_animation_target(&mut self.hover_animation, 0.0, &theme, ctx);
299 set_action_card_press_animation_target(&mut self.press_animation, 0.0, &theme, ctx);
300 ctx.request_paint();
301 ctx.request_semantics();
302 }
303 return;
304 }
305
306 match event {
307 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
308 self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
309 }
310 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Enter => {
311 self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
312 }
313 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Leave => {
314 self.set_hovered(false, ctx);
315 }
316 Event::Pointer(pointer)
317 if pointer.kind == PointerEventKind::Down
318 && pointer.button == Some(PointerButton::Primary) =>
319 {
320 let theme = self.resolved_theme();
321 self.pressed = true;
322 self.hovered = true;
323 set_action_card_hover_animation_target(&mut self.hover_animation, 1.0, &theme, ctx);
324 set_action_card_press_animation_target(&mut self.press_animation, 1.0, &theme, ctx);
325 ctx.request_pointer_capture(pointer.pointer_id);
326 ctx.request_focus();
327 ctx.request_paint();
328 ctx.request_semantics();
329 ctx.set_handled();
330 }
331 Event::Pointer(pointer)
332 if pointer.kind == PointerEventKind::Up
333 && pointer.button == Some(PointerButton::Primary) =>
334 {
335 let theme = self.resolved_theme();
336 let hovered = ctx.bounds().contains(pointer.position);
337 let activate = self.pressed && hovered;
338 self.pressed = false;
339 self.hovered = hovered;
340 set_action_card_hover_animation_target(
341 &mut self.hover_animation,
342 hovered as u8 as f32,
343 &theme,
344 ctx,
345 );
346 set_action_card_press_animation_target(&mut self.press_animation, 0.0, &theme, ctx);
347 ctx.release_pointer_capture(pointer.pointer_id);
348 if activate {
349 self.activate(ctx);
350 }
351 ctx.request_paint();
352 ctx.request_semantics();
353 ctx.set_handled();
354 }
355 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
356 if self.pressed {
357 let theme = self.resolved_theme();
358 self.pressed = false;
359 self.hovered = false;
360 set_action_card_hover_animation_target(
361 &mut self.hover_animation,
362 0.0,
363 &theme,
364 ctx,
365 );
366 set_action_card_press_animation_target(
367 &mut self.press_animation,
368 0.0,
369 &theme,
370 ctx,
371 );
372 ctx.release_pointer_capture(pointer.pointer_id);
373 ctx.request_paint();
374 ctx.request_semantics();
375 ctx.set_handled();
376 }
377 }
378 Event::Keyboard(key)
379 if key.state == KeyState::Pressed
380 && ctx.is_focused()
381 && matches!(key.key.as_str(), "Enter" | " ") =>
382 {
383 self.activate(ctx);
384 ctx.request_paint();
385 ctx.request_semantics();
386 ctx.set_handled();
387 }
388 _ => {}
389 }
390 }
391
392 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
393 let theme = self.resolved_theme();
394 let metrics = theme.metrics;
395 let padding = self.resolved_padding(metrics);
396 let title_style = self.resolved_title_style();
397 let description_style = self.resolved_description_style();
398 let title = measure_text(ctx, &self.title, &title_style);
399 let description = measure_text(ctx, &self.description, &description_style);
400 self.title_measurement = Some(title);
401 self.description_measurement = Some(description);
402
403 let icon_extent = self
404 .icon
405 .map(|_| metrics.action_card_icon_box_size + metrics.action_card_icon_gap)
406 .unwrap_or(0.0);
407 let text_width = title.width.max(description.width).min(320.0);
408 let natural = Size::new(
409 self.resolved_min_width(metrics).max(
410 padding.left
411 + icon_extent
412 + text_width
413 + metrics.action_card_trailing_gap
414 + padding.right,
415 ),
416 self.resolved_min_height(metrics).max(
417 padding.top
418 + title.height.max(title_style.line_height)
419 + metrics.action_card_text_gap
420 + description.height.max(description_style.line_height)
421 + padding.bottom,
422 ),
423 );
424 constraints.clamp(natural)
425 }
426
427 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
428 if bounds.width() <= 0.0 || bounds.height() <= 0.0 {
429 self.clear_transient_state_for_hidden_bounds(ctx);
430 }
431 }
432
433 fn paint(&self, ctx: &mut PaintCtx) {
434 let theme = self.resolved_theme();
435 let palette = theme.palette;
436 let metrics = theme.metrics;
437 let enabled = self.is_enabled();
438 let hover = if enabled {
439 self.hover_animation.get(ctx)
440 } else {
441 0.0
442 };
443 let press = if enabled {
444 self.press_animation.get(ctx)
445 } else {
446 0.0
447 };
448 let mut background = mix_color(palette.button, palette.button_hover, hover);
449 background = mix_color(background, palette.button_pressed, press * 0.55);
450 if !enabled {
451 background = mix_color(background, palette.surface, 0.68).with_alpha(0.82);
452 }
453 let (accent, icon_fill, icon_border, icon_ink) = match (self.accent, self.decorative) {
456 (Some(accent), _) => (
457 accent,
458 mix_color(background, accent, 0.14),
459 accent.with_alpha(0.42),
460 accent,
461 ),
462 (None, hue) => {
463 let roles = hue
464 .map(|hue| theme.decorative.get(hue))
465 .unwrap_or_else(|| theme.tone_roles(self.tone));
466 (roles.solid, roles.soft, roles.border, roles.text)
467 }
468 };
469 let border = if !enabled {
470 palette.border.with_alpha(0.55)
471 } else if ctx.is_focused() {
472 palette.border_focus
473 } else {
474 mix_color(palette.border, palette.border_hover, hover)
475 };
476
477 if enabled {
480 paint_theme_shadow(
481 ctx,
482 ctx.bounds(),
483 [metrics.corner_radius; 4],
484 &theme.shadows.box_shadow.md,
485 );
486 }
487
488 draw_control_frame(
489 ctx,
490 ctx.bounds(),
491 metrics.corner_radius,
492 metrics,
493 background,
494 border,
495 (self.focus_animation.get(ctx) > Progress::EPSILON && enabled).then_some(
496 palette
497 .focus_ring
498 .with_alpha(palette.focus_ring.alpha * self.focus_animation.get(ctx)),
499 ),
500 );
501
502 let bounds = ctx.bounds();
503 let content = self.content_rect(bounds, metrics);
504 let accent_inset = metrics.action_card_accent_inset.min(bounds.height() * 0.5);
505 let accent_height = (bounds.height() - accent_inset * 2.0).max(0.0);
506 let accent_rail = Rect::new(
507 bounds.x(),
508 bounds.y() + accent_inset,
509 metrics.action_card_accent_width,
510 accent_height,
511 );
512 ctx.fill(
513 Path::rounded_rect(accent_rail, metrics.action_card_accent_width * 0.5),
514 accent,
515 );
516
517 if let Some(icon) = self.icon {
518 let icon_box_size = metrics
519 .action_card_icon_box_size
520 .min(content.width())
521 .min(content.height())
522 .max(0.0);
523 let icon_box = Rect::new(
524 content.x(),
525 content.y() + ((content.height() - icon_box_size) * 0.5),
526 icon_box_size,
527 icon_box_size,
528 );
529 draw_control_shape(
530 ctx,
531 icon_box,
532 metrics.corner_radius,
533 physical_pixels(ctx, 1.0),
534 icon_fill,
535 if enabled {
536 icon_border
537 } else {
538 icon_border.with_alpha(0.5)
539 },
540 );
541 let icon_size = metrics
542 .action_card_icon_size
543 .min(icon_box.width())
544 .min(icon_box.height())
545 .max(0.0);
546 let icon_rect = Rect::new(
547 icon_box.x() + ((icon_box.width() - icon_size) * 0.5),
548 icon_box.y() + ((icon_box.height() - icon_size) * 0.5),
549 icon_size,
550 icon_size,
551 );
552 draw_icon_glyph(
553 ctx,
554 icon,
555 icon_rect,
556 if enabled {
557 icon_ink
558 } else {
559 palette.text_disabled
560 },
561 );
562 }
563
564 let text_bounds = self.text_bounds(bounds, metrics);
565 let title_style = self.resolved_title_style();
566 let description_style = self.resolved_description_style();
567 let title_height = title_style.line_height.max(
568 self.title_measurement
569 .map(|measurement| measurement.height)
570 .unwrap_or(title_style.line_height),
571 );
572 let description_min_height = description_style.line_height.max(
573 self.description_measurement
574 .map(|measurement| measurement.height)
575 .unwrap_or(description_style.line_height),
576 );
577 let description_height =
578 (text_bounds.height() - title_height - metrics.action_card_text_gap)
579 .max(description_min_height)
580 .min((description_style.line_height * 2.0).max(description_min_height));
581 let text_block_height = title_height + metrics.action_card_text_gap + description_height;
582 let text_y = text_bounds.y() + ((text_bounds.height() - text_block_height) * 0.5).max(0.0);
583 let title_slot = Rect::new(text_bounds.x(), text_y, text_bounds.width(), title_height);
584 let description_slot = Rect::new(
585 text_bounds.x(),
586 title_slot.max_y() + metrics.action_card_text_gap,
587 text_bounds.width(),
588 description_height,
589 );
590 let title_paint_style = TextStyle {
591 color: if enabled {
592 palette.text
593 } else {
594 palette.text.with_alpha(0.45)
595 },
596 ..title_style
597 };
598 let description_paint_style = TextStyle {
599 color: if enabled {
600 palette.placeholder
601 } else {
602 palette.placeholder.with_alpha(0.45)
603 },
604 ..description_style
605 };
606 let description_layout = {
607 let mut layout_style = description_paint_style.clone();
608 layout_style.color = Color::WHITE;
609 ctx.shape_text(
610 self.description.clone(),
611 Size::new(
612 description_slot.width().max(1.0),
613 description_slot.height().max(1.0),
614 ),
615 layout_style,
616 )
617 .ok()
618 };
619 ctx.push_clip_rect(title_slot);
620 paint_text(
621 ctx,
622 title_slot,
623 &self.title,
624 &title_paint_style,
625 TextAlign::Start,
626 );
627 ctx.pop_clip();
628 ctx.push_clip_rect(description_slot);
629 if let Some(layout) = description_layout.filter(|layout| layout.lines().len() > 1) {
630 let measurement = layout.measurement();
631 let width = measurement.width.min(description_slot.width()).max(0.0);
632 let height = description_paint_style
633 .line_height
634 .max(measurement.height)
635 .min(description_slot.height());
636 let description_rect = Rect::new(
637 description_slot.x(),
638 description_slot.y() + ((description_slot.height() - height).max(0.0) * 0.5),
639 width,
640 height,
641 );
642 ctx.draw_text_layout_with_color(
643 description_rect.origin,
644 &layout,
645 description_paint_style.color,
646 );
647 } else {
648 paint_text(
649 ctx,
650 description_slot,
651 &self.description,
652 &description_paint_style,
653 TextAlign::Start,
654 );
655 }
656 ctx.pop_clip();
657
658 let chevron_size = metrics
659 .action_card_chevron_size
660 .min(content.width())
661 .min(content.height())
662 .max(0.0);
663 let chevron = Rect::new(
664 content.max_x() - chevron_size,
665 content.y() + ((content.height() - chevron_size) * 0.5),
666 chevron_size,
667 chevron_size,
668 );
669 draw_icon_glyph(
670 ctx,
671 IconGlyph::ChevronRight,
672 chevron,
673 if enabled {
674 mix_color(palette.placeholder, accent, hover * 0.45).with_alpha(0.74)
675 } else {
676 palette.placeholder.with_alpha(0.32)
677 },
678 );
679 }
680
681 fn layer_options(&self) -> LayerOptions {
682 LayerOptions {
686 paint_boundary: PaintBoundaryMode::Explicit,
687 ..LayerOptions::default()
688 }
689 }
690
691 fn semantics(&self, ctx: &mut SemanticsCtx) {
692 let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Button, ctx.bounds());
693 node.name = Some(self.title.clone());
694 node.description = Some(self.description.clone());
695 node.value = Some(SemanticsValue::Text(self.description.clone()));
696 node.state.focused = ctx.is_focused();
697 node.state.hovered = self.hovered && self.is_enabled();
698 node.state.disabled = !self.is_enabled();
699 node.actions = if self.is_enabled() {
700 vec![SemanticsAction::Focus, SemanticsAction::Activate]
701 } else {
702 Vec::new()
703 };
704 ctx.push(node);
705 }
706
707 fn accepts_focus(&self) -> bool {
708 self.is_enabled()
709 }
710
711 fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
712 let theme = self.resolved_theme();
713 set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
714 ctx.request_paint();
715 ctx.request_semantics();
716 }
717}
718
719pub(super) fn set_animation_target(
720 animation: &mut impl StateMotion,
721 target: f32,
722 duration: f64,
723 easing: crate::Easing,
724 ctx: &mut EventCtx,
725) -> bool {
726 animation.start(target, AnimationSpec::tween(duration, easing), ctx)
727}
728
729pub(super) fn set_hover_animation_target(
730 animation: &mut impl StateMotion,
731 target: f32,
732 theme: &DefaultTheme,
733 ctx: &mut EventCtx,
734) -> bool {
735 set_animation_target(
736 animation,
737 target,
738 theme.motion.hover_duration(),
739 theme.motion.hover_easing(),
740 ctx,
741 )
742}
743
744pub(super) fn set_item_press<T: Copy>(
747 current: &mut Option<T>,
748 visual: &mut Option<T>,
749 animation: &mut Progress,
750 pressed: Option<T>,
751 theme: &DefaultTheme,
752 ctx: &mut EventCtx,
753) {
754 *current = pressed;
755 if let Some(item) = pressed {
756 *visual = Some(item);
757 *animation = Progress::new(0.0);
758 set_press_animation_target(animation, 1.0, theme, ctx);
759 } else if !set_press_animation_target(animation, 0.0, theme, ctx) {
760 *visual = None;
761 }
762 ctx.request_paint();
763 ctx.request_semantics();
764}
765
766pub(super) fn set_press_animation_target(
767 animation: &mut impl StateMotion,
768 target: f32,
769 theme: &DefaultTheme,
770 ctx: &mut EventCtx,
771) -> bool {
772 set_animation_target(
773 animation,
774 target,
775 theme.motion.press_duration(),
776 theme.motion.press_easing(),
777 ctx,
778 )
779}
780
781pub(super) fn set_focus_animation_target(
782 animation: &mut impl StateMotion,
783 target: f32,
784 theme: &DefaultTheme,
785 ctx: &mut EventCtx,
786) -> bool {
787 set_animation_target(
788 animation,
789 target,
790 theme.motion.focus_duration(),
791 theme.motion.focus_easing(),
792 ctx,
793 )
794}
795
796pub(super) fn set_action_card_animation_target(
797 animation: &mut impl StateMotion,
798 target: f32,
799 duration: f64,
800 easing: crate::Easing,
801 ctx: &mut EventCtx,
802) {
803 set_animation_target(animation, target, duration, easing, ctx);
804}
805
806pub(super) fn set_action_card_hover_animation_target(
807 animation: &mut impl StateMotion,
808 target: f32,
809 theme: &DefaultTheme,
810 ctx: &mut EventCtx,
811) {
812 set_action_card_animation_target(
813 animation,
814 target,
815 theme.motion.hover_duration(),
816 theme.motion.hover_easing(),
817 ctx,
818 );
819}
820
821pub(super) fn set_action_card_press_animation_target(
822 animation: &mut impl StateMotion,
823 target: f32,
824 theme: &DefaultTheme,
825 ctx: &mut EventCtx,
826) {
827 set_action_card_animation_target(
828 animation,
829 target,
830 theme.motion.press_duration(),
831 theme.motion.press_easing(),
832 ctx,
833 );
834}
835
836#[derive(Debug, Clone, Copy, PartialEq, Eq)]
837pub enum PropertyRowLayout {
838 Stacked,
839 Inline,
840}
841
842#[derive(Debug, Clone, Copy, PartialEq, Eq)]
843pub(super) enum PropertyRowDefaults {
844 Property,
845 Form,
846}
847
848pub struct PropertyRow {
849 pub(super) theme: Box<DefaultTheme>,
850 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
851 pub(super) label: String,
852 pub(super) defaults: PropertyRowDefaults,
853 pub(super) layout: PropertyRowLayout,
854 pub(super) label_width: Option<f32>,
855 pub(super) control_width: Option<f32>,
856 pub(super) auto_control_width: bool,
857 pub(super) gap: Option<f32>,
858 pub(super) label_style: Option<TextStyle>,
859 pub(super) child: SingleChild,
860 pub(super) label_measurement: Option<TextMeasurement>,
861}
862
863impl PropertyRow {
864 pub fn new<W>(label: impl Into<String>, control: W) -> Self
865 where
866 W: Widget + 'static,
867 {
868 Self {
869 theme: Box::new(DefaultTheme::default()),
870 theme_reader: None,
871 label: label.into(),
872 defaults: PropertyRowDefaults::Property,
873 layout: PropertyRowLayout::Stacked,
874 label_width: None,
875 control_width: None,
876 auto_control_width: true,
877 gap: None,
878 label_style: None,
879 child: SingleChild::new(control),
880 label_measurement: None,
881 }
882 }
883
884 pub fn theme(mut self, theme: DefaultTheme) -> Self {
885 self.theme = Box::new(theme);
886 self.theme_reader = None;
887 self
888 }
889
890 pub fn theme_when<F>(mut self, theme: F) -> Self
891 where
892 F: Fn() -> DefaultTheme + 'static,
893 {
894 self.theme_reader = Some(Box::new(theme));
895 self
896 }
897
898 pub fn layout(mut self, layout: PropertyRowLayout) -> Self {
899 self.layout = layout;
900 self
901 }
902
903 pub fn stacked(self) -> Self {
904 self.layout(PropertyRowLayout::Stacked)
905 }
906
907 pub fn inline(self) -> Self {
908 self.layout(PropertyRowLayout::Inline)
909 }
910
911 pub fn label_width(mut self, width: f32) -> Self {
912 self.label_width = Some(width.max(0.0));
913 self
914 }
915
916 pub fn control_width(mut self, width: f32) -> Self {
917 self.control_width = Some(width.max(0.0));
918 self.auto_control_width = false;
919 self
920 }
921
922 pub fn auto_control_width(mut self) -> Self {
923 self.control_width = None;
924 self.auto_control_width = true;
925 self
926 }
927
928 pub fn gap(mut self, gap: f32) -> Self {
929 self.gap = Some(gap.max(0.0));
930 self
931 }
932
933 pub fn label_style(mut self, style: TextStyle) -> Self {
934 self.label_style = Some(style);
935 self
936 }
937
938 pub fn child_pod(&self) -> &sui_runtime::WidgetPod {
939 self.child.child()
940 }
941
942 pub fn child_mut(&mut self) -> &mut sui_runtime::WidgetPod {
943 self.child.child_mut()
944 }
945
946 pub(super) fn resolved_label_style(&self) -> TextStyle {
947 let theme = self.resolved_theme();
948 self.label_style
949 .clone()
950 .unwrap_or_else(|| text_token_style(&theme, theme.text.sm, theme.palette.text_muted))
951 }
952
953 pub(super) fn resolved_theme(&self) -> DefaultTheme {
954 self.theme_reader
955 .as_ref()
956 .map(|theme| theme())
957 .unwrap_or_else(|| *self.theme)
958 }
959
960 pub(super) fn with_form_defaults(mut self) -> Self {
961 self.defaults = PropertyRowDefaults::Form;
962 self.auto_control_width = true;
963 self
964 }
965
966 pub(super) fn resolved_label_width(&self, metrics: ControlMetrics) -> f32 {
967 self.label_width.unwrap_or(match self.defaults {
968 PropertyRowDefaults::Property => metrics.property_row_label_width,
969 PropertyRowDefaults::Form => metrics.form_row_label_width,
970 })
971 }
972
973 pub(super) fn resolved_gap(&self, metrics: ControlMetrics) -> f32 {
974 self.gap.unwrap_or(match self.defaults {
975 PropertyRowDefaults::Form => metrics.form_row_gap,
976 PropertyRowDefaults::Property => match self.layout {
977 PropertyRowLayout::Stacked => metrics.property_row_stacked_gap,
978 PropertyRowLayout::Inline => metrics.property_row_inline_gap,
979 },
980 })
981 }
982
983 pub(super) fn resolved_control_width(&self, metrics: ControlMetrics) -> Option<f32> {
984 if self.auto_control_width {
985 None
986 } else {
987 self.control_width.or_else(|| {
988 matches!(self.defaults, PropertyRowDefaults::Form)
989 .then_some(metrics.form_row_control_width)
990 })
991 }
992 }
993
994 pub(super) fn label_height(&self, style: &TextStyle) -> f32 {
995 self.label_measurement
996 .map(|measurement| measurement.height)
997 .unwrap_or(style.line_height)
998 .max(style.line_height)
999 }
1000
1001 pub(super) fn child_constraints(
1002 &self,
1003 constraints: Constraints,
1004 label_extent: f32,
1005 metrics: ControlMetrics,
1006 ) -> Constraints {
1007 let max_width = constraints.max.width;
1008 let gap = self.resolved_gap(metrics);
1009 let available = match self.layout {
1010 PropertyRowLayout::Stacked => max_width,
1011 PropertyRowLayout::Inline => {
1012 if max_width.is_finite() {
1013 (max_width - label_extent - gap).max(0.0)
1014 } else {
1015 f32::INFINITY
1016 }
1017 }
1018 };
1019 let width = self
1020 .resolved_control_width(metrics)
1021 .map(|width| width.min(available).max(0.0));
1022
1023 match width {
1024 Some(width) => Constraints::new(
1025 Size::new(width, 0.0),
1026 Size::new(width, constraints.max.height),
1027 ),
1028 None => Constraints::new(Size::ZERO, Size::new(available, constraints.max.height)),
1029 }
1030 }
1031
1032 pub(super) fn child_width_for_bounds(
1033 &self,
1034 bounds: Rect,
1035 label_extent: f32,
1036 metrics: ControlMetrics,
1037 ) -> f32 {
1038 let gap = self.resolved_gap(metrics);
1039 let available = match self.layout {
1040 PropertyRowLayout::Stacked => bounds.width(),
1041 PropertyRowLayout::Inline => (bounds.width() - label_extent - gap).max(0.0),
1042 };
1043 self.resolved_control_width(metrics)
1044 .unwrap_or(available)
1045 .min(available)
1046 .max(0.0)
1047 }
1048}
1049
1050impl Widget for PropertyRow {
1051 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
1052 let theme = self.resolved_theme();
1053 let metrics = theme.metrics;
1054 let gap = self.resolved_gap(metrics);
1055 let label_style = self.resolved_label_style();
1056 let label_measurement = measure_text(ctx, &self.label, &label_style);
1057 self.label_measurement = Some(label_measurement);
1058 let label_height = self.label_height(&label_style);
1059 let label_extent = match self.layout {
1060 PropertyRowLayout::Stacked => label_measurement.width,
1061 PropertyRowLayout::Inline => self
1062 .resolved_label_width(metrics)
1063 .max(label_measurement.width),
1064 };
1065 let child_size = self.child.measure(
1066 ctx,
1067 self.child_constraints(constraints, label_extent, metrics),
1068 );
1069 let natural = match self.layout {
1070 PropertyRowLayout::Stacked => Size::new(
1071 label_measurement.width.max(child_size.width),
1072 label_height + gap + child_size.height,
1073 ),
1074 PropertyRowLayout::Inline => Size::new(
1075 label_extent + gap + child_size.width,
1076 label_height.max(child_size.height),
1077 ),
1078 };
1079
1080 constraints.clamp(natural)
1081 }
1082
1083 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
1084 let theme = self.resolved_theme();
1085 let metrics = theme.metrics;
1086 let gap = self.resolved_gap(metrics);
1087 let label_style = self.resolved_label_style();
1088 let label_height = self.label_height(&label_style);
1089 let label_width = match self.layout {
1090 PropertyRowLayout::Stacked => bounds.width(),
1091 PropertyRowLayout::Inline => self
1092 .resolved_label_width(metrics)
1093 .min(bounds.width())
1094 .max(0.0),
1095 };
1096 let child_measured = self.child.child().measured_size();
1097 let child_width = self.child_width_for_bounds(bounds, label_width, metrics);
1098 let child_height = child_measured.height.min(bounds.height()).max(0.0);
1099
1100 let child_bounds = match self.layout {
1101 PropertyRowLayout::Stacked => Rect::new(
1102 bounds.x(),
1103 bounds.y() + label_height + gap,
1104 child_width,
1105 child_height.min((bounds.height() - label_height - gap).max(0.0)),
1106 ),
1107 PropertyRowLayout::Inline => Rect::new(
1108 bounds.x() + label_width + gap,
1109 bounds.y() + ((bounds.height() - child_height) * 0.5).max(0.0),
1110 child_width,
1111 child_height,
1112 ),
1113 };
1114 self.child.arrange(ctx, child_bounds);
1115 }
1116
1117 fn paint(&self, ctx: &mut PaintCtx) {
1118 let theme = self.resolved_theme();
1119 let metrics = theme.metrics;
1120 let label_style = self.resolved_label_style();
1121 let label_height = self.label_height(&label_style);
1122 let bounds = ctx.bounds();
1123 let label_rect = match self.layout {
1124 PropertyRowLayout::Stacked => {
1125 Rect::new(bounds.x(), bounds.y(), bounds.width(), label_height)
1126 }
1127 PropertyRowLayout::Inline => Rect::new(
1128 bounds.x(),
1129 bounds.y() + ((bounds.height() - label_height) * 0.5).max(0.0),
1130 self.resolved_label_width(metrics)
1131 .min(bounds.width())
1132 .max(0.0),
1133 label_height,
1134 ),
1135 };
1136 ctx.push_clip_rect(label_rect);
1137 paint_text_line(ctx, label_rect, &self.label, &label_style, TextAlign::Start);
1138 ctx.pop_clip();
1139 self.child.paint(ctx);
1140 }
1141
1142 fn semantics(&self, ctx: &mut SemanticsCtx) {
1143 let theme = self.resolved_theme();
1144 let metrics = theme.metrics;
1145 let mut row = SemanticsNode::new(
1146 ctx.widget_id(),
1147 SemanticsRole::GenericContainer,
1148 ctx.bounds(),
1149 );
1150 row.name = Some(self.label.clone());
1151 ctx.push(row);
1152
1153 let label_style = self.resolved_label_style();
1154 let label_height = self.label_height(&label_style);
1155 let label_bounds = match self.layout {
1156 PropertyRowLayout::Stacked => Rect::new(
1157 ctx.bounds().x(),
1158 ctx.bounds().y(),
1159 ctx.bounds().width(),
1160 label_height,
1161 ),
1162 PropertyRowLayout::Inline => Rect::new(
1163 ctx.bounds().x(),
1164 ctx.bounds().y() + ((ctx.bounds().height() - label_height) * 0.5).max(0.0),
1165 self.resolved_label_width(metrics)
1166 .min(ctx.bounds().width())
1167 .max(0.0),
1168 label_height,
1169 ),
1170 };
1171 let mut label = SemanticsNode::new(
1172 property_row_label_id(ctx.widget_id()),
1173 SemanticsRole::Text,
1174 label_bounds,
1175 );
1176 label.parent = Some(ctx.widget_id());
1177 label.name = Some(self.label.clone());
1178 label.value = Some(SemanticsValue::Text(self.label.clone()));
1179 ctx.push(label);
1180
1181 self.child.semantics(ctx);
1182 }
1183
1184 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
1185 self.child.visit_children(visitor);
1186 }
1187
1188 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
1189 self.child.visit_children_mut(visitor);
1190 }
1191}
1192
1193pub(super) fn property_row_label_id(parent: WidgetId) -> WidgetId {
1194 const TAG: u64 = 1_u64 << 51;
1195 const LOW_MASK: u64 = (1_u64 << 51) - 1;
1196
1197 WidgetId::new(TAG | (parent.get().wrapping_mul(271).wrapping_add(1) & LOW_MASK))
1198}
1199
1200pub struct SectionLabel {
1201 pub(super) theme: Box<DefaultTheme>,
1202 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
1203 pub(super) label: String,
1204 pub(super) semantic_name: Option<String>,
1205 pub(super) color: Option<Color>,
1206}
1207
1208impl SectionLabel {
1209 pub fn new(label: impl Into<String>) -> Self {
1210 Self {
1211 theme: Box::new(DefaultTheme::default()),
1212 theme_reader: None,
1213 label: label.into(),
1214 semantic_name: None,
1215 color: None,
1216 }
1217 }
1218
1219 pub fn theme(mut self, theme: DefaultTheme) -> Self {
1220 self.theme = Box::new(theme);
1221 self.theme_reader = None;
1222 self
1223 }
1224
1225 pub fn theme_when<F>(mut self, theme: F) -> Self
1226 where
1227 F: Fn() -> DefaultTheme + 'static,
1228 {
1229 self.theme_reader = Some(Box::new(theme));
1230 self
1231 }
1232
1233 pub fn semantic_name(mut self, name: impl Into<String>) -> Self {
1234 self.semantic_name = Some(name.into());
1235 self
1236 }
1237
1238 pub fn color(mut self, color: Color) -> Self {
1239 self.color = Some(color);
1240 self
1241 }
1242
1243 pub(super) fn resolved_theme(&self) -> DefaultTheme {
1244 self.theme_reader
1245 .as_ref()
1246 .map(|theme| theme())
1247 .unwrap_or_else(|| *self.theme)
1248 }
1249
1250 pub(super) fn display_text(&self) -> String {
1251 self.label.to_uppercase()
1252 }
1253
1254 pub(super) fn text_style(&self, theme: &DefaultTheme) -> TextStyle {
1255 section_label_text_style(theme, self.color)
1256 }
1257}
1258
1259impl Widget for SectionLabel {
1260 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
1261 let theme = self.resolved_theme();
1262 let style = self.text_style(&theme);
1263 let text = self.display_text();
1264 let measured = measure_text(ctx, &text, &style);
1265 constraints.clamp(Size::new(measured.width, style.line_height))
1266 }
1267
1268 fn paint(&self, ctx: &mut PaintCtx) {
1269 let theme = self.resolved_theme();
1270 let style = self.text_style(&theme);
1271 let text = self.display_text();
1272 ctx.push_clip_rect(ctx.bounds());
1273 paint_text_line(ctx, ctx.bounds(), &text, &style, TextAlign::Start);
1274 ctx.pop_clip();
1275 }
1276
1277 fn semantics(&self, ctx: &mut SemanticsCtx) {
1278 let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Text, ctx.bounds());
1279 let name = self
1280 .semantic_name
1281 .clone()
1282 .unwrap_or_else(|| self.label.clone());
1283 node.name = Some(name.clone());
1284 node.value = Some(SemanticsValue::Text(name));
1285 ctx.push(node);
1286 }
1287}
1288
1289#[derive(Debug, Clone, Copy, Default)]
1290pub struct SectionLabelPaint {
1291 pub(super) color: Option<Color>,
1292}
1293
1294impl SectionLabelPaint {
1295 pub const fn new() -> Self {
1296 Self { color: None }
1297 }
1298
1299 pub const fn color(mut self, color: Color) -> Self {
1300 self.color = Some(color);
1301 self
1302 }
1303}
1304
1305pub fn paint_section_label(
1306 ctx: &mut PaintCtx,
1307 theme: &DefaultTheme,
1308 rect: Rect,
1309 label: &str,
1310 paint: SectionLabelPaint,
1311) {
1312 let style = section_label_text_style(theme, paint.color);
1313 let text = label.to_uppercase();
1314 ctx.push_clip_rect(rect);
1315 paint_text_line(ctx, rect, &text, &style, TextAlign::Start);
1316 ctx.pop_clip();
1317}
1318
1319pub fn paint_section_label_detail(
1320 ctx: &mut PaintCtx,
1321 theme: &DefaultTheme,
1322 rect: Rect,
1323 label: &str,
1324 detail: &str,
1325 paint: SectionLabelPaint,
1326) {
1327 let style = section_label_text_style(theme, paint.color);
1328 let text = if detail.trim().is_empty() {
1329 label.to_uppercase()
1330 } else {
1331 format!("{} · {detail}", label.to_uppercase())
1332 };
1333 ctx.push_clip_rect(rect);
1334 paint_text_line(ctx, rect, &text, &style, TextAlign::Start);
1335 ctx.pop_clip();
1336}
1337
1338pub(super) fn section_label_text_style(theme: &DefaultTheme, color: Option<Color>) -> TextStyle {
1339 let mut style = text_token_style(
1340 theme,
1341 theme.text.xs,
1342 color.unwrap_or(theme.surfaces.text_faint),
1343 );
1344 style.weight = FontWeight::SEMIBOLD;
1345 style
1346}
1347
1348pub struct DetailRow {
1349 pub(super) theme: Box<DefaultTheme>,
1350 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
1351 pub(super) label: String,
1352 pub(super) label_reader: Option<Box<dyn Fn() -> String>>,
1353 pub(super) value: String,
1354 pub(super) value_reader: Option<Box<dyn Fn() -> String>>,
1355 pub(super) max_value_lines: Option<usize>,
1356}
1357
1358impl DetailRow {
1359 pub fn new(label: impl Into<String>, value: impl Into<String>) -> Self {
1360 Self {
1361 theme: Box::new(DefaultTheme::default()),
1362 theme_reader: None,
1363 label: label.into(),
1364 label_reader: None,
1365 value: value.into(),
1366 value_reader: None,
1367 max_value_lines: None,
1368 }
1369 }
1370
1371 pub fn theme(mut self, theme: DefaultTheme) -> Self {
1372 self.theme = Box::new(theme);
1373 self.theme_reader = None;
1374 self
1375 }
1376
1377 pub fn theme_when<F>(mut self, theme: F) -> Self
1378 where
1379 F: Fn() -> DefaultTheme + 'static,
1380 {
1381 self.theme_reader = Some(Box::new(theme));
1382 self
1383 }
1384
1385 pub fn label_when<F>(mut self, label: F) -> Self
1386 where
1387 F: Fn() -> String + 'static,
1388 {
1389 self.label_reader = Some(Box::new(label));
1390 self
1391 }
1392
1393 pub fn value_when<F>(mut self, value: F) -> Self
1394 where
1395 F: Fn() -> String + 'static,
1396 {
1397 self.value_reader = Some(Box::new(value));
1398 self
1399 }
1400
1401 pub fn max_value_lines(mut self, max_lines: usize) -> Self {
1402 self.max_value_lines = Some(max_lines.max(1));
1403 self
1404 }
1405
1406 pub(super) fn label(&self) -> String {
1407 self.label_reader
1408 .as_ref()
1409 .map(|reader| reader())
1410 .unwrap_or_else(|| self.label.clone())
1411 }
1412
1413 pub(super) fn value(&self) -> String {
1414 self.value_reader
1415 .as_ref()
1416 .map(|reader| reader())
1417 .unwrap_or_else(|| self.value.clone())
1418 }
1419
1420 pub(super) fn resolved_theme(&self) -> DefaultTheme {
1421 self.theme_reader
1422 .as_ref()
1423 .map(|theme| theme())
1424 .unwrap_or_else(|| *self.theme)
1425 }
1426}
1427
1428impl Widget for DetailRow {
1429 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
1430 let theme = self.resolved_theme();
1431 let label_style = detail_row_label_style(&theme);
1432 let value_style = detail_row_value_style(&theme);
1433 let width = if constraints.max.width.is_finite() {
1434 constraints.max.width.max(0.0)
1435 } else {
1436 let label = measure_text(ctx, &self.label().to_uppercase(), &label_style);
1437 let value = measure_text(ctx, &self.value(), &value_style);
1438 label.width.max(value.width)
1439 };
1440 let value = self.value();
1441 let lines = wrap_detail_row_value(&value, width, self.max_value_lines, |text| {
1442 measure_text(ctx, text, &value_style).width
1443 });
1444 constraints.clamp(Size::new(width, detail_row_height(&theme, lines.len())))
1445 }
1446
1447 fn paint(&self, ctx: &mut PaintCtx) {
1448 let theme = self.resolved_theme();
1449 paint_detail_row_at(
1450 ctx,
1451 &theme,
1452 Point::new(ctx.bounds().x(), ctx.bounds().y()),
1453 ctx.bounds().width(),
1454 &self.label(),
1455 &self.value(),
1456 self.max_value_lines,
1457 );
1458 }
1459
1460 fn semantics(&self, ctx: &mut SemanticsCtx) {
1461 let mut node = SemanticsNode::new(
1462 ctx.widget_id(),
1463 SemanticsRole::GenericContainer,
1464 ctx.bounds(),
1465 );
1466 node.name = Some(self.label());
1467 node.value = Some(SemanticsValue::Text(self.value()));
1468 ctx.push(node);
1469 }
1470}
1471
1472pub fn paint_detail_row_at(
1473 ctx: &mut PaintCtx,
1474 theme: &DefaultTheme,
1475 origin: Point,
1476 width: f32,
1477 label: &str,
1478 value: &str,
1479 max_value_lines: Option<usize>,
1480) -> f32 {
1481 let width = width.max(0.0);
1482 let label_style = detail_row_label_style(theme);
1483 let value_style = detail_row_value_style(theme);
1484 let lines = wrap_detail_row_value(value, width, max_value_lines, |text| {
1485 ctx.measure_text(text.to_string(), value_style.clone())
1486 .map(|measurement| measurement.width)
1487 .unwrap_or(0.0)
1488 });
1489 let height = detail_row_height(theme, lines.len());
1490 let clip = Rect::new(origin.x, origin.y, width, height);
1491
1492 ctx.push_clip_rect(clip);
1493 paint_text(
1494 ctx,
1495 Rect::new(origin.x, origin.y, width, label_style.line_height),
1496 &label.to_uppercase(),
1497 &label_style,
1498 TextAlign::Start,
1499 );
1500
1501 let mut y = origin.y + label_style.line_height + detail_row_label_value_gap(theme);
1502 for line in lines {
1503 paint_text(
1504 ctx,
1505 Rect::new(origin.x, y, width, value_style.line_height),
1506 &line,
1507 &value_style,
1508 TextAlign::Start,
1509 );
1510 y += value_style.line_height;
1511 }
1512 ctx.pop_clip();
1513 height
1514}
1515
1516pub fn detail_row_height_for_value(
1517 ctx: &PaintCtx,
1518 theme: &DefaultTheme,
1519 width: f32,
1520 value: &str,
1521 max_value_lines: Option<usize>,
1522) -> f32 {
1523 let width = width.max(0.0);
1524 let value_style = detail_row_value_style(theme);
1525 let lines = wrap_detail_row_value(value, width, max_value_lines, |text| {
1526 ctx.measure_text(text.to_string(), value_style.clone())
1527 .map(|measurement| measurement.width)
1528 .unwrap_or(0.0)
1529 });
1530 detail_row_height(theme, lines.len())
1531}
1532
1533pub(super) fn detail_row_label_style(theme: &DefaultTheme) -> TextStyle {
1534 let mut style = text_token_style(theme, theme.text.xs, theme.palette.text_muted);
1535 style.weight = FontWeight::SEMIBOLD;
1536 style
1537}
1538
1539pub(super) fn detail_row_value_style(theme: &DefaultTheme) -> TextStyle {
1540 text_token_style(theme, theme.text.sm, theme.palette.text)
1541}
1542
1543pub(super) fn detail_row_label_value_gap(theme: &DefaultTheme) -> f32 {
1544 (theme.metrics.icon_label_gap * 0.35).max(2.0)
1545}
1546
1547pub(super) fn detail_row_bottom_gap(theme: &DefaultTheme) -> f32 {
1548 theme.metrics.property_row_stacked_gap.max(6.0)
1549}
1550
1551pub(super) fn detail_row_height(theme: &DefaultTheme, value_lines: usize) -> f32 {
1552 let label_style = detail_row_label_style(theme);
1553 let value_style = detail_row_value_style(theme);
1554 label_style.line_height
1555 + detail_row_label_value_gap(theme)
1556 + value_style.line_height * value_lines.max(1) as f32
1557 + detail_row_bottom_gap(theme)
1558}
1559
1560pub(super) fn wrap_detail_row_value<F>(
1561 value: &str,
1562 width: f32,
1563 max_lines: Option<usize>,
1564 mut measure: F,
1565) -> Vec<String>
1566where
1567 F: FnMut(&str) -> f32,
1568{
1569 let max_lines = max_lines.unwrap_or(usize::MAX).max(1);
1570 let width = width.max(1.0);
1571 let mut lines = Vec::new();
1572
1573 for paragraph in value.split('\n') {
1574 if lines.len() >= max_lines {
1575 break;
1576 }
1577 let mut current = String::new();
1578 for word in paragraph.split_whitespace() {
1579 let candidate = if current.is_empty() {
1580 word.to_string()
1581 } else {
1582 format!("{current} {word}")
1583 };
1584 if current.is_empty() || measure(&candidate) <= width {
1585 current = candidate;
1586 } else {
1587 lines.push(std::mem::take(&mut current));
1588 if lines.len() >= max_lines {
1589 break;
1590 }
1591 current = word.to_string();
1592 }
1593 }
1594 if lines.len() < max_lines {
1595 lines.push(current);
1596 }
1597 }
1598
1599 if lines.is_empty() {
1600 lines.push(String::new());
1601 }
1602 lines
1603}
1604
1605pub struct FormRow {
1606 pub(super) row: PropertyRow,
1607}
1608
1609impl FormRow {
1610 pub fn new<W>(label: impl Into<String>, control: W) -> Self
1611 where
1612 W: Widget + 'static,
1613 {
1614 Self {
1615 row: PropertyRow::new(label, control)
1616 .inline()
1617 .with_form_defaults(),
1618 }
1619 }
1620
1621 pub fn theme(mut self, theme: DefaultTheme) -> Self {
1622 self.row = self.row.theme(theme);
1623 self
1624 }
1625
1626 pub fn theme_when<F>(mut self, theme: F) -> Self
1627 where
1628 F: Fn() -> DefaultTheme + 'static,
1629 {
1630 self.row = self.row.theme_when(theme);
1631 self
1632 }
1633
1634 pub fn stacked(mut self) -> Self {
1635 self.row = self.row.stacked();
1636 self
1637 }
1638
1639 pub fn inline(mut self) -> Self {
1640 self.row = self.row.inline();
1641 self
1642 }
1643
1644 pub fn label_width(mut self, width: f32) -> Self {
1645 self.row = self.row.label_width(width);
1646 self
1647 }
1648
1649 pub fn control_width(mut self, width: f32) -> Self {
1650 self.row = self.row.control_width(width);
1651 self
1652 }
1653
1654 pub fn auto_control_width(mut self) -> Self {
1655 self.row = self.row.auto_control_width();
1656 self
1657 }
1658
1659 pub fn gap(mut self, gap: f32) -> Self {
1660 self.row = self.row.gap(gap);
1661 self
1662 }
1663
1664 pub fn label_style(mut self, style: TextStyle) -> Self {
1665 self.row = self.row.label_style(style);
1666 self
1667 }
1668
1669 pub fn child_pod(&self) -> &sui_runtime::WidgetPod {
1670 self.row.child_pod()
1671 }
1672
1673 pub fn child_mut(&mut self) -> &mut sui_runtime::WidgetPod {
1674 self.row.child_mut()
1675 }
1676}
1677
1678impl Widget for FormRow {
1679 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
1680 self.row.measure(ctx, constraints)
1681 }
1682
1683 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
1684 self.row.arrange(ctx, bounds);
1685 }
1686
1687 fn paint(&self, ctx: &mut PaintCtx) {
1688 self.row.paint(ctx);
1689 }
1690
1691 fn semantics(&self, ctx: &mut SemanticsCtx) {
1692 self.row.semantics(ctx);
1693 }
1694
1695 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
1696 self.row.visit_children(visitor);
1697 }
1698
1699 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
1700 self.row.visit_children_mut(visitor);
1701 }
1702}
1703
1704pub struct FieldGroup {
1705 pub(super) theme: Box<DefaultTheme>,
1706 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
1707 pub(super) children: WidgetChildren,
1708 pub(super) spacing: Option<f32>,
1709 pub(super) padding: Insets,
1710 pub(super) max_width: Option<f32>,
1711 pub(super) fill_width: bool,
1712}
1713
1714impl FieldGroup {
1715 pub fn new() -> Self {
1716 Self {
1717 theme: Box::new(DefaultTheme::default()),
1718 theme_reader: None,
1719 children: WidgetChildren::new(),
1720 spacing: None,
1721 padding: Insets::ZERO,
1722 max_width: None,
1723 fill_width: false,
1724 }
1725 }
1726
1727 pub fn theme(mut self, theme: DefaultTheme) -> Self {
1728 self.theme = Box::new(theme);
1729 self.theme_reader = None;
1730 self
1731 }
1732
1733 pub fn theme_when<F>(mut self, theme: F) -> Self
1734 where
1735 F: Fn() -> DefaultTheme + 'static,
1736 {
1737 self.theme_reader = Some(Box::new(theme));
1738 self
1739 }
1740
1741 pub fn with_child<W>(mut self, child: W) -> Self
1742 where
1743 W: Widget + 'static,
1744 {
1745 self.children.push(child);
1746 self
1747 }
1748
1749 pub fn push<W>(&mut self, child: W)
1750 where
1751 W: Widget + 'static,
1752 {
1753 self.children.push(child);
1754 }
1755
1756 pub fn gap(mut self, gap: f32) -> Self {
1757 self.spacing = Some(gap.max(0.0));
1758 self
1759 }
1760
1761 pub fn padding(mut self, padding: Insets) -> Self {
1762 self.padding = padding;
1763 self
1764 }
1765
1766 pub fn max_width(mut self, width: f32) -> Self {
1767 self.max_width = Some(width.max(0.0));
1768 self
1769 }
1770
1771 pub fn auto_width(mut self) -> Self {
1772 self.max_width = None;
1773 self
1774 }
1775
1776 pub fn fill_width(mut self, fill_width: bool) -> Self {
1777 self.fill_width = fill_width;
1778 self
1779 }
1780
1781 pub fn children(&self) -> &[sui_runtime::WidgetPod] {
1782 self.children.as_slice()
1783 }
1784
1785 pub fn children_mut(&mut self) -> &mut [sui_runtime::WidgetPod] {
1786 self.children.as_mut_slice()
1787 }
1788
1789 pub(super) fn content_max_width(&self, constraints: Constraints) -> f32 {
1790 let available = if constraints.max.width.is_finite() {
1791 (constraints.max.width - self.padding.left - self.padding.right).max(0.0)
1792 } else {
1793 f32::INFINITY
1794 };
1795 self.max_width
1796 .map(|width| width.min(available))
1797 .unwrap_or(available)
1798 }
1799
1800 pub(super) fn content_rect(&self, bounds: Rect) -> Rect {
1801 let inset = inset_rect(bounds, self.padding);
1802 let width = self
1803 .max_width
1804 .map(|max_width| max_width.min(inset.width()))
1805 .unwrap_or(inset.width())
1806 .max(0.0);
1807 Rect::new(inset.x(), inset.y(), width, inset.height())
1808 }
1809
1810 pub(super) fn resolved_theme(&self) -> DefaultTheme {
1811 self.theme_reader
1812 .as_ref()
1813 .map(|theme| theme())
1814 .unwrap_or_else(|| *self.theme)
1815 }
1816
1817 pub(super) fn resolved_spacing(&self) -> f32 {
1818 self.spacing
1819 .unwrap_or_else(|| self.resolved_theme().metrics.field_group_spacing)
1820 }
1821}
1822
1823impl Default for FieldGroup {
1824 fn default() -> Self {
1825 Self::new()
1826 }
1827}
1828
1829impl Widget for FieldGroup {
1830 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
1831 let spacing = self.resolved_spacing();
1832 let content_max_width = self.content_max_width(constraints);
1833 let mut y: f32 = 0.0;
1834 let mut width: f32 = 0.0;
1835 for (index, child) in self.children.as_mut_slice().iter_mut().enumerate() {
1836 if index > 0 {
1837 y += spacing;
1838 }
1839 let child_size = child.measure(
1840 ctx,
1841 Constraints::new(
1842 Size::ZERO,
1843 Size::new(content_max_width, constraints.max.height),
1844 ),
1845 );
1846 y += child_size.height;
1847 width = width.max(child_size.width);
1848 }
1849
1850 if self.fill_width && content_max_width.is_finite() {
1851 width = content_max_width;
1852 }
1853
1854 constraints.clamp(Size::new(
1855 width + self.padding.left + self.padding.right,
1856 y + self.padding.top + self.padding.bottom,
1857 ))
1858 }
1859
1860 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
1861 let spacing = self.resolved_spacing();
1862 let content = self.content_rect(bounds);
1863 let mut y = content.y();
1864 for (index, child) in self.children.as_mut_slice().iter_mut().enumerate() {
1865 if index > 0 {
1866 y += spacing;
1867 }
1868 let measured = child.measured_size();
1869 let width = if self.fill_width {
1870 content.width()
1871 } else {
1872 measured.width.min(content.width())
1873 };
1874 child.arrange(ctx, Rect::new(content.x(), y, width, measured.height));
1875 y += measured.height;
1876 }
1877 }
1878
1879 fn paint(&self, ctx: &mut PaintCtx) {
1880 self.children.paint(ctx);
1881 }
1882
1883 fn semantics(&self, ctx: &mut SemanticsCtx) {
1884 self.children.semantics(ctx);
1885 }
1886
1887 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
1888 self.children.visit_children(visitor);
1889 }
1890
1891 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
1892 self.children.visit_children_mut(visitor);
1893 }
1894}
1895
1896pub struct FormSection {
1897 pub(super) theme: Box<DefaultTheme>,
1898 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
1899 pub(super) title: String,
1900 pub(super) description: Option<String>,
1901 pub(super) title_style: Option<TextStyle>,
1902 pub(super) description_style: Option<TextStyle>,
1903 pub(super) header_action: Option<SingleChild>,
1904 pub(super) child: SingleChild,
1905 pub(super) padding: Option<Insets>,
1906 pub(super) body_gap: Option<f32>,
1907 pub(super) header_gap: Option<f32>,
1908 pub(super) description_gap: Option<f32>,
1909 pub(super) max_width: Option<f32>,
1910 pub(super) auto_width: bool,
1911 pub(super) radius: Option<f32>,
1912 pub(super) elevation: SurfaceElevation,
1913 pub(super) fill_width: bool,
1914 pub(super) title_measurement: Option<TextMeasurement>,
1915 pub(super) description_measurement: Option<TextMeasurement>,
1916}
1917
1918impl FormSection {
1919 pub fn new<W>(title: impl Into<String>, child: W) -> Self
1920 where
1921 W: Widget + 'static,
1922 {
1923 Self {
1924 theme: Box::new(DefaultTheme::default()),
1925 theme_reader: None,
1926 title: title.into(),
1927 description: None,
1928 title_style: None,
1929 description_style: None,
1930 header_action: None,
1931 child: SingleChild::new(child),
1932 padding: None,
1933 body_gap: None,
1934 header_gap: None,
1935 description_gap: None,
1936 max_width: None,
1937 auto_width: false,
1938 radius: None,
1939 elevation: SurfaceElevation::Small,
1940 fill_width: false,
1941 title_measurement: None,
1942 description_measurement: None,
1943 }
1944 }
1945
1946 pub fn theme(mut self, theme: DefaultTheme) -> Self {
1947 self.theme = Box::new(theme);
1948 self.theme_reader = None;
1949 self
1950 }
1951
1952 pub fn theme_when<F>(mut self, theme: F) -> Self
1953 where
1954 F: Fn() -> DefaultTheme + 'static,
1955 {
1956 self.theme_reader = Some(Box::new(theme));
1957 self
1958 }
1959
1960 pub fn description(mut self, description: impl Into<String>) -> Self {
1961 self.description = Some(description.into());
1962 self
1963 }
1964
1965 pub fn title_style(mut self, style: TextStyle) -> Self {
1966 self.title_style = Some(style);
1967 self
1968 }
1969
1970 pub fn description_style(mut self, style: TextStyle) -> Self {
1971 self.description_style = Some(style);
1972 self
1973 }
1974
1975 pub fn header_action<W>(mut self, action: W) -> Self
1976 where
1977 W: Widget + 'static,
1978 {
1979 self.header_action = Some(SingleChild::new(action));
1980 self
1981 }
1982
1983 pub fn padding(mut self, padding: Insets) -> Self {
1984 self.padding = Some(padding);
1985 self
1986 }
1987
1988 pub fn body_gap(mut self, gap: f32) -> Self {
1989 self.body_gap = Some(gap.max(0.0));
1990 self
1991 }
1992
1993 pub fn header_gap(mut self, gap: f32) -> Self {
1994 self.header_gap = Some(gap.max(0.0));
1995 self
1996 }
1997
1998 pub fn max_width(mut self, width: f32) -> Self {
1999 self.max_width = Some(width.max(0.0));
2000 self.auto_width = false;
2001 self
2002 }
2003
2004 pub fn auto_width(mut self) -> Self {
2005 self.max_width = None;
2006 self.auto_width = true;
2007 self
2008 }
2009
2010 pub fn fill_width(mut self, fill_width: bool) -> Self {
2011 self.fill_width = fill_width;
2012 self
2013 }
2014
2015 pub fn corner_radius(mut self, corner_radius: f32) -> Self {
2016 self.radius = Some(corner_radius.max(0.0));
2017 self
2018 }
2019
2020 pub fn elevation(mut self, elevation: SurfaceElevation) -> Self {
2021 self.elevation = elevation;
2022 self
2023 }
2024
2025 pub fn child_pod(&self) -> &sui_runtime::WidgetPod {
2026 self.child.child()
2027 }
2028
2029 pub fn child_mut(&mut self) -> &mut sui_runtime::WidgetPod {
2030 self.child.child_mut()
2031 }
2032
2033 pub(super) fn resolved_theme(&self) -> DefaultTheme {
2034 self.theme_reader
2035 .as_ref()
2036 .map(|theme| theme())
2037 .unwrap_or_else(|| *self.theme)
2038 }
2039
2040 pub(super) fn resolved_padding(&self, metrics: ControlMetrics) -> Insets {
2041 self.padding.unwrap_or(metrics.form_section_padding)
2042 }
2043
2044 pub(super) fn resolved_body_gap(&self, metrics: ControlMetrics) -> f32 {
2045 self.body_gap.unwrap_or(metrics.form_section_body_gap)
2046 }
2047
2048 pub(super) fn resolved_header_gap(&self, metrics: ControlMetrics) -> f32 {
2049 self.header_gap.unwrap_or(metrics.form_section_header_gap)
2050 }
2051
2052 pub(super) fn resolved_description_gap(&self, metrics: ControlMetrics) -> f32 {
2053 self.description_gap
2054 .unwrap_or(metrics.form_section_description_gap)
2055 }
2056
2057 pub(super) fn resolved_max_width(&self, metrics: ControlMetrics) -> Option<f32> {
2058 if self.auto_width {
2059 None
2060 } else {
2061 Some(self.max_width.unwrap_or(metrics.form_section_max_width))
2062 }
2063 }
2064
2065 pub(super) fn resolved_radius(&self, metrics: ControlMetrics) -> f32 {
2066 self.radius.unwrap_or(metrics.form_section_radius).max(0.0)
2067 }
2068
2069 pub(super) fn resolved_title_style(&self) -> TextStyle {
2070 let theme = self.resolved_theme();
2071 self.title_style.clone().unwrap_or_else(|| TextStyle {
2072 weight: FontWeight::SEMIBOLD,
2073 ..text_token_style(&theme, theme.text.sm, theme.surfaces.text)
2074 })
2075 }
2076
2077 pub(super) fn resolved_description_style(&self) -> TextStyle {
2078 let theme = self.resolved_theme();
2079 self.description_style
2080 .clone()
2081 .unwrap_or_else(|| text_token_style(&theme, theme.text.xs, theme.surfaces.text_muted))
2082 }
2083
2084 pub(super) fn title_height(&self, style: &TextStyle) -> f32 {
2085 self.title_measurement
2086 .map(|measurement| measurement.height)
2087 .unwrap_or(style.line_height)
2088 .max(style.line_height)
2089 }
2090
2091 pub(super) fn description_height(&self, style: &TextStyle) -> f32 {
2092 if self.description.is_some() {
2093 self.description_measurement
2094 .map(|measurement| measurement.height)
2095 .unwrap_or(style.line_height)
2096 .max(style.line_height)
2097 } else {
2098 0.0
2099 }
2100 }
2101
2102 pub(super) fn text_block_height(
2103 &self,
2104 title_style: &TextStyle,
2105 description_style: &TextStyle,
2106 ) -> f32 {
2107 let title = self.title_height(title_style);
2108 let description = self.description_height(description_style);
2109 if description > 0.0 {
2110 let metrics = self.resolved_theme().metrics;
2111 title + self.resolved_description_gap(metrics) + description
2112 } else {
2113 title
2114 }
2115 }
2116
2117 pub(super) fn content_max_width(&self, available_width: f32) -> f32 {
2118 let theme = self.resolved_theme();
2119 let metrics = theme.metrics;
2120 let padding = self.resolved_padding(metrics);
2121 let available = if available_width.is_finite() {
2122 (available_width - padding.left - padding.right).max(0.0)
2123 } else {
2124 f32::INFINITY
2125 };
2126 self.resolved_max_width(metrics)
2127 .map(|width| width.min(available))
2128 .unwrap_or(available)
2129 }
2130
2131 pub(super) fn card_rect(&self, bounds: Rect) -> Rect {
2132 let theme = self.resolved_theme();
2133 let metrics = theme.metrics;
2134 let padding = self.resolved_padding(metrics);
2135 let width = if self.fill_width {
2136 bounds.width()
2137 } else {
2138 self.resolved_max_width(metrics)
2139 .map(|max_width| (max_width + padding.left + padding.right).min(bounds.width()))
2140 .unwrap_or(bounds.width())
2141 }
2142 .max(0.0);
2143 let x = if self.fill_width || width >= bounds.width() {
2144 bounds.x()
2145 } else {
2146 bounds.x() + ((bounds.width() - width) * 0.5)
2147 };
2148 Rect::new(x, bounds.y(), width, bounds.height())
2149 }
2150
2151 pub(super) fn content_rect(&self, bounds: Rect) -> Rect {
2152 let theme = self.resolved_theme();
2153 inset_rect(self.card_rect(bounds), self.resolved_padding(theme.metrics))
2154 }
2155
2156 pub(super) fn header_height(
2157 &self,
2158 title_style: &TextStyle,
2159 description_style: &TextStyle,
2160 ) -> f32 {
2161 let text_height = self.text_block_height(title_style, description_style);
2162 let action_height = self
2163 .header_action
2164 .as_ref()
2165 .map(|action| action.child().measured_size().height)
2166 .unwrap_or(0.0);
2167 text_height.max(action_height)
2168 }
2169}
2170
2171impl Widget for FormSection {
2172 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
2173 let theme = self.resolved_theme();
2174 let metrics = theme.metrics;
2175 let padding = self.resolved_padding(metrics);
2176 let body_gap = self.resolved_body_gap(metrics);
2177 let header_gap = self.resolved_header_gap(metrics);
2178 let title_style = self.resolved_title_style();
2179 let description_style = self.resolved_description_style();
2180 let title = measure_text(ctx, &self.title, &title_style);
2181 self.title_measurement = Some(title);
2182 let description = self
2183 .description
2184 .as_ref()
2185 .map(|description| measure_text(ctx, description, &description_style));
2186 self.description_measurement = description;
2187
2188 let content_max_width = self.content_max_width(constraints.max.width);
2189 let action_size = self
2190 .header_action
2191 .as_mut()
2192 .map(|action| {
2193 action.measure(
2194 ctx,
2195 Constraints::new(
2196 Size::ZERO,
2197 Size::new(content_max_width, constraints.max.height),
2198 ),
2199 )
2200 })
2201 .unwrap_or(Size::ZERO);
2202 let action_extent = if self.header_action.is_some() {
2203 action_size.width + header_gap
2204 } else {
2205 0.0
2206 };
2207 let text_width = title.width.max(
2208 description
2209 .map(|measurement| measurement.width)
2210 .unwrap_or(0.0),
2211 );
2212 let header_width = (text_width + action_extent).min(content_max_width);
2213 let child_size = self.child.measure(
2214 ctx,
2215 Constraints::new(
2216 Size::ZERO,
2217 Size::new(content_max_width, constraints.max.height),
2218 ),
2219 );
2220 let content_width = header_width.max(child_size.width).min(content_max_width);
2221 let header_height = self.header_height(&title_style, &description_style);
2222
2223 let mut width = content_width + padding.left + padding.right;
2224 if self.fill_width && constraints.max.width.is_finite() {
2225 width = constraints.max.width;
2226 }
2227 let height = padding.top + header_height + body_gap + child_size.height + padding.bottom;
2228 constraints.clamp(Size::new(width, height))
2229 }
2230
2231 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
2232 let theme = self.resolved_theme();
2233 let metrics = theme.metrics;
2234 let body_gap = self.resolved_body_gap(metrics);
2235 let content = self.content_rect(bounds);
2236 let title_style = self.resolved_title_style();
2237 let description_style = self.resolved_description_style();
2238 let header_height = self.header_height(&title_style, &description_style);
2239
2240 if let Some(action) = &mut self.header_action {
2241 let action_size = action.child().measured_size();
2242 action.arrange(
2243 ctx,
2244 Rect::new(
2245 content.max_x() - action_size.width,
2246 content.y() + ((header_height - action_size.height) * 0.5).max(0.0),
2247 action_size.width,
2248 action_size.height,
2249 ),
2250 );
2251 }
2252
2253 let child_size = self.child.child().measured_size();
2254 self.child.arrange(
2255 ctx,
2256 Rect::new(
2257 content.x(),
2258 content.y() + header_height + body_gap,
2259 child_size.width.min(content.width()),
2260 child_size.height,
2261 ),
2262 );
2263 }
2264
2265 fn paint(&self, ctx: &mut PaintCtx) {
2266 let theme = self.resolved_theme();
2267 let metrics = theme.metrics;
2268 let card = self.card_rect(ctx.bounds());
2269 let radius = self
2270 .resolved_radius(metrics)
2271 .min(card.width().min(card.height()) * 0.5);
2272 let shadow = match self.elevation {
2273 SurfaceElevation::None => None,
2274 SurfaceElevation::Small => Some(&theme.shadows.box_shadow.sm),
2275 SurfaceElevation::Medium => Some(&theme.shadows.box_shadow.md),
2276 SurfaceElevation::Large => Some(&theme.shadows.box_shadow.lg),
2277 };
2278 if let Some(shadow) = shadow {
2279 paint_theme_shadow(ctx, card, [radius; 4], shadow);
2280 }
2281
2282 let background = theme.surfaces.panel;
2283 let border = theme.surfaces.border;
2284 draw_control_shape(
2285 ctx,
2286 card,
2287 radius,
2288 physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
2289 background,
2290 border,
2291 );
2292
2293 let content = inset_rect(card, self.resolved_padding(metrics));
2294 let title_style = self.resolved_title_style();
2295 let description_style = self.resolved_description_style();
2296 let title_height = self.title_height(&title_style);
2297 let description_height = self.description_height(&description_style);
2298 let header_gap = self.resolved_header_gap(metrics);
2299 let description_gap = self.resolved_description_gap(metrics);
2300 let action_width = self
2301 .header_action
2302 .as_ref()
2303 .map(|action| action.child().measured_size().width + header_gap)
2304 .unwrap_or(0.0)
2305 .min(content.width());
2306 let text_width = (content.width() - action_width).max(0.0);
2307 let text_block_height = self.text_block_height(&title_style, &description_style);
2308 let header_height = self.header_height(&title_style, &description_style);
2309 let text_y = content.y() + ((header_height - text_block_height) * 0.5).max(0.0);
2310 let title_slot = Rect::new(content.x(), text_y, text_width, title_height);
2311 ctx.push_clip_rect(title_slot);
2312 paint_text(ctx, title_slot, &self.title, &title_style, TextAlign::Start);
2313 ctx.pop_clip();
2314 if let Some(description) = &self.description {
2315 let description_slot = Rect::new(
2316 content.x(),
2317 title_slot.max_y() + description_gap,
2318 text_width,
2319 description_height,
2320 );
2321 ctx.push_clip_rect(description_slot);
2322 paint_text(
2323 ctx,
2324 description_slot,
2325 description,
2326 &description_style,
2327 TextAlign::Start,
2328 );
2329 ctx.pop_clip();
2330 }
2331
2332 if let Some(action) = &self.header_action {
2333 action.paint(ctx);
2334 }
2335 self.child.paint(ctx);
2336 }
2337
2338 fn semantics(&self, ctx: &mut SemanticsCtx) {
2339 let card = self.card_rect(ctx.bounds());
2340 let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::GenericContainer, card);
2341 node.name = Some(self.title.clone());
2342 node.description = self.description.clone();
2343 ctx.push(node);
2344 if let Some(action) = &self.header_action {
2345 action.semantics(ctx);
2346 }
2347 self.child.semantics(ctx);
2348 }
2349
2350 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
2351 if let Some(action) = &self.header_action {
2352 action.visit_children(visitor);
2353 }
2354 self.child.visit_children(visitor);
2355 }
2356
2357 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
2358 if let Some(action) = &mut self.header_action {
2359 action.visit_children_mut(visitor);
2360 }
2361 self.child.visit_children_mut(visitor);
2362 }
2363}
2364
2365pub struct PanelSection {
2366 pub(super) theme: Box<DefaultTheme>,
2367 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
2368 pub(super) title: String,
2369 pub(super) gap: Option<f32>,
2370 pub(super) action_gap: Option<f32>,
2371 pub(super) title_style: Option<TextStyle>,
2372 pub(super) header_action: Option<SingleChild>,
2373 pub(super) child: SingleChild,
2374 pub(super) title_measurement: Option<TextMeasurement>,
2375 pub(super) collapsible: bool,
2376 pub(super) expanded: bool,
2377 pub(super) hovered_header: bool,
2378 pub(super) pressed_header: bool,
2379 pub(super) hover_animation: Progress,
2380 pub(super) press_animation: Progress,
2381 pub(super) focus_animation: Progress,
2382}
2383
2384impl PanelSection {
2385 pub fn new<W>(title: impl Into<String>, child: W) -> Self
2386 where
2387 W: Widget + 'static,
2388 {
2389 Self {
2390 theme: Box::new(DefaultTheme::default()),
2391 theme_reader: None,
2392 title: title.into(),
2393 gap: None,
2394 action_gap: None,
2395 title_style: None,
2396 header_action: None,
2397 child: SingleChild::new(child),
2398 title_measurement: None,
2399 collapsible: false,
2400 expanded: true,
2401 hovered_header: false,
2402 pressed_header: false,
2403 hover_animation: Progress::new(0.0),
2404 press_animation: Progress::new(0.0),
2405 focus_animation: Progress::new(0.0),
2406 }
2407 }
2408
2409 pub fn theme(mut self, theme: DefaultTheme) -> Self {
2410 self.theme = Box::new(theme);
2411 self.theme_reader = None;
2412 self
2413 }
2414
2415 pub fn theme_when<F>(mut self, theme: F) -> Self
2416 where
2417 F: Fn() -> DefaultTheme + 'static,
2418 {
2419 self.theme_reader = Some(Box::new(theme));
2420 self
2421 }
2422
2423 pub fn gap(mut self, gap: f32) -> Self {
2424 self.gap = Some(gap.max(0.0));
2425 self
2426 }
2427
2428 pub fn title_style(mut self, style: TextStyle) -> Self {
2429 self.title_style = Some(style);
2430 self
2431 }
2432
2433 pub fn header_action<W>(mut self, action: W) -> Self
2434 where
2435 W: Widget + 'static,
2436 {
2437 self.header_action = Some(SingleChild::new(action));
2438 self
2439 }
2440
2441 pub fn action_gap(mut self, gap: f32) -> Self {
2442 self.action_gap = Some(gap.max(0.0));
2443 self
2444 }
2445
2446 pub fn collapsible(mut self, collapsible: bool) -> Self {
2447 self.collapsible = collapsible;
2448 self
2449 }
2450
2451 pub fn expanded(mut self, expanded: bool) -> Self {
2452 self.expanded = expanded;
2453 self
2454 }
2455
2456 pub fn child_pod(&self) -> &sui_runtime::WidgetPod {
2457 self.child.child()
2458 }
2459
2460 pub fn child_mut(&mut self) -> &mut sui_runtime::WidgetPod {
2461 self.child.child_mut()
2462 }
2463
2464 pub(super) fn resolved_title_style(&self) -> TextStyle {
2465 let theme = self.resolved_theme();
2466 self.title_style
2467 .clone()
2468 .unwrap_or_else(|| text_token_style(&theme, theme.text.xs, theme.palette.text_muted))
2469 }
2470
2471 pub(super) fn resolved_theme(&self) -> DefaultTheme {
2472 self.theme_reader
2473 .as_ref()
2474 .map(|theme| theme())
2475 .unwrap_or_else(|| *self.theme)
2476 }
2477
2478 pub(super) fn resolved_gap(&self, metrics: ControlMetrics) -> f32 {
2479 self.gap.unwrap_or(metrics.panel_section_gap)
2480 }
2481
2482 pub(super) fn resolved_action_gap(&self, metrics: ControlMetrics) -> f32 {
2483 self.action_gap.unwrap_or(metrics.panel_section_action_gap)
2484 }
2485
2486 pub(super) fn title_height(&self, style: &TextStyle) -> f32 {
2487 self.title_measurement
2488 .map(|measurement| measurement.height)
2489 .unwrap_or(style.line_height)
2490 .max(style.line_height)
2491 }
2492
2493 pub(super) fn header_height(&self, title_style: &TextStyle) -> f32 {
2494 let action_height = self
2495 .header_action
2496 .as_ref()
2497 .map(|action| action.child().measured_size().height)
2498 .unwrap_or(0.0);
2499 self.title_height(title_style).max(action_height)
2500 }
2501
2502 pub(super) fn is_expanded(&self) -> bool {
2503 !self.collapsible || self.expanded
2504 }
2505
2506 pub(super) fn disclosure_width(&self, metrics: ControlMetrics) -> f32 {
2507 if self.collapsible {
2508 metrics.panel_section_disclosure_size
2509 } else {
2510 0.0
2511 }
2512 }
2513
2514 pub(super) fn title_rect(&self, bounds: Rect, header_height: f32, title_height: f32) -> Rect {
2515 let metrics = self.resolved_theme().metrics;
2516 let action_width = self
2517 .header_action
2518 .as_ref()
2519 .map(|action| action.child().measured_size().width + self.resolved_action_gap(metrics))
2520 .unwrap_or(0.0)
2521 .min(bounds.width());
2522 let disclosure_width = self.disclosure_width(metrics);
2523 Rect::new(
2524 bounds.x() + disclosure_width,
2525 bounds.y() + ((header_height - title_height) * 0.5).max(0.0),
2526 (bounds.width() - action_width - disclosure_width).max(0.0),
2527 title_height,
2528 )
2529 }
2530
2531 pub(super) fn header_rect(&self, bounds: Rect) -> Rect {
2532 let title_style = self.resolved_title_style();
2533 let header_height = self.header_height(&title_style);
2534 Rect::new(bounds.x(), bounds.y(), bounds.width(), header_height)
2535 }
2536
2537 pub(super) fn header_hit_rect(&self, bounds: Rect) -> Rect {
2538 let metrics = self.resolved_theme().metrics;
2539 let header = self.header_rect(bounds);
2540 let action_width = self
2541 .header_action
2542 .as_ref()
2543 .map(|action| action.child().measured_size().width + self.resolved_action_gap(metrics))
2544 .unwrap_or(0.0)
2545 .min(header.width());
2546 Rect::new(
2547 header.x(),
2548 header.y(),
2549 (header.width() - action_width).max(0.0),
2550 header.height(),
2551 )
2552 }
2553
2554 pub(super) fn toggle(&mut self, ctx: &mut EventCtx) {
2555 if !self.collapsible {
2556 return;
2557 }
2558
2559 self.expanded = !self.expanded;
2560 self.set_pressed_header(false, ctx);
2561 ctx.request_measure();
2562 ctx.request_paint();
2563 ctx.request_semantics();
2564 }
2565
2566 pub(super) fn set_hovered_header(&mut self, hovered: bool, ctx: &mut EventCtx) {
2567 if self.hovered_header == hovered {
2568 return;
2569 }
2570 let theme = self.resolved_theme();
2571 self.hovered_header = hovered;
2572 set_hover_animation_target(&mut self.hover_animation, hovered as u8 as f32, &theme, ctx);
2573 ctx.request_paint();
2574 ctx.request_semantics();
2575 }
2576
2577 pub(super) fn set_pressed_header(&mut self, pressed: bool, ctx: &mut EventCtx) {
2578 if self.pressed_header == pressed {
2579 return;
2580 }
2581 let theme = self.resolved_theme();
2582 self.pressed_header = pressed;
2583 set_press_animation_target(&mut self.press_animation, pressed as u8 as f32, &theme, ctx);
2584 ctx.request_paint();
2585 ctx.request_semantics();
2586 }
2587}
2588
2589impl Widget for PanelSection {
2590 fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
2591 if !self.collapsible {
2592 return;
2593 }
2594
2595 match event {
2596 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
2597 let hovered = self
2598 .header_hit_rect(ctx.bounds())
2599 .contains(pointer.position);
2600 self.set_hovered_header(hovered, ctx);
2601 }
2602 Event::Pointer(pointer)
2603 if pointer.kind == PointerEventKind::Down
2604 && pointer.button == Some(PointerButton::Primary)
2605 && self
2606 .header_hit_rect(ctx.bounds())
2607 .contains(pointer.position) =>
2608 {
2609 self.set_hovered_header(true, ctx);
2610 self.set_pressed_header(true, ctx);
2611 ctx.request_focus();
2612 ctx.request_pointer_capture(pointer.pointer_id);
2613 ctx.set_handled();
2614 }
2615 Event::Pointer(pointer)
2616 if pointer.kind == PointerEventKind::Up
2617 && pointer.button == Some(PointerButton::Primary)
2618 && self.pressed_header =>
2619 {
2620 let hovered = self
2621 .header_hit_rect(ctx.bounds())
2622 .contains(pointer.position);
2623 if hovered {
2624 self.toggle(ctx);
2625 }
2626 self.set_hovered_header(hovered, ctx);
2627 self.set_pressed_header(false, ctx);
2628 ctx.release_pointer_capture(pointer.pointer_id);
2629 ctx.set_handled();
2630 }
2631 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Leave => {
2632 self.set_hovered_header(false, ctx);
2633 }
2634 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
2635 if self.pressed_header || self.hovered_header {
2636 self.set_hovered_header(false, ctx);
2637 self.set_pressed_header(false, ctx);
2638 ctx.release_pointer_capture(pointer.pointer_id);
2639 ctx.set_handled();
2640 }
2641 }
2642 Event::Keyboard(key) if ctx.is_focused() && key.state == KeyState::Pressed => {
2643 match key.key.as_str() {
2644 "Enter" | " " => {
2645 self.toggle(ctx);
2646 ctx.set_handled();
2647 }
2648 _ => {}
2649 }
2650 }
2651 _ => {}
2652 }
2653 }
2654
2655 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
2656 let theme = self.resolved_theme();
2657 let metrics = theme.metrics;
2658 let gap = self.resolved_gap(metrics);
2659 let action_gap = self.resolved_action_gap(metrics);
2660 let title_style = self.resolved_title_style();
2661 let title_measurement = measure_text(ctx, &self.title, &title_style);
2662 self.title_measurement = Some(title_measurement);
2663 let action_size = self
2664 .header_action
2665 .as_mut()
2666 .map(|action| {
2667 action.measure(
2668 ctx,
2669 Constraints::new(Size::ZERO, Size::new(constraints.max.width, f32::INFINITY)),
2670 )
2671 })
2672 .unwrap_or(Size::ZERO);
2673 let header_height = self.title_height(&title_style).max(action_size.height);
2674 let child_size = if self.is_expanded() {
2675 self.child.measure(ctx, constraints)
2676 } else {
2677 Size::ZERO
2678 };
2679 let header_width = if self.header_action.is_some() {
2680 self.disclosure_width(metrics)
2681 + title_measurement.width
2682 + action_gap
2683 + action_size.width
2684 } else {
2685 self.disclosure_width(metrics) + title_measurement.width
2686 };
2687 let natural = Size::new(
2688 header_width.max(child_size.width),
2689 header_height
2690 + if self.is_expanded() && child_size.height > 0.0 {
2691 gap + child_size.height
2692 } else {
2693 0.0
2694 },
2695 );
2696
2697 constraints.clamp(natural)
2698 }
2699
2700 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
2701 let theme = self.resolved_theme();
2702 let metrics = theme.metrics;
2703 let gap = self.resolved_gap(metrics);
2704 let title_style = self.resolved_title_style();
2705 let header_height = self.header_height(&title_style);
2706 if let Some(action) = &mut self.header_action {
2707 let action_size = action.child().measured_size();
2708 action.arrange(
2709 ctx,
2710 Rect::new(
2711 bounds.max_x() - action_size.width.min(bounds.width()),
2712 bounds.y() + ((header_height - action_size.height) * 0.5).max(0.0),
2713 action_size.width.min(bounds.width()).max(0.0),
2714 action_size.height,
2715 ),
2716 );
2717 }
2718 let child_size = if self.is_expanded() {
2719 self.child.child().measured_size()
2720 } else {
2721 Size::ZERO
2722 };
2723 let child_height = if self.is_expanded() {
2724 child_size
2725 .height
2726 .min((bounds.height() - header_height - gap).max(0.0))
2727 } else {
2728 0.0
2729 };
2730 self.child.arrange(
2731 ctx,
2732 Rect::new(
2733 bounds.x(),
2734 bounds.y() + header_height + gap,
2735 bounds.width().min(child_size.width).max(0.0),
2736 child_height,
2737 ),
2738 );
2739 }
2740
2741 fn paint(&self, ctx: &mut PaintCtx) {
2742 let theme = self.resolved_theme();
2743 let metrics = theme.metrics;
2744 let title_style = self.resolved_title_style();
2745 let title_height = self.title_height(&title_style);
2746 let header_height = self.header_height(&title_style);
2747 let title_slot = self.title_rect(ctx.bounds(), header_height, title_height);
2748 if self.collapsible {
2749 let header_hit = self.header_hit_rect(ctx.bounds());
2750 let hover_amount = self.hover_animation.get(ctx);
2751 let press_amount = self.press_animation.get(ctx);
2752 let focus_amount = self.focus_animation.get(ctx);
2753 if focus_amount > Progress::EPSILON {
2754 draw_focus_ring(
2755 ctx,
2756 header_hit,
2757 metrics.indicator_corner_radius,
2758 metrics,
2759 theme
2760 .palette
2761 .focus_ring
2762 .with_alpha(theme.palette.focus_ring.alpha * focus_amount),
2763 );
2764 }
2765 let hover_alpha = (theme.interaction.hover_blend * 0.07 * hover_amount).min(0.08);
2766 let press_alpha = (theme.interaction.selected_blend * 0.48 * press_amount).min(0.14);
2767 let header_fill = if press_alpha > 0.0 {
2768 theme.palette.text.with_alpha(press_alpha)
2769 } else if hover_alpha > 0.0 {
2770 theme.palette.text.with_alpha(hover_alpha)
2771 } else {
2772 theme.palette.surface.with_alpha(0.001)
2773 };
2774 ctx.fill(
2775 Path::rounded_rect(header_hit, metrics.indicator_corner_radius),
2776 header_fill,
2777 );
2778 paint_panel_section_disclosure(
2779 ctx,
2780 self.header_rect(ctx.bounds()),
2781 self.expanded,
2782 hover_amount,
2783 press_amount,
2784 &theme,
2785 metrics.panel_section_disclosure_size,
2786 );
2787 }
2788 ctx.push_clip_rect(title_slot);
2789 paint_text(ctx, title_slot, &self.title, &title_style, TextAlign::Start);
2790 ctx.pop_clip();
2791 if let Some(action) = &self.header_action {
2792 action.paint(ctx);
2793 }
2794 if self.is_expanded() {
2795 self.child.paint(ctx);
2796 }
2797 }
2798
2799 fn semantics(&self, ctx: &mut SemanticsCtx) {
2800 let mut section = SemanticsNode::new(
2801 ctx.widget_id(),
2802 SemanticsRole::GenericContainer,
2803 ctx.bounds(),
2804 );
2805 section.name = Some(self.title.clone());
2806 section.state.focused = ctx.is_focused();
2807 section.state.hovered = self.hovered_header;
2808 if self.collapsible {
2809 section.state.expanded = Some(self.expanded);
2810 section.actions = vec![
2811 SemanticsAction::Focus,
2812 SemanticsAction::Expand,
2813 SemanticsAction::Collapse,
2814 ];
2815 }
2816 ctx.push(section);
2817
2818 let title_style = self.resolved_title_style();
2819 let title_height = self.title_height(&title_style);
2820 let header_height = self.header_height(&title_style);
2821 let mut title = SemanticsNode::new(
2822 panel_section_title_id(ctx.widget_id()),
2823 SemanticsRole::Text,
2824 self.title_rect(ctx.bounds(), header_height, title_height),
2825 );
2826 title.parent = Some(ctx.widget_id());
2827 title.name = Some(self.title.clone());
2828 title.value = Some(SemanticsValue::Text(self.title.clone()));
2829 ctx.push(title);
2830
2831 if let Some(action) = &self.header_action {
2832 action.semantics(ctx);
2833 }
2834 if self.is_expanded() {
2835 self.child.semantics(ctx);
2836 }
2837 }
2838
2839 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
2840 if let Some(action) = &self.header_action {
2841 action.visit_children(visitor);
2842 }
2843 if self.is_expanded() {
2844 self.child.visit_children(visitor);
2845 }
2846 }
2847
2848 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
2849 if let Some(action) = &mut self.header_action {
2850 action.visit_children_mut(visitor);
2851 }
2852 if self.is_expanded() {
2853 self.child.visit_children_mut(visitor);
2854 }
2855 }
2856
2857 fn accepts_focus(&self) -> bool {
2858 self.collapsible
2859 }
2860
2861 fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
2862 if self.collapsible {
2863 let theme = self.resolved_theme();
2864 set_focus_animation_target(
2865 &mut self.focus_animation,
2866 focused as u8 as f32,
2867 &theme,
2868 ctx,
2869 );
2870 ctx.request_paint();
2871 ctx.request_semantics();
2872 }
2873 }
2874}
2875
2876pub(super) fn panel_section_title_id(parent: WidgetId) -> WidgetId {
2877 const TAG: u64 = 3_u64 << 50;
2878 const LOW_MASK: u64 = (1_u64 << 50) - 1;
2879
2880 WidgetId::new(TAG | (parent.get().wrapping_mul(431).wrapping_add(7) & LOW_MASK))
2881}
2882
2883pub(super) fn paint_panel_section_disclosure(
2884 ctx: &mut PaintCtx,
2885 header: Rect,
2886 expanded: bool,
2887 hover_amount: f32,
2888 press_amount: f32,
2889 theme: &DefaultTheme,
2890 disclosure_size: f32,
2891) {
2892 let palette = theme.palette;
2893 let center = Point::new(
2894 header.x() + disclosure_size * 0.5,
2895 header.y() + header.height() * 0.5,
2896 );
2897 let half = disclosure_size * 0.25;
2898 let tip = disclosure_size * 0.22;
2899 let base_color = palette.text.with_alpha(0.68);
2900 let hover_color = mix_color(base_color, palette.text, hover_amount);
2901 let color = mix_color(hover_color, palette.text, press_amount);
2902 let mut builder = PathBuilder::new();
2903 if expanded {
2904 builder
2905 .move_to(Point::new(center.x - half, center.y - tip * 0.55))
2906 .line_to(Point::new(center.x + half, center.y - tip * 0.55))
2907 .line_to(Point::new(center.x, center.y + tip));
2908 } else {
2909 builder
2910 .move_to(Point::new(center.x - tip * 0.55, center.y - half))
2911 .line_to(Point::new(center.x + tip, center.y))
2912 .line_to(Point::new(center.x - tip * 0.55, center.y + half));
2913 }
2914 ctx.fill(builder.build(), color);
2915}
2916
2917pub struct DockPanel {
2918 pub(super) theme: Box<DefaultTheme>,
2919 pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
2920 pub(super) name: Option<String>,
2921 pub(super) title: String,
2922 pub(super) header_height: Option<f32>,
2923 pub(super) padding: Option<Insets>,
2924 pub(super) background: Option<Color>,
2925 pub(super) header_background: Option<Color>,
2926 pub(super) child: SingleChild,
2927 pub(super) title_measurement: Option<TextMeasurement>,
2928}
2929
2930impl DockPanel {
2931 pub fn new<W>(title: impl Into<String>, child: W) -> Self
2932 where
2933 W: Widget + 'static,
2934 {
2935 Self {
2936 theme: Box::new(DefaultTheme::default()),
2937 theme_reader: None,
2938 name: None,
2939 title: title.into(),
2940 header_height: None,
2941 padding: None,
2942 background: None,
2943 header_background: None,
2944 child: SingleChild::new(child),
2945 title_measurement: None,
2946 }
2947 }
2948
2949 pub fn theme(mut self, theme: DefaultTheme) -> Self {
2950 self.theme = Box::new(theme);
2951 self.theme_reader = None;
2952 self
2953 }
2954
2955 pub fn theme_when<F>(mut self, theme: F) -> Self
2956 where
2957 F: Fn() -> DefaultTheme + 'static,
2958 {
2959 self.theme_reader = Some(Box::new(theme));
2960 self
2961 }
2962
2963 pub fn name(mut self, name: impl Into<String>) -> Self {
2964 self.name = Some(name.into());
2965 self
2966 }
2967
2968 pub fn header_height(mut self, height: f32) -> Self {
2969 self.header_height = Some(height.max(0.0));
2970 self
2971 }
2972
2973 pub fn padding(mut self, padding: Insets) -> Self {
2974 self.padding = Some(padding);
2975 self
2976 }
2977
2978 pub fn background(mut self, color: Color) -> Self {
2979 self.background = Some(color);
2980 self
2981 }
2982
2983 pub fn header_background(mut self, color: Color) -> Self {
2984 self.header_background = Some(color);
2985 self
2986 }
2987
2988 pub fn child_pod(&self) -> &sui_runtime::WidgetPod {
2989 self.child.child()
2990 }
2991
2992 pub fn child_mut(&mut self) -> &mut sui_runtime::WidgetPod {
2993 self.child.child_mut()
2994 }
2995
2996 pub(super) fn resolved_title_style(&self) -> TextStyle {
2997 let theme = self.resolved_theme();
2998 text_token_style(&theme, theme.text.sm, theme.palette.text)
2999 }
3000
3001 pub(super) fn resolved_theme(&self) -> DefaultTheme {
3002 self.theme_reader
3003 .as_ref()
3004 .map(|theme| theme())
3005 .unwrap_or_else(|| *self.theme)
3006 }
3007
3008 pub(super) fn resolved_header_height(&self, metrics: ControlMetrics) -> f32 {
3009 self.header_height
3010 .unwrap_or(metrics.dock_panel_header_height)
3011 }
3012
3013 pub(super) fn resolved_padding(&self, metrics: ControlMetrics) -> Insets {
3014 self.padding.unwrap_or(metrics.dock_panel_padding)
3015 }
3016
3017 pub(super) fn title_height(&self, style: &TextStyle) -> f32 {
3018 self.title_measurement
3019 .map(|measurement| measurement.height)
3020 .unwrap_or(style.line_height)
3021 .max(style.line_height)
3022 }
3023
3024 pub(super) fn header_rect(&self, bounds: Rect) -> Rect {
3025 let theme = self.resolved_theme();
3026 Rect::new(
3027 bounds.x(),
3028 bounds.y(),
3029 bounds.width(),
3030 self.resolved_header_height(theme.metrics),
3031 )
3032 }
3033
3034 pub(super) fn content_rect(&self, bounds: Rect) -> Rect {
3035 let theme = self.resolved_theme();
3036 let metrics = theme.metrics;
3037 let header_height = self.resolved_header_height(metrics);
3038 inset_rect(
3039 Rect::new(
3040 bounds.x(),
3041 bounds.y() + header_height,
3042 bounds.width(),
3043 (bounds.height() - header_height).max(0.0),
3044 ),
3045 self.resolved_padding(metrics),
3046 )
3047 }
3048
3049 pub(super) fn child_constraints(&self, constraints: Constraints) -> Constraints {
3050 let theme = self.resolved_theme();
3051 let metrics = theme.metrics;
3052 let padding = self.resolved_padding(metrics);
3053 let header_height = self.resolved_header_height(metrics);
3054 let width = if constraints.max.width.is_finite() {
3055 (constraints.max.width - padding.left - padding.right).max(0.0)
3056 } else {
3057 f32::INFINITY
3058 };
3059 let height = if constraints.max.height.is_finite() {
3060 (constraints.max.height - header_height - padding.top - padding.bottom).max(0.0)
3061 } else {
3062 f32::INFINITY
3063 };
3064 Constraints::new(Size::ZERO, Size::new(width, height))
3065 }
3066}
3067
3068impl Widget for DockPanel {
3069 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
3070 let theme = self.resolved_theme();
3071 let metrics = theme.metrics;
3072 let padding = self.resolved_padding(metrics);
3073 let header_height = self.resolved_header_height(metrics);
3074 let title_style = self.resolved_title_style();
3075 let title_measurement = measure_text(ctx, &self.title, &title_style);
3076 self.title_measurement = Some(title_measurement);
3077 let child_size = self.child.measure(ctx, self.child_constraints(constraints));
3078 let natural = Size::new(
3079 (title_measurement.width + padding.left + padding.right)
3080 .max(child_size.width + padding.left + padding.right),
3081 header_height + padding.top + child_size.height + padding.bottom,
3082 );
3083
3084 constraints.clamp(natural)
3085 }
3086
3087 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
3088 self.child.arrange(ctx, self.content_rect(bounds));
3089 }
3090
3091 fn paint(&self, ctx: &mut PaintCtx) {
3092 let theme = self.resolved_theme();
3093 let palette = theme.palette;
3094 let metrics = theme.metrics;
3095 let padding = self.resolved_padding(metrics);
3096 let bounds = ctx.bounds();
3097 let header = self.header_rect(bounds);
3098 let title_style = self.resolved_title_style();
3099 let title_height = self.title_height(&title_style);
3100 let title_slot = Rect::new(
3101 header.x() + padding.left,
3102 header.y() + ((header.height() - title_height) * 0.5).max(0.0),
3103 (header.width() - padding.left - padding.right).max(0.0),
3104 title_height,
3105 );
3106 let divider_height = physical_pixels(ctx, 1.0);
3107
3108 ctx.fill_rect(bounds, self.background.unwrap_or(palette.surface));
3109 ctx.fill_rect(
3110 header,
3111 self.header_background
3112 .unwrap_or_else(|| palette.surface_raised.with_alpha(0.72)),
3113 );
3114 ctx.fill_rect(
3115 Rect::new(
3116 header.x(),
3117 header.max_y() - divider_height,
3118 header.width(),
3119 divider_height,
3120 ),
3121 palette.border,
3122 );
3123 ctx.push_clip_rect(title_slot);
3124 paint_text(ctx, title_slot, &self.title, &title_style, TextAlign::Start);
3125 ctx.pop_clip();
3126
3127 self.child.paint(ctx);
3128 }
3129
3130 fn semantics(&self, ctx: &mut SemanticsCtx) {
3131 let theme = self.resolved_theme();
3132 let padding = self.resolved_padding(theme.metrics);
3133 let mut panel = SemanticsNode::new(
3134 ctx.widget_id(),
3135 SemanticsRole::GenericContainer,
3136 ctx.bounds(),
3137 );
3138 panel.name = Some(self.name.clone().unwrap_or_else(|| self.title.clone()));
3139 ctx.push(panel);
3140
3141 let title_style = self.resolved_title_style();
3142 let title_height = self.title_height(&title_style);
3143 let header = self.header_rect(ctx.bounds());
3144 let mut title = SemanticsNode::new(
3145 dock_panel_title_id(ctx.widget_id()),
3146 SemanticsRole::Text,
3147 Rect::new(
3148 header.x() + padding.left,
3149 header.y() + ((header.height() - title_height) * 0.5).max(0.0),
3150 (header.width() - padding.left - padding.right).max(0.0),
3151 title_height,
3152 ),
3153 );
3154 title.parent = Some(ctx.widget_id());
3155 title.name = Some(self.title.clone());
3156 title.value = Some(SemanticsValue::Text(self.title.clone()));
3157 ctx.push(title);
3158
3159 self.child.semantics(ctx);
3160 }
3161
3162 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
3163 self.child.visit_children(visitor);
3164 }
3165
3166 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
3167 self.child.visit_children_mut(visitor);
3168 }
3169}
3170
3171pub(super) fn dock_panel_title_id(parent: WidgetId) -> WidgetId {
3172 const TAG: u64 = 5_u64 << 50;
3173 const LOW_MASK: u64 = (1_u64 << 50) - 1;
3174
3175 WidgetId::new(TAG | (parent.get().wrapping_mul(467).wrapping_add(11) & LOW_MASK))
3176}