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::*;
|
|
|
|
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,
|
2023-11-03 21:34:11 +00:00
|
|
|
Avatar,
|
2023-10-07 15:18:06 +00:00
|
|
|
Breadcrumb,
|
2023-10-06 21:47:10 +00:00
|
|
|
Buffer,
|
2023-11-03 21:34:11 +00:00
|
|
|
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,
|
2023-11-03 21:34:11 +00:00
|
|
|
Colors,
|
2023-10-09 15:20:10 +00:00
|
|
|
CommandPalette,
|
2023-10-09 15:25:33 +00:00
|
|
|
ContextMenu,
|
2023-11-03 21:34:11 +00:00
|
|
|
Copilot,
|
|
|
|
Details,
|
2023-10-07 16:02:42 +00:00
|
|
|
Facepile,
|
2023-11-03 21:34:11 +00:00
|
|
|
Focus,
|
|
|
|
Icon,
|
|
|
|
Input,
|
2023-10-09 15:09:44 +00:00
|
|
|
Keybinding,
|
2023-11-03 21:34:11 +00:00
|
|
|
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,
|
2023-11-03 21:34:11 +00:00
|
|
|
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,
|
2023-11-03 21:34:11 +00:00
|
|
|
Text,
|
2023-10-09 15:44:08 +00:00
|
|
|
ThemeSelector,
|
2023-10-07 16:10:39 +00:00
|
|
|
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-11-03 21:34:11 +00:00
|
|
|
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 {
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::AssistantPanel => cx.build_view(|_| ui::AssistantPanelStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Avatar => cx.build_view(|_| AvatarStory).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::Breadcrumb => cx.build_view(|_| ui::BreadcrumbStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Buffer => cx.build_view(|_| ui::BufferStory).into(),
|
|
|
|
Self::Button => cx.build_view(|_| ButtonStory).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::ChatPanel => cx.build_view(|_| ui::ChatPanelStory).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::CollabPanel => cx.build_view(|_| ui::CollabPanelStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Colors => cx.build_view(|_| ColorsStory).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::CommandPalette => cx.build_view(|_| ui::CommandPaletteStory).into(),
|
|
|
|
Self::ContextMenu => cx.build_view(|_| ui::ContextMenuStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Copilot => cx.build_view(|_| ui::CopilotModalStory).into(),
|
|
|
|
Self::Details => cx.build_view(|_| DetailsStory).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::Facepile => cx.build_view(|_| ui::FacepileStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Focus => FocusStory::view(cx).into(),
|
|
|
|
Self::Icon => cx.build_view(|_| IconStory).into(),
|
|
|
|
Self::Input => cx.build_view(|_| InputStory).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::Keybinding => cx.build_view(|_| ui::KeybindingStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Label => cx.build_view(|_| LabelStory).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
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(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Scroll => ScrollStory::view(cx).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::Tab => cx.build_view(|_| ui::TabStory).into(),
|
|
|
|
Self::TabBar => cx.build_view(|_| ui::TabBarStory).into(),
|
|
|
|
Self::Terminal => cx.build_view(|_| ui::TerminalStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Text => TextStory::view(cx).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
Self::ThemeSelector => cx.build_view(|_| ui::ThemeSelectorStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::TitleBar => ui::TitleBarStory::view(cx).into(),
|
2023-10-31 15:16:30 +00:00
|
|
|
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(),
|
2023-11-03 21:34:11 +00:00
|
|
|
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),
|
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))
|
|
|
|
}
|
|
|
|
}
|