zed/crates/storybook2/src/story_selector.rs

115 lines
3.5 KiB
Rust
Raw Normal View History

2023-10-04 16:49:06 +00:00
use std::str::FromStr;
use std::sync::OnceLock;
2023-10-17 06:52:26 +00:00
use crate::stories::*;
2023-10-12 20:06:54 +00:00
use anyhow::anyhow;
2023-10-04 16:49:06 +00:00
use clap::builder::PossibleValue;
use clap::ValueEnum;
use gpui::{AnyView, VisualContext};
2023-10-04 16:49:06 +00:00
use strum::{EnumIter, EnumString, IntoEnumIterator};
use ui::prelude::*;
2023-10-04 16:49:06 +00:00
#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display, EnumString, EnumIter)]
#[strum(serialize_all = "snake_case")]
pub enum ComponentStory {
Avatar,
Button,
2023-11-05 06:06:41 +00:00
Checkbox,
2023-10-09 15:25:33 +00:00
ContextMenu,
Focus,
Icon,
IconButton,
2023-10-09 15:09:44 +00:00
Keybinding,
Label,
2023-11-27 16:55:23 +00:00
ListItem,
Scroll,
Text,
ZIndex,
2023-11-06 22:26:10 +00:00
Picker,
2023-10-04 16:49:06 +00:00
}
impl ComponentStory {
2023-10-12 20:06:54 +00:00
pub fn story(&self, cx: &mut WindowContext) -> AnyView {
2023-10-04 16:49:06 +00:00
match self {
2023-11-27 16:55:23 +00:00
Self::Avatar => cx.build_view(|_| ui::AvatarStory).into(),
Self::Button => cx.build_view(|_| ui::ButtonStory).into(),
2023-11-05 06:06:41 +00:00
Self::Checkbox => cx.build_view(|_| ui::CheckboxStory).into(),
Self::ContextMenu => cx.build_view(|_| ui::ContextMenuStory).into(),
Self::Focus => FocusStory::view(cx).into(),
2023-11-27 16:55:23 +00:00
Self::Icon => cx.build_view(|_| ui::IconStory).into(),
Self::IconButton => cx.build_view(|_| ui::IconButtonStory).into(),
Self::Keybinding => cx.build_view(|_| ui::KeybindingStory).into(),
2023-11-27 16:55:23 +00:00
Self::Label => cx.build_view(|_| ui::LabelStory).into(),
Self::ListItem => cx.build_view(|_| ui::ListItemStory).into(),
Self::Scroll => ScrollStory::view(cx).into(),
Self::Text => TextStory::view(cx).into(),
Self::ZIndex => cx.build_view(|_| ZIndexStory).into(),
2023-11-06 22:26:10 +00:00
Self::Picker => PickerStory::new(cx).into(),
2023-10-04 16:49:06 +00:00
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum StorySelector {
Component(ComponentStory),
KitchenSink,
}
impl FromStr for StorySelector {
type Err = anyhow::Error;
fn from_str(raw_story_name: &str) -> std::result::Result<Self, Self::Err> {
2023-10-12 20:06:54 +00:00
use anyhow::Context;
2023-10-04 16:49:06 +00:00
let story = raw_story_name.to_ascii_lowercase();
if story == "kitchen_sink" {
return Ok(Self::KitchenSink);
}
if let Some((_, story)) = story.split_once("components/") {
let component_story = ComponentStory::from_str(story)
.with_context(|| format!("story not found for component '{story}'"))?;
return Ok(Self::Component(component_story));
}
Err(anyhow!("story not found for '{raw_story_name}'"))
}
}
impl StorySelector {
2023-10-12 20:06:54 +00:00
pub fn story(&self, cx: &mut WindowContext) -> AnyView {
2023-10-04 16:49:06 +00:00
match self {
2023-10-12 16:18:35 +00:00
Self::Component(component_story) => component_story.story(cx),
Self::KitchenSink => KitchenSinkStory::view(cx).into(),
2023-10-04 16:49:06 +00:00
}
}
}
/// The list of all stories available in the storybook.
static ALL_STORY_SELECTORS: OnceLock<Vec<StorySelector>> = OnceLock::new();
impl ValueEnum for StorySelector {
fn value_variants<'a>() -> &'a [Self] {
let stories = ALL_STORY_SELECTORS.get_or_init(|| {
let component_stories = ComponentStory::iter().map(StorySelector::Component);
component_stories
2023-10-04 16:49:06 +00:00
.chain(std::iter::once(StorySelector::KitchenSink))
.collect::<Vec<_>>()
});
stories
}
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
let value = match self {
Self::Component(story) => format!("components/{story}"),
Self::KitchenSink => "kitchen_sink".to_string(),
};
Some(PossibleValue::new(value))
}
}