From d344c9d9a3b08659d1edf01bb83624b3bf1029ab Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 11 Jan 2021 18:14:34 +0100 Subject: [PATCH] Image and ImageButton will no longer stretch to fill a justified layout --- CHANGELOG.md | 1 + egui/src/ui.rs | 6 +++--- egui/src/widgets/button.rs | 10 +++++----- egui/src/widgets/image.rs | 14 +++++++------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb07e0bf..d9c71772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Fixed 🐛 * Fixed a bug that would sometimes trigger a "Mismatching panels" panic in debug builds. +* `Image` and `ImageButton` will no longer stretch to fill a justified layout. ## 0.7.0 - 2021-01-04 diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 8a602001..a22bb15d 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -822,9 +822,9 @@ impl Ui { response } - /// Show an image here with the given size - pub fn image(&mut self, texture_id: TextureId, desired_size: impl Into) -> Response { - self.add(Image::new(texture_id, desired_size)) + /// Show an image here with the given size. + pub fn image(&mut self, texture_id: TextureId, size: impl Into) -> Response { + self.add(Image::new(texture_id, size)) } } diff --git a/egui/src/widgets/button.rs b/egui/src/widgets/button.rs index 48a058a1..c863e51b 100644 --- a/egui/src/widgets/button.rs +++ b/egui/src/widgets/button.rs @@ -328,9 +328,9 @@ pub struct ImageButton { } impl ImageButton { - pub fn new(texture_id: TextureId, desired_size: impl Into) -> Self { + pub fn new(texture_id: TextureId, size: impl Into) -> Self { Self { - image: widgets::Image::new(texture_id, desired_size), + image: widgets::Image::new(texture_id, size), sense: Sense::click(), frame: true, selected: false, @@ -379,8 +379,8 @@ impl Widget for ImageButton { } = self; let button_padding = ui.style().spacing.button_padding; - let desired_size = image.desired_size() + 2.0 * button_padding; - let (rect, response) = ui.allocate_at_least(desired_size, sense); + let size = image.size() + 2.0 * button_padding; + let (rect, response) = ui.allocate_exact_size(size, sense); if ui.clip_rect().intersects(rect) { let visuals = ui.style().interact(&response); @@ -400,7 +400,7 @@ impl Widget for ImageButton { let image_rect = ui .layout() - .align_size_within_rect(image.desired_size(), rect.shrink2(button_padding)); + .align_size_within_rect(image.size(), rect.shrink2(button_padding)); image.paint_at(ui, image_rect); } diff --git a/egui/src/widgets/image.rs b/egui/src/widgets/image.rs index 2835cae1..5b9e79ae 100644 --- a/egui/src/widgets/image.rs +++ b/egui/src/widgets/image.rs @@ -6,17 +6,17 @@ use crate::*; pub struct Image { texture_id: TextureId, uv: Rect, - desired_size: Vec2, + size: Vec2, bg_fill: Color32, tint: Color32, } impl Image { - pub fn new(texture_id: TextureId, desired_size: impl Into) -> Self { + pub fn new(texture_id: TextureId, size: impl Into) -> Self { Self { texture_id, uv: Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), - desired_size: desired_size.into(), + size: size.into(), bg_fill: Default::default(), tint: Color32::WHITE, } @@ -42,8 +42,8 @@ impl Image { } impl Image { - pub fn desired_size(&self) -> Vec2 { - self.desired_size + pub fn size(&self) -> Vec2 { + self.size } pub fn paint_at(&self, ui: &mut Ui, rect: Rect) { @@ -51,7 +51,7 @@ impl Image { let Self { texture_id, uv, - desired_size: _, + size: _, bg_fill, tint, } = self; @@ -73,7 +73,7 @@ impl Image { impl Widget for Image { fn ui(self, ui: &mut Ui) -> Response { - let (rect, response) = ui.allocate_at_least(self.desired_size, Sense::hover()); + let (rect, response) = ui.allocate_exact_size(self.size, Sense::hover()); self.paint_at(ui, rect); response }