Clear highlighted matches when dismissing FindBar

This commit is contained in:
Antonio Scandurra 2022-01-31 09:58:03 +01:00
parent b1639e5677
commit 611538f6bd
2 changed files with 56 additions and 27 deletions

View file

@ -20,7 +20,7 @@ use std::{
use workspace::{ItemViewHandle, Pane, Settings, Toolbar, Workspace};
action!(Deploy, bool);
action!(Cancel);
action!(Dismiss);
action!(FocusEditor);
action!(ToggleMode, SearchMode);
action!(GoToMatch, Direction);
@ -42,7 +42,7 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_bindings([
Binding::new("cmd-f", Deploy(true), Some("Editor && mode == full")),
Binding::new("cmd-e", Deploy(false), Some("Editor && mode == full")),
Binding::new("escape", Cancel, Some("FindBar")),
Binding::new("escape", Dismiss, Some("FindBar")),
Binding::new("cmd-f", FocusEditor, Some("FindBar")),
Binding::new("enter", GoToMatch(Direction::Next), Some("FindBar")),
Binding::new("shift-enter", GoToMatch(Direction::Prev), Some("FindBar")),
@ -50,7 +50,7 @@ pub fn init(cx: &mut MutableAppContext) {
Binding::new("cmd-shift-G", GoToMatch(Direction::Prev), Some("Pane")),
]);
cx.add_action(FindBar::deploy);
cx.add_action(FindBar::cancel);
cx.add_action(FindBar::dismiss);
cx.add_action(FindBar::focus_editor);
cx.add_action(FindBar::toggle_mode);
cx.add_action(FindBar::go_to_match);
@ -157,6 +157,14 @@ impl Toolbar for FindBar {
false
}
}
fn on_dismiss(&mut self, cx: &mut ViewContext<Self>) {
self.active_editor.take();
self.active_editor_subscription.take();
self.active_match_index.take();
self.pending_search.take();
self.clear_matches(cx);
}
}
impl FindBar {
@ -312,10 +320,10 @@ impl FindBar {
});
}
fn cancel(workspace: &mut Workspace, _: &Cancel, cx: &mut ViewContext<Workspace>) {
fn dismiss(workspace: &mut Workspace, _: &Dismiss, cx: &mut ViewContext<Workspace>) {
workspace
.active_pane()
.update(cx, |pane, cx| pane.hide_toolbar(cx));
.update(cx, |pane, cx| pane.dismiss_toolbar(cx));
}
fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext<Self>) {
@ -403,16 +411,8 @@ impl FindBar {
) {
match event {
editor::Event::Edited => {
for editor in self.highlighted_editors.drain() {
if let Some(editor) = editor.upgrade(cx) {
if Some(&editor) != self.active_editor.as_ref() {
editor.update(cx, |editor, cx| {
editor.clear_highlighted_ranges::<Self>(cx)
});
}
}
}
self.query_contains_error = false;
self.clear_matches(cx);
self.update_matches(cx);
cx.notify();
}
@ -433,6 +433,16 @@ impl FindBar {
}
}
fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
for editor in self.highlighted_editors.drain() {
if let Some(editor) = editor.upgrade(cx) {
if Some(&editor) != self.active_editor.as_ref() {
editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
}
}
}
}
fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
let query = self.query_editor.read(cx).text(cx);
self.pending_search.take();

View file

@ -92,6 +92,7 @@ pub trait Toolbar: View {
item: Option<Box<dyn ItemViewHandle>>,
cx: &mut ViewContext<Self>,
) -> bool;
fn on_dismiss(&mut self, cx: &mut ViewContext<Self>);
}
trait ToolbarHandle {
@ -100,6 +101,7 @@ trait ToolbarHandle {
item: Option<Box<dyn ItemViewHandle>>,
cx: &mut MutableAppContext,
) -> bool;
fn on_dismiss(&self, cx: &mut MutableAppContext);
fn to_any(&self) -> AnyViewHandle;
}
@ -401,20 +403,31 @@ impl Pane {
V: Toolbar,
{
let type_id = TypeId::of::<V>();
let active_item = self.active_item();
self.toolbars
.entry(type_id)
.or_insert_with(|| Box::new(cx.add_view(build_toolbar)));
self.active_toolbar_type = Some(type_id);
self.active_toolbar_visible = self.toolbars[&type_id].active_item_changed(active_item, cx);
cx.notify();
if self.active_toolbar_type != Some(type_id) {
self.dismiss_toolbar(cx);
let active_item = self.active_item();
self.toolbars
.entry(type_id)
.or_insert_with(|| Box::new(cx.add_view(build_toolbar)));
self.active_toolbar_type = Some(type_id);
self.active_toolbar_visible =
self.toolbars[&type_id].active_item_changed(active_item, cx);
cx.notify();
}
}
pub fn hide_toolbar(&mut self, cx: &mut ViewContext<Self>) {
self.active_toolbar_type = None;
self.active_toolbar_visible = false;
self.focus_active_item(cx);
cx.notify();
pub fn dismiss_toolbar(&mut self, cx: &mut ViewContext<Self>) {
if let Some(active_toolbar_type) = self.active_toolbar_type.take() {
self.toolbars
.get_mut(&active_toolbar_type)
.unwrap()
.on_dismiss(cx);
self.active_toolbar_visible = false;
self.focus_active_item(cx);
cx.notify();
}
}
pub fn toolbar<T: Toolbar>(&self) -> Option<ViewHandle<T>> {
@ -437,7 +450,9 @@ impl Pane {
if let Some(type_id) = self.active_toolbar_type {
if let Some(toolbar) = self.toolbars.get(&type_id) {
self.active_toolbar_visible = toolbar.active_item_changed(
Some(self.item_views[self.active_item_index].1.clone()),
self.item_views
.get(self.active_item_index)
.map(|i| i.1.clone()),
cx,
);
}
@ -621,6 +636,10 @@ impl<T: Toolbar> ToolbarHandle for ViewHandle<T> {
self.update(cx, |this, cx| this.active_item_changed(item, cx))
}
fn on_dismiss(&self, cx: &mut MutableAppContext) {
self.update(cx, |this, cx| this.on_dismiss(cx));
}
fn to_any(&self) -> AnyViewHandle {
self.into()
}