use std::{
cell::{Cell, RefCell},
sync::Arc,
};
use gpui_kit::component::list::{List, ListDelegate, ListEvent, ListItem, ListState};
use gpui_kit::component::{ActiveTheme as _, IndexPath, Sizable as _, Size as ComponentSize};
use gpui_kit::{
AnyElement, App, AppContext as _, Context, Entity, IntoElement, ParentElement as _,
SharedString, Styled as _, Subscription, Task, Window, div, px,
};
use rust_i18n::t;
use crate::{
actions::CommitEdit,
model::{CellValue, GridRow, OwnedValue, display_text},
state::edit::{
CellEditor, EditError, EditInput, EditorCell, EditorField, EditorOpen, EditorStart,
},
};
#[derive(Clone, Debug)]
pub struct SelectOption {
value: OwnedValue,
label: SharedString,
}
impl SelectOption {
pub fn new(value: OwnedValue, label: impl Into<SharedString>) -> Self {
Self {
value,
label: label.into(),
}
}
pub fn value(&self) -> &OwnedValue {
&self.value
}
pub fn label(&self) -> &SharedString {
&self.label
}
}
#[derive(Clone, Debug)]
pub struct SelectEditor {
items: Arc<[SelectOption]>,
has_empty: bool,
}
impl SelectEditor {
pub fn new(options: impl IntoIterator<Item = SelectOption>) -> Self {
Self {
items: options.into_iter().collect(),
has_empty: false,
}
}
pub fn with_empty(self, label: impl Into<SharedString>) -> Self {
let empty = SelectOption::new(OwnedValue::Empty, label);
let options = if self.has_empty {
&self.items[1..]
} else {
&self.items[..]
};
Self {
items: std::iter::once(empty)
.chain(options.iter().cloned())
.collect(),
has_empty: true,
}
}
pub fn options(&self) -> &[SelectOption] {
if self.has_empty {
&self.items[1..]
} else {
&self.items
}
}
pub fn empty_label(&self) -> Option<&SharedString> {
self.has_empty.then(|| self.items[0].label())
}
fn picked(&self, value: CellValue<'_>, input: &EditInput) -> Option<Option<usize>> {
match input {
EditInput::Clear => self.has_empty.then_some(Some(0)),
_ => {
let value = OwnedValue::from_cell(value);
Some(item_of(&self.items, &value))
}
}
}
}
fn item_of(items: &[SelectOption], value: &OwnedValue) -> Option<usize> {
items
.iter()
.position(|item| item.value.cmp_total(value).is_eq())
}
impl<R: GridRow> CellEditor<R> for SelectEditor {
fn open(&self, start: &EditorStart<'_, R>, window: &mut Window, cx: &mut App) -> EditorOpen {
let Some(picked) = self.picked(start.value(), start.input()) else {
return EditorOpen::Refuse;
};
let options = OptionList {
matches: (0..self.items.len()).collect(),
items: self.items.clone(),
};
let list = cx.new(|cx| ListState::new(options, window, cx).searchable(true));
let query = match start.input() {
EditInput::Typing(text) => Some(text.clone()),
_ => None,
};
list.update(cx, |list, cx| match start.input() {
EditInput::Typing(text) => {
list.delegate_mut().filter(text);
let first = (!list.delegate().matches.is_empty()).then(|| IndexPath::new(0));
list.set_selected_index(first, window, cx);
}
_ => {
let row = picked.map(IndexPath::new);
list.set_selected_index(row, window, cx);
if let Some(row) = row {
list.scroll_to_item(row, gpui_kit::ScrollStrategy::Center, window, cx);
}
}
});
let confirm = window.subscribe(&list, cx, |_, event: &ListEvent, window, cx| {
if let ListEvent::Confirm(_) = event {
window.dispatch_action(Box::new(CommitEdit), cx);
}
});
EditorOpen::Field(Box::new(SelectField {
list,
start: RefCell::new(OwnedValue::from_cell(start.value())),
initial: Cell::new(picked),
query: RefCell::new(query),
drawn: Cell::new(false),
_confirm: confirm,
}))
}
}
struct OptionList {
items: Arc<[SelectOption]>,
matches: Vec<usize>,
}
impl OptionList {
fn filter(&mut self, query: &str) {
let query = query.to_lowercase();
self.matches = (0..self.items.len())
.filter(|ix| query.is_empty() || self.items[*ix].label.to_lowercase().contains(&query))
.collect();
}
}
impl ListDelegate for OptionList {
type Item = ListItem;
fn perform_search(
&mut self,
query: &str,
_: &mut Window,
_: &mut Context<ListState<Self>>,
) -> Task<()> {
self.filter(query);
Task::ready(())
}
fn items_count(&self, _: usize, _: &App) -> usize {
self.matches.len()
}
fn render_item(
&mut self,
ix: IndexPath,
_: &mut Window,
_: &mut Context<ListState<Self>>,
) -> Option<ListItem> {
let item = &self.items[*self.matches.get(ix.row)?];
Some(
ListItem::new(ix.row)
.accessibility_label(item.label.clone())
.child(item.label.clone()),
)
}
fn render_empty(
&mut self,
_: &mut Window,
cx: &mut Context<ListState<Self>>,
) -> impl IntoElement {
div()
.px(px(8.))
.py(px(6.))
.text_sm()
.text_color(cx.theme().muted_foreground)
.child(SharedString::from(t!("live_grid.edit.no_matches")))
}
fn set_selected_index(
&mut self,
_: Option<IndexPath>,
_: &mut Window,
_: &mut Context<ListState<Self>>,
) {
}
}
struct SelectField {
list: Entity<ListState<OptionList>>,
start: RefCell<OwnedValue>,
initial: Cell<Option<usize>>,
query: RefCell<Option<SharedString>>,
drawn: Cell<bool>,
_confirm: Subscription,
}
impl SelectField {
fn picked(&self, cx: &App) -> Option<usize> {
let list = self.list.read(cx);
let row = list.selected_index()?.row;
list.delegate().matches.get(row).copied()
}
fn picked_item(&self, cx: &App) -> Option<SelectOption> {
let ix = self.picked(cx)?;
Some(self.list.read(cx).delegate().items[ix].clone())
}
}
impl EditorField for SelectField {
fn render(&self, cell: &EditorCell<'_>, window: &mut Window, cx: &mut App) -> AnyElement {
if self.query.borrow().is_some() {
if self.drawn.replace(true) {
let query = self.query.take().unwrap_or_default();
self.list
.update(cx, |list, cx| list.set_query(&query, window, cx));
} else {
self.list.update(cx, |_, cx| cx.notify());
}
}
let text = self.picked_item(cx).map_or_else(
|| {
let mut text = String::new();
display_text(self.start.borrow().as_cell(), &mut text).to_owned()
},
|item| item.label.to_string(),
);
let size = popup_size(cell.size());
let width = cell.bounds().size.width.max(px(160.));
div()
.flex_1()
.min_w_0()
.px(px(4.))
.truncate()
.child(text)
.child(
cell.popup(
div().w(width).child(
List::new(&self.list)
.search_placeholder(SharedString::from(t!("live_grid.edit.search")))
.with_size(size)
.max_h(px(240.))
.p(px(4.)),
),
cx,
),
)
.into_any_element()
}
fn focus(&self, window: &mut Window, cx: &mut App) {
self.list.update(cx, |list, cx| list.focus(window, cx));
}
fn value(&self, cx: &App) -> Result<OwnedValue, EditError> {
Ok(self
.picked_item(cx)
.map_or_else(|| self.start.borrow().clone(), |item| item.value))
}
fn set_value(&self, value: &OwnedValue, window: &mut Window, cx: &mut App) {
self.list.update(cx, |list, cx| {
let row = list
.delegate()
.matches
.iter()
.position(|ix| list.delegate().items[*ix].value.cmp_total(value).is_eq());
list.set_selected_index(row.map(IndexPath::new), window, cx);
cx.notify();
});
*self.start.borrow_mut() = value.clone();
self.initial.set(self.picked(cx));
}
fn is_dirty(&self, cx: &App) -> bool {
self.picked(cx) != self.initial.get()
}
}
fn popup_size(size: ComponentSize) -> ComponentSize {
match size {
ComponentSize::XSmall => ComponentSize::Small,
size => size,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sides() -> SelectEditor {
SelectEditor::new([
SelectOption::new(OwnedValue::Text("Buy".into()), "Buy"),
SelectOption::new(OwnedValue::Text("Sell".into()), "Sell"),
])
}
#[test]
fn test_select_picked() {
let editor = sides();
assert_eq!(editor.options().len(), 2);
assert_eq!(editor.empty_label(), None);
assert_eq!(
editor.picked(CellValue::Text("Sell"), &EditInput::Command),
Some(Some(1))
);
assert_eq!(
editor.picked(CellValue::Text("Hold"), &EditInput::Command),
Some(None)
);
assert_eq!(
editor.picked(CellValue::Text("Sell"), &EditInput::Clear),
None
);
let editor = sides().with_empty("None").with_empty("—");
assert_eq!(editor.options().len(), 2, "one empty item");
assert_eq!(editor.empty_label().map(AsRef::as_ref), Some("—"));
assert_eq!(
editor.picked(CellValue::Text("Sell"), &EditInput::Clear),
Some(Some(0))
);
assert_eq!(
editor.picked(CellValue::Empty, &EditInput::Command),
Some(Some(0))
);
assert_eq!(
editor.picked(CellValue::Text("Buy"), &EditInput::Press { click_count: 2 }),
Some(Some(1))
);
}
}