storybook.rs

  1#![allow(dead_code, unused_variables)]
  2
  3use crate::theme::Theme;
  4use ::theme as legacy_theme;
  5use element_ext::ElementExt;
  6use gpui2::{serde_json, vec2f, view, Element, RectF, ViewContext, WindowBounds};
  7use legacy_theme::ThemeSettings;
  8use log::LevelFilter;
  9use settings::{default_settings, SettingsStore};
 10use simplelog::SimpleLogger;
 11
 12mod collab_panel;
 13mod components;
 14mod element_ext;
 15mod prelude;
 16mod theme;
 17mod ui;
 18mod workspace;
 19
 20gpui2::actions! {
 21    storybook,
 22    [ToggleInspector]
 23}
 24
 25fn main() {
 26    SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
 27
 28    gpui2::App::new(Assets).unwrap().run(|cx| {
 29        let mut store = SettingsStore::default();
 30        store
 31            .set_default_settings(default_settings().as_ref(), cx)
 32            .unwrap();
 33        cx.set_global(store);
 34        legacy_theme::init(Assets, cx);
 35        // load_embedded_fonts(cx.platform().as_ref());
 36
 37        cx.add_window(
 38            gpui2::WindowOptions {
 39                bounds: WindowBounds::Fixed(RectF::new(vec2f(0., 0.), vec2f(1600., 900.))),
 40                center: true,
 41                ..Default::default()
 42            },
 43            |cx| {
 44                view(|cx| {
 45                    // cx.enable_inspector();
 46                    storybook(&mut ViewContext::new(cx))
 47                })
 48            },
 49        );
 50        cx.platform().activate(true);
 51    });
 52}
 53
 54fn storybook<V: 'static>(cx: &mut ViewContext<V>) -> impl Element<V> {
 55    workspace().themed(current_theme(cx))
 56}
 57
 58// Nathan: During the transition to gpui2, we will include the base theme on the legacy Theme struct.
 59fn current_theme<V: 'static>(cx: &mut ViewContext<V>) -> Theme {
 60    settings::get::<ThemeSettings>(cx)
 61        .theme
 62        .deserialized_base_theme
 63        .lock()
 64        .get_or_insert_with(|| {
 65            let theme: Theme =
 66                serde_json::from_value(settings::get::<ThemeSettings>(cx).theme.base_theme.clone())
 67                    .unwrap();
 68            Box::new(theme)
 69        })
 70        .downcast_ref::<Theme>()
 71        .unwrap()
 72        .clone()
 73}
 74
 75use anyhow::{anyhow, Result};
 76use gpui2::AssetSource;
 77use rust_embed::RustEmbed;
 78use workspace::workspace;
 79
 80#[derive(RustEmbed)]
 81#[folder = "../../assets"]
 82#[include = "themes/**/*"]
 83#[include = "fonts/**/*"]
 84#[include = "icons/**/*"]
 85#[exclude = "*.DS_Store"]
 86pub struct Assets;
 87
 88impl AssetSource for Assets {
 89    fn load(&self, path: &str) -> Result<std::borrow::Cow<[u8]>> {
 90        Self::get(path)
 91            .map(|f| f.data)
 92            .ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
 93    }
 94
 95    fn list(&self, path: &str) -> Vec<std::borrow::Cow<'static, str>> {
 96        Self::iter().filter(|p| p.starts_with(path)).collect()
 97    }
 98}
 99
100// fn load_embedded_fonts(platform: &dyn gpui2::Platform) {
101//     let font_paths = Assets.list("fonts");
102//     let mut embedded_fonts = Vec::new();
103//     for font_path in &font_paths {
104//         if font_path.ends_with(".ttf") {
105//             let font_path = &*font_path;
106//             let font_bytes = Assets.load(font_path).unwrap().to_vec();
107//             embedded_fonts.push(Arc::from(font_bytes));
108//         }
109//     }
110//     platform.fonts().add_fonts(&embedded_fonts).unwrap();
111// }