use crate::icons::CarbonIcon;
use gpui_kit::{AnyElement, SharedString};
use std::rc::Rc;
pub type PickerMark = Rc<dyn Fn(f32) -> AnyElement>;
#[derive(Clone)]
pub struct PickerModel {
pub id: SharedString,
pub name: SharedString,
pub description: Option<SharedString>,
pub badge: Option<SharedString>,
pub mark: Option<PickerMark>,
pub chosen: bool,
pub favourite: Option<bool>,
pub also: Option<bool>,
pub key: Option<SharedString>,
pub disabled: bool,
}
impl PickerModel {
pub fn new(id: impl Into<SharedString>, name: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
name: name.into(),
description: None,
badge: None,
mark: None,
chosen: false,
favourite: None,
also: None,
key: None,
disabled: false,
}
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
pub fn badge(mut self, badge: impl Into<SharedString>) -> Self {
self.badge = Some(badge.into());
self
}
pub fn mark(mut self, mark: PickerMark) -> Self {
self.mark = Some(mark);
self
}
pub fn chosen(mut self, chosen: bool) -> Self {
self.chosen = chosen;
self
}
pub fn favourite(mut self, on: bool) -> Self {
self.favourite = Some(on);
self
}
pub fn also(mut self, on: bool) -> Self {
self.also = Some(on);
self
}
pub fn key(mut self, key: impl Into<SharedString>) -> Self {
self.key = Some(key.into());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
#[derive(Clone)]
pub struct PickerProvider {
pub id: SharedString,
pub name: SharedString,
pub mark: Option<PickerMark>,
pub icon: Option<CarbonIcon>,
pub starting: bool,
pub notice: Option<SharedString>,
pub notice_action: Option<SharedString>,
pub notice_short: Option<SharedString>,
pub tooltip: Option<SharedString>,
pub disabled: bool,
}
impl PickerProvider {
pub fn new(id: impl Into<SharedString>, name: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
name: name.into(),
mark: None,
icon: None,
starting: false,
notice: None,
notice_action: None,
notice_short: None,
tooltip: None,
disabled: false,
}
}
pub fn mark(mut self, mark: PickerMark) -> Self {
self.mark = Some(mark);
self
}
pub fn icon(mut self, icon: CarbonIcon) -> Self {
self.icon = Some(icon);
self
}
pub fn starting(mut self, starting: bool) -> Self {
self.starting = starting;
self
}
pub fn notice(mut self, notice: impl Into<SharedString>) -> Self {
self.notice = Some(notice.into());
self
}
pub fn notice_action(mut self, label: impl Into<SharedString>) -> Self {
self.notice_action = Some(label.into());
self
}
pub fn notice_short(mut self, note: impl Into<SharedString>) -> Self {
self.notice_short = Some(note.into());
self
}
pub fn tooltip(mut self, tip: impl Into<SharedString>) -> Self {
self.tooltip = Some(tip.into());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub(super) fn note(&self, texts: &ModelPickerTexts) -> Option<SharedString> {
self.notice_short
.clone()
.or_else(|| self.notice.as_ref().map(|_| texts.needs_setup.clone()))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PickerChoice {
pub value: SharedString,
pub name: SharedString,
pub on: bool,
}
impl PickerChoice {
pub fn new(value: impl Into<SharedString>, name: impl Into<SharedString>, on: bool) -> Self {
Self {
value: value.into(),
name: name.into(),
on,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PickerOption {
pub id: SharedString,
pub name: SharedString,
pub choices: Vec<PickerChoice>,
}
impl PickerOption {
pub fn new(
id: impl Into<SharedString>,
name: impl Into<SharedString>,
choices: Vec<PickerChoice>,
) -> Self {
Self {
id: id.into(),
name: name.into(),
choices,
}
}
pub(super) fn current(&self) -> Option<SharedString> {
self.choices.iter().find(|c| c.on).map(|c| c.name.clone())
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PickerSwitch {
pub label: SharedString,
pub on: bool,
}
#[derive(Clone)]
pub struct ModelPickerTexts {
pub needs_setup: SharedString,
pub settings: SharedString,
pub search: Rc<dyn Fn(&str) -> SharedString>,
pub speed: SharedString,
pub empty: SharedString,
pub add_favourite: SharedString,
pub remove_favourite: SharedString,
pub also: SharedString,
}
impl Default for ModelPickerTexts {
fn default() -> Self {
Self {
needs_setup: "Needs setup".into(),
settings: "Agent settings".into(),
search: Rc::new(|p| format!("Search {p} models").into()),
speed: "Speed".into(),
empty: "No models".into(),
add_favourite: "Add to favourites".into(),
remove_favourite: "Remove from favourites".into(),
also: "Also run on".into(),
}
}
}
pub fn filter_models<'a>(models: &'a [PickerModel], query: &str) -> Vec<&'a PickerModel> {
let q = query.to_lowercase();
models
.iter()
.filter(|m| m.name.to_lowercase().contains(&q))
.collect()
}
pub const MODEL_PICKER_GEOMETRY: (f32, f32, f32, f32) = (176., 160., 736., 352.);
pub const MODEL_PICKER_MIN_LIST: f32 = 280.;
pub const MODEL_PICKER_NARROW_SIDE: f32 = 48.;
pub fn narrow_tabs(width: f32, options: bool) -> bool {
let (side, opts, ..) = MODEL_PICKER_GEOMETRY;
width < side + if options { opts } else { 0. } + MODEL_PICKER_MIN_LIST
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn search_filters_by_name() {
let models = vec![
PickerModel::new("opus", "Opus"),
PickerModel::new("sonnet", "Sonnet"),
];
let hits = filter_models(&models, "SON");
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].id, "sonnet");
assert_eq!(filter_models(&models, "").len(), 2);
}
#[test]
fn the_tabs_narrow_to_their_marks_only_where_the_models_would_be_crowded() {
let (side, opts, full, _) = MODEL_PICKER_GEOMETRY;
assert!(!narrow_tabs(full, true));
assert!(!narrow_tabs(side + opts + MODEL_PICKER_MIN_LIST, true));
assert!(narrow_tabs(side + opts + MODEL_PICKER_MIN_LIST - 1., true));
assert!(!narrow_tabs(side + MODEL_PICKER_MIN_LIST, false));
}
#[test]
fn a_tabs_note_is_its_own_else_needs_setup_where_it_has_a_notice() {
let texts = ModelPickerTexts::default();
assert_eq!(PickerProvider::new("a", "A").note(&texts), None);
let notice = PickerProvider::new("a", "A").notice("Sign in");
assert_eq!(notice.note(&texts).as_deref(), Some("Needs setup"));
let own = PickerProvider::new("a", "A").notice_short("Starting");
assert_eq!(own.note(&texts).as_deref(), Some("Starting"));
}
#[test]
fn an_options_current_value_is_the_choice_its_agent_reports() {
let o = PickerOption::new(
"mode",
"Mode",
vec![
PickerChoice::new("a", "Ask", false),
PickerChoice::new("b", "Auto", true),
],
);
assert_eq!(o.current().as_deref(), Some("Auto"));
let none = PickerOption::new("m", "M", vec![PickerChoice::new("a", "A", false)]);
assert_eq!(none.current(), None);
}
}