Make all pickers respect mouse clicks
This commit is contained in:
parent
c7527f92a4
commit
20657566b3
3 changed files with 45 additions and 36 deletions
|
@ -1,7 +1,7 @@
|
||||||
use fuzzy::PathMatch;
|
use fuzzy::PathMatch;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, elements::*, impl_internal_actions, AppContext, Entity, ModelHandle,
|
actions, elements::*, AppContext, Entity, ModelHandle, MutableAppContext, RenderContext, Task,
|
||||||
MutableAppContext, RenderContext, Task, View, ViewContext, ViewHandle,
|
View, ViewContext, ViewHandle,
|
||||||
};
|
};
|
||||||
use picker::{Picker, PickerDelegate};
|
use picker::{Picker, PickerDelegate};
|
||||||
use project::{Project, ProjectPath, WorktreeId};
|
use project::{Project, ProjectPath, WorktreeId};
|
||||||
|
@ -28,11 +28,7 @@ pub struct FileFinder {
|
||||||
cancel_flag: Arc<AtomicBool>,
|
cancel_flag: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Select(pub ProjectPath);
|
|
||||||
|
|
||||||
actions!(file_finder, [Toggle]);
|
actions!(file_finder, [Toggle]);
|
||||||
impl_internal_actions!(file_finder, [Select]);
|
|
||||||
|
|
||||||
pub fn init(cx: &mut MutableAppContext) {
|
pub fn init(cx: &mut MutableAppContext) {
|
||||||
cx.add_action(FileFinder::toggle);
|
cx.add_action(FileFinder::toggle);
|
||||||
|
@ -240,33 +236,21 @@ impl PickerDelegate for FileFinder {
|
||||||
};
|
};
|
||||||
let (file_name, file_name_positions, full_path, full_path_positions) =
|
let (file_name, file_name_positions, full_path, full_path_positions) =
|
||||||
self.labels_for_match(path_match);
|
self.labels_for_match(path_match);
|
||||||
let action = Select(ProjectPath {
|
Flex::column()
|
||||||
worktree_id: WorktreeId::from_usize(path_match.worktree_id),
|
.with_child(
|
||||||
path: path_match.path.clone(),
|
Label::new(file_name.to_string(), style.label.clone())
|
||||||
});
|
.with_highlights(file_name_positions)
|
||||||
|
.boxed(),
|
||||||
EventHandler::new(
|
)
|
||||||
Flex::column()
|
.with_child(
|
||||||
.with_child(
|
Label::new(full_path, style.label.clone())
|
||||||
Label::new(file_name.to_string(), style.label.clone())
|
.with_highlights(full_path_positions)
|
||||||
.with_highlights(file_name_positions)
|
.boxed(),
|
||||||
.boxed(),
|
)
|
||||||
)
|
.flex(1., false)
|
||||||
.with_child(
|
.contained()
|
||||||
Label::new(full_path, style.label.clone())
|
.with_style(style.container)
|
||||||
.with_highlights(full_path_positions)
|
.named("match")
|
||||||
.boxed(),
|
|
||||||
)
|
|
||||||
.flex(1., false)
|
|
||||||
.contained()
|
|
||||||
.with_style(style.container)
|
|
||||||
.boxed(),
|
|
||||||
)
|
|
||||||
.on_mouse_down(move |cx| {
|
|
||||||
cx.dispatch_action(action.clone());
|
|
||||||
true
|
|
||||||
})
|
|
||||||
.named("match")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use editor::Editor;
|
use editor::Editor;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
elements::{
|
elements::{
|
||||||
ChildView, Flex, FlexItem, Label, ParentElement, ScrollTarget, UniformList,
|
ChildView, EventHandler, Flex, FlexItem, Label, ParentElement, ScrollTarget, UniformList,
|
||||||
UniformListState,
|
UniformListState,
|
||||||
},
|
},
|
||||||
keymap, AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, Task,
|
keymap, AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, Task,
|
||||||
|
@ -9,7 +9,9 @@ use gpui::{
|
||||||
};
|
};
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use workspace::menu::{Cancel, Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev};
|
use workspace::menu::{
|
||||||
|
Cancel, Confirm, SelectFirst, SelectIndex, SelectLast, SelectNext, SelectPrev,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Picker<D: PickerDelegate> {
|
pub struct Picker<D: PickerDelegate> {
|
||||||
delegate: WeakViewHandle<D>,
|
delegate: WeakViewHandle<D>,
|
||||||
|
@ -78,6 +80,7 @@ impl<D: PickerDelegate> Picker<D> {
|
||||||
cx.add_action(Self::select_last);
|
cx.add_action(Self::select_last);
|
||||||
cx.add_action(Self::select_next);
|
cx.add_action(Self::select_next);
|
||||||
cx.add_action(Self::select_prev);
|
cx.add_action(Self::select_prev);
|
||||||
|
cx.add_action(Self::select_index);
|
||||||
cx.add_action(Self::confirm);
|
cx.add_action(Self::confirm);
|
||||||
cx.add_action(Self::cancel);
|
cx.add_action(Self::cancel);
|
||||||
}
|
}
|
||||||
|
@ -126,7 +129,14 @@ impl<D: PickerDelegate> Picker<D> {
|
||||||
let delegate = delegate.read(cx);
|
let delegate = delegate.read(cx);
|
||||||
let selected_ix = delegate.selected_index();
|
let selected_ix = delegate.selected_index();
|
||||||
range.end = cmp::min(range.end, delegate.match_count());
|
range.end = cmp::min(range.end, delegate.match_count());
|
||||||
items.extend(range.map(move |ix| delegate.render_match(ix, ix == selected_ix, cx)));
|
items.extend(range.map(move |ix| {
|
||||||
|
EventHandler::new(delegate.render_match(ix, ix == selected_ix, cx))
|
||||||
|
.on_mouse_down(move |cx| {
|
||||||
|
cx.dispatch_action(SelectIndex(ix));
|
||||||
|
true
|
||||||
|
})
|
||||||
|
.boxed()
|
||||||
|
}));
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.contained()
|
.contained()
|
||||||
|
@ -181,6 +191,16 @@ impl<D: PickerDelegate> Picker<D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn select_index(&mut self, action: &SelectIndex, cx: &mut ViewContext<Self>) {
|
||||||
|
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||||
|
let index = action.0;
|
||||||
|
delegate.update(cx, |delegate, cx| {
|
||||||
|
delegate.set_selected_index(index, cx);
|
||||||
|
delegate.confirm(cx);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
|
pub fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
|
||||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||||
let index = delegate.update(cx, |delegate, cx| {
|
let index = delegate.update(cx, |delegate, cx| {
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SelectIndex(pub usize);
|
||||||
|
|
||||||
gpui::actions!(
|
gpui::actions!(
|
||||||
menu,
|
menu,
|
||||||
[
|
[
|
||||||
|
@ -9,3 +12,5 @@ gpui::actions!(
|
||||||
SelectLast
|
SelectLast
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
gpui::impl_internal_actions!(menu, [SelectIndex]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue