diff --git a/crates/gpui/src/image_cache.rs b/crates/gpui/src/image_cache.rs index d7682a43fd201935ae8683e1e73c166091f8efa7..00d62a16a5e17c4301cc8bf9fe042d6ddefe05d4 100644 --- a/crates/gpui/src/image_cache.rs +++ b/crates/gpui/src/image_cache.rs @@ -84,7 +84,6 @@ impl ImageCache { let format = image::guess_format(&body)?; let image = image::load_from_memory_with_format(&body, format)?.into_bgra8(); - Ok(ImageData::new(image)) } } diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index da0153d383458b415c2f2cae255386a85fed5146..8fbccaa83828ada97ae45060dc3eb4a8d8b7ea3b 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, AssetSource, Context, LayoutId, MainThread, - MainThreadOnly, Platform, PlatformDispatcher, RootView, SvgRenderer, TextStyle, - TextStyleRefinement, TextSystem, Window, WindowContext, WindowHandle, WindowId, + current_platform, image_cache::ImageCache, 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}; @@ -23,24 +23,33 @@ use std::{ mem, sync::{Arc, Weak}, }; -use util::ResultExt; +use util::{ + http::{self, HttpClient}, + ResultExt, +}; #[derive(Clone)] pub struct App(Arc>>); impl App { pub fn production(asset_source: Arc) -> Self { - Self::new(current_platform(), asset_source) + let http_client = http::client(); + Self::new(current_platform(), asset_source, http_client) } #[cfg(any(test, feature = "test"))] pub fn test() -> Self { let platform = Arc::new(super::TestPlatform::new()); let asset_source = Arc::new(()); - Self::new(platform, asset_source) + let http_client = util::http::FakeHttpClient::with_404_response(); + Self::new(platform, asset_source, http_client) } - fn new(platform: Arc, asset_source: Arc) -> Self { + fn new( + platform: Arc, + asset_source: Arc, + http_client: Arc, + ) -> Self { let dispatcher = platform.dispatcher(); let text_system = Arc::new(TextSystem::new(platform.text_system())); let entities = EntityMap::new(); @@ -52,6 +61,7 @@ impl App { dispatcher, text_system, svg_renderer: SvgRenderer::new(asset_source), + image_cache: ImageCache::new(http_client), pending_updates: 0, text_style_stack: Vec::new(), state_stacks_by_type: HashMap::default(), @@ -87,6 +97,7 @@ pub struct AppContext { text_system: Arc, pending_updates: usize, pub(crate) svg_renderer: SvgRenderer, + pub(crate) image_cache: ImageCache, 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 15af277f6620830adf7a73cbb317eb9572cda3d8..f1e90f6c4e25a9cd6984c72e5c49eb5306aabff2 100644 --- a/crates/gpui3/src/assets.rs +++ b/crates/gpui3/src/assets.rs @@ -3,7 +3,9 @@ use anyhow::anyhow; use image::{Bgra, ImageBuffer}; use std::{ borrow::Cow, + cmp::Ordering, fmt, + hash::{Hash, Hasher}, sync::atomic::{AtomicUsize, Ordering::SeqCst}, }; @@ -25,18 +27,21 @@ impl AssetSource for () { } } +#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub struct ImageId(usize); + pub struct ImageData { - pub id: usize, + pub id: ImageId, data: ImageBuffer, Vec>, } impl ImageData { - pub fn from_raw(size: Size, bytes: Vec) -> Self { + pub fn new(data: ImageBuffer, Vec>) -> Self { static NEXT_ID: AtomicUsize = AtomicUsize::new(0); Self { - id: NEXT_ID.fetch_add(1, SeqCst), - data: ImageBuffer::from_raw(size.width.into(), size.height.into(), bytes).unwrap(), + id: ImageId(NEXT_ID.fetch_add(1, SeqCst)), + data, } } @@ -44,10 +49,6 @@ 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/img.rs b/crates/gpui3/src/elements/img.rs index 63f3e78d3db58ff928762d788cea9120ddb21c94..e0c85f80560d75cd6b93f7c525d12d890b4010fa 100644 --- a/crates/gpui3/src/elements/img.rs +++ b/crates/gpui3/src/elements/img.rs @@ -1,11 +1,14 @@ -use crate::{Element, Layout, LayoutId, Result, Style, StyleHelpers, Styled}; +use crate::{ + Element, Layout, LayoutId, Result, SharedString, Style, StyleHelpers, Styled, ViewContext, +}; +use futures::FutureExt; use refineable::RefinementCascade; use std::marker::PhantomData; -use util::arc_cow::ArcCow; +use util::ResultExt; pub struct Img { style: RefinementCascade