zed/crates/storybook2/src/story_selector.rs

223 lines
7.8 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};
2023-10-12 20:06:54 +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 ElementStory {
2023-10-04 19:34:14 +00:00
Avatar,
2023-10-07 15:41:48 +00:00
Button,
2023-10-09 15:55:27 +00:00
Details,
2023-10-18 18:13:06 +00:00
Focus,
2023-10-04 17:46:45 +00:00
Icon,
2023-10-06 21:58:23 +00:00
Input,
2023-10-04 16:49:06 +00:00
Label,
2023-10-20 15:31:42 +00:00
Scroll,
2023-10-17 06:52:26 +00:00
Text,
2023-10-09 22:00:49 +00:00
ZIndex,
2023-10-04 16:49:06 +00:00
}
impl ElementStory {
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::Avatar => { cx.build_view(|cx| (), |_, _| ui::AvatarStory.render()) }.into_any(),
Self::Button => { cx.build_view(|cx| (), |_, _| ui::ButtonStory.render()) }.into_any(),
Self::Details => {
{ cx.build_view(|cx| (), |_, _| ui::DetailsStory.render()) }.into_any()
}
2023-10-18 18:13:06 +00:00
Self::Focus => FocusStory::view(cx).into_any(),
Self::Icon => { cx.build_view(|cx| (), |_, _| ui::IconStory.render()) }.into_any(),
Self::Input => { cx.build_view(|cx| (), |_, _| ui::InputStory.render()) }.into_any(),
Self::Label => { cx.build_view(|cx| (), |_, _| ui::LabelStory.render()) }.into_any(),
2023-10-20 15:31:42 +00:00
Self::Scroll => ScrollStory::view(cx).into_any(),
2023-10-17 06:52:26 +00:00
Self::Text => TextStory::view(cx).into_any(),
Self::ZIndex => { cx.build_view(|cx| (), |_, _| ZIndexStory.render()) }.into_any(),
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,
2023-10-07 15:18:06 +00:00
Breadcrumb,
2023-10-06 21:47:10 +00:00
Buffer,
2023-10-07 15:50:41 +00:00
ChatPanel,
2023-10-09 15:04:53 +00:00
CollabPanel,
2023-10-09 15:20:10 +00:00
CommandPalette,
Copilot,
2023-10-09 15:25:33 +00:00
ContextMenu,
Facepile,
2023-10-09 15:09:44 +00:00
Keybinding,
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,
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,
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,
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(|cx| (), |_, _| ui::AssistantPanelStory.render()) }.into_any()
}
Self::Buffer => { cx.build_view(|cx| (), |_, _| ui::BufferStory.render()) }.into_any(),
Self::Breadcrumb => {
{ cx.build_view(|cx| (), |_, _| ui::BreadcrumbStory.render()) }.into_any()
}
Self::ChatPanel => {
{ cx.build_view(|cx| (), |_, _| ui::ChatPanelStory.render()) }.into_any()
}
Self::CollabPanel => {
{ cx.build_view(|cx| (), |_, _| ui::CollabPanelStory.render()) }.into_any()
}
Self::CommandPalette => {
{ cx.build_view(|cx| (), |_, _| ui::CommandPaletteStory.render()) }.into_any()
}
Self::ContextMenu => {
{ cx.build_view(|cx| (), |_, _| ui::ContextMenuStory.render()) }.into_any()
}
Self::Facepile => {
{ cx.build_view(|cx| (), |_, _| ui::FacepileStory.render()) }.into_any()
}
Self::Keybinding => {
{ cx.build_view(|cx| (), |_, _| ui::KeybindingStory.render()) }.into_any()
}
Self::LanguageSelector => {
{ cx.build_view(|cx| (), |_, _| ui::LanguageSelectorStory.render()) }.into_any()
2023-10-12 20:06:54 +00:00
}
Self::MultiBuffer => {
{ cx.build_view(|cx| (), |_, _| ui::MultiBufferStory.render()) }.into_any()
}
Self::NotificationsPanel => {
{ cx.build_view(|cx| (), |_, _| ui::NotificationsPanelStory.render()) }.into_any()
}
Self::Palette => {
{ cx.build_view(|cx| (), |_, _| ui::PaletteStory.render()) }.into_any()
}
Self::Panel => { cx.build_view(|cx| (), |_, _| ui::PanelStory.render()) }.into_any(),
Self::ProjectPanel => {
{ cx.build_view(|cx| (), |_, _| ui::ProjectPanelStory.render()) }.into_any()
2023-10-12 20:06:54 +00:00
}
Self::RecentProjects => {
{ cx.build_view(|cx| (), |_, _| ui::RecentProjectsStory.render()) }.into_any()
}
Self::Tab => { cx.build_view(|cx| (), |_, _| ui::TabStory.render()) }.into_any(),
Self::TabBar => { cx.build_view(|cx| (), |_, _| ui::TabBarStory.render()) }.into_any(),
Self::Terminal => {
{ cx.build_view(|cx| (), |_, _| ui::TerminalStory.render()) }.into_any()
}
Self::ThemeSelector => {
{ cx.build_view(|cx| (), |_, _| ui::ThemeSelectorStory.render()) }.into_any()
2023-10-12 20:06:54 +00:00
}
2023-10-13 16:21:51 +00:00
Self::TitleBar => ui::TitleBarStory::view(cx).into_any(),
Self::Toast => { cx.build_view(|cx| (), |_, _| ui::ToastStory.render()) }.into_any(),
Self::Toolbar => {
{ cx.build_view(|cx| (), |_, _| ui::ToolbarStory.render()) }.into_any()
}
Self::TrafficLights => {
{ cx.build_view(|cx| (), |_, _| ui::TrafficLightsStory.render()) }.into_any()
}
Self::Copilot => {
{ cx.build_view(|cx| (), |_, _| ui::CopilotModalStory.render()) }.into_any()
2023-10-12 20:06:54 +00:00
}
Self::Workspace => ui::WorkspaceStory::view(cx).into_any(),
2023-10-04 16:49:06 +00:00
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum StorySelector {
Element(ElementStory),
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("elements/") {
let element_story = ElementStory::from_str(story)
.with_context(|| format!("story not found for element '{story}'"))?;
return Ok(Self::Element(element_story));
}
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 20:06:54 +00:00
Self::Element(element_story) => element_story.story(cx),
2023-10-12 16:18:35 +00:00
Self::Component(component_story) => component_story.story(cx),
2023-10-17 06:52:26 +00:00
Self::KitchenSink => KitchenSinkStory::view(cx).into_any(),
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 element_stories = ElementStory::iter().map(StorySelector::Element);
let component_stories = ComponentStory::iter().map(StorySelector::Component);
element_stories
.chain(component_stories)
.chain(std::iter::once(StorySelector::KitchenSink))
.collect::<Vec<_>>()
});
stories
}
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
let value = match self {
Self::Element(story) => format!("elements/{story}"),
Self::Component(story) => format!("components/{story}"),
Self::KitchenSink => "kitchen_sink".to_string(),
};
Some(PossibleValue::new(value))
}
}