multi_buffer.rs

 1use std::marker::PhantomData;
 2
 3use crate::prelude::*;
 4use crate::{v_stack, Buffer, Icon, IconButton, Label};
 5
 6#[derive(Element)]
 7pub struct MultiBuffer<S: 'static + Send + Sync + Clone> {
 8    state_type: PhantomData<S>,
 9    buffers: Vec<Buffer<S>>,
10}
11
12impl<S: 'static + Send + Sync + Clone> MultiBuffer<S> {
13    pub fn new(buffers: Vec<Buffer<S>>) -> Self {
14        Self {
15            state_type: PhantomData,
16            buffers,
17        }
18    }
19
20    fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
21        let color = ThemeColor::new(cx);
22
23        v_stack()
24            .w_full()
25            .h_full()
26            .flex_1()
27            .children(self.buffers.clone().into_iter().map(|buffer| {
28                v_stack()
29                    .child(
30                        div()
31                            .flex()
32                            .items_center()
33                            .justify_between()
34                            .p_4()
35                            .bg(color.editor_subheader)
36                            .child(Label::new("main.rs"))
37                            .child(IconButton::new("arrow_up_right", Icon::ArrowUpRight)),
38                    )
39                    .child(buffer)
40            }))
41    }
42}
43
44#[cfg(feature = "stories")]
45pub use stories::*;
46
47#[cfg(feature = "stories")]
48mod stories {
49    use crate::{hello_world_rust_buffer_example, Story};
50
51    use super::*;
52
53    #[derive(Element)]
54    pub struct MultiBufferStory<S: 'static + Send + Sync + Clone> {
55        state_type: PhantomData<S>,
56    }
57
58    impl<S: 'static + Send + Sync + Clone> MultiBufferStory<S> {
59        pub fn new() -> Self {
60            Self {
61                state_type: PhantomData,
62            }
63        }
64
65        fn render(
66            &mut self,
67            _view: &mut S,
68            cx: &mut ViewContext<S>,
69        ) -> impl Element<ViewState = S> {
70            let color = ThemeColor::new(cx);
71
72            Story::container(cx)
73                .child(Story::title_for::<_, MultiBuffer<S>>(cx))
74                .child(Story::label(cx, "Default"))
75                .child(MultiBuffer::new(vec![
76                    hello_world_rust_buffer_example(&color),
77                    hello_world_rust_buffer_example(&color),
78                    hello_world_rust_buffer_example(&color),
79                    hello_world_rust_buffer_example(&color),
80                    hello_world_rust_buffer_example(&color),
81                ]))
82        }
83    }
84}