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