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::*;
 11use ui::{AvatarStory, ButtonStory, DetailsStory, IconStory, InputStory, LabelStory};
 12
 13#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display, EnumString, EnumIter)]
 14#[strum(serialize_all = "snake_case")]
 15pub enum ComponentStory {
 16    AssistantPanel,
 17    Avatar,
 18    Breadcrumb,
 19    Buffer,
 20    Button,
 21    ChatPanel,
 22    Checkbox,
 23    CollabPanel,
 24    Colors,
 25    CommandPalette,
 26    ContextMenu,
 27    Copilot,
 28    Details,
 29    Facepile,
 30    Focus,
 31    Icon,
 32    Input,
 33    Keybinding,
 34    Label,
 35    LanguageSelector,
 36    MultiBuffer,
 37    NotificationsPanel,
 38    Palette,
 39    Panel,
 40    ProjectPanel,
 41    Players,
 42    RecentProjects,
 43    Scroll,
 44    Tab,
 45    TabBar,
 46    Terminal,
 47    Text,
 48    ThemeSelector,
 49    TitleBar,
 50    Toast,
 51    Toolbar,
 52    TrafficLights,
 53    Workspace,
 54    ZIndex,
 55}
 56
 57impl ComponentStory {
 58    pub fn story(&self, cx: &mut WindowContext) -> AnyView {
 59        match self {
 60            Self::AssistantPanel => cx.build_view(|_| ui::AssistantPanelStory).into(),
 61            Self::Avatar => cx.build_view(|_| AvatarStory).into(),
 62            Self::Breadcrumb => cx.build_view(|_| ui::BreadcrumbStory).into(),
 63            Self::Buffer => cx.build_view(|_| ui::BufferStory).into(),
 64            Self::Button => cx.build_view(|_| ButtonStory).into(),
 65            Self::ChatPanel => cx.build_view(|_| ui::ChatPanelStory).into(),
 66            Self::Checkbox => cx.build_view(|_| ui::CheckboxStory).into(),
 67            Self::CollabPanel => cx.build_view(|_| ui::CollabPanelStory).into(),
 68            Self::Colors => cx.build_view(|_| ColorsStory).into(),
 69            Self::CommandPalette => cx.build_view(|_| ui::CommandPaletteStory).into(),
 70            Self::ContextMenu => cx.build_view(|_| ui::ContextMenuStory).into(),
 71            Self::Copilot => cx.build_view(|_| ui::CopilotModalStory).into(),
 72            Self::Details => cx.build_view(|_| DetailsStory).into(),
 73            Self::Facepile => cx.build_view(|_| ui::FacepileStory).into(),
 74            Self::Focus => FocusStory::view(cx).into(),
 75            Self::Icon => cx.build_view(|_| IconStory).into(),
 76            Self::Input => cx.build_view(|_| InputStory).into(),
 77            Self::Keybinding => cx.build_view(|_| ui::KeybindingStory).into(),
 78            Self::Label => cx.build_view(|_| LabelStory).into(),
 79            Self::LanguageSelector => cx.build_view(|_| ui::LanguageSelectorStory).into(),
 80            Self::MultiBuffer => cx.build_view(|_| ui::MultiBufferStory).into(),
 81            Self::NotificationsPanel => cx.build_view(|cx| ui::NotificationsPanelStory).into(),
 82            Self::Palette => cx.build_view(|cx| ui::PaletteStory).into(),
 83            Self::Players => cx.build_view(|_| theme2::PlayerStory).into(),
 84            Self::Panel => cx.build_view(|cx| ui::PanelStory).into(),
 85            Self::ProjectPanel => cx.build_view(|_| ui::ProjectPanelStory).into(),
 86            Self::RecentProjects => cx.build_view(|_| ui::RecentProjectsStory).into(),
 87            Self::Scroll => ScrollStory::view(cx).into(),
 88            Self::Tab => cx.build_view(|_| ui::TabStory).into(),
 89            Self::TabBar => cx.build_view(|_| ui::TabBarStory).into(),
 90            Self::Terminal => cx.build_view(|_| ui::TerminalStory).into(),
 91            Self::Text => TextStory::view(cx).into(),
 92            Self::ThemeSelector => cx.build_view(|_| ui::ThemeSelectorStory).into(),
 93            Self::TitleBar => ui::TitleBarStory::view(cx).into(),
 94            Self::Toast => cx.build_view(|_| ui::ToastStory).into(),
 95            Self::Toolbar => cx.build_view(|_| ui::ToolbarStory).into(),
 96            Self::TrafficLights => cx.build_view(|_| ui::TrafficLightsStory).into(),
 97            Self::Workspace => ui::WorkspaceStory::view(cx).into(),
 98            Self::ZIndex => cx.build_view(|_| ZIndexStory).into(),
 99        }
100    }
101}
102
103#[derive(Debug, PartialEq, Eq, Clone, Copy)]
104pub enum StorySelector {
105    Component(ComponentStory),
106    KitchenSink,
107}
108
109impl FromStr for StorySelector {
110    type Err = anyhow::Error;
111
112    fn from_str(raw_story_name: &str) -> std::result::Result<Self, Self::Err> {
113        use anyhow::Context;
114
115        let story = raw_story_name.to_ascii_lowercase();
116
117        if story == "kitchen_sink" {
118            return Ok(Self::KitchenSink);
119        }
120
121        if let Some((_, story)) = story.split_once("components/") {
122            let component_story = ComponentStory::from_str(story)
123                .with_context(|| format!("story not found for component '{story}'"))?;
124
125            return Ok(Self::Component(component_story));
126        }
127
128        Err(anyhow!("story not found for '{raw_story_name}'"))
129    }
130}
131
132impl StorySelector {
133    pub fn story(&self, cx: &mut WindowContext) -> AnyView {
134        match self {
135            Self::Component(component_story) => component_story.story(cx),
136            Self::KitchenSink => KitchenSinkStory::view(cx).into(),
137        }
138    }
139}
140
141/// The list of all stories available in the storybook.
142static ALL_STORY_SELECTORS: OnceLock<Vec<StorySelector>> = OnceLock::new();
143
144impl ValueEnum for StorySelector {
145    fn value_variants<'a>() -> &'a [Self] {
146        let stories = ALL_STORY_SELECTORS.get_or_init(|| {
147            let component_stories = ComponentStory::iter().map(StorySelector::Component);
148
149            component_stories
150                .chain(std::iter::once(StorySelector::KitchenSink))
151                .collect::<Vec<_>>()
152        });
153
154        stories
155    }
156
157    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
158        let value = match self {
159            Self::Component(story) => format!("components/{story}"),
160            Self::KitchenSink => "kitchen_sink".to_string(),
161        };
162
163        Some(PossibleValue::new(value))
164    }
165}