1use std::str::FromStr;
2use std::sync::OnceLock;
3
4use crate::stories::*;
5use anyhow::anyhow;
6use clap::builder::PossibleValue;
7use clap::ValueEnum;
8use gpui::{AnyView, VisualContext};
9use strum::{EnumIter, EnumString, IntoEnumIterator};
10use ui::prelude::*;
11
12#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display, EnumString, EnumIter)]
13#[strum(serialize_all = "snake_case")]
14pub enum ComponentStory {
15 AutoHeightEditor,
16 Avatar,
17 Button,
18 Checkbox,
19 ContextMenu,
20 Cursor,
21 Disclosure,
22 Focus,
23 Icon,
24 IconButton,
25 Keybinding,
26 Label,
27 List,
28 ListHeader,
29 ListItem,
30 Scroll,
31 Tab,
32 Text,
33 ZIndex,
34 Picker,
35}
36
37impl ComponentStory {
38 pub fn story(&self, cx: &mut WindowContext) -> AnyView {
39 match self {
40 Self::AutoHeightEditor => AutoHeightEditorStory::new(cx).into(),
41 Self::Avatar => cx.build_view(|_| ui::AvatarStory).into(),
42 Self::Button => cx.build_view(|_| ui::ButtonStory).into(),
43 Self::Checkbox => cx.build_view(|_| ui::CheckboxStory).into(),
44 Self::ContextMenu => cx.build_view(|_| ui::ContextMenuStory).into(),
45 Self::Cursor => cx.build_view(|_| crate::stories::CursorStory).into(),
46 Self::Disclosure => cx.build_view(|_| ui::DisclosureStory).into(),
47 Self::Focus => FocusStory::view(cx).into(),
48 Self::Icon => cx.build_view(|_| ui::IconStory).into(),
49 Self::IconButton => cx.build_view(|_| ui::IconButtonStory).into(),
50 Self::Keybinding => cx.build_view(|_| ui::KeybindingStory).into(),
51 Self::Label => cx.build_view(|_| ui::LabelStory).into(),
52 Self::List => cx.build_view(|_| ui::ListStory).into(),
53 Self::ListHeader => cx.build_view(|_| ui::ListHeaderStory).into(),
54 Self::ListItem => cx.build_view(|_| ui::ListItemStory).into(),
55 Self::Scroll => ScrollStory::view(cx).into(),
56 Self::Text => TextStory::view(cx).into(),
57 Self::Tab => cx.build_view(|_| ui::TabStory).into(),
58 Self::ZIndex => cx.build_view(|_| ZIndexStory).into(),
59 Self::Picker => PickerStory::new(cx).into(),
60 }
61 }
62}
63
64#[derive(Debug, PartialEq, Eq, Clone, Copy)]
65pub enum StorySelector {
66 Component(ComponentStory),
67 KitchenSink,
68}
69
70impl FromStr for StorySelector {
71 type Err = anyhow::Error;
72
73 fn from_str(raw_story_name: &str) -> std::result::Result<Self, Self::Err> {
74 use anyhow::Context;
75
76 let story = raw_story_name.to_ascii_lowercase();
77
78 if story == "kitchen_sink" {
79 return Ok(Self::KitchenSink);
80 }
81
82 if let Some((_, story)) = story.split_once("components/") {
83 let component_story = ComponentStory::from_str(story)
84 .with_context(|| format!("story not found for component '{story}'"))?;
85
86 return Ok(Self::Component(component_story));
87 }
88
89 Err(anyhow!("story not found for '{raw_story_name}'"))
90 }
91}
92
93impl StorySelector {
94 pub fn story(&self, cx: &mut WindowContext) -> AnyView {
95 match self {
96 Self::Component(component_story) => component_story.story(cx),
97 Self::KitchenSink => KitchenSinkStory::view(cx).into(),
98 }
99 }
100}
101
102/// The list of all stories available in the storybook.
103static ALL_STORY_SELECTORS: OnceLock<Vec<StorySelector>> = OnceLock::new();
104
105impl ValueEnum for StorySelector {
106 fn value_variants<'a>() -> &'a [Self] {
107 let stories = ALL_STORY_SELECTORS.get_or_init(|| {
108 let component_stories = ComponentStory::iter().map(StorySelector::Component);
109
110 component_stories
111 .chain(std::iter::once(StorySelector::KitchenSink))
112 .collect::<Vec<_>>()
113 });
114
115 stories
116 }
117
118 fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
119 let value = match self {
120 Self::Component(story) => format!("components/{story}"),
121 Self::KitchenSink => "kitchen_sink".to_string(),
122 };
123
124 Some(PossibleValue::new(value))
125 }
126}