editor_pane.rs

 1use std::path::PathBuf;
 2
 3use gpui3::{view, Context, View};
 4
 5use crate::prelude::*;
 6use crate::{
 7    hello_world_rust_editor_with_status_example, v_stack, Breadcrumb, Buffer, BufferSearch, Icon,
 8    IconButton, IconColor, Symbol, Tab, TabBar, Toolbar,
 9};
10
11#[derive(Clone)]
12pub struct EditorPane {
13    tabs: Vec<Tab<Self>>,
14    path: PathBuf,
15    symbols: Vec<Symbol>,
16    buffer: Buffer<Self>,
17    buffer_search: View<BufferSearch>,
18    is_buffer_search_open: bool,
19}
20
21impl EditorPane {
22    pub fn new(
23        cx: &mut WindowContext,
24        tabs: Vec<Tab<Self>>,
25        path: PathBuf,
26        symbols: Vec<Symbol>,
27        buffer: Buffer<Self>,
28    ) -> Self {
29        Self {
30            tabs,
31            path,
32            symbols,
33            buffer,
34            buffer_search: BufferSearch::view(cx),
35            is_buffer_search_open: false,
36        }
37    }
38
39    pub fn toggle_buffer_search(&mut self, cx: &mut ViewContext<Self>) {
40        self.is_buffer_search_open = !self.is_buffer_search_open;
41
42        cx.notify();
43    }
44
45    pub fn view(cx: &mut WindowContext) -> View<Self> {
46        let theme = theme(cx);
47
48        view(
49            cx.entity(|cx| hello_world_rust_editor_with_status_example(cx)),
50            Self::render,
51        )
52    }
53
54    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> {
55        v_stack()
56            .w_full()
57            .h_full()
58            .flex_1()
59            .child(TabBar::new(self.tabs.clone()))
60            .child(
61                Toolbar::new()
62                    .left_item(Breadcrumb::new(self.path.clone(), self.symbols.clone()))
63                    .right_items(vec![
64                        IconButton::new(Icon::InlayHint),
65                        IconButton::<Self>::new(Icon::MagnifyingGlass)
66                            .when(self.is_buffer_search_open, |this| {
67                                this.color(IconColor::Accent)
68                            })
69                            .on_click(|editor, cx| {
70                                editor.toggle_buffer_search(cx);
71                            }),
72                        IconButton::new(Icon::MagicWand),
73                    ]),
74            )
75            .children(Some(self.buffer_search.clone()).filter(|_| self.is_buffer_search_open))
76            .child(self.buffer.clone())
77    }
78}