ZIm/crates/storybook/src/storybook.rs
张小白 63a5f46df4
Remember window restore size (#10429)
Now, regardless of how the Zed window is closed, Zed can remember the
window's restore size.

- [x] Windows implementation
- [x] macOS implementation
- [x] Linux implementation (partial)
- [x] update SQL data base (mark column `fullscreen` as deprecated)

The current implementation on Linux is basic, and I'm not sure if it's
correct.

The variable `fullscreen` in SQL can be removed, but I'm unsure how to
do it.
edit: mark `fullscreen` as deprecated

### Case 1

When the window is closed as maximized, reopening it will open in the
maximized state, and returning from maximized state will restore the
position and size it had when it was maximized.



https://github.com/zed-industries/zed/assets/14981363/7207752e-878a-4d43-93a7-41ad1fdb3a06


### Case 2

When the window is closed as fullscreen, reopening it will open in
fullscreen mode, and toggling fullscreen will restore the position and
size it had when it entered fullscreen (note that the fullscreen
application was not recorded in the video, showing a black screen, but
it had actually entered fullscreen mode).



https://github.com/zed-industries/zed/assets/14981363/ea5aa70d-b296-462a-afb3-4c3372883ea3

### What's more

- As English is not my native language, some variable and struct names
may need to be modified to match their actual meaning.
- I am not familiar with the APIs related to macOS and Linux, so
implementation for these two platforms has not been done for now.
- Any suggestions and ideas are welcome.

Release Notes:

- N/A
2024-05-07 23:29:03 -06:00

153 lines
4.1 KiB
Rust

mod actions;
mod app_menus;
mod assets;
mod stories;
mod story_selector;
use clap::Parser;
use dialoguer::FuzzySelect;
use gpui::{
div, px, size, AnyView, AppContext, Bounds, Render, ViewContext, VisualContext, WindowBounds,
WindowOptions,
};
use log::LevelFilter;
use project::Project;
use settings::{KeymapFile, Settings};
use simplelog::SimpleLogger;
use strum::IntoEnumIterator;
use theme::{ThemeRegistry, ThemeSettings};
use ui::prelude::*;
use crate::app_menus::app_menus;
use crate::assets::Assets;
use crate::story_selector::{ComponentStory, StorySelector};
use actions::Quit;
pub use indoc::indoc;
#[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>,
}
fn main() {
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
menu::init();
let args = Args::parse();
let story_selector = args.story.unwrap_or_else(|| {
let stories = ComponentStory::iter().collect::<Vec<_>>();
ctrlc::set_handler(move || {}).unwrap();
let result = FuzzySelect::new()
.with_prompt("Choose a story to run:")
.items(&stories)
.interact();
let Ok(selection) = result else {
dialoguer::console::Term::stderr().show_cursor().unwrap();
std::process::exit(0);
};
StorySelector::Component(stories[selection])
});
let theme_name = args.theme.unwrap_or("One Dark".to_string());
gpui::App::new().with_assets(Assets).run(move |cx| {
load_embedded_fonts(cx).unwrap();
settings::init(cx);
theme::init(theme::LoadThemes::All(Box::new(Assets)), cx);
let selector = story_selector;
let theme_registry = ThemeRegistry::global(cx);
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);
language::init(cx);
editor::init(cx);
Project::init_settings(cx);
init(cx);
load_storybook_keymap(cx);
cx.set_menus(app_menus());
let size = size(px(1500.), px(780.));
let bounds = Bounds::centered(None, size, cx);
let _window = cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
move |cx| {
let ui_font_size = ThemeSettings::get_global(cx).ui_font_size;
cx.set_rem_size(ui_font_size);
cx.new_view(|cx| StoryWrapper::new(selector.story(cx)))
},
);
cx.activate(true);
});
}
#[derive(Clone)]
pub struct StoryWrapper {
story: AnyView,
}
impl StoryWrapper {
pub(crate) fn new(story: AnyView) -> Self {
Self { story }
}
}
impl Render for StoryWrapper {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.size_full()
.font_family("Zed Mono")
.child(self.story.clone())
}
}
fn load_embedded_fonts(cx: &AppContext) -> gpui::Result<()> {
let font_paths = cx.asset_source().list("fonts")?;
let mut embedded_fonts = Vec::new();
for font_path in font_paths {
if font_path.ends_with(".ttf") {
let font_bytes = cx.asset_source().load(&font_path)?;
embedded_fonts.push(font_bytes);
}
}
cx.text_system().add_fonts(embedded_fonts)
}
fn load_storybook_keymap(cx: &mut AppContext) {
KeymapFile::load_asset("keymaps/storybook.json", cx).unwrap();
}
pub fn init(cx: &mut AppContext) {
cx.on_action(quit);
}
fn quit(_: &Quit, cx: &mut AppContext) {
cx.spawn(|cx| async move {
cx.update(|cx| cx.quit())?;
anyhow::Ok(())
})
.detach_and_log_err(cx);
}