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