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, WindowOptions,
11};
12use log::LevelFilter;
13use project::Project;
14use settings::{KeymapFile, Settings};
15use simplelog::SimpleLogger;
16use strum::IntoEnumIterator;
17use theme::{ThemeRegistry, ThemeSettings};
18use ui::prelude::*;
19
20use crate::app_menus::app_menus;
21use crate::assets::Assets;
22use crate::story_selector::{ComponentStory, StorySelector};
23use actions::Quit;
24pub use indoc::indoc;
25
26#[derive(Parser)]
27#[command(author, version, about, long_about = None)]
28struct Args {
29 #[arg(value_enum)]
30 story: Option<StorySelector>,
31
32 /// The name of the theme to use in the storybook.
33 ///
34 /// If not provided, the default theme will be used.
35 #[arg(long)]
36 theme: Option<String>,
37}
38
39fn main() {
40 SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
41
42 menu::init();
43 let args = Args::parse();
44
45 let story_selector = args.story.unwrap_or_else(|| {
46 let stories = ComponentStory::iter().collect::<Vec<_>>();
47
48 ctrlc::set_handler(move || {}).unwrap();
49
50 let result = FuzzySelect::new()
51 .with_prompt("Choose a story to run:")
52 .items(&stories)
53 .interact();
54
55 let Ok(selection) = result else {
56 dialoguer::console::Term::stderr().show_cursor().unwrap();
57 std::process::exit(0);
58 };
59
60 StorySelector::Component(stories[selection])
61 });
62 let theme_name = args.theme.unwrap_or("One Dark".to_string());
63
64 gpui::App::new().with_assets(Assets).run(move |cx| {
65 load_embedded_fonts(cx).unwrap();
66
67 settings::init(cx);
68 theme::init(theme::LoadThemes::All(Box::new(Assets)), cx);
69
70 let selector = story_selector;
71
72 let theme_registry = ThemeRegistry::global(cx);
73 let mut theme_settings = ThemeSettings::get_global(cx).clone();
74 theme_settings.active_theme = theme_registry.get(&theme_name).unwrap();
75 ThemeSettings::override_global(theme_settings, cx);
76
77 language::init(cx);
78 editor::init(cx);
79 Project::init_settings(cx);
80 init(cx);
81 load_storybook_keymap(cx);
82 cx.set_menus(app_menus());
83
84 let size = size(px(1500.), px(780.));
85 let bounds = Bounds::centered(None, size, cx);
86 let _window = cx.open_window(
87 WindowOptions {
88 bounds: Some(bounds),
89 ..Default::default()
90 },
91 move |cx| {
92 let ui_font_size = ThemeSettings::get_global(cx).ui_font_size;
93 cx.set_rem_size(ui_font_size);
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 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.asset_source().load(&font_path)?;
131 embedded_fonts.push(font_bytes);
132 }
133 }
134
135 cx.text_system().add_fonts(embedded_fonts)
136}
137
138fn load_storybook_keymap(cx: &mut AppContext) {
139 KeymapFile::load_asset("keymaps/storybook.json", cx).unwrap();
140}
141
142pub fn init(cx: &mut AppContext) {
143 cx.on_action(quit);
144}
145
146fn quit(_: &Quit, cx: &mut AppContext) {
147 cx.spawn(|cx| async move {
148 cx.update(|cx| cx.quit())?;
149 anyhow::Ok(())
150 })
151 .detach_and_log_err(cx);
152}