use gpui_kit::{App, Global};
pub const PREFERENCE_KEY: &str = "app.agent-icons";
pub const DEFAULT_AGENT_ICONS: AgentIcons = AgentIcons::Small;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AgentIcons {
Large,
Small,
Off,
}
impl Global for AgentIcons {}
impl Default for AgentIcons {
fn default() -> Self {
DEFAULT_AGENT_ICONS
}
}
impl AgentIcons {
pub const ALL: [Self; 3] = [Self::Large, Self::Small, Self::Off];
pub fn current(cx: &App) -> Self {
cx.try_global::<Self>().copied().unwrap_or_default()
}
pub fn apply(self, cx: &mut App) {
cx.set_global(self);
cx.refresh_windows();
}
pub fn key(self) -> &'static str {
match self {
Self::Large => "large",
Self::Small => "small",
Self::Off => "off",
}
}
pub fn label(self) -> &'static str {
match self {
Self::Large => "Large",
Self::Small => "Small",
Self::Off => "Off",
}
}
pub fn from_key(value: Option<&str>) -> Self {
Self::ALL
.into_iter()
.find(|icons| Some(icons.key()) == value)
.unwrap_or_default()
}
}
pub(crate) fn initialize(cx: &mut App) {
AgentIcons::from_key(crate::prefs::get(cx, PREFERENCE_KEY).as_deref()).apply(cx);
}
#[cfg(test)]
mod tests {
use super::{AgentIcons, DEFAULT_AGENT_ICONS};
#[test]
fn a_missing_or_unknown_value_reads_as_the_default_and_each_key_round_trips() {
assert_eq!(DEFAULT_AGENT_ICONS, AgentIcons::Small);
assert_eq!(AgentIcons::from_key(None), DEFAULT_AGENT_ICONS);
assert_eq!(AgentIcons::from_key(Some("huge")), DEFAULT_AGENT_ICONS);
for icons in AgentIcons::ALL {
assert_eq!(AgentIcons::from_key(Some(icons.key())), icons);
}
let labels: Vec<_> = AgentIcons::ALL.map(AgentIcons::label).into();
assert_eq!(labels, ["Large", "Small", "Off"]);
}
}