Deploy FindBar when hitting cmd-f

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-01-27 17:39:58 +01:00
parent e7d4c385d5
commit bebde782fa
6 changed files with 99 additions and 2 deletions

10
Cargo.lock generated
View file

@ -1719,6 +1719,15 @@ dependencies = [
"workspace", "workspace",
] ]
[[package]]
name = "find"
version = "0.1.0"
dependencies = [
"editor",
"gpui",
"workspace",
]
[[package]] [[package]]
name = "fixedbitset" name = "fixedbitset"
version = "0.2.0" version = "0.2.0"
@ -5725,6 +5734,7 @@ dependencies = [
"editor", "editor",
"env_logger", "env_logger",
"file_finder", "file_finder",
"find",
"fsevent", "fsevent",
"futures", "futures",
"fuzzy", "fuzzy",

12
crates/find/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "find"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/find.rs"
[dependencies]
editor = { path = "../editor" }
gpui = { path = "../gpui" }
workspace = { path = "../workspace" }

45
crates/find/src/find.rs Normal file
View file

@ -0,0 +1,45 @@
use gpui::{
action, color::Color, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext,
View, ViewContext,
};
use workspace::Workspace;
action!(Deploy);
pub fn init(cx: &mut MutableAppContext) {
cx.add_bindings([Binding::new(
"cmd-f",
Deploy,
Some("Editor && mode == full"),
)]);
cx.add_action(FindBar::deploy);
}
struct FindBar;
impl Entity for FindBar {
type Event = ();
}
impl View for FindBar {
fn ui_name() -> &'static str {
"FindBar"
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
Empty::new()
.contained()
.with_background_color(Color::red())
.constrained()
.with_height(30.)
.boxed()
}
}
impl FindBar {
fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
workspace
.active_pane()
.update(cx, |pane, cx| pane.show_toolbar(cx, |_| FindBar));
}
}

View file

@ -7,11 +7,17 @@ use gpui::{
geometry::{rect::RectF, vector::vec2f}, geometry::{rect::RectF, vector::vec2f},
keymap::Binding, keymap::Binding,
platform::CursorStyle, platform::CursorStyle,
Entity, MutableAppContext, Quad, RenderContext, Task, View, ViewContext, ViewHandle, AnyViewHandle, Entity, MutableAppContext, Quad, RenderContext, Task, View, ViewContext,
ViewHandle,
}; };
use postage::watch; use postage::watch;
use project::ProjectPath; use project::ProjectPath;
use std::{any::Any, cell::RefCell, cmp, mem, rc::Rc}; use std::{
any::{Any, TypeId},
cell::RefCell,
cmp, mem,
rc::Rc,
};
use util::ResultExt; use util::ResultExt;
action!(Split, SplitDirection); action!(Split, SplitDirection);
@ -75,6 +81,8 @@ pub struct Pane {
active_item_index: usize, active_item_index: usize,
settings: watch::Receiver<Settings>, settings: watch::Receiver<Settings>,
nav_history: Rc<RefCell<NavHistory>>, nav_history: Rc<RefCell<NavHistory>>,
toolbars: HashMap<TypeId, AnyViewHandle>,
active_toolbar: Option<AnyViewHandle>,
} }
// #[derive(Debug, Eq, PartialEq)] // #[derive(Debug, Eq, PartialEq)]
@ -120,6 +128,8 @@ impl Pane {
active_item_index: 0, active_item_index: 0,
settings, settings,
nav_history: Default::default(), nav_history: Default::default(),
toolbars: Default::default(),
active_toolbar: Default::default(),
} }
} }
@ -365,6 +375,19 @@ impl Pane {
cx.emit(Event::Split(direction)); cx.emit(Event::Split(direction));
} }
pub fn show_toolbar<F, V>(&mut self, cx: &mut ViewContext<Self>, build_toolbar: F)
where
F: FnOnce(&mut ViewContext<V>) -> V,
V: View,
{
let handle = self
.toolbars
.entry(TypeId::of::<V>())
.or_insert_with(|| cx.add_view(build_toolbar).into());
self.active_toolbar = Some(handle.clone());
cx.notify();
}
fn render_tabs(&self, cx: &mut RenderContext<Self>) -> ElementBox { fn render_tabs(&self, cx: &mut RenderContext<Self>) -> ElementBox {
let settings = self.settings.borrow(); let settings = self.settings.borrow();
let theme = &settings.theme; let theme = &settings.theme;
@ -516,6 +539,11 @@ impl View for Pane {
if let Some(active_item) = self.active_item() { if let Some(active_item) = self.active_item() {
Flex::column() Flex::column()
.with_child(self.render_tabs(cx)) .with_child(self.render_tabs(cx))
.with_children(
self.active_toolbar
.as_ref()
.map(|view| ChildView::new(view).boxed()),
)
.with_child(ChildView::new(active_item).flexible(1., true).boxed()) .with_child(ChildView::new(active_item).flexible(1., true).boxed())
.named("pane") .named("pane")
} else { } else {

View file

@ -36,6 +36,7 @@ contacts_panel = { path = "../contacts_panel" }
diagnostics = { path = "../diagnostics" } diagnostics = { path = "../diagnostics" }
editor = { path = "../editor" } editor = { path = "../editor" }
file_finder = { path = "../file_finder" } file_finder = { path = "../file_finder" }
find = { path = "../find" }
fsevent = { path = "../fsevent" } fsevent = { path = "../fsevent" }
fuzzy = { path = "../fuzzy" } fuzzy = { path = "../fuzzy" }
go_to_line = { path = "../go_to_line" } go_to_line = { path = "../go_to_line" }

View file

@ -62,6 +62,7 @@ fn main() {
outline::init(cx); outline::init(cx);
project_panel::init(cx); project_panel::init(cx);
diagnostics::init(cx); diagnostics::init(cx);
find::init(cx);
cx.spawn({ cx.spawn({
let client = client.clone(); let client = client.clone();
|cx| async move { |cx| async move {