storybook.rs

  1#![allow(dead_code, unused_variables)]
  2
  3mod collab_panel;
  4mod stories;
  5mod story;
  6mod story_selector;
  7mod workspace;
  8
  9use ::theme as legacy_theme;
 10use clap::Parser;
 11use gpui2::{serde_json, vec2f, view, Element, IntoElement, RectF, ViewContext, WindowBounds};
 12use legacy_theme::ThemeSettings;
 13use log::LevelFilter;
 14use settings::{default_settings, SettingsStore};
 15use simplelog::SimpleLogger;
 16use stories::components::breadcrumb::BreadcrumbStory;
 17use stories::components::facepile::FacepileStory;
 18use stories::components::toolbar::ToolbarStory;
 19use stories::components::traffic_lights::TrafficLightsStory;
 20use stories::elements::avatar::AvatarStory;
 21use ui::{ElementExt, Theme};
 22
 23use crate::story_selector::{ComponentStory, ElementStory, StorySelector};
 24
 25gpui2::actions! {
 26    storybook,
 27    [ToggleInspector]
 28}
 29
 30#[derive(Parser)]
 31#[command(author, version, about, long_about = None)]
 32struct Args {
 33    #[arg(value_enum)]
 34    story: Option<StorySelector>,
 35}
 36
 37fn main() {
 38    SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
 39
 40    let args = Args::parse();
 41
 42    gpui2::App::new(Assets).unwrap().run(move |cx| {
 43        let mut store = SettingsStore::default();
 44        store
 45            .set_default_settings(default_settings().as_ref(), cx)
 46            .unwrap();
 47        cx.set_global(store);
 48        legacy_theme::init(Assets, cx);
 49        // load_embedded_fonts(cx.platform().as_ref());
 50
 51        cx.add_window(
 52            gpui2::WindowOptions {
 53                bounds: WindowBounds::Fixed(RectF::new(vec2f(0., 0.), vec2f(1600., 900.))),
 54                center: true,
 55                ..Default::default()
 56            },
 57            |cx| match args.story {
 58                Some(StorySelector::Element(ElementStory::Avatar)) => {
 59                    view(|cx| render_story(&mut ViewContext::new(cx), AvatarStory::default()))
 60                }
 61                Some(StorySelector::Component(ComponentStory::Breadcrumb)) => {
 62                    view(|cx| render_story(&mut ViewContext::new(cx), BreadcrumbStory::default()))
 63                }
 64                Some(StorySelector::Component(ComponentStory::Facepile)) => {
 65                    view(|cx| render_story(&mut ViewContext::new(cx), FacepileStory::default()))
 66                }
 67                Some(StorySelector::Component(ComponentStory::Toolbar)) => {
 68                    view(|cx| render_story(&mut ViewContext::new(cx), ToolbarStory::default()))
 69                }
 70                Some(StorySelector::Component(ComponentStory::TrafficLights)) => view(|cx| {
 71                    render_story(&mut ViewContext::new(cx), TrafficLightsStory::default())
 72                }),
 73                None => {
 74                    view(|cx| render_story(&mut ViewContext::new(cx), WorkspaceElement::default()))
 75                }
 76            },
 77        );
 78        cx.platform().activate(true);
 79    });
 80}
 81
 82fn render_story<V: 'static, S: IntoElement<V>>(
 83    cx: &mut ViewContext<V>,
 84    story: S,
 85) -> impl Element<V> {
 86    story.into_element().themed(current_theme(cx))
 87}
 88
 89// Nathan: During the transition to gpui2, we will include the base theme on the legacy Theme struct.
 90fn current_theme<V: 'static>(cx: &mut ViewContext<V>) -> Theme {
 91    settings::get::<ThemeSettings>(cx)
 92        .theme
 93        .deserialized_base_theme
 94        .lock()
 95        .get_or_insert_with(|| {
 96            let theme: Theme =
 97                serde_json::from_value(settings::get::<ThemeSettings>(cx).theme.base_theme.clone())
 98                    .unwrap();
 99            Box::new(theme)
100        })
101        .downcast_ref::<Theme>()
102        .unwrap()
103        .clone()
104}
105
106use anyhow::{anyhow, Result};
107use gpui2::AssetSource;
108use rust_embed::RustEmbed;
109use workspace::WorkspaceElement;
110
111#[derive(RustEmbed)]
112#[folder = "../../assets"]
113#[include = "themes/**/*"]
114#[include = "fonts/**/*"]
115#[include = "icons/**/*"]
116#[exclude = "*.DS_Store"]
117pub struct Assets;
118
119impl AssetSource for Assets {
120    fn load(&self, path: &str) -> Result<std::borrow::Cow<[u8]>> {
121        Self::get(path)
122            .map(|f| f.data)
123            .ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
124    }
125
126    fn list(&self, path: &str) -> Vec<std::borrow::Cow<'static, str>> {
127        Self::iter().filter(|p| p.starts_with(path)).collect()
128    }
129}
130
131// fn load_embedded_fonts(platform: &dyn gpui2::Platform) {
132//     let font_paths = Assets.list("fonts");
133//     let mut embedded_fonts = Vec::new();
134//     for font_path in &font_paths {
135//         if font_path.ends_with(".ttf") {
136//             let font_path = &*font_path;
137//             let font_bytes = Assets.load(font_path).unwrap().to_vec();
138//             embedded_fonts.push(Arc::from(font_bytes));
139//         }
140//     }
141//     platform.fonts().add_fonts(&embedded_fonts).unwrap();
142// }