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 let ui_font_size = ThemeSettings::get_global(cx).ui_font_size;
94 cx.set_rem_size(ui_font_size);
95
96 cx.new_view(|cx| StoryWrapper::new(selector.story(cx)))
97 },
98 );
99
100 cx.activate(true);
101 });
102}
103
104#[derive(Clone)]
105pub struct StoryWrapper {
106 story: AnyView,
107}
108
109impl StoryWrapper {
110 pub(crate) fn new(story: AnyView) -> Self {
111 Self { story }
112 }
113}
114
115impl Render for StoryWrapper {
116 fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
117 div()
118 .flex()
119 .flex_col()
120 .size_full()
121 .font_family("Zed Mono")
122 .child(self.story.clone())
123 }
124}
125
126fn load_embedded_fonts(cx: &AppContext) -> gpui::Result<()> {
127 let font_paths = cx.asset_source().list("fonts")?;
128 let mut embedded_fonts = Vec::new();
129 for font_path in font_paths {
130 if font_path.ends_with(".ttf") {
131 let font_bytes = cx.asset_source().load(&font_path)?;
132 embedded_fonts.push(font_bytes);
133 }
134 }
135
136 cx.text_system().add_fonts(embedded_fonts)
137}
138
139fn load_storybook_keymap(cx: &mut AppContext) {
140 KeymapFile::load_asset("keymaps/storybook.json", cx).unwrap();
141}
142
143pub fn init(cx: &mut AppContext) {
144 cx.on_action(quit);
145}
146
147fn quit(_: &Quit, cx: &mut AppContext) {
148 cx.spawn(|cx| async move {
149 cx.update(|cx| cx.quit())?;
150 anyhow::Ok(())
151 })
152 .detach_and_log_err(cx);
153}