directory_context_picker.rs

  1// TODO: Remove this once we've implemented the functionality.
  2#![allow(unused)]
  3
  4use std::sync::Arc;
  5
  6use fuzzy::PathMatch;
  7use gpui::{AppContext, DismissEvent, FocusHandle, FocusableView, Task, View, WeakModel, WeakView};
  8use picker::{Picker, PickerDelegate};
  9use project::{PathMatchCandidateSet, WorktreeId};
 10use ui::{prelude::*, ListItem};
 11use util::ResultExt as _;
 12use workspace::Workspace;
 13
 14use crate::context_picker::{ConfirmBehavior, ContextPicker};
 15use crate::context_store::ContextStore;
 16
 17pub struct DirectoryContextPicker {
 18    picker: View<Picker<DirectoryContextPickerDelegate>>,
 19}
 20
 21impl DirectoryContextPicker {
 22    pub fn new(
 23        context_picker: WeakView<ContextPicker>,
 24        workspace: WeakView<Workspace>,
 25        context_store: WeakModel<ContextStore>,
 26        confirm_behavior: ConfirmBehavior,
 27        cx: &mut ViewContext<Self>,
 28    ) -> Self {
 29        let delegate = DirectoryContextPickerDelegate::new(
 30            context_picker,
 31            workspace,
 32            context_store,
 33            confirm_behavior,
 34        );
 35        let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
 36
 37        Self { picker }
 38    }
 39}
 40
 41impl FocusableView for DirectoryContextPicker {
 42    fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
 43        self.picker.focus_handle(cx)
 44    }
 45}
 46
 47impl Render for DirectoryContextPicker {
 48    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
 49        self.picker.clone()
 50    }
 51}
 52
 53pub struct DirectoryContextPickerDelegate {
 54    context_picker: WeakView<ContextPicker>,
 55    workspace: WeakView<Workspace>,
 56    context_store: WeakModel<ContextStore>,
 57    confirm_behavior: ConfirmBehavior,
 58    matches: Vec<PathMatch>,
 59    selected_index: usize,
 60}
 61
 62impl DirectoryContextPickerDelegate {
 63    pub fn new(
 64        context_picker: WeakView<ContextPicker>,
 65        workspace: WeakView<Workspace>,
 66        context_store: WeakModel<ContextStore>,
 67        confirm_behavior: ConfirmBehavior,
 68    ) -> Self {
 69        Self {
 70            context_picker,
 71            workspace,
 72            context_store,
 73            confirm_behavior,
 74            matches: Vec::new(),
 75            selected_index: 0,
 76        }
 77    }
 78}
 79
 80impl PickerDelegate for DirectoryContextPickerDelegate {
 81    type ListItem = ListItem;
 82
 83    fn match_count(&self) -> usize {
 84        self.matches.len()
 85    }
 86
 87    fn selected_index(&self) -> usize {
 88        self.selected_index
 89    }
 90
 91    fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>) {
 92        self.selected_index = ix;
 93    }
 94
 95    fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
 96        "Search folders…".into()
 97    }
 98
 99    fn update_matches(&mut self, _query: String, _cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
100        // TODO: Implement this once we fix the issues with the file context picker.
101        Task::ready(())
102    }
103
104    fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
105        // TODO: Implement this once we fix the issues with the file context picker.
106        match self.confirm_behavior {
107            ConfirmBehavior::KeepOpen => {}
108            ConfirmBehavior::Close => self.dismissed(cx),
109        }
110    }
111
112    fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
113        self.context_picker
114            .update(cx, |this, cx| {
115                this.reset_mode();
116                cx.emit(DismissEvent);
117            })
118            .ok();
119    }
120
121    fn render_match(
122        &self,
123        _ix: usize,
124        _selected: bool,
125        _cx: &mut ViewContext<Picker<Self>>,
126    ) -> Option<Self::ListItem> {
127        None
128    }
129}