fc_ui/capabilities/effects/
dot_pattern.rs1use gpui::{prelude::FluentBuilder as _, *};
4
5use crate::capabilities::foundation::theme::use_theme;
6
7#[derive(IntoElement)]
8pub struct DotPattern {
9 spacing: Pixels,
10 dot_size: Pixels,
11 color: Option<Hsla>,
12 dot_opacity: f32,
13 children: Vec<AnyElement>,
14 style: StyleRefinement,
15}
16
17impl DotPattern {
18 pub fn new() -> Self {
19 Self {
20 spacing: px(20.0),
21 dot_size: px(2.0),
22 color: None,
23 dot_opacity: 0.3,
24 children: Vec::new(),
25 style: StyleRefinement::default(),
26 }
27 }
28
29 pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
30 self.spacing = spacing.into().max(px(1.0));
31 self
32 }
33
34 pub fn dot_size(mut self, size: impl Into<Pixels>) -> Self {
35 self.dot_size = size.into();
36 self
37 }
38
39 pub fn color(mut self, color: Hsla) -> Self {
40 self.color = Some(color);
41 self
42 }
43
44 pub fn opacity(mut self, opacity: f32) -> Self {
45 self.dot_opacity = opacity.clamp(0.0, 1.0);
46 self
47 }
48}
49
50impl Default for DotPattern {
51 fn default() -> Self {
52 Self::new()
53 }
54}
55
56impl Styled for DotPattern {
57 fn style(&mut self) -> &mut StyleRefinement {
58 &mut self.style
59 }
60}
61
62impl ParentElement for DotPattern {
63 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
64 self.children.extend(elements);
65 }
66}
67
68impl RenderOnce for DotPattern {
69 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
70 let theme = use_theme(cx);
71 let user_style = self.style;
72 let dot_color = self
73 .color
74 .unwrap_or(theme.tokens.border)
75 .opacity(self.dot_opacity);
76 let spacing = self.spacing;
77 let dot_size = self.dot_size;
78
79 div()
80 .relative()
81 .w_full()
82 .h_full()
83 .overflow_hidden()
84 .child(
85 canvas(
86 move |_bounds, _window, _cx| {},
87 move |bounds, _, window, _cx| {
88 let origin = bounds.origin;
89 let size = bounds.size;
90 let spacing_f32 = spacing / px(1.0);
91 let cols = ((size.width / px(1.0)) / spacing_f32).ceil() as usize + 1;
92 let rows = ((size.height / px(1.0)) / spacing_f32).ceil() as usize + 1;
93 let half_dot = dot_size / px(1.0) / 2.0;
94 let origin_x = origin.x / px(1.0);
95 let origin_y = origin.y / px(1.0);
96
97 for row in 0..rows {
98 for col in 0..cols {
99 let cx_pos = origin_x + col as f32 * spacing_f32;
100 let cy_pos = origin_y + row as f32 * spacing_f32;
101
102 let dot_bounds = Bounds {
103 origin: point(px(cx_pos - half_dot), px(cy_pos - half_dot)),
104 size: gpui::size(dot_size, dot_size),
105 };
106
107 window.paint_quad(PaintQuad {
108 bounds: dot_bounds,
109 corner_radii: Corners::all(dot_size),
110 background: dot_color.into(),
111 border_widths: Edges::default(),
112 border_color: transparent_black(),
113 border_style: BorderStyle::default(),
114 continuous_corners: false,
115 transform: Default::default(),
116 blend_mode: Default::default(),
117 });
118 }
119 }
120 },
121 )
122 .absolute()
123 .inset_0()
124 .size_full(),
125 )
126 .child(div().relative().size_full().children(self.children))
127 .map(|this| {
128 let mut el = this;
129 el.style().refine(&user_style);
130 el
131 })
132 }
133}