storybook.rs

  1mod actions;
  2mod app_menus;
  3mod assets;
  4mod stories;
  5mod story_selector;
  6
  7use std::sync::Arc;
  8
  9use clap::Parser;
 10use dialoguer::FuzzySelect;
 11use gpui::{
 12    AnyView, App, Bounds, Context, Render, Window, WindowBounds, WindowOptions,
 13    colors::{Colors, GlobalColors},
 14    div, px, size,
 15};
 16use log::LevelFilter;
 17use reqwest_client::ReqwestClient;
 18use settings::{KeymapFile, Settings};
 19use simplelog::SimpleLogger;
 20use strum::IntoEnumIterator;
 21use theme::ThemeSettings;
 22use ui::prelude::*;
 23
 24use crate::app_menus::app_menus;
 25use crate::assets::Assets;
 26use crate::story_selector::{ComponentStory, StorySelector};
 27use actions::Quit;
 28pub use indoc::indoc;
 29
 30#[derive(Parser)]
 31#[command(author, version, about, long_about = None)]
 32struct Args {
 33    #[arg(value_enum)]
 34    story: Option<StorySelector>,
 35
 36    /// The name of the theme to use in the storybook.
 37    ///
 38    /// If not provided, the default theme will be used.
 39    #[arg(long)]
 40    theme: Option<String>,
 41}
 42
 43fn main() {
 44    SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
 45
 46    menu::init();
 47    let args = Args::parse();
 48
 49    let story_selector = args.story.unwrap_or_else(|| {
 50        let stories = ComponentStory::iter().collect::<Vec<_>>();
 51
 52        ctrlc::set_handler(move || {}).unwrap();
 53
 54        let result = FuzzySelect::new()
 55            .with_prompt("Choose a story to run:")
 56            .items(&stories)
 57            .interact();
 58
 59        let Ok(selection) = result else {
 60            dialoguer::console::Term::stderr().show_cursor().unwrap();
 61            std::process::exit(0);
 62        };
 63
 64        StorySelector::Component(stories[selection])
 65    });
 66    let theme_name = args.theme.unwrap_or("One Dark".to_string());
 67
 68    gpui::Application::new().with_assets(Assets).run(move |cx| {
 69        load_embedded_fonts(cx).unwrap();
 70
 71        cx.set_global(GlobalColors(Arc::new(Colors::default())));
 72
 73        let http_client = ReqwestClient::user_agent("zed_storybook").unwrap();
 74        cx.set_http_client(Arc::new(http_client));
 75
 76        settings::init(cx);
 77        theme::init(theme::LoadThemes::All(Box::new(Assets)), cx);
 78
 79        let selector = story_selector;
 80
 81        let mut theme_settings = ThemeSettings::get_global(cx).clone();
 82        theme_settings.theme =
 83            theme::ThemeSelection::Static(settings::ThemeName(theme_name.into()));
 84        ThemeSettings::override_global(theme_settings, cx);
 85
 86        editor::init(cx);
 87        init(cx);
 88        load_storybook_keymap(cx);
 89        cx.set_menus(app_menus());
 90
 91        let size = size(px(1500.), px(780.));
 92        let bounds = Bounds::centered(None, size, cx);
 93        let _window = cx.open_window(
 94            WindowOptions {
 95                window_bounds: Some(WindowBounds::Windowed(bounds)),
 96                ..Default::default()
 97            },
 98            move |window, cx| {
 99                theme::setup_ui_font(window, cx);
100
101                cx.new(|cx| StoryWrapper::new(selector.story(window, cx)))
102            },
103        );
104
105        cx.activate(true);
106    });
107}
108
109#[derive(Clone)]
110pub struct StoryWrapper {
111    story: AnyView,
112}
113
114impl StoryWrapper {
115    pub(crate) fn new(story: AnyView) -> Self {
116        Self { story }
117    }
118}
119
120impl Render for StoryWrapper {
121    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
122        div()
123            .flex()
124            .flex_col()
125            .size_full()
126            .font_family(".ZedMono")
127            .child(self.story.clone())
128    }
129}
130
131fn load_embedded_fonts(cx: &App) -> anyhow::Result<()> {
132    let font_paths = cx.asset_source().list("fonts")?;
133    let mut embedded_fonts = Vec::new();
134    for font_path in font_paths {
135        if font_path.ends_with(".ttf") {
136            let font_bytes = cx
137                .asset_source()
138                .load(&font_path)?
139                .expect("Should never be None in the storybook");
140            embedded_fonts.push(font_bytes);
141        }
142    }
143
144    cx.text_system().add_fonts(embedded_fonts)
145}
146
147fn load_storybook_keymap(cx: &mut App) {
148    cx.bind_keys(KeymapFile::load_asset("keymaps/storybook.json", None, cx).unwrap());
149}
150
151pub fn init(cx: &mut App) {
152    cx.on_action(quit);
153}
154
155fn quit(_: &Quit, cx: &mut App) {
156    cx.spawn(async move |cx| {
157        cx.update(|cx| cx.quit())?;
158        anyhow::Ok(())
159    })
160    .detach_and_log_err(cx);
161}