mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-03 17:44:30 +00:00
5a149b970c
Some checks are pending
CI / Check formatting and spelling (push) Waiting to run
CI / (macOS) Run Clippy and tests (push) Waiting to run
CI / (Linux) Run Clippy and tests (push) Waiting to run
CI / (Windows) Run Clippy and tests (push) Waiting to run
CI / Create a macOS bundle (push) Blocked by required conditions
CI / Create a Linux bundle (push) Blocked by required conditions
Deploy Docs / Deploy Docs (push) Waiting to run
When running the tests for linux, I found a lot of benign errors getting logged. This PR cuts down some of the noise from unnecessary workspace serialization and SVG renders Release Notes: - N/A
76 lines
2 KiB
Rust
76 lines
2 KiB
Rust
use crate::{size, DevicePixels, Result, SharedString, Size};
|
|
|
|
use image::{Bgra, ImageBuffer};
|
|
use std::{
|
|
borrow::Cow,
|
|
fmt,
|
|
hash::Hash,
|
|
sync::atomic::{AtomicUsize, Ordering::SeqCst},
|
|
};
|
|
|
|
/// A source of assets for this app to use.
|
|
pub trait AssetSource: 'static + Send + Sync {
|
|
/// Load the given asset from the source path.
|
|
fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>>;
|
|
|
|
/// List the assets at the given path.
|
|
fn list(&self, path: &str) -> Result<Vec<SharedString>>;
|
|
}
|
|
|
|
impl AssetSource for () {
|
|
fn load(&self, _path: &str) -> Result<Option<Cow<'static, [u8]>>> {
|
|
Ok(None)
|
|
}
|
|
|
|
fn list(&self, _path: &str) -> Result<Vec<SharedString>> {
|
|
Ok(vec![])
|
|
}
|
|
}
|
|
|
|
/// A unique identifier for the image cache
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
pub struct ImageId(usize);
|
|
|
|
#[derive(PartialEq, Eq, Hash, Clone)]
|
|
pub(crate) struct RenderImageParams {
|
|
pub(crate) image_id: ImageId,
|
|
}
|
|
|
|
/// A cached and processed image.
|
|
pub struct ImageData {
|
|
/// The ID associated with this image
|
|
pub id: ImageId,
|
|
data: ImageBuffer<Bgra<u8>, Vec<u8>>,
|
|
}
|
|
|
|
impl ImageData {
|
|
/// Create a new image from the given data.
|
|
pub fn new(data: ImageBuffer<Bgra<u8>, Vec<u8>>) -> Self {
|
|
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
Self {
|
|
id: ImageId(NEXT_ID.fetch_add(1, SeqCst)),
|
|
data,
|
|
}
|
|
}
|
|
|
|
/// Convert this image into a byte slice.
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
&self.data
|
|
}
|
|
|
|
/// Get the size of this image, in pixels
|
|
pub fn size(&self) -> Size<DevicePixels> {
|
|
let (width, height) = self.data.dimensions();
|
|
size(width.into(), height.into())
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for ImageData {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
f.debug_struct("ImageData")
|
|
.field("id", &self.id)
|
|
.field("size", &self.data.dimensions())
|
|
.finish()
|
|
}
|
|
}
|