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