find.rs

 1use editor::{Editor, EditorSettings};
 2use gpui::{
 3    action, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext, View,
 4    ViewContext, ViewHandle,
 5};
 6use postage::watch;
 7use std::sync::Arc;
 8use workspace::{ItemViewHandle, Settings, Toolbar, Workspace};
 9
10action!(Deploy);
11
12pub fn init(cx: &mut MutableAppContext) {
13    cx.add_bindings([Binding::new(
14        "cmd-f",
15        Deploy,
16        Some("Editor && mode == full"),
17    )]);
18    cx.add_action(FindBar::deploy);
19}
20
21struct FindBar {
22    settings: watch::Receiver<Settings>,
23    query_editor: ViewHandle<Editor>,
24    active_editor: Option<ViewHandle<Editor>>,
25}
26
27impl Entity for FindBar {
28    type Event = ();
29}
30
31impl View for FindBar {
32    fn ui_name() -> &'static str {
33        "FindBar"
34    }
35
36    fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
37        ChildView::new(&self.query_editor)
38            .contained()
39            .with_style(self.settings.borrow().theme.selector.input_editor.container)
40            .boxed()
41    }
42}
43
44impl Toolbar for FindBar {
45    fn active_item_changed(
46        &mut self,
47        item: Option<Box<dyn ItemViewHandle>>,
48        cx: &mut ViewContext<Self>,
49    ) -> bool {
50        self.active_editor = item.and_then(|item| item.act_as::<Editor>(cx));
51        self.active_editor.is_some()
52    }
53}
54
55impl FindBar {
56    fn new(settings: watch::Receiver<Settings>, cx: &mut ViewContext<Self>) -> Self {
57        let query_editor = cx.add_view(|cx| {
58            Editor::single_line(
59                {
60                    let settings = settings.clone();
61                    Arc::new(move |_| {
62                        let settings = settings.borrow();
63                        EditorSettings {
64                            style: settings.theme.selector.input_editor.as_editor(),
65                            tab_size: settings.tab_size,
66                            soft_wrap: editor::SoftWrap::None,
67                        }
68                    })
69                },
70                cx,
71            )
72        });
73        cx.subscribe(&query_editor, Self::on_query_editor_event)
74            .detach();
75
76        Self {
77            query_editor,
78            active_editor: None,
79            settings,
80        }
81    }
82
83    fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
84        let settings = workspace.settings();
85        workspace.active_pane().update(cx, |pane, cx| {
86            pane.show_toolbar(cx, |cx| FindBar::new(settings, cx));
87            if let Some(toolbar) = pane.active_toolbar() {
88                cx.focus(toolbar);
89            }
90        });
91    }
92
93    fn cancel(workspace: &mut Workspace, _: &Cancel, cx: &mut ViewContext<Workspace>) {
94        workspace
95            .active_pane()
96            .update(cx, |pane, cx| pane.hide_toolbar(cx));
97    }
98}