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