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;
|
2023-11-06 18:46:10 +00:00
|
|
|
use gpui::{AnyView, VisualContext};
|
2023-10-04 16:49:06 +00:00
|
|
|
use strum::{EnumIter, EnumString, IntoEnumIterator};
|
2023-11-03 21:34:11 +00:00
|
|
|
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 {
|
2023-11-03 21:34:11 +00:00
|
|
|
Avatar,
|
|
|
|
Button,
|
2023-11-05 06:06:41 +00:00
|
|
|
Checkbox,
|
2023-10-09 15:25:33 +00:00
|
|
|
ContextMenu,
|
2023-11-03 21:34:11 +00:00
|
|
|
Focus,
|
|
|
|
Icon,
|
2023-11-28 22:01:53 +00:00
|
|
|
IconButton,
|
2023-10-09 15:09:44 +00:00
|
|
|
Keybinding,
|
2023-11-03 21:34:11 +00:00
|
|
|
Label,
|
2023-11-27 16:55:23 +00:00
|
|
|
ListItem,
|
2023-11-03 21:34:11 +00:00
|
|
|
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(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::ContextMenu => cx.build_view(|_| ui::ContextMenuStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Focus => FocusStory::view(cx).into(),
|
2023-11-27 16:55:23 +00:00
|
|
|
Self::Icon => cx.build_view(|_| ui::IconStory).into(),
|
2023-11-28 22:01:53 +00:00
|
|
|
Self::IconButton => cx.build_view(|_| ui::IconButtonStory).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
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(),
|
2023-11-03 21:34:11 +00:00
|
|
|
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),
|
2023-10-31 15:16:30 +00:00
|
|
|
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);
|
|
|
|
|
2023-11-03 21:34:11 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|