story_selector.rs

  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    CollabNotification,
 18    ContextMenu,
 19    Cursor,
 20    DefaultColors,
 21    Disclosure,
 22    Focus,
 23    Icon,
 24    IconButton,
 25    Keybinding,
 26    List,
 27    ListHeader,
 28    ListItem,
 29    OverflowScroll,
 30    Picker,
 31    Scroll,
 32    Tab,
 33    TabBar,
 34    Text,
 35    ToggleButton,
 36    ViewportUnits,
 37    WithRemSize,
 38    Vector,
 39}
 40
 41impl ComponentStory {
 42    pub fn story(&self, window: &mut Window, cx: &mut App) -> AnyView {
 43        match self {
 44            Self::ApplicationMenu => cx
 45                .new(|cx| title_bar::ApplicationMenuStory::new(window, cx))
 46                .into(),
 47            Self::AutoHeightEditor => AutoHeightEditorStory::new(window, cx).into(),
 48            Self::CollabNotification => cx
 49                .new(|_| collab_ui::notifications::CollabNotificationStory)
 50                .into(),
 51            Self::ContextMenu => cx.new(|_| ui::ContextMenuStory).into(),
 52            Self::Cursor => cx.new(|_| crate::stories::CursorStory).into(),
 53            Self::DefaultColors => DefaultColorsStory::model(cx).into(),
 54            Self::Disclosure => cx.new(|_| ui::DisclosureStory).into(),
 55            Self::Focus => FocusStory::model(window, cx).into(),
 56            Self::Icon => cx.new(|_| ui::IconStory).into(),
 57            Self::IconButton => cx.new(|_| ui::IconButtonStory).into(),
 58            Self::Keybinding => cx.new(|_| ui::KeybindingStory).into(),
 59            Self::List => cx.new(|_| ui::ListStory).into(),
 60            Self::ListHeader => cx.new(|_| ui::ListHeaderStory).into(),
 61            Self::ListItem => cx.new(|_| ui::ListItemStory).into(),
 62            Self::OverflowScroll => cx.new(|_| crate::stories::OverflowScrollStory).into(),
 63            Self::Picker => PickerStory::new(window, cx).into(),
 64            Self::Scroll => ScrollStory::model(cx).into(),
 65            Self::Tab => cx.new(|_| ui::TabStory).into(),
 66            Self::TabBar => cx.new(|_| ui::TabBarStory).into(),
 67            Self::Text => TextStory::model(cx).into(),
 68            Self::ToggleButton => cx.new(|_| ui::ToggleButtonStory).into(),
 69            Self::ViewportUnits => cx.new(|_| crate::stories::ViewportUnitsStory).into(),
 70            Self::WithRemSize => cx.new(|_| crate::stories::WithRemSizeStory).into(),
 71            Self::Vector => cx.new(|_| ui::VectorStory).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 as _;
 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, window: &mut Window, cx: &mut App) -> AnyView {
107        match self {
108            Self::Component(component_story) => component_story.story(window, cx),
109            Self::KitchenSink => KitchenSinkStory::model(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}