1use crate::{AnyPiece, Boundary, BuildCx, Flex, Layout, LayoutOps, Piece, RNode};
6use day_spec::{Proposal, Rect, Size, kinds};
7use std::rc::Rc;
8
9const SHEET_SCRIM: day_spec::Color = day_spec::Color::rgba(0.0, 0.0, 0.0, 0.32);
11const SHEET_RADIUS: f64 = 12.0;
13
14pub struct ComposedSheet {
18 pub content: AnyPiece,
19 pub large: bool,
21}
22
23impl Piece for ComposedSheet {
24 fn build(self, cx: &mut BuildCx) -> RNode {
25 let fill = Flex {
26 grow_w: true,
27 grow_h: true,
28 ..Default::default()
29 };
30 let node = cx.native(
31 kinds::CONTAINER,
32 &day_spec::props::ContainerProps::default(),
33 Rc::new(SheetLayout { large: self.large }),
34 fill,
35 Boundary::Yes,
36 );
37 cx.under(node, |cx| {
38 cx.native(
39 kinds::CONTAINER,
40 &day_spec::props::ContainerProps {
41 background: Some(SHEET_SCRIM),
42 role: Some(day_spec::SurfaceRole::Scrim),
43 ..Default::default()
44 },
45 Rc::new(FillLayout),
46 fill,
47 Boundary::No,
48 );
49 let card = cx.native(
50 kinds::CONTAINER,
51 &day_spec::props::ContainerProps {
52 background: None,
53 corner_radius: SHEET_RADIUS,
54 clips: true,
55 role: Some(day_spec::SurfaceRole::Window),
56 },
57 Rc::new(FillLayout),
58 fill,
59 Boundary::Yes,
60 );
61 cx.under(card, |cx| {
62 let _ = self.content.build(cx);
63 });
64 });
65 node
66 }
67}
68
69fn sheet_card_size(area: Size, large: bool) -> Size {
73 let max = if large {
74 day_spec::props::LARGE_SHEET_MAX
75 } else {
76 day_spec::props::SHEET_MAX
77 };
78 Size::new(
79 (area.width - 40.0).clamp(320.0, max.width).min(area.width),
80 (area.height - 80.0)
81 .clamp(300.0, max.height)
82 .min(area.height),
83 )
84}
85
86struct SheetLayout {
88 large: bool,
89}
90
91impl Layout for SheetLayout {
92 fn measure(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
93 Size::new(p.width.unwrap_or(0.0), p.height.unwrap_or(0.0))
94 }
95 fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
96 let area = bounds.size;
97 if let Some(&scrim) = children.first() {
98 cx.place_child(scrim, Rect::from_size(area));
99 }
100 if let Some(&card) = children.get(1) {
101 let size = sheet_card_size(area, self.large);
102 cx.place_child(
103 card,
104 Rect::new(
105 ((area.width - size.width) / 2.0).floor(),
106 ((area.height - size.height) / 2.0).floor(),
107 size.width,
108 size.height,
109 ),
110 );
111 }
112 }
113}
114
115struct FillLayout;
117
118impl Layout for FillLayout {
119 fn measure(&self, _cx: &mut dyn LayoutOps, _children: &[RNode], p: Proposal) -> Size {
120 Size::new(p.width.unwrap_or(0.0), p.height.unwrap_or(0.0))
121 }
122 fn place(&self, cx: &mut dyn LayoutOps, children: &[RNode], bounds: Rect) {
123 for &child in children {
124 cx.place_child(child, Rect::from_size(bounds.size));
125 }
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn large_card_grows_within_the_window() {
135 let area = Size::new(1280.0, 800.0);
136 assert_eq!(sheet_card_size(area, false), Size::new(640.0, 620.0));
137 assert_eq!(sheet_card_size(area, true), Size::new(1040.0, 720.0));
138 let small = Size::new(500.0, 400.0);
140 assert_eq!(sheet_card_size(small, true), sheet_card_size(small, false));
141 }
142}