diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-10-05 23:23:09 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-10-05 23:23:09 +0200 |
| commit | 61fdc85b13824c2f80e62ff12fe654465d850181 (patch) | |
| tree | 2ff19286c3240360b81bb2fd7492f5d9a7819cf9 /src/layout/image.rs | |
| parent | b69c0355ecda6a2da275cac42fcb0dfa9e31581b (diff) | |
Refactor a bit
Diffstat (limited to 'src/layout/image.rs')
| -rw-r--r-- | src/layout/image.rs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/layout/image.rs b/src/layout/image.rs index 81c48b3e..4327375b 100644 --- a/src/layout/image.rs +++ b/src/layout/image.rs @@ -17,28 +17,29 @@ impl Layout for ImageNode { fn layout( &self, ctx: &mut LayoutContext, - &Regions { current, base, expand, .. }: &Regions, + regions: &Regions, ) -> Vec<Constrained<Rc<Frame>>> { - let mut constraints = Constraints::new(expand); - constraints.set_base_if_linear(base, Spec::new(self.width, self.height)); - - let width = self.width.map(|w| w.resolve(base.w)); - let height = self.height.map(|w| w.resolve(base.h)); - let img = ctx.images.get(self.id); let pixel_size = Spec::new(img.width() as f64, img.height() as f64); let pixel_ratio = pixel_size.x / pixel_size.y; + let mut constraints = Constraints::new(regions.expand); + constraints.set_base_if_linear(regions.base, Spec::new(self.width, self.height)); + + let width = self.width.map(|w| w.resolve(regions.base.w)); + let height = self.height.map(|w| w.resolve(regions.base.h)); + let size = match (width, height) { (Some(width), Some(height)) => Size::new(width, height), (Some(width), None) => Size::new(width, width / pixel_ratio), (None, Some(height)) => Size::new(height * pixel_ratio, height), (None, None) => { - constraints.exact.x = Some(current.w); - if current.w.is_finite() { - Size::new(current.w, current.w / pixel_ratio) + constraints.exact.x = Some(regions.current.w); + if regions.current.w.is_finite() { + // Fit to width. + Size::new(regions.current.w, regions.current.w / pixel_ratio) } else { - // Totally unbounded region, we have to make up something, + // Unbounded width, we have to make up something, // so it is 1pt per pixel. pixel_size.map(Length::pt).to_size() } |
