sequoia_elements/
primitives.rs1use std::rc::Rc;
2
3use sequoia_core::{Edges, Length};
4use sequoia_layout::LayoutStyle;
5use sequoia_style::{BoxStyle, Theme};
6use sequoia_view::{AnyView, PaintCtx, Unit, View};
7
8pub use crate::container::Container;
9pub use sequoia_view::{Canvas, Column, Icon, Image, Portal, Row, ScrollView, Text};
10
11pub const PRESS_BLEND_RATE: f32 = 12.0;
12
13#[derive(Clone)]
14pub struct Deco {
15 content: AnyView,
16 resolve: Rc<dyn Fn(&Theme) -> BoxStyle>,
17 style: LayoutStyle,
18 hit: bool,
19}
20
21impl Deco {
22 pub fn new<V: View + 'static>(
23 content: V,
24 resolve: impl Fn(&Theme) -> BoxStyle + 'static,
25 ) -> Self {
26 Self {
27 content: AnyView::new(content),
28 resolve: Rc::new(resolve),
29 style: LayoutStyle {
30 width: Length::Fit,
31 height: Length::Fit,
32 ..LayoutStyle::default()
33 },
34 hit: true,
35 }
36 }
37
38 pub fn fixed<V: View + 'static>(content: V, deco: BoxStyle) -> Self {
39 Self::new(content, move |_| deco)
40 }
41
42 pub fn size_px(mut self, w: f32, h: f32) -> Self {
43 self.style = self
44 .style
45 .width(Length::Px(w.max(0.0)))
46 .height(Length::Px(h.max(0.0)));
47 self
48 }
49
50 pub fn square_px(self, px: f32) -> Self {
51 self.size_px(px, px)
52 }
53
54 pub fn hit_testable(mut self, on: bool) -> Self {
55 self.hit = on;
56 self
57 }
58
59 pub fn margin(mut self, margin: impl Into<Edges>) -> Self {
60 self.style = self.style.margin(margin);
61 self
62 }
63
64 pub fn margin_xy(self, x: f32, y: f32) -> Self {
65 self.margin(Edges::xy(x, y))
66 }
67
68 pub fn margin_x(mut self, x: f32) -> Self {
69 self.style = self.style.margin_x(x);
70 self
71 }
72
73 pub fn margin_y(mut self, y: f32) -> Self {
74 self.style = self.style.margin_y(y);
75 self
76 }
77
78 pub fn margin_left(mut self, v: f32) -> Self {
79 self.style = self.style.margin_left(v);
80 self
81 }
82
83 pub fn margin_right(mut self, v: f32) -> Self {
84 self.style = self.style.margin_right(v);
85 self
86 }
87
88 pub fn margin_top(mut self, v: f32) -> Self {
89 self.style = self.style.margin_top(v);
90 self
91 }
92
93 pub fn margin_bottom(mut self, v: f32) -> Self {
94 self.style = self.style.margin_bottom(v);
95 self
96 }
97}
98
99impl View for Deco {
100 fn body(&self) -> impl View + 'static {
101 Unit
102 }
103
104 fn children_override(&self) -> Option<Vec<AnyView>> {
105 Some(vec![self.content.clone()])
106 }
107
108 fn layout_style(&self) -> LayoutStyle {
109 self.style
110 }
111
112 fn hit_testable(&self) -> bool {
113 self.hit
114 }
115
116 fn paint_self(&self, ctx: &mut PaintCtx) {
117 ctx.paint_box((self.resolve)(&ctx.theme()));
118 }
119}