1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

Better define the meaning of SizeHint (#7079)

This commit is contained in:
Emil Ernerfeldt
2025-05-23 13:52:36 +02:00
committed by GitHub
parent ec8b41f7ec
commit 87de733da3
3 changed files with 59 additions and 39 deletions

View File

@@ -151,14 +151,21 @@ pub enum SizeHint {
/// Scale original size by some factor.
Scale(OrderedFloat<f32>),
/// Scale to width.
/// Scale to exactly this pixel width, keeping the original aspect ratio.
Width(u32),
/// Scale to height.
/// Scale to exactly this pixel height, keeping the original aspect ratio.
Height(u32),
/// Scale to size.
Size(u32, u32),
/// Scale to this pixel size.
Size {
width: u32,
height: u32,
/// If true, the image will be as large as possible
/// while still fitting within the given width/height.
maintain_aspect_ratio: bool,
},
}
impl Default for SizeHint {
@@ -168,13 +175,6 @@ impl Default for SizeHint {
}
}
impl From<Vec2> for SizeHint {
#[inline]
fn from(value: Vec2) -> Self {
Self::Size(value.x.round() as u32, value.y.round() as u32)
}
}
/// Represents a byte buffer.
///
/// This is essentially `Cow<'static, [u8]>` but with the `Owned` variant being an `Arc`.

View File

@@ -384,7 +384,11 @@ impl<'a> Image<'a> {
let texture = self.source(ui.ctx()).clone().load(
ui.ctx(),
self.texture_options,
SizeHint::Size(pixel_size.x as _, pixel_size.y as _),
SizeHint::Size {
width: pixel_size.x as _,
height: pixel_size.y as _,
maintain_aspect_ratio: false, // no - just get exactly what we asked for
},
);
paint_texture_load_result(
@@ -481,22 +485,30 @@ impl ImageFit {
impl ImageSize {
/// Size hint for e.g. rasterizing an svg.
pub fn hint(&self, available_size: Vec2, pixels_per_point: f32) -> SizeHint {
let point_size = match self.fit {
let Self {
maintain_aspect_ratio,
max_size,
fit,
} = *self;
let point_size = match fit {
ImageFit::Original { scale } => {
return SizeHint::Scale((pixels_per_point * scale).ord())
}
ImageFit::Fraction(fract) => available_size * fract,
ImageFit::Exact(size) => size,
};
let point_size = point_size.at_most(self.max_size);
let point_size = point_size.at_most(max_size);
let pixel_size = pixels_per_point * point_size;
// `inf` on an axis means "any value"
match (pixel_size.x.is_finite(), pixel_size.y.is_finite()) {
(true, true) => {
SizeHint::Size(pixel_size.x.round() as u32, pixel_size.y.round() as u32)
}
(true, true) => SizeHint::Size {
width: pixel_size.x.round() as u32,
height: pixel_size.y.round() as u32,
maintain_aspect_ratio,
},
(true, false) => SizeHint::Width(pixel_size.x.round() as u32),
(false, true) => SizeHint::Height(pixel_size.y.round() as u32),
(false, false) => SizeHint::Scale(pixels_per_point.ord()),