Finish up ThemeRegistry and remove AnyAssetSource

This commit is contained in:
Marshall Bowers
2023-10-24 17:47:41 +02:00
parent 51c66f508b
commit 4ca7ddfc42
9 changed files with 39 additions and 51 deletions
+3 -4
View File
@@ -13,7 +13,7 @@ use crate::{
ClipboardItem, Context, DispatchPhase, DisplayId, Executor, FocusEvent, FocusHandle, FocusId,
KeyBinding, Keymap, LayoutId, MainThread, MainThreadOnly, Pixels, Platform, Point,
SharedString, SubscriberSet, Subscription, SvgRenderer, Task, TextStyle, TextStyleRefinement,
TextSystem, View, Window, WindowContext, WindowHandle, WindowId, AnyAssetSource,
TextSystem, View, Window, WindowContext, WindowHandle, WindowId,
};
use anyhow::{anyhow, Result};
use collections::{HashMap, HashSet, VecDeque};
@@ -66,7 +66,6 @@ impl App {
os_version: platform.os_version().ok(),
app_version: platform.app_version().ok(),
};
let asset_source = AnyAssetSource(asset_source);
Self(Arc::new_cyclic(|this| {
Mutex::new(AppContext {
@@ -183,7 +182,7 @@ pub struct AppContext {
pub(crate) next_frame_callbacks: HashMap<DisplayId, Vec<FrameCallback>>,
pub(crate) executor: Executor,
pub(crate) svg_renderer: SvgRenderer,
asset_source: AnyAssetSource,
asset_source: Arc<dyn AssetSource>,
pub(crate) image_cache: ImageCache,
pub(crate) text_style_stack: Vec<TextStyleRefinement>,
pub(crate) globals_by_type: HashMap<TypeId, AnyBox>,
@@ -511,7 +510,7 @@ impl AppContext {
});
}
pub fn asset_source(&self) -> &AnyAssetSource {
pub fn asset_source(&self) -> &Arc<dyn AssetSource> {
&self.asset_source
}
+5 -19
View File
@@ -5,37 +5,23 @@ use std::{
borrow::Cow,
fmt,
hash::Hash,
sync::{atomic::{AtomicUsize, Ordering::SeqCst}, Arc},
sync::atomic::{AtomicUsize, Ordering::SeqCst},
};
pub trait AssetSource: 'static + Send + Sync {
fn load(&self, path: SharedString) -> Result<Cow<[u8]>>;
fn list(&self, path: SharedString) -> Result<Vec<SharedString>>;
fn load(&self, path: &str) -> Result<Cow<[u8]>>;
fn list(&self, path: &str) -> Result<Vec<SharedString>>;
}
#[derive(Clone)]
pub struct AnyAssetSource(pub(crate) Arc<dyn AssetSource>);
impl AnyAssetSource {
pub fn load(&self, path: impl Into<SharedString>) -> Result<Cow<[u8]>> {
self.0.load(path.into())
}
pub fn list(&self, path: impl Into<SharedString>) -> Result<Vec<SharedString>> {
self.0.list(path.into())
}
}
impl AssetSource for () {
fn load(&self, path: SharedString) -> Result<Cow<[u8]>> {
fn load(&self, path: &str) -> Result<Cow<[u8]>> {
Err(anyhow!(
"get called on empty asset provider with \"{}\"",
path
))
}
fn list(&self, _path: SharedString) -> Result<Vec<SharedString>> {
fn list(&self, _path: &str) -> Result<Vec<SharedString>> {
Ok(vec![])
}
}
+5 -5
View File
@@ -1,6 +1,6 @@
use crate::{DevicePixels, IsZero, Result, SharedString, Size, AnyAssetSource};
use crate::{DevicePixels, IsZero, Result, SharedString, Size, AssetSource};
use anyhow::anyhow;
use std::hash::Hash;
use std::{hash::Hash, sync::Arc};
#[derive(Clone, PartialEq, Hash, Eq)]
pub struct RenderSvgParams {
@@ -9,11 +9,11 @@ pub struct RenderSvgParams {
}
pub struct SvgRenderer {
asset_source: AnyAssetSource,
asset_source: Arc<dyn AssetSource>,
}
impl SvgRenderer {
pub fn new(asset_source: AnyAssetSource) -> Self {
pub fn new(asset_source: Arc<dyn AssetSource>) -> Self {
Self { asset_source }
}
@@ -23,7 +23,7 @@ impl SvgRenderer {
}
// Load the tree.
let bytes = self.asset_source.load(params.path.clone())?;
let bytes = self.asset_source.load(&params.path)?;
let tree = usvg::Tree::from_data(&bytes, &usvg::Options::default())?;
// Render the SVG to a pixmap with the specified width and height.