storybook.rs

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