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, div, px, size,
13};
14use log::LevelFilter;
15use project::Project;
16use reqwest_client::ReqwestClient;
17use settings::{KeymapFile, Settings};
18use simplelog::SimpleLogger;
19use strum::IntoEnumIterator;
20use theme::{ThemeRegistry, ThemeSettings};
21use ui::prelude::*;
22use workspace;
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 let http_client = ReqwestClient::user_agent("zed_storybook").unwrap();
72 cx.set_http_client(Arc::new(http_client));
73
74 settings::init(cx);
75 theme::init(theme::LoadThemes::All(Box::new(Assets)), cx);
76
77 let selector = story_selector;
78
79 let theme_registry = ThemeRegistry::global(cx);
80 let mut theme_settings = ThemeSettings::get_global(cx).clone();
81 theme_settings.active_theme = theme_registry.get(&theme_name).unwrap();
82 ThemeSettings::override_global(theme_settings, cx);
83
84 language::init(cx);
85 editor::init(cx);
86 Project::init_settings(cx);
87 workspace::init_settings(cx);
88 init(cx);
89 load_storybook_keymap(cx);
90 cx.set_menus(app_menus());
91
92 let size = size(px(1500.), px(780.));
93 let bounds = Bounds::centered(None, size, cx);
94 let _window = cx.open_window(
95 WindowOptions {
96 window_bounds: Some(WindowBounds::Windowed(bounds)),
97 ..Default::default()
98 },
99 move |window, cx| {
100 theme::setup_ui_font(window, cx);
101
102 cx.new(|cx| StoryWrapper::new(selector.story(window, cx)))
103 },
104 );
105
106 cx.activate(true);
107 });
108}
109
110#[derive(Clone)]
111pub struct StoryWrapper {
112 story: AnyView,
113}
114
115impl StoryWrapper {
116 pub(crate) fn new(story: AnyView) -> Self {
117 Self { story }
118 }
119}
120
121impl Render for StoryWrapper {
122 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
123 div()
124 .flex()
125 .flex_col()
126 .size_full()
127 .font_family("Zed Plex Mono")
128 .child(self.story.clone())
129 }
130}
131
132fn load_embedded_fonts(cx: &App) -> anyhow::Result<()> {
133 let font_paths = cx.asset_source().list("fonts")?;
134 let mut embedded_fonts = Vec::new();
135 for font_path in font_paths {
136 if font_path.ends_with(".ttf") {
137 let font_bytes = cx
138 .asset_source()
139 .load(&font_path)?
140 .expect("Should never be None in the storybook");
141 embedded_fonts.push(font_bytes);
142 }
143 }
144
145 cx.text_system().add_fonts(embedded_fonts)
146}
147
148fn load_storybook_keymap(cx: &mut App) {
149 cx.bind_keys(KeymapFile::load_asset("keymaps/storybook.json", cx).unwrap());
150}
151
152pub fn init(cx: &mut App) {
153 cx.on_action(quit);
154}
155
156fn quit(_: &Quit, cx: &mut App) {
157 cx.spawn(async move |cx| {
158 cx.update(|cx| cx.quit())?;
159 anyhow::Ok(())
160 })
161 .detach_and_log_err(cx);
162}