diff options
author | 2019-11-10 06:05:20 +0100 | |
---|---|---|
committer | 2019-11-11 03:08:00 +0100 | |
commit | 0240c3981b716c82ecb3364945815335b420a63e (patch) | |
tree | 441eebaa9441649a4e878bde71cdec20d4a67391 /wgpu/src/renderer/widget/image.rs | |
parent | 2303111e09d806ef2a652bddc2b73be6dccf6ae2 (diff) | |
download | iced-0240c3981b716c82ecb3364945815335b420a63e.tar.gz iced-0240c3981b716c82ecb3364945815335b420a63e.tar.bz2 iced-0240c3981b716c82ecb3364945815335b420a63e.zip |
Draft custom layout engine based on `druid`
Diffstat (limited to 'wgpu/src/renderer/widget/image.rs')
-rw-r--r-- | wgpu/src/renderer/widget/image.rs | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/wgpu/src/renderer/widget/image.rs b/wgpu/src/renderer/widget/image.rs index ea9d19c7..0afb11e3 100644 --- a/wgpu/src/renderer/widget/image.rs +++ b/wgpu/src/renderer/widget/image.rs @@ -1,18 +1,31 @@ use crate::{Primitive, Renderer}; -use iced_native::{image, layout, Image, Layout, MouseCursor, Rectangle}; +use iced_native::{image, layout, Image, Layout, Length, MouseCursor, Size}; impl image::Renderer for Renderer { - fn layout(&self, image: &Image, limits: &layout::Limits) -> Layout { - // TODO - Layout::new(Rectangle { - x: 0.0, - y: 0.0, - width: 0.0, - height: 0.0, - }) + fn layout(&self, image: &Image, limits: &layout::Limits) -> layout::Node { + let (width, height) = self.image_pipeline.dimensions(&image.path); + + let aspect_ratio = width as f32 / height as f32; + + // TODO: Deal with additional cases + let (width, height) = match (image.width, image.height) { + (Length::Units(width), _) => ( + image.width, + Length::Units((width as f32 / aspect_ratio).round() as u16), + ), + (_, _) => { + (Length::Units(width as u16), Length::Units(height as u16)) + } + }; + + let mut size = limits.width(width).height(height).resolve(Size::ZERO); + + size.height = size.width / aspect_ratio; + + layout::Node::new(size) } - fn draw(&mut self, image: &Image, layout: &Layout) -> Self::Output { + fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output { ( Primitive::Image { path: image.path.clone(), |