Skip to content

Commit

Permalink
Remove Option from scale
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 13, 2023
1 parent 5cdbeff commit e6fae9a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 9 additions & 9 deletions crates/egui/src/widgets/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ impl<'a> Image<'a> {
self
}

/// Fit the image to its original size.
/// Fit the image to its original size with some scaling.
///
/// This will cause the image to overflow if it is larger than the available space.
///
/// If [`Image::max_size`] is set, this is guaranteed to never exceed that limit.
#[inline]
pub fn fit_to_original_size(mut self, scale: Option<f32>) -> Self {
self.size.fit = ImageFit::Original(scale);
pub fn fit_to_original_size(mut self, scale: f32) -> Self {
self.size.fit = ImageFit::Original { scale };
self
}

Expand Down Expand Up @@ -352,8 +352,8 @@ pub struct ImageSize {
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ImageFit {
/// Fit the image to its original size, optionally scaling it by some factor.
Original(Option<f32>),
/// Fit the image to its original size, scaled by some factor.
Original { scale: f32 },

/// Fit the image to a fraction of the available size.
Fraction(Vec2),
Expand All @@ -365,7 +365,7 @@ pub enum ImageFit {
impl ImageFit {
pub fn resolve(self, available_size: Vec2, image_size: Vec2) -> Vec2 {
match self {
ImageFit::Original(scale) => image_size * scale.unwrap_or(1.0),
ImageFit::Original { scale } => image_size * scale,
ImageFit::Fraction(fract) => available_size * fract,
ImageFit::Exact(size) => size,
}
Expand All @@ -379,7 +379,7 @@ impl ImageSize {
};

let fit = match self.fit {
ImageFit::Original(scale) => return SizeHint::Scale(scale.unwrap_or(1.0).ord()),
ImageFit::Original { scale } => return SizeHint::Scale(scale.ord()),
ImageFit::Fraction(fract) => available_size * fract,
ImageFit::Exact(size) => size,
};
Expand All @@ -398,8 +398,8 @@ impl ImageSize {
fn get(&self, available_size: Vec2, image_size: Vec2) -> Vec2 {
let max_size = self.max_size;
match self.fit {
ImageFit::Original(scale) => {
let image_size = image_size * scale.unwrap_or(1.0);
ImageFit::Original { scale } => {
let image_size = image_size * scale;

if image_size.x <= max_size.x && image_size.y <= max_size.y {
image_size
Expand Down
8 changes: 4 additions & 4 deletions crates/egui_demo_app/src/apps/image_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ impl eframe::App for ImageViewer {
ui.add(Slider::new(&mut fract.y, 0.0..=1.0).text("height"));
}
ChosenFit::OriginalSize => {
if !matches!(self.fit, ImageFit::Original(_)) {
self.fit = ImageFit::Original(Some(1.0));
if !matches!(self.fit, ImageFit::Original { .. }) {
self.fit = ImageFit::Original { scale: 1.0 };
}
let ImageFit::Original(Some(scale)) = &mut self.fit else {
let ImageFit::Original{scale} = &mut self.fit else {
unreachable!()
};
ui.add(Slider::new(scale, 0.1..=4.0).text("scale"));
Expand Down Expand Up @@ -196,7 +196,7 @@ impl eframe::App for ImageViewer {
});
image = image.rotate(angle, origin);
match self.fit {
ImageFit::Original(scale) => image = image.fit_to_original_size(scale),
ImageFit::Original { scale } => image = image.fit_to_original_size(scale),
ImageFit::Fraction(fract) => image = image.fit_to_fraction(fract),
ImageFit::Exact(size) => image = image.fit_to_exact_size(size),
}
Expand Down

0 comments on commit e6fae9a

Please sign in to comment.