use std::rc::Rc;
use gpui_kit::{Action, App, SharedString, Window};
use crate::{Row, SearchDoc, SearchIndex};
pub type Callback = Rc<dyn Fn(&mut Window, &mut App)>;
pub type PickCallback = Rc<dyn Fn(&SharedString, &mut Window, &mut App)>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PaletteOption {
pub value: SharedString,
pub label: SharedString,
pub hint: Option<SharedString>,
pub checked: bool,
}
impl PaletteOption {
pub fn new(value: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
Self {
value: value.into(),
label: label.into(),
hint: None,
checked: false,
}
}
pub fn hint(mut self, hint: impl Into<SharedString>) -> Self {
self.hint = Some(hint.into());
self
}
pub fn checked(mut self, checked: bool) -> Self {
self.checked = checked;
self
}
}
pub struct SubList {
pub placeholder: SharedString,
pub options: Vec<PaletteOption>,
pub on_pick: PickCallback,
}
impl SubList {
pub fn new(
placeholder: impl Into<SharedString>,
options: Vec<PaletteOption>,
on_pick: impl Fn(&SharedString, &mut Window, &mut App) + 'static,
) -> Self {
Self {
placeholder: placeholder.into(),
options,
on_pick: Rc::new(on_pick),
}
}
}
pub enum CommandRun {
Action(Box<dyn Action>),
Callback(Callback),
Sub(Rc<SubList>),
}
impl Clone for CommandRun {
fn clone(&self) -> Self {
match self {
Self::Action(action) => Self::Action(action.boxed_clone()),
Self::Callback(callback) => Self::Callback(callback.clone()),
Self::Sub(sub) => Self::Sub(sub.clone()),
}
}
}
#[derive(Clone)]
pub struct PaletteCommand {
pub label: SharedString,
pub keywords: SharedString,
pub shortcut: Option<SharedString>,
pub run: CommandRun,
}
impl PaletteCommand {
pub fn new(label: impl Into<SharedString>, action: impl Action) -> Self {
Self::with_run(label, CommandRun::Action(Box::new(action)))
}
pub fn callback(
label: impl Into<SharedString>,
run: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
Self::with_run(label, CommandRun::Callback(Rc::new(run)))
}
pub fn sub(label: impl Into<SharedString>, sub: SubList) -> Self {
Self::with_run(label, CommandRun::Sub(Rc::new(sub)))
}
fn with_run(label: impl Into<SharedString>, run: CommandRun) -> Self {
Self {
label: label.into(),
keywords: SharedString::default(),
shortcut: None,
run,
}
}
pub fn keywords(mut self, keywords: impl Into<SharedString>) -> Self {
self.keywords = keywords.into();
self
}
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
}
impl std::fmt::Debug for PaletteCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let run = match &self.run {
CommandRun::Action(action) => action.name(),
CommandRun::Callback(_) => "callback",
CommandRun::Sub(_) => "sub-list",
};
f.debug_struct("PaletteCommand")
.field("label", &self.label)
.field("run", &run)
.finish()
}
}
pub struct ItemSearch {
index: SearchIndex,
rows: Vec<Row>,
}
impl ItemSearch {
pub fn new(items: impl IntoIterator<Item = (SearchDoc, Row)>) -> Self {
let (docs, rows) = items.into_iter().unzip();
Self {
index: SearchIndex::new(docs),
rows,
}
}
pub fn search(&self, query: &str, limit: usize) -> Vec<(&SearchDoc, &Row)> {
self.index
.search(query, limit)
.into_iter()
.map(|hit| (&self.index.docs()[hit.index], &self.rows[hit.index]))
.collect()
}
pub fn find(&self, id: &str) -> Option<(&SearchDoc, &Row)> {
let at = self.index.docs().iter().position(|doc| doc.id == id)?;
Some((&self.index.docs()[at], &self.rows[at]))
}
pub fn is_empty(&self) -> bool {
self.index.is_empty()
}
}
#[derive(Clone)]
pub struct Search {
pub placeholder: SharedString,
pub items: Rc<ItemSearch>,
}