diff options
author | 2023-03-09 19:05:38 +0100 | |
---|---|---|
committer | 2023-03-09 19:05:38 +0100 | |
commit | caf2836b1b15bff6e8a2ea72441d67f297eb8707 (patch) | |
tree | 0ffa0d1d604780999892b88de85ee93e3ed7d539 /renderer/src/backend.rs | |
parent | 11b2c3bbe31a43e73a61b9bd9f022233f302ae27 (diff) | |
parent | 424ac8177309440bbd8efe0dd9f7622cb10807ce (diff) | |
download | iced-caf2836b1b15bff6e8a2ea72441d67f297eb8707.tar.gz iced-caf2836b1b15bff6e8a2ea72441d67f297eb8707.tar.bz2 iced-caf2836b1b15bff6e8a2ea72441d67f297eb8707.zip |
Merge pull request #1748 from iced-rs/feature/software-renderer
Software renderer, runtime renderer fallback, and core consolidation
Diffstat (limited to 'renderer/src/backend.rs')
-rw-r--r-- | renderer/src/backend.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/renderer/src/backend.rs b/renderer/src/backend.rs new file mode 100644 index 00000000..e77b708b --- /dev/null +++ b/renderer/src/backend.rs @@ -0,0 +1,98 @@ +use crate::core::text; +use crate::core::{Font, Point, Size}; +use crate::graphics::backend; + +use std::borrow::Cow; + +#[allow(clippy::large_enum_variant)] +pub enum Backend { + #[cfg(feature = "wgpu")] + Wgpu(iced_wgpu::Backend), + #[cfg(feature = "tiny-skia")] + TinySkia(iced_tiny_skia::Backend), +} + +macro_rules! delegate { + ($backend:expr, $name:ident, $body:expr) => { + match $backend { + #[cfg(feature = "wgpu")] + Self::Wgpu($name) => $body, + #[cfg(feature = "tiny-skia")] + Self::TinySkia($name) => $body, + } + }; +} + +impl iced_graphics::Backend for Backend { + fn trim_measurements(&mut self) { + delegate!(self, backend, backend.trim_measurements()); + } +} + +impl backend::Text for Backend { + const ICON_FONT: Font = Font::Name("Iced-Icons"); + const CHECKMARK_ICON: char = '\u{f00c}'; + const ARROW_DOWN_ICON: char = '\u{e800}'; + + fn default_font(&self) -> Font { + delegate!(self, backend, backend.default_font()) + } + + fn default_size(&self) -> f32 { + delegate!(self, backend, backend.default_size()) + } + + fn measure( + &self, + contents: &str, + size: f32, + font: Font, + bounds: Size, + ) -> (f32, f32) { + delegate!(self, backend, backend.measure(contents, size, font, bounds)) + } + + fn hit_test( + &self, + contents: &str, + size: f32, + font: Font, + bounds: Size, + position: Point, + nearest_only: bool, + ) -> Option<text::Hit> { + delegate!( + self, + backend, + backend.hit_test( + contents, + size, + font, + bounds, + position, + nearest_only + ) + ) + } + + fn load_font(&mut self, font: Cow<'static, [u8]>) { + delegate!(self, backend, backend.load_font(font)); + } +} + +#[cfg(feature = "image")] +impl backend::Image for Backend { + fn dimensions(&self, handle: &crate::core::image::Handle) -> Size<u32> { + delegate!(self, backend, backend.dimensions(handle)) + } +} + +#[cfg(feature = "svg")] +impl backend::Svg for Backend { + fn viewport_dimensions( + &self, + handle: &crate::core::svg::Handle, + ) -> Size<u32> { + delegate!(self, backend, backend.viewport_dimensions(handle)) + } +} |