Inform toolbars when active item changes
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
co-authored by
Nathan Sobo
parent
bebde782fa
commit
05e20ca72b
+62
-9
@@ -1,8 +1,11 @@
|
||||
use editor::{Editor, EditorSettings};
|
||||
use gpui::{
|
||||
action, color::Color, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext,
|
||||
View, ViewContext,
|
||||
action, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext, View,
|
||||
ViewContext, ViewHandle,
|
||||
};
|
||||
use workspace::Workspace;
|
||||
use postage::watch;
|
||||
use std::sync::Arc;
|
||||
use workspace::{ItemViewHandle, Settings, Toolbar, Workspace};
|
||||
|
||||
action!(Deploy);
|
||||
|
||||
@@ -15,7 +18,11 @@ pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(FindBar::deploy);
|
||||
}
|
||||
|
||||
struct FindBar;
|
||||
struct FindBar {
|
||||
settings: watch::Receiver<Settings>,
|
||||
query_editor: ViewHandle<Editor>,
|
||||
active_editor: Option<ViewHandle<Editor>>,
|
||||
}
|
||||
|
||||
impl Entity for FindBar {
|
||||
type Event = ();
|
||||
@@ -27,19 +34,65 @@ impl View for FindBar {
|
||||
}
|
||||
|
||||
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
||||
Empty::new()
|
||||
ChildView::new(&self.query_editor)
|
||||
.contained()
|
||||
.with_background_color(Color::red())
|
||||
.constrained()
|
||||
.with_height(30.)
|
||||
.with_style(self.settings.borrow().theme.selector.input_editor.container)
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
impl Toolbar for FindBar {
|
||||
fn active_item_changed(
|
||||
&mut self,
|
||||
item: Option<Box<dyn ItemViewHandle>>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> bool {
|
||||
self.active_editor = item.and_then(|item| item.act_as::<Editor>(cx));
|
||||
self.active_editor.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl FindBar {
|
||||
fn new(settings: watch::Receiver<Settings>, cx: &mut ViewContext<Self>) -> Self {
|
||||
let query_editor = cx.add_view(|cx| {
|
||||
Editor::single_line(
|
||||
{
|
||||
let settings = settings.clone();
|
||||
Arc::new(move |_| {
|
||||
let settings = settings.borrow();
|
||||
EditorSettings {
|
||||
style: settings.theme.selector.input_editor.as_editor(),
|
||||
tab_size: settings.tab_size,
|
||||
soft_wrap: editor::SoftWrap::None,
|
||||
}
|
||||
})
|
||||
},
|
||||
cx,
|
||||
)
|
||||
});
|
||||
cx.subscribe(&query_editor, Self::on_query_editor_event)
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
query_editor,
|
||||
active_editor: None,
|
||||
settings,
|
||||
}
|
||||
}
|
||||
|
||||
fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
|
||||
let settings = workspace.settings();
|
||||
workspace.active_pane().update(cx, |pane, cx| {
|
||||
pane.show_toolbar(cx, |cx| FindBar::new(settings, cx));
|
||||
if let Some(toolbar) = pane.active_toolbar() {
|
||||
cx.focus(toolbar);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn cancel(workspace: &mut Workspace, _: &Cancel, cx: &mut ViewContext<Workspace>) {
|
||||
workspace
|
||||
.active_pane()
|
||||
.update(cx, |pane, cx| pane.show_toolbar(cx, |_| FindBar));
|
||||
.update(cx, |pane, cx| pane.hide_toolbar(cx));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user