Skip to main content

ferrum_flow/plugins/
background.rs

1use gpui::{Element, ParentElement, Styled, div, px, rgb};
2
3use crate::plugin::Plugin;
4
5pub struct BackgroundPlugin;
6
7impl BackgroundPlugin {
8    pub fn new() -> Self {
9        Self {}
10    }
11}
12
13impl Plugin for BackgroundPlugin {
14    fn name(&self) -> &'static str {
15        "background"
16    }
17    fn setup(&mut self, _ctx: &mut crate::plugin::InitPluginContext) {}
18    fn on_event(
19        &mut self,
20        _event: &crate::plugin::FlowEvent,
21        _context: &mut crate::plugin::PluginContext,
22    ) -> crate::plugin::EventResult {
23        crate::plugin::EventResult::Continue
24    }
25    fn priority(&self) -> i32 {
26        0
27    }
28    fn render_layer(&self) -> crate::plugin::RenderLayer {
29        crate::plugin::RenderLayer::Background
30    }
31    fn render(&mut self, ctx: &mut crate::plugin::RenderContext) -> Option<gpui::AnyElement> {
32        let base_grid = 40.0;
33        let zoom = ctx.viewport.zoom;
34
35        let grid = base_grid * zoom;
36
37        let offset = ctx.viewport.offset;
38
39        let start_x = f32::from(offset.x) % grid;
40        let start_y = f32::from(offset.y) % grid;
41
42        let mut dots = Vec::new();
43
44        let bounds = ctx.window.bounds();
45        let width = f32::from(bounds.size.width);
46        let height = f32::from(bounds.size.height);
47
48        let mut x = start_x;
49
50        while x < width {
51            let mut y = start_y;
52
53            while y < height {
54                dots.push(
55                    div()
56                        .absolute()
57                        .left(px(x))
58                        .top(px(y))
59                        .w(px(2.0))
60                        .h(px(2.0))
61                        .rounded_full()
62                        .bg(rgb(ctx.theme.background_grid_dot)),
63                );
64
65                y += grid;
66            }
67
68            x += grid;
69        }
70        Some(
71            div()
72                .absolute()
73                .w(px(width))
74                .h(px(height))
75                .bg(gpui::rgb(ctx.theme.background))
76                .children(dots)
77                .into_any(),
78        )
79    }
80}