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