Make viewer widget inner naming and syntax closer to image widget

This commit is contained in:
gigas002 2024-05-10 01:31:23 +00:00
parent 22d55ccecb
commit c95e296962

View file

@ -1,6 +1,6 @@
//! Zoom and pan on an image. //! Zoom and pan on an image.
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::image; use crate::core::image::{self, FilterMethod};
use crate::core::layout; use crate::core::layout;
use crate::core::mouse; use crate::core::mouse;
use crate::core::renderer; use crate::core::renderer;
@ -20,27 +20,27 @@ pub struct Viewer<Handle> {
max_scale: f32, max_scale: f32,
scale_step: f32, scale_step: f32,
handle: Handle, handle: Handle,
filter_method: image::FilterMethod, filter_method: FilterMethod,
content_fit: ContentFit, content_fit: ContentFit,
} }
impl<Handle> Viewer<Handle> { impl<Handle> Viewer<Handle> {
/// Creates a new [`Viewer`] with the given [`State`]. /// Creates a new [`Viewer`] with the given [`State`].
pub fn new(handle: Handle) -> Self { pub fn new<T: Into<Handle>>(handle: T) -> Self {
Viewer { Viewer {
handle, handle: handle.into(),
padding: 0.0, padding: 0.0,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
min_scale: 0.25, min_scale: 0.25,
max_scale: 10.0, max_scale: 10.0,
scale_step: 0.10, scale_step: 0.10,
filter_method: image::FilterMethod::default(), filter_method: FilterMethod::default(),
content_fit: ContentFit::Contain, content_fit: ContentFit::default(),
} }
} }
/// Sets the [`image::FilterMethod`] of the [`Viewer`]. /// Sets the [`FilterMethod`] of the [`Viewer`].
pub fn filter_method(mut self, filter_method: image::FilterMethod) -> Self { pub fn filter_method(mut self, filter_method: image::FilterMethod) -> Self {
self.filter_method = filter_method; self.filter_method = filter_method;
self self
@ -124,25 +124,24 @@ where
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
// The raw w/h of the underlying image // The raw w/h of the underlying image
let image_size = { let image_size = renderer.measure_image(&self.handle);
let Size { width, height } = renderer.measure_image(&self.handle); let image_size =
Size::new(width as f32, height as f32) Size::new(image_size.width as f32, image_size.height as f32);
};
// The size to be available to the widget prior to `Shrink`ing // The size to be available to the widget prior to `Shrink`ing
let raw_size = limits.resolve(self.width, self.height, image_size); let raw_size = limits.resolve(self.width, self.height, image_size);
// The uncropped size of the image when fit to the bounds above // The uncropped size of the image when fit to the bounds above
let fit_size = self.content_fit.fit(image_size, raw_size); let full_size = self.content_fit.fit(image_size, raw_size);
// Shrink the widget to fit the resized image, if requested // Shrink the widget to fit the resized image, if requested
let final_size = Size { let final_size = Size {
width: match self.width { width: match self.width {
Length::Shrink => f32::min(raw_size.width, fit_size.width), Length::Shrink => f32::min(raw_size.width, full_size.width),
_ => raw_size.width, _ => raw_size.width,
}, },
height: match self.height { height: match self.height {
Length::Shrink => f32::min(raw_size.height, fit_size.height), Length::Shrink => f32::min(raw_size.height, full_size.height),
_ => raw_size.height, _ => raw_size.height,
}, },
}; };
@ -323,7 +322,7 @@ where
let state = tree.state.downcast_ref::<State>(); let state = tree.state.downcast_ref::<State>();
let bounds = layout.bounds(); let bounds = layout.bounds();
let image_size = scaled_image_size( let final_size = scaled_image_size(
renderer, renderer,
&self.handle, &self.handle,
state, state,
@ -332,8 +331,8 @@ where
); );
let translation = { let translation = {
let diff_w = bounds.width - image_size.width; let diff_w = bounds.width - final_size.width;
let diff_h = bounds.height - image_size.height; let diff_h = bounds.height - final_size.height;
let image_top_left = match self.content_fit { let image_top_left = match self.content_fit {
ContentFit::None => { ContentFit::None => {
@ -342,9 +341,10 @@ where
_ => Vector::new(diff_w / 2.0, diff_h / 2.0), _ => Vector::new(diff_w / 2.0, diff_h / 2.0),
}; };
image_top_left - state.offset(bounds, image_size) image_top_left - state.offset(bounds, final_size)
}; };
let drawing_bounds = Rectangle::new(bounds.position(), image_size);
let drawing_bounds = Rectangle::new(bounds.position(), final_size);
let render = |renderer: &mut Renderer| { let render = |renderer: &mut Renderer| {
renderer.with_translation(translation, |renderer| { renderer.with_translation(translation, |renderer| {
@ -434,11 +434,13 @@ pub fn scaled_image_size<Renderer>(
where where
Renderer: image::Renderer, Renderer: image::Renderer,
{ {
let image_size = { let Size { width, height } = renderer.measure_image(handle);
let Size { width, height } = renderer.measure_image(handle); let image_size = Size::new(width as f32, height as f32);
Size::new(width as f32, height as f32)
};
let fit_size = content_fit.fit(image_size, bounds);
Size::new(fit_size.width * state.scale, fit_size.height * state.scale) let adjusted_fit = content_fit.fit(image_size, bounds);
Size::new(
adjusted_fit.width * state.scale,
adjusted_fit.height * state.scale,
)
} }