From 4cf2ba20c22ee5a819674278c0828c68022543c5 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 4 Oct 2023 10:51:47 +0200 Subject: [PATCH] Checkpoint: render SVGs --- crates/gpui3/src/app.rs | 18 ++-- crates/gpui3/src/assets.rs | 4 + crates/gpui3/src/elements/svg.rs | 30 ++---- crates/gpui3/src/gpui3.rs | 4 +- crates/gpui3/src/platform.rs | 12 ++- .../gpui3/src/platform/mac/metal_renderer.rs | 12 +-- crates/gpui3/src/platform/mac/window.rs | 4 +- crates/gpui3/src/svg_library.rs | 102 ------------------ crates/gpui3/src/svg_renderer.rs | 47 ++++++++ crates/gpui3/src/window.rs | 59 ++++++++-- crates/storybook2/src/assets.rs | 30 ++++++ crates/storybook2/src/storybook2.rs | 30 ++---- crates/util/src/arc_cow.rs | 10 ++ 13 files changed, 187 insertions(+), 175 deletions(-) delete mode 100644 crates/gpui3/src/svg_library.rs create mode 100644 crates/gpui3/src/svg_renderer.rs create mode 100644 crates/storybook2/src/assets.rs diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index 124cc55c3c0cf71772669e32412f80bc94d525cc..da0153d383458b415c2f2cae255386a85fed5146 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -8,9 +8,9 @@ pub use model_context::*; use refineable::Refineable; use crate::{ - current_platform, run_on_main, spawn_on_main, Context, LayoutId, MainThread, MainThreadOnly, - Platform, PlatformDispatcher, RootView, TextStyle, TextStyleRefinement, TextSystem, Window, - WindowContext, WindowHandle, WindowId, + current_platform, run_on_main, spawn_on_main, AssetSource, Context, LayoutId, MainThread, + MainThreadOnly, Platform, PlatformDispatcher, RootView, SvgRenderer, TextStyle, + TextStyleRefinement, TextSystem, Window, WindowContext, WindowHandle, WindowId, }; use anyhow::{anyhow, Result}; use collections::{HashMap, VecDeque}; @@ -29,16 +29,18 @@ use util::ResultExt; pub struct App(Arc>>); impl App { - pub fn production() -> Self { - Self::new(current_platform()) + pub fn production(asset_source: Arc) -> Self { + Self::new(current_platform(), asset_source) } #[cfg(any(test, feature = "test"))] pub fn test() -> Self { - Self::new(Arc::new(super::TestPlatform::new())) + let platform = Arc::new(super::TestPlatform::new()); + let asset_source = Arc::new(()); + Self::new(platform, asset_source) } - fn new(platform: Arc) -> Self { + fn new(platform: Arc, asset_source: Arc) -> Self { let dispatcher = platform.dispatcher(); let text_system = Arc::new(TextSystem::new(platform.text_system())); let entities = EntityMap::new(); @@ -49,6 +51,7 @@ impl App { platform: MainThreadOnly::new(platform, dispatcher.clone()), dispatcher, text_system, + svg_renderer: SvgRenderer::new(asset_source), pending_updates: 0, text_style_stack: Vec::new(), state_stacks_by_type: HashMap::default(), @@ -83,6 +86,7 @@ pub struct AppContext { dispatcher: Arc, text_system: Arc, pending_updates: usize, + pub(crate) svg_renderer: SvgRenderer, pub(crate) text_style_stack: Vec, pub(crate) state_stacks_by_type: HashMap>>, pub(crate) unit_entity: Handle<()>, diff --git a/crates/gpui3/src/assets.rs b/crates/gpui3/src/assets.rs index 5d737fcd2f84685c15278fddb9df9df64df7f54b..15af277f6620830adf7a73cbb317eb9572cda3d8 100644 --- a/crates/gpui3/src/assets.rs +++ b/crates/gpui3/src/assets.rs @@ -44,6 +44,10 @@ impl ImageData { &self.data } + pub fn into_bytes(self) -> Vec { + self.data.into_raw() + } + pub fn size(&self) -> Size { let (width, height) = self.data.dimensions(); size(width.into(), height.into()) diff --git a/crates/gpui3/src/elements/svg.rs b/crates/gpui3/src/elements/svg.rs index 70abf63cb4e62fd95af760c8a8538b96e70cf772..dbb4ffb15539f05337a64da45525ecf05f01e37c 100644 --- a/crates/gpui3/src/elements/svg.rs +++ b/crates/gpui3/src/elements/svg.rs @@ -1,9 +1,9 @@ -use crate::{Element, Layout, LayoutId, Result, Style, StyleHelpers, Styled}; +use crate::{Element, Layout, LayoutId, Result, SharedString, Style, StyleHelpers, Styled}; use refineable::RefinementCascade; -use std::{borrow::Cow, marker::PhantomData}; +use std::marker::PhantomData; pub struct Svg { - path: Option>, + path: Option, style: RefinementCascade