buffer_search.rs

 1use gpui2::{view, 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 WindowContext) -> View<Self> {
25        let color = ThemeColor::new(cx);
26
27        view(cx.entity(|cx| Self::new()), Self::render)
28    }
29
30    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> {
31        let color = ThemeColor::new(cx);
32
33        h_stack().bg(color.toolbar).p_2().child(
34            h_stack().child(Input::new("Search")).child(
35                IconButton::<Self>::new("replace", Icon::Replace)
36                    .when(self.is_replace_open, |this| this.color(IconColor::Accent))
37                    .on_click(|buffer_search, cx| {
38                        buffer_search.toggle_replace(cx);
39                    }),
40            ),
41        )
42    }
43}