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