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 {
|
2024-07-08 20:55:55 +00:00
|
|
|
ApplicationMenu,
|
2023-11-30 17:00:41 +00:00
|
|
|
AutoHeightEditor,
|
2023-11-03 21:34:11 +00:00
|
|
|
Avatar,
|
|
|
|
Button,
|
2023-11-05 06:06:41 +00:00
|
|
|
Checkbox,
|
2024-01-09 02:54:59 +00:00
|
|
|
CollabNotification,
|
2023-10-09 15:25:33 +00:00
|
|
|
ContextMenu,
|
2023-12-04 16:20:41 +00:00
|
|
|
Cursor,
|
2023-11-29 19:07:48 +00:00
|
|
|
Disclosure,
|
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-29 18:34:50 +00:00
|
|
|
List,
|
2023-11-30 20:55:31 +00:00
|
|
|
ListHeader,
|
2023-11-27 16:55:23 +00:00
|
|
|
ListItem,
|
2023-12-12 21:38:25 +00:00
|
|
|
OverflowScroll,
|
2024-04-23 23:23:26 +00:00
|
|
|
Picker,
|
2023-11-03 21:34:11 +00:00
|
|
|
Scroll,
|
2024-06-26 17:02:58 +00:00
|
|
|
Setting,
|
2023-12-07 17:30:43 +00:00
|
|
|
Tab,
|
2023-12-12 20:50:05 +00:00
|
|
|
TabBar,
|
2024-04-23 23:23:26 +00:00
|
|
|
Text,
|
2023-12-20 23:23:21 +00:00
|
|
|
ToggleButton,
|
2024-05-13 21:58:08 +00:00
|
|
|
ToolStrip,
|
2023-12-11 23:19:11 +00:00
|
|
|
ViewportUnits,
|
2024-05-16 18:37:55 +00:00
|
|
|
WithRemSize,
|
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 {
|
2024-07-08 20:55:55 +00:00
|
|
|
Self::ApplicationMenu => cx.new_view(|_| title_bar::ApplicationMenuStory).into(),
|
2023-11-30 17:00:41 +00:00
|
|
|
Self::AutoHeightEditor => AutoHeightEditorStory::new(cx).into(),
|
2024-01-01 19:01:29 +00:00
|
|
|
Self::Avatar => cx.new_view(|_| ui::AvatarStory).into(),
|
|
|
|
Self::Button => cx.new_view(|_| ui::ButtonStory).into(),
|
|
|
|
Self::Checkbox => cx.new_view(|_| ui::CheckboxStory).into(),
|
2024-01-09 02:54:59 +00:00
|
|
|
Self::CollabNotification => cx
|
|
|
|
.new_view(|_| collab_ui::notifications::CollabNotificationStory)
|
|
|
|
.into(),
|
2024-01-01 19:01:29 +00:00
|
|
|
Self::ContextMenu => cx.new_view(|_| ui::ContextMenuStory).into(),
|
|
|
|
Self::Cursor => cx.new_view(|_| crate::stories::CursorStory).into(),
|
|
|
|
Self::Disclosure => cx.new_view(|_| ui::DisclosureStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Focus => FocusStory::view(cx).into(),
|
2024-01-01 19:01:29 +00:00
|
|
|
Self::Icon => cx.new_view(|_| ui::IconStory).into(),
|
|
|
|
Self::IconButton => cx.new_view(|_| ui::IconButtonStory).into(),
|
|
|
|
Self::Keybinding => cx.new_view(|_| ui::KeybindingStory).into(),
|
|
|
|
Self::Label => cx.new_view(|_| ui::LabelStory).into(),
|
|
|
|
Self::List => cx.new_view(|_| ui::ListStory).into(),
|
|
|
|
Self::ListHeader => cx.new_view(|_| ui::ListHeaderStory).into(),
|
|
|
|
Self::ListItem => cx.new_view(|_| ui::ListItemStory).into(),
|
|
|
|
Self::OverflowScroll => cx.new_view(|_| crate::stories::OverflowScrollStory).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Scroll => ScrollStory::view(cx).into(),
|
2024-06-26 17:02:58 +00:00
|
|
|
Self::Setting => cx.new_view(|cx| ui::SettingStory::init(cx)).into(),
|
2023-11-03 21:34:11 +00:00
|
|
|
Self::Text => TextStory::view(cx).into(),
|
2024-01-01 19:01:29 +00:00
|
|
|
Self::Tab => cx.new_view(|_| ui::TabStory).into(),
|
|
|
|
Self::TabBar => cx.new_view(|_| ui::TabBarStory).into(),
|
|
|
|
Self::ToggleButton => cx.new_view(|_| ui::ToggleButtonStory).into(),
|
2024-05-13 21:58:08 +00:00
|
|
|
Self::ToolStrip => cx.new_view(|_| ui::ToolStripStory).into(),
|
2024-01-01 19:01:29 +00:00
|
|
|
Self::ViewportUnits => cx.new_view(|_| crate::stories::ViewportUnitsStory).into(),
|
2024-05-16 18:37:55 +00:00
|
|
|
Self::WithRemSize => cx.new_view(|_| crate::stories::WithRemSizeStory).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))
|
|
|
|
}
|
|
|
|
}
|