story_selector.rs

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