zed/crates/storybook2/src/storybook2.rs

133 lines
3.5 KiB
Rust
Raw Normal View History

2023-09-20 20:32:55 +00:00
#![allow(dead_code, unused_variables)]
2023-10-04 08:51:47 +00:00
mod assets;
2023-10-04 16:49:06 +00:00
mod stories;
mod story;
mod story_selector;
2023-09-26 19:42:58 +00:00
mod themes;
2023-09-20 20:32:55 +00:00
2023-10-04 17:33:28 +00:00
use std::sync::Arc;
use clap::Parser;
2023-10-21 14:01:47 +00:00
use gpui2::{
div, px, size, AnyView, AppContext, Bounds, ViewContext, VisualContext, WindowBounds,
WindowOptions,
2023-10-04 17:33:28 +00:00
};
use log::LevelFilter;
use settings2::{default_settings, Settings, SettingsStore};
2023-10-04 17:33:28 +00:00
use simplelog::SimpleLogger;
use story_selector::ComponentStory;
use theme2::{ThemeRegistry, ThemeSettings};
2023-10-22 16:25:24 +00:00
use ui::{prelude::*, themed};
2023-10-04 17:33:28 +00:00
use crate::assets::Assets;
use crate::story_selector::StorySelector;
2023-09-20 20:32:55 +00:00
// gpui2::actions! {
// storybook,
// [ToggleInspector]
// }
2023-10-04 17:33:28 +00:00
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(value_enum)]
story: Option<StorySelector>,
/// The name of the theme to use in the storybook.
///
/// If not provided, the default theme will be used.
#[arg(long)]
theme: Option<String>,
}
2023-09-20 20:32:55 +00:00
fn main() {
2023-09-30 02:53:24 +00:00
// unsafe { backtrace_on_stack_overflow::enable() };
2023-09-20 20:32:55 +00:00
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
2023-10-04 17:33:28 +00:00
let args = Args::parse();
let story_selector = args.story.clone();
let theme_name = args.theme.unwrap_or("One Dark".to_string());
let theme = themes::load_theme(theme_name.clone()).unwrap();
2023-10-04 08:51:47 +00:00
let asset_source = Arc::new(Assets);
2023-10-21 14:01:47 +00:00
gpui2::App::production(asset_source).run(move |cx| {
load_embedded_fonts(cx).unwrap();
2023-10-24 10:32:30 +00:00
let mut store = SettingsStore::default();
store
.set_default_settings(default_settings().as_ref(), cx)
.unwrap();
cx.set_global(store);
theme2::init(cx);
let selector =
story_selector.unwrap_or(StorySelector::Component(ComponentStory::Workspace));
let theme_registry = cx.global::<ThemeRegistry>();
let mut theme_settings = ThemeSettings::get_global(cx).clone();
theme_settings.active_theme = theme_registry.get(&theme_name).unwrap();
ThemeSettings::override_global(theme_settings, cx);
2023-10-23 09:34:35 +00:00
cx.set_global(theme.clone());
ui::settings::init(cx);
let window = cx.open_window(
WindowOptions {
bounds: WindowBounds::Fixed(Bounds {
origin: Default::default(),
size: size(px(1700.), px(980.)).into(),
}),
..Default::default()
},
move |cx| {
cx.build_view(
|cx| StoryWrapper::new(selector.story(cx), theme),
StoryWrapper::render,
)
},
);
2023-09-30 16:01:59 +00:00
cx.activate(true);
});
2023-09-20 20:32:55 +00:00
}
2023-10-04 22:25:43 +00:00
#[derive(Clone)]
2023-10-04 17:33:28 +00:00
pub struct StoryWrapper {
story: AnyView,
theme: Theme,
2023-10-04 17:33:28 +00:00
}
impl StoryWrapper {
pub(crate) fn new(story: AnyView, theme: Theme) -> Self {
2023-10-22 16:25:24 +00:00
Self { story, theme }
2023-10-04 17:33:28 +00:00
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {
2023-10-22 16:25:24 +00:00
themed(self.theme.clone(), cx, |cx| {
div()
.flex()
.flex_col()
.size_full()
.child(self.story.clone())
2023-10-04 17:33:28 +00:00
})
}
}
2023-10-24 12:11:13 +00:00
fn load_embedded_fonts(cx: &AppContext) -> gpui2::Result<()> {
2023-10-24 14:59:01 +00:00
let font_paths = cx.asset_source().list("fonts")?;
2023-10-24 10:32:30 +00:00
let mut embedded_fonts = Vec::new();
2023-10-24 14:59:01 +00:00
for font_path in font_paths {
2023-10-24 10:32:30 +00:00
if font_path.ends_with(".ttf") {
let font_bytes = cx.asset_source().load(&font_path)?.to_vec();
2023-10-24 10:32:30 +00:00
embedded_fonts.push(Arc::from(font_bytes));
}
}
cx.text_system().add_fonts(&embedded_fonts)
2023-10-24 10:32:30 +00:00
}