diff --git a/crates/story/examples/input.rs b/crates/story/examples/input.rs new file mode 100644 index 00000000..f75e9a07 --- /dev/null +++ b/crates/story/examples/input.rs @@ -0,0 +1,35 @@ +use gpui::*; +use story::{Assets, InputStory}; + +pub struct Example { + root: View, +} + +impl Example { + pub fn new(cx: &mut ViewContext) -> Self { + let root = InputStory::view(cx); + + Self { root } + } + + fn view(cx: &mut WindowContext) -> View { + cx.new_view(Self::new) + } +} + +impl Render for Example { + fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { + div().p_4().size_full().child(self.root.clone()) + } +} + +fn main() { + let app = App::new().with_assets(Assets); + + app.run(move |cx| { + story::init(cx); + cx.activate(true); + + story::create_new_window("Input Example", Example::view, cx); + }); +} diff --git a/crates/story/examples/list.rs b/crates/story/examples/list.rs new file mode 100644 index 00000000..c18258cf --- /dev/null +++ b/crates/story/examples/list.rs @@ -0,0 +1,35 @@ +use gpui::*; +use story::{Assets, ListStory}; + +pub struct Example { + root: View, +} + +impl Example { + pub fn new(cx: &mut ViewContext) -> Self { + let root = ListStory::view(cx); + + Self { root } + } + + fn view(cx: &mut WindowContext) -> View { + cx.new_view(Self::new) + } +} + +impl Render for Example { + fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { + div().p_4().size_full().child(self.root.clone()) + } +} + +fn main() { + let app = App::new().with_assets(Assets); + + app.run(move |cx| { + story::init(cx); + cx.activate(true); + + story::create_new_window("List Example", Example::view, cx); + }); +} diff --git a/crates/story/examples/sidebar.rs b/crates/story/examples/sidebar.rs new file mode 100644 index 00000000..064095e0 --- /dev/null +++ b/crates/story/examples/sidebar.rs @@ -0,0 +1,35 @@ +use gpui::*; +use story::{Assets, SidebarStory}; + +pub struct Example { + root: View, +} + +impl Example { + pub fn new(cx: &mut ViewContext) -> Self { + let root = SidebarStory::view(cx); + + Self { root } + } + + fn view(cx: &mut WindowContext) -> View { + cx.new_view(Self::new) + } +} + +impl Render for Example { + fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { + div().p_4().size_full().child(self.root.clone()) + } +} + +fn main() { + let app = App::new().with_assets(Assets); + + app.run(move |cx| { + story::init(cx); + cx.activate(true); + + story::create_new_window("Sidebar Example", Example::view, cx); + }); +} diff --git a/crates/story/examples/table.rs b/crates/story/examples/table.rs new file mode 100644 index 00000000..b4c9e845 --- /dev/null +++ b/crates/story/examples/table.rs @@ -0,0 +1,35 @@ +use gpui::*; +use story::{Assets, TableStory}; + +pub struct Example { + table: View, +} + +impl Example { + pub fn new(cx: &mut ViewContext) -> Self { + let table = TableStory::view(cx); + + Self { table } + } + + fn view(cx: &mut WindowContext) -> View { + cx.new_view(Self::new) + } +} + +impl Render for Example { + fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { + div().p_4().size_full().child(self.table.clone()) + } +} + +fn main() { + let app = App::new().with_assets(Assets); + + app.run(move |cx| { + story::init(cx); + cx.activate(true); + + story::create_new_window("Table Example", Example::view, cx); + }); +} diff --git a/crates/story/src/lib.rs b/crates/story/src/lib.rs index ca29b0aa..9c50c5e7 100644 --- a/crates/story/src/lib.rs +++ b/crates/story/src/lib.rs @@ -29,10 +29,10 @@ pub use dropdown_story::DropdownStory; pub use form_story::FormStory; use gpui::{ - actions, div, prelude::FluentBuilder as _, px, AnyElement, AnyView, AppContext, Context as _, - Div, EventEmitter, FocusableView, Global, Hsla, InteractiveElement, IntoElement, Model, - ParentElement, Render, SharedString, StatefulInteractiveElement, Styled as _, View, - ViewContext, VisualContext, WindowContext, + actions, div, prelude::FluentBuilder as _, px, size, AnyElement, AnyView, AppContext, Bounds, + Context as _, Div, EventEmitter, FocusableView, Global, Hsla, InteractiveElement, IntoElement, + Model, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled as _, View, + ViewContext, VisualContext, WindowBounds, WindowContext, WindowKind, WindowOptions, }; pub use icon_story::IconStory; pub use image_story::ImageStory; @@ -59,7 +59,7 @@ use ui::{ label::Label, notification::Notification, popup_menu::PopupMenu, - v_flex, ActiveTheme, ContextModal, IconName, + v_flex, ActiveTheme, ContextModal, IconName, Root, }; const PANEL_NAME: &str = "StoryContainer"; @@ -84,9 +84,63 @@ impl AppState { } } +pub fn create_new_window(title: &str, crate_view_fn: F, cx: &mut AppContext) +where + E: Into, + F: FnOnce(&mut WindowContext) -> E + Send + 'static, +{ + let mut window_size = size(px(1600.0), px(1200.0)); + if let Some(display) = cx.primary_display() { + let display_size = display.bounds().size; + window_size.width = window_size.width.min(display_size.width * 0.85); + window_size.height = window_size.height.min(display_size.height * 0.85); + } + let window_bounds = Bounds::centered(None, window_size, cx); + let title = SharedString::from(title.to_string()); + + cx.spawn(|mut cx| async move { + let options = WindowOptions { + window_bounds: Some(WindowBounds::Windowed(window_bounds)), + // titlebar: Some(TitlebarOptions { + // title: Some(title.clone()), + // appears_transparent: true, + // traffic_light_position: Some(point(px(9.0), px(9.0))), + // }), + window_min_size: Some(gpui::Size { + width: px(640.), + height: px(480.), + }), + kind: WindowKind::Normal, + #[cfg(target_os = "linux")] + window_background: gpui::WindowBackgroundAppearance::Transparent, + #[cfg(target_os = "linux")] + window_decorations: Some(gpui::WindowDecorations::Client), + ..Default::default() + }; + + let window = cx + .open_window(options, |cx| { + let view = crate_view_fn(cx); + cx.new_view(|cx| Root::new(view.into(), cx)) + }) + .expect("failed to open window"); + + window + .update(&mut cx, |_, cx| { + cx.activate_window(); + cx.set_window_title(&title); + }) + .expect("failed to update window"); + + Ok::<_, anyhow::Error>(()) + }) + .detach(); +} + impl Global for AppState {} pub fn init(cx: &mut AppContext) { + ui::init(cx); AppState::init(cx); input_story::init(cx); dropdown_story::init(cx);