1use crate::{
10 BackgroundSizing, Brush, ColorTransition, ColumnDefinition, CommonState, CommonStates,
11 ControlStates, FLUENT_ICON_FONT_FAMILY, FluentSymbol, FocusVisual, FontIcon, Grid, GridCell,
12 GridLength, HorizontalAlignment, Margin, RowDefinition, ThemeResources, ToolTip,
13 ToolTipService, control_text_style,
14};
15use std::fmt;
16use waft_embedder::{Color, FontWeight};
17use waft_foundation::{App, Listener};
18use waft_painting::{AlignmentGeometry, TextStyle};
19use waft_rendering::BoxConstraints;
20use waft_widgets::*;
21
22pub const SETTINGS_CARD_BORDER_THICKNESS: [f64; 4] = [1.0; 4];
24pub const SETTINGS_CARD_PADDING: [f64; 4] = [16.0; 4];
26pub const SETTINGS_CARD_MIN_WIDTH: f64 = 148.0;
28pub const SETTINGS_CARD_MIN_HEIGHT: f64 = 68.0;
30pub const SETTINGS_CARD_DESCRIPTION_FONT_SIZE: f64 = 12.0;
32pub const SETTINGS_CARD_HEADER_ICON_MAX_SIZE: f64 = 20.0;
34pub const SETTINGS_CARD_LEFT_INDENTION: f64 = 0.0;
36pub const SETTINGS_CARD_CONTENT_MIN_WIDTH: f64 = 120.0;
38pub const SETTINGS_CARD_HEADER_ICON_MARGIN: [f64; 4] = [2.0, 0.0, 20.0, 0.0];
40pub const SETTINGS_CARD_ACTION_ICON_MARGIN: [f64; 4] = [14.0, 0.0, 0.0, 0.0];
42pub const SETTINGS_CARD_ACTION_ICON_MAX_SIZE: f64 = 13.0;
44pub const SETTINGS_CARD_VERTICAL_HEADER_CONTENT_SPACING: f64 = 8.0;
46pub const SETTINGS_CARD_WRAP_THRESHOLD: f64 = 476.0;
48pub const SETTINGS_CARD_WRAP_NO_ICON_THRESHOLD: f64 = 286.0;
50const HEADER_PANEL_MARGIN: [f64; 4] = [0.0, 0.0, 24.0, 0.0];
52const FOCUS_VISUAL_MARGIN: [f64; 4] = [-3.0; 4];
54
55#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
57pub enum ContentAlignment {
58 #[default]
60 Right,
61 Left,
63 Vertical,
65}
66
67#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub enum ContentAlignmentState {
70 Right,
71 RightWrapped,
73 RightWrappedNoIcon,
75 Left,
76 Vertical,
77}
78
79impl ContentAlignmentState {
80 pub fn of(content_alignment: ContentAlignment, width: f64) -> ContentAlignmentState {
82 if width < SETTINGS_CARD_WRAP_NO_ICON_THRESHOLD {
83 ContentAlignmentState::RightWrappedNoIcon
84 } else if width < SETTINGS_CARD_WRAP_THRESHOLD {
85 ContentAlignmentState::RightWrapped
86 } else {
87 match content_alignment {
88 ContentAlignment::Right => ContentAlignmentState::Right,
89 ContentAlignment::Left => ContentAlignmentState::Left,
90 ContentAlignment::Vertical => ContentAlignmentState::Vertical,
91 }
92 }
93 }
94
95 fn collapses_header_icon(self) -> bool {
97 matches!(
98 self,
99 ContentAlignmentState::RightWrappedNoIcon | ContentAlignmentState::Left
100 )
101 }
102
103 fn collapses_header(self) -> bool {
105 self == ContentAlignmentState::Left
106 }
107}
108
109#[derive(Clone)]
111pub struct SettingsCard {
112 pub header: Option<WidgetRef>,
114 pub description: Option<WidgetRef>,
116 pub header_icon: Option<WidgetRef>,
118 pub action_icon: WidgetRef,
120 pub action_icon_tool_tip: Option<String>,
122 pub is_click_enabled: bool,
124 pub content_alignment: ContentAlignment,
126 pub is_action_icon_visible: bool,
128 pub content: Option<WidgetRef>,
130 pub click: Option<Listener>,
132 pub is_enabled: bool,
134 pub horizontal_content_alignment: HorizontalAlignment,
136 pub background: Option<Color>,
138 pub border_thickness: Option<[f64; 4]>,
140 pub corner_radius: Option<f64>,
142 pub min_height: Option<f64>,
144 pub padding: Option<[f64; 4]>,
146 pub key: Option<KeyRef>,
147}
148
149impl Default for SettingsCard {
150 fn default() -> SettingsCard {
151 SettingsCard {
152 header: None,
153 description: None,
154 header_icon: None,
155 action_icon: FontIcon::symbol(FluentSymbol::ChevronRight)
157 .mirrored_when_right_to_left(true)
158 .into_widget(),
159 action_icon_tool_tip: None,
160 is_click_enabled: false,
161 content_alignment: ContentAlignment::Right,
162 is_action_icon_visible: true,
163 content: None,
164 click: None,
165 is_enabled: true,
166 horizontal_content_alignment: HorizontalAlignment::Right,
167 background: None,
168 border_thickness: None,
169 corner_radius: None,
170 min_height: None,
171 padding: None,
172 key: None,
173 }
174 }
175}
176
177impl SettingsCard {
178 pub fn new() -> SettingsCard {
179 SettingsCard::default()
180 }
181
182 #[cfg_attr(debug_assertions, track_caller)]
184 pub fn header<K>(mut self, header: impl IntoWidget<K>) -> SettingsCard {
185 self.header = Some(header.into_widget());
186 self
187 }
188
189 #[cfg_attr(debug_assertions, track_caller)]
191 pub fn description<K>(mut self, description: impl IntoWidget<K>) -> SettingsCard {
192 self.description = Some(description.into_widget());
193 self
194 }
195
196 #[cfg_attr(debug_assertions, track_caller)]
198 pub fn header_icon<K>(mut self, header_icon: impl IntoWidget<K>) -> SettingsCard {
199 self.header_icon = Some(header_icon.into_widget());
200 self
201 }
202
203 #[cfg_attr(debug_assertions, track_caller)]
205 pub fn action_icon<K>(mut self, action_icon: impl IntoWidget<K>) -> SettingsCard {
206 self.action_icon = action_icon.into_widget();
207 self
208 }
209
210 pub fn action_icon_tool_tip(mut self, tool_tip: impl Into<String>) -> SettingsCard {
212 self.action_icon_tool_tip = Some(tool_tip.into());
213 self
214 }
215
216 pub fn is_click_enabled(mut self, is_click_enabled: bool) -> SettingsCard {
218 self.is_click_enabled = is_click_enabled;
219 self
220 }
221
222 pub fn content_alignment(mut self, content_alignment: ContentAlignment) -> SettingsCard {
224 self.content_alignment = content_alignment;
225 self
226 }
227
228 pub fn is_action_icon_visible(mut self, is_action_icon_visible: bool) -> SettingsCard {
230 self.is_action_icon_visible = is_action_icon_visible;
231 self
232 }
233
234 #[cfg_attr(debug_assertions, track_caller)]
236 pub fn content<K>(mut self, content: impl IntoWidget<K>) -> SettingsCard {
237 self.content = Some(content.into_widget());
238 self
239 }
240
241 pub fn click(mut self, click: Listener) -> SettingsCard {
243 self.click = Some(click);
244 self
245 }
246
247 pub fn is_enabled(mut self, is_enabled: bool) -> SettingsCard {
249 self.is_enabled = is_enabled;
250 self
251 }
252
253 pub fn horizontal_content_alignment(mut self, alignment: HorizontalAlignment) -> SettingsCard {
255 self.horizontal_content_alignment = alignment;
256 self
257 }
258
259 pub fn background(mut self, background: Color) -> SettingsCard {
261 self.background = Some(background);
262 self
263 }
264
265 pub fn border_thickness(mut self, border_thickness: [f64; 4]) -> SettingsCard {
267 self.border_thickness = Some(border_thickness);
268 self
269 }
270
271 pub fn corner_radius(mut self, corner_radius: f64) -> SettingsCard {
273 self.corner_radius = Some(corner_radius);
274 self
275 }
276
277 pub fn min_height(mut self, min_height: f64) -> SettingsCard {
279 self.min_height = Some(min_height);
280 self
281 }
282
283 pub fn padding(mut self, padding: [f64; 4]) -> SettingsCard {
285 self.padding = Some(padding);
286 self
287 }
288
289 pub fn key(mut self, key: KeyRef) -> SettingsCard {
290 self.key = Some(key);
291 self
292 }
293}
294
295impl fmt::Debug for SettingsCard {
296 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
297 f.debug_struct("SettingsCard")
298 .field("is_click_enabled", &self.is_click_enabled)
299 .field("content_alignment", &self.content_alignment)
300 .field("is_enabled", &self.is_enabled)
301 .finish_non_exhaustive()
302 }
303}
304
305impl StatelessWidget for SettingsCard {
306 fn key(&self) -> Option<&KeyRef> {
307 self.key.as_ref()
308 }
309
310 fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
311 let card = self.clone();
312 LayoutBuilder::new(move |app, context, constraints| {
314 let width = constraints.max_width;
315 if !card.is_click_enabled {
316 let states = ControlStates {
318 common: if card.is_enabled {
319 CommonState::Normal
320 } else {
321 CommonState::Disabled
322 },
323 focused: false,
324 };
325 return while_enabled(
326 card.is_enabled,
327 template(app, context, &card, states, width),
328 );
329 }
330 let click = card.click.clone().unwrap_or_else(|| Listener::new(|_| {}));
331 let templated = card.clone();
332 let button = CommonStates::new(click, move |app, context, states| {
333 template(app, context, &templated, states, width)
334 })
335 .is_enabled(card.is_enabled);
336 while_enabled(card.is_enabled, button)
337 })
338 .into_widget()
339 }
340}
341
342#[cfg_attr(debug_assertions, track_caller)]
344fn while_enabled<K>(is_enabled: bool, child: impl IntoWidget<K>) -> WidgetRef {
345 ExcludeFocus::new(IgnorePointer::new().ignoring(!is_enabled).child(child))
346 .excluding(!is_enabled)
347 .into_widget()
348}
349
350struct CardBrushes {
352 background: Option<Color>,
354 border_brush: Brush,
356 header: Color,
358 description: Color,
360 action_icon: Color,
362}
363
364impl CardBrushes {
365 fn of(r: &ThemeResources, state: CommonState) -> CardBrushes {
366 let foreground = r.text_fill_color_primary_brush;
368 let description = r.text_fill_color_secondary_brush;
369 let border_brush = Brush::Solid(r.card_stroke_color_default_brush);
371 match state {
372 CommonState::Normal => CardBrushes {
373 background: None,
374 border_brush,
375 header: foreground,
376 description,
377 action_icon: foreground,
378 },
379 CommonState::PointerOver => CardBrushes {
380 background: Some(r.control_fill_color_secondary_brush),
382 border_brush: Brush::ControlElevation(r.control_elevation_border_brush),
384 header: r.text_fill_color_primary_brush,
386 description,
387 action_icon: foreground,
388 },
389 CommonState::Pressed => CardBrushes {
390 background: Some(r.control_fill_color_tertiary_brush),
392 border_brush: Brush::Solid(r.control_stroke_color_default_brush),
394 header: r.text_fill_color_secondary_brush,
396 description: r.text_fill_color_secondary_brush,
397 action_icon: foreground,
398 },
399 CommonState::Disabled => CardBrushes {
401 background: None,
402 border_brush,
403 header: r.text_fill_color_disabled_brush,
404 description: r.text_fill_color_disabled_brush,
405 action_icon: r.text_fill_color_disabled_brush,
406 },
407 }
408 }
409}
410
411fn template(
413 app: &mut App,
414 context: BuildContext,
415 card: &SettingsCard,
416 states: ControlStates,
417 width: f64,
418) -> WidgetRef {
419 let resources = ThemeResources::of(app, context);
420 let r = &*resources;
421 let brushes = CardBrushes::of(r, states.common);
422 let alignment = ContentAlignmentState::of(card.content_alignment, width);
423 let header = card
424 .header
425 .clone()
426 .filter(|_| !alignment.collapses_header());
427 let description = card
428 .description
429 .clone()
430 .filter(|_| !alignment.collapses_header());
431 let content_spacing = alignment != ContentAlignmentState::Right
433 && alignment != ContentAlignmentState::Left
434 && card.content.is_some()
435 && (card.header.is_some() || card.description.is_some());
436
437 let mut children = Vec::new();
438 if let Some(icon) = card.header_icon.clone()
439 && !alignment.collapses_header_icon()
440 {
441 children.push(header_icon_presenter_holder(r, icon, brushes.header));
442 }
443 children.push(header_panel(r, &brushes, alignment, header, description));
444 if let Some(content) = card.content.clone() {
445 children.push(content_presenter(r, card, alignment, content));
446 }
447 if card.is_click_enabled && card.is_action_icon_visible {
448 children.push(action_icon_presenter_holder(r, card, brushes.action_icon));
449 }
450
451 let corner_radius = card.corner_radius.unwrap_or(r.control_corner_radius[0]);
452 let border_thickness = card
453 .border_thickness
454 .unwrap_or(SETTINGS_CARD_BORDER_THICKNESS);
455 let padding = card.padding.unwrap_or(SETTINGS_CARD_PADDING);
456 let border_brush = brushes.border_brush;
457 let background = brushes
459 .background
460 .or(card.background)
461 .unwrap_or(r.card_background_fill_color_default_brush);
462 let root_grid = ColorTransition::new(
464 background,
465 r.control_faster_animation_duration,
466 move |_app, background| {
467 Grid::new()
468 .row_definitions([
469 RowDefinition::new(GridLength::STAR),
470 RowDefinition::new(GridLength::AUTO),
471 ])
472 .column_definitions([
473 ColumnDefinition::new(GridLength::AUTO).min_width(SETTINGS_CARD_LEFT_INDENTION),
474 ColumnDefinition::new(GridLength::STAR),
475 ColumnDefinition::new(GridLength::AUTO),
476 ColumnDefinition::new(GridLength::AUTO),
477 ])
478 .row_spacing(if content_spacing {
479 SETTINGS_CARD_VERTICAL_HEADER_CONTENT_SPACING
480 } else {
481 0.0
482 })
483 .background(Brush::Solid(background))
484 .background_sizing(BackgroundSizing::InnerBorderEdge)
485 .border_brush(border_brush)
486 .border_thickness_ltrb(border_thickness)
487 .corner_radius(corner_radius)
488 .padding(padding)
489 .children(children.clone())
490 .into_widget()
491 },
492 );
493 let min_width = if width.is_finite() {
495 width.max(SETTINGS_CARD_MIN_WIDTH)
496 } else {
497 SETTINGS_CARD_MIN_WIDTH
498 };
499 let sized = ConstrainedBox::new(
500 BoxConstraints::new()
501 .min_width(min_width)
502 .min_height(card.min_height.unwrap_or(SETTINGS_CARD_MIN_HEIGHT)),
503 )
504 .child(root_grid);
505 FocusVisual::new(sized, r.theme)
506 .visible(states.focused)
507 .margin(FOCUS_VISUAL_MARGIN)
508 .corner_radius(corner_radius)
509 .into_widget()
510}
511
512fn icon_viewbox(icon: WidgetRef, max_size: f64, text: TextStyle) -> WidgetRef {
514 SizedBox::new()
515 .width(max_size)
516 .height(max_size)
517 .child(FittedBox::new().child(DefaultTextStyle::new(text, icon)))
518 .into_widget()
519}
520
521fn header_icon_presenter_holder(
523 r: &ThemeResources,
524 icon: WidgetRef,
525 foreground: Color,
526) -> WidgetRef {
527 let text = control_text_style(
528 r,
529 r.control_content_theme_font_size,
530 FontWeight::W400,
531 foreground,
532 );
533 GridCell::new(Margin::new(
534 SETTINGS_CARD_HEADER_ICON_MARGIN,
535 Center::new()
536 .width_factor(1.0)
537 .height_factor(1.0)
538 .child(icon_viewbox(icon, SETTINGS_CARD_HEADER_ICON_MAX_SIZE, text)),
539 ))
540 .into_widget()
541}
542
543fn header_panel(
545 r: &ThemeResources,
546 brushes: &CardBrushes,
547 alignment: ContentAlignmentState,
548 header: Option<WidgetRef>,
549 description: Option<WidgetRef>,
550) -> WidgetRef {
551 let header = header.map(|header| {
552 DefaultTextStyle::new(
553 control_text_style(
554 r,
555 r.control_content_theme_font_size,
556 FontWeight::W400,
557 brushes.header,
558 ),
559 header,
560 )
561 .into_widget()
562 });
563 let description = description.map(|description| {
564 DefaultTextStyle::new(
565 control_text_style(
566 r,
567 SETTINGS_CARD_DESCRIPTION_FONT_SIZE,
568 FontWeight::W400,
569 brushes.description,
570 ),
571 description,
572 )
573 .into_widget()
574 });
575 let wrapped = matches!(
576 alignment,
577 ContentAlignmentState::RightWrapped | ContentAlignmentState::RightWrappedNoIcon
578 );
579 let panel = Column::new()
580 .main_axis_size(MainAxisSize::Min)
581 .cross_axis_alignment(CrossAxisAlignment::Start)
582 .children(header.into_iter().chain(description).collect::<Vec<_>>());
583 GridCell::new(Margin::new(
584 if wrapped {
585 [0.0; 4]
586 } else {
587 HEADER_PANEL_MARGIN
588 },
589 Align::new()
590 .alignment(AlignmentGeometry::CENTER_LEFT)
591 .width_factor(1.0)
592 .height_factor(1.0)
593 .child(panel),
594 ))
595 .column(1)
596 .into_widget()
597}
598
599fn content_presenter(
601 r: &ThemeResources,
602 card: &SettingsCard,
603 alignment: ContentAlignmentState,
604 content: WidgetRef,
605) -> WidgetRef {
606 let content = DefaultTextStyle::new(
608 control_text_style(
609 r,
610 r.control_content_theme_font_size,
611 FontWeight::W400,
612 r.text_fill_color_primary_brush,
613 ),
614 content,
615 );
616 let aligned = |alignment: AlignmentGeometry| {
617 Align::new()
618 .alignment(alignment)
619 .width_factor(1.0)
620 .height_factor(1.0)
621 .child(content.clone())
622 .into_widget()
623 };
624 let across = match alignment {
625 ContentAlignmentState::Right => {
626 return GridCell::new(aligned(AlignmentGeometry::CENTER_RIGHT))
627 .column(2)
628 .into_widget();
629 }
630 ContentAlignmentState::RightWrapped | ContentAlignmentState::RightWrappedNoIcon => {
632 HorizontalAlignment::Left
633 }
634 ContentAlignmentState::Left => HorizontalAlignment::Left,
635 ContentAlignmentState::Vertical => card.horizontal_content_alignment,
637 };
638 let presenter = match across {
639 HorizontalAlignment::Left => aligned(AlignmentGeometry::CENTER_LEFT),
640 HorizontalAlignment::Center => aligned(AlignmentGeometry::CENTER),
641 HorizontalAlignment::Right => aligned(AlignmentGeometry::CENTER_RIGHT),
642 HorizontalAlignment::Stretch => content.into_widget(),
643 };
644 GridCell::new(presenter).row(1).column(1).into_widget()
645}
646
647fn action_icon_presenter_holder(
649 r: &ThemeResources,
650 card: &SettingsCard,
651 foreground: Color,
652) -> WidgetRef {
653 let text = TextStyle::new()
655 .font_family(FLUENT_ICON_FONT_FAMILY)
656 .font_size(r.control_content_theme_font_size)
657 .color(foreground);
658 let icon = icon_viewbox(
659 card.action_icon.clone(),
660 SETTINGS_CARD_ACTION_ICON_MAX_SIZE,
661 text,
662 );
663 let icon = match card.action_icon_tool_tip.clone() {
664 Some(tool_tip) => ToolTipService::new(icon, ToolTip::text(tool_tip)).into_widget(),
665 None => icon,
666 };
667 GridCell::new(Margin::new(
668 SETTINGS_CARD_ACTION_ICON_MARGIN,
669 Center::new()
670 .width_factor(1.0)
671 .height_factor(1.0)
672 .child(icon),
673 ))
674 .column(3)
675 .row_span(2)
676 .into_widget()
677}