buffer_search.rs

 1use gpui2::{AppContext, Context, View};
 2
 3use crate::prelude::*;
 4use crate::{h_stack, Icon, IconButton, IconColor, Input};
 5
 6#[derive(Clone)]
 7pub struct BufferSearch {
 8    is_replace_open: bool,
 9}
10
11impl BufferSearch {
12    pub fn new() -> Self {
13        Self {
14            is_replace_open: false,
15        }
16    }
17
18    fn toggle_replace(&mut self, cx: &mut ViewContext<Self>) {
19        self.is_replace_open = !self.is_replace_open;
20
21        cx.notify();
22    }
23
24    pub fn view(cx: &mut AppContext) -> View<Self> {
25        {
26            let state = cx.build_model(|cx| Self::new());
27            let render = Self::render;
28            View::for_handle(state, render)
29        }
30    }
31
32    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {
33        let theme = theme(cx);
34
35        h_stack().bg(theme.toolbar).p_2().child(
36            h_stack().child(Input::new("Search")).child(
37                IconButton::<Self>::new("replace", Icon::Replace)
38                    .when(self.is_replace_open, |this| this.color(IconColor::Accent))
39                    .on_click(|buffer_search, cx| {
40                        buffer_search.toggle_replace(cx);
41                    }),
42            ),
43        )
44    }
45}