story_selector.rs

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