use super::*;
pub trait InfiniteSelectValue: Sized + Clone + Default + PartialEq + 'static {
fn variants() -> Vec<Self>;
fn variant_name(&self) -> &'static str;
fn variant_key(&self) -> &'static str;
fn variant_label(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
fn has_inner(&self) -> bool;
fn child_variant_names(&self) -> Vec<&'static str>;
fn child_variant_keys(&self) -> Vec<&'static str>;
fn child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString>;
fn set_child_by_index(&self, index: usize) -> Option<Self>;
fn set_child_by_key(&self, key: &str) -> Option<Self>;
fn set_child_by_path(&self, path: &[usize]) -> Option<Self>;
fn set_child_by_key_path(&self, path: &[String]) -> Option<Self>;
fn child_depth(&self) -> usize;
fn depth() -> usize;
fn selection_path(&self) -> InfiniteSelectPath;
fn selection_key_path(&self) -> InfiniteSelectKeyPath;
fn inner_child_variant_names(&self) -> Vec<&'static str>;
fn inner_child_variant_keys(&self) -> Vec<&'static str>;
fn inner_child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString>;
fn inner_set_child_by_index(&self, index: usize) -> Option<Self>;
fn inner_set_child_by_key(&self, key: &str) -> Option<Self>;
fn inner_has_inner(&self) -> bool;
fn type_label(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
fn type_description(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
fn child_label_at_depth(
&self,
depth: usize,
cx: &impl std::borrow::Borrow<App>,
) -> Option<SharedString>;
fn child_description_at_depth(
&self,
depth: usize,
cx: &impl std::borrow::Borrow<App>,
) -> Option<SharedString>;
fn inner_child_label_at_depth(
&self,
depth: usize,
cx: &impl std::borrow::Borrow<App>,
) -> Option<SharedString>;
fn inner_child_description_at_depth(
&self,
depth: usize,
cx: &impl std::borrow::Borrow<App>,
) -> Option<SharedString>;
}
#[derive(Clone)]
pub struct InfiniteSelectItem<T: InfiniteSelectValue> {
value: T,
title: SharedString,
}
impl<T: InfiniteSelectValue> InfiniteSelectItem<T> {
pub fn new(value: T, title: impl Into<SharedString>) -> Self {
Self {
value,
title: title.into(),
}
}
pub fn from_variant(value: T, cx: &impl std::borrow::Borrow<App>) -> Self {
let title = value.variant_label(cx);
Self { value, title }
}
pub fn get_value(&self) -> &T {
&self.value
}
pub fn into_value(self) -> T {
self.value
}
pub fn has_inner(&self) -> bool {
self.value.has_inner()
}
pub fn child_variant_names(&self) -> Vec<&'static str> {
self.value.child_variant_names()
}
pub fn child_variant_keys(&self) -> Vec<&'static str> {
self.value.child_variant_keys()
}
pub fn child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString> {
self.value.child_variant_labels(cx)
}
pub fn with_child_at(&self, index: usize, cx: &impl std::borrow::Borrow<App>) -> Option<Self> {
let title = self.value.child_variant_labels(cx).get(index).cloned()?;
self.value
.set_child_by_index(index)
.map(|value| Self::new(value, title))
}
}
impl<T: InfiniteSelectValue> SelectItem for InfiniteSelectItem<T> {
type Value = T;
fn title(&self) -> SharedString {
self.title.clone()
}
fn value(&self) -> &Self::Value {
&self.value
}
}
pub fn to_select_items<T>(cx: &impl std::borrow::Borrow<App>) -> Vec<InfiniteSelectItem<T>>
where
T: InfiniteSelectValue,
{
T::variants()
.into_iter()
.map(|value| InfiniteSelectItem::from_variant(value, cx))
.collect()
}