zed/crates/storybook2/src/story_selector.rs

164 lines
5.7 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 gpui2::{AnyView, VisualContext};
2023-10-04 16:49:06 +00:00
use strum::{EnumIter, EnumString, IntoEnumIterator};
use ui::prelude::*;
use ui::{AvatarStory, ButtonStory, DetailsStory, IconStory, InputStory, LabelStory};
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-10-06 21:24:52 +00:00
AssistantPanel,
Avatar,
2023-10-07 15:18:06 +00:00
Breadcrumb,
2023-10-06 21:47:10 +00:00
Buffer,
Button,
2023-10-07 15:50:41 +00:00
ChatPanel,
2023-11-05 06:06:41 +00:00
Checkbox,
2023-10-09 15:04:53 +00:00
CollabPanel,
Colors,
2023-10-09 15:20:10 +00:00
CommandPalette,
2023-10-09 15:25:33 +00:00
ContextMenu,
Copilot,
Details,
Facepile,
Focus,
Icon,
Input,
2023-10-09 15:09:44 +00:00
Keybinding,
Label,
2023-10-09 15:39:42 +00:00
LanguageSelector,
2023-10-09 15:36:09 +00:00
MultiBuffer,
2023-10-20 20:30:45 +00:00
NotificationsPanel,
2023-10-09 15:15:50 +00:00
Palette,
2023-10-04 16:49:06 +00:00
Panel,
2023-10-06 21:58:23 +00:00
ProjectPanel,
2023-10-09 15:47:22 +00:00
RecentProjects,
Scroll,
2023-10-06 22:43:25 +00:00
Tab,
2023-10-07 15:13:54 +00:00
TabBar,
2023-10-06 22:50:49 +00:00
Terminal,
Text,
2023-10-09 15:44:08 +00:00
ThemeSelector,
TitleBar,
2023-10-09 15:31:56 +00:00
Toast,
2023-10-07 15:21:09 +00:00
Toolbar,
2023-10-07 15:55:10 +00:00
TrafficLights,
2023-10-06 22:19:12 +00:00
Workspace,
ZIndex,
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 {
Self::AssistantPanel => cx.build_view(|_| ui::AssistantPanelStory).into(),
Self::Avatar => cx.build_view(|_| AvatarStory).into(),
Self::Breadcrumb => cx.build_view(|_| ui::BreadcrumbStory).into(),
Self::Buffer => cx.build_view(|_| ui::BufferStory).into(),
Self::Button => cx.build_view(|_| ButtonStory).into(),
Self::ChatPanel => cx.build_view(|_| ui::ChatPanelStory).into(),
2023-11-05 06:06:41 +00:00
Self::Checkbox => cx.build_view(|_| ui::CheckboxStory).into(),
Self::CollabPanel => cx.build_view(|_| ui::CollabPanelStory).into(),
Self::Colors => cx.build_view(|_| ColorsStory).into(),
Self::CommandPalette => cx.build_view(|_| ui::CommandPaletteStory).into(),
Self::ContextMenu => cx.build_view(|_| ui::ContextMenuStory).into(),
Self::Copilot => cx.build_view(|_| ui::CopilotModalStory).into(),
Self::Details => cx.build_view(|_| DetailsStory).into(),
Self::Facepile => cx.build_view(|_| ui::FacepileStory).into(),
Self::Focus => FocusStory::view(cx).into(),
Self::Icon => cx.build_view(|_| IconStory).into(),
Self::Input => cx.build_view(|_| InputStory).into(),
Self::Keybinding => cx.build_view(|_| ui::KeybindingStory).into(),
Self::Label => cx.build_view(|_| LabelStory).into(),
Self::LanguageSelector => cx.build_view(|_| ui::LanguageSelectorStory).into(),
Self::MultiBuffer => cx.build_view(|_| ui::MultiBufferStory).into(),
Self::NotificationsPanel => cx.build_view(|cx| ui::NotificationsPanelStory).into(),
Self::Palette => cx.build_view(|cx| ui::PaletteStory).into(),
Self::Panel => cx.build_view(|cx| ui::PanelStory).into(),
Self::ProjectPanel => cx.build_view(|_| ui::ProjectPanelStory).into(),
Self::RecentProjects => cx.build_view(|_| ui::RecentProjectsStory).into(),
Self::Scroll => ScrollStory::view(cx).into(),
Self::Tab => cx.build_view(|_| ui::TabStory).into(),
Self::TabBar => cx.build_view(|_| ui::TabBarStory).into(),
Self::Terminal => cx.build_view(|_| ui::TerminalStory).into(),
Self::Text => TextStory::view(cx).into(),
Self::ThemeSelector => cx.build_view(|_| ui::ThemeSelectorStory).into(),
Self::TitleBar => ui::TitleBarStory::view(cx).into(),
Self::Toast => cx.build_view(|_| ui::ToastStory).into(),
Self::Toolbar => cx.build_view(|_| ui::ToolbarStory).into(),
Self::TrafficLights => cx.build_view(|_| ui::TrafficLightsStory).into(),
Self::Workspace => ui::WorkspaceStory::view(cx).into(),
Self::ZIndex => cx.build_view(|_| ZIndexStory).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))
}
}