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