1#![allow(dead_code, unused_variables)]
2
3mod stories;
4mod story;
5mod story_selector;
6
7use std::{process::Command, sync::Arc};
8
9use ::theme as legacy_theme;
10use clap::Parser;
11use gpui2::{
12 serde_json, vec2f, view, Element, IntoElement, ParentElement, RectF, ViewContext, WindowBounds,
13};
14use legacy_theme::{ThemeRegistry, ThemeSettings};
15use log::LevelFilter;
16use settings::{default_settings, SettingsStore};
17use simplelog::SimpleLogger;
18use ui::prelude::*;
19use ui::{ElementExt, Theme, WorkspaceElement};
20
21use crate::story_selector::StorySelector;
22
23gpui2::actions! {
24 storybook,
25 [ToggleInspector]
26}
27
28#[derive(Parser)]
29#[command(author, version, about, long_about = None)]
30struct Args {
31 #[arg(value_enum)]
32 story: Option<StorySelector>,
33
34 /// The name of the theme to use in the storybook.
35 ///
36 /// If not provided, the default theme will be used.
37 #[arg(long)]
38 theme: Option<String>,
39}
40
41async fn watch_zed_changes(fs: Arc<dyn fs::Fs>) -> Option<()> {
42 if std::env::var("ZED_HOT_RELOAD").is_err() {
43 return None;
44 }
45 use futures::StreamExt;
46 let mut events = fs
47 .watch(".".as_ref(), std::time::Duration::from_millis(100))
48 .await;
49 let mut current_child: Option<std::process::Child> = None;
50 while let Some(events) = events.next().await {
51 if !events.iter().any(|event| {
52 event
53 .path
54 .to_str()
55 .map(|path| path.contains("/crates/"))
56 .unwrap_or_default()
57 }) {
58 continue;
59 }
60 let child = current_child.take().map(|mut child| child.kill());
61 log::info!("Storybook changed, rebuilding...");
62 current_child = Some(
63 Command::new("cargo")
64 .args(["run", "-p", "storybook"])
65 .spawn()
66 .ok()?,
67 );
68 }
69 Some(())
70}
71
72fn main() {
73 SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
74
75 let args = Args::parse();
76
77 let fs = Arc::new(fs::RealFs);
78
79 gpui2::App::new(Assets).unwrap().run(move |cx| {
80 let mut store = SettingsStore::default();
81 store
82 .set_default_settings(default_settings().as_ref(), cx)
83 .unwrap();
84 cx.set_global(store);
85 legacy_theme::init(Assets, cx);
86 // load_embedded_fonts(cx.platform().as_ref());
87
88 let theme_registry = cx.global::<Arc<ThemeRegistry>>();
89
90 let theme_override = args
91 .theme
92 .and_then(|theme| {
93 theme_registry
94 .list_names(true)
95 .find(|known_theme| theme == *known_theme)
96 })
97 .and_then(|theme_name| theme_registry.get(&theme_name).ok());
98
99 cx.spawn(|_| async move {
100 watch_zed_changes(fs).await;
101 })
102 .detach();
103 cx.add_window(
104 gpui2::WindowOptions {
105 bounds: WindowBounds::Fixed(RectF::new(vec2f(0., 0.), vec2f(1700., 980.))),
106 center: true,
107 ..Default::default()
108 },
109 |cx| match args.story {
110 Some(selector) => view(move |cx| {
111 render_story(
112 &mut ViewContext::new(cx),
113 theme_override.clone(),
114 div().flex().flex_col().h_full().child_any(selector.story()),
115 )
116 }),
117 None => view(move |cx| {
118 render_story(
119 &mut ViewContext::new(cx),
120 theme_override.clone(),
121 WorkspaceElement::default(),
122 )
123 }),
124 },
125 );
126 cx.platform().activate(true);
127 });
128}
129
130fn render_story<V: 'static, S: IntoElement<V>>(
131 cx: &mut ViewContext<V>,
132 theme_override: Option<Arc<legacy_theme::Theme>>,
133 story: S,
134) -> impl Element<V> {
135 let theme = current_theme(cx, theme_override);
136
137 story.into_element().themed(theme)
138}
139
140fn current_theme<V: 'static>(
141 cx: &mut ViewContext<V>,
142 theme_override: Option<Arc<legacy_theme::Theme>>,
143) -> Theme {
144 let legacy_theme =
145 theme_override.unwrap_or_else(|| settings::get::<ThemeSettings>(cx).theme.clone());
146
147 let new_theme: Theme = serde_json::from_value(legacy_theme.base_theme.clone()).unwrap();
148
149 add_base_theme_to_legacy_theme(&legacy_theme, new_theme)
150}
151
152// Nathan: During the transition to gpui2, we will include the base theme on the legacy Theme struct.
153fn add_base_theme_to_legacy_theme(legacy_theme: &legacy_theme::Theme, new_theme: Theme) -> Theme {
154 legacy_theme
155 .deserialized_base_theme
156 .lock()
157 .get_or_insert_with(|| Box::new(new_theme))
158 .downcast_ref::<Theme>()
159 .unwrap()
160 .clone()
161}
162
163use anyhow::{anyhow, Result};
164use gpui2::AssetSource;
165use rust_embed::RustEmbed;
166
167#[derive(RustEmbed)]
168#[folder = "../../assets"]
169#[include = "themes/**/*"]
170#[include = "fonts/**/*"]
171#[include = "icons/**/*"]
172#[exclude = "*.DS_Store"]
173pub struct Assets;
174
175impl AssetSource for Assets {
176 fn load(&self, path: &str) -> Result<std::borrow::Cow<[u8]>> {
177 Self::get(path)
178 .map(|f| f.data)
179 .ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
180 }
181
182 fn list(&self, path: &str) -> Vec<std::borrow::Cow<'static, str>> {
183 Self::iter().filter(|p| p.starts_with(path)).collect()
184 }
185}
186
187// fn load_embedded_fonts(platform: &dyn gpui2::Platform) {
188// let font_paths = Assets.list("fonts");
189// let mut embedded_fonts = Vec::new();
190// for font_path in &font_paths {
191// if font_path.ends_with(".ttf") {
192// let font_path = &*font_path;
193// let font_bytes = Assets.load(font_path).unwrap().to_vec();
194// embedded_fonts.push(Arc::from(font_bytes));
195// }
196// }
197// platform.fonts().add_fonts(&embedded_fonts).unwrap();
198// }