Implement content_fit for viewer
This commit is contained in:
parent
0524e9b457
commit
15f1566578
1 changed files with 49 additions and 77 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
//! Zoom and pan on an image.
|
//! Zoom and pan on an image.
|
||||||
|
use iced_renderer::core::ContentFit;
|
||||||
use crate::core::event::{self, Event};
|
use crate::core::event::{self, Event};
|
||||||
use crate::core::image;
|
use crate::core::image;
|
||||||
use crate::core::layout;
|
use crate::core::layout;
|
||||||
|
|
@ -23,6 +24,7 @@ pub struct Viewer<Handle> {
|
||||||
scale_step: f32,
|
scale_step: f32,
|
||||||
handle: Handle,
|
handle: Handle,
|
||||||
filter_method: image::FilterMethod,
|
filter_method: image::FilterMethod,
|
||||||
|
content_fit: ContentFit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Handle> Viewer<Handle> {
|
impl<Handle> Viewer<Handle> {
|
||||||
|
|
@ -37,6 +39,7 @@ impl<Handle> Viewer<Handle> {
|
||||||
max_scale: 10.0,
|
max_scale: 10.0,
|
||||||
scale_step: 0.10,
|
scale_step: 0.10,
|
||||||
filter_method: image::FilterMethod::default(),
|
filter_method: image::FilterMethod::default(),
|
||||||
|
content_fit: ContentFit::Contain,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,6 +49,12 @@ impl<Handle> Viewer<Handle> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the [`iced_renderer::core::ContentFit`] of the [`Viewer`].
|
||||||
|
pub fn content_fit(mut self, content_fit: ContentFit) -> Self {
|
||||||
|
self.content_fit = content_fit;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the padding of the [`Viewer`].
|
/// Sets the padding of the [`Viewer`].
|
||||||
pub fn padding(mut self, padding: impl Into<Pixels>) -> Self {
|
pub fn padding(mut self, padding: impl Into<Pixels>) -> Self {
|
||||||
self.padding = padding.into().0;
|
self.padding = padding.into().0;
|
||||||
|
|
@ -117,36 +126,25 @@ where
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
limits: &layout::Limits,
|
limits: &layout::Limits,
|
||||||
) -> layout::Node {
|
) -> layout::Node {
|
||||||
let Size { width, height } = renderer.dimensions(&self.handle);
|
let image_size = {
|
||||||
|
let Size { width, height } = renderer.dimensions(&self.handle);
|
||||||
|
Size::new(width as f32, height as f32)
|
||||||
|
};
|
||||||
|
let raw_size = limits.resolve(self.width, self.height, image_size);
|
||||||
|
let full_size = self.content_fit.fit(image_size, raw_size);
|
||||||
|
|
||||||
let mut size = limits.resolve(
|
let final_size = Size {
|
||||||
self.width,
|
width: match self.width {
|
||||||
self.height,
|
Length::Shrink => f32::min(raw_size.width, full_size.width),
|
||||||
Size::new(width as f32, height as f32),
|
_ => raw_size.width,
|
||||||
);
|
},
|
||||||
|
height: match self.height {
|
||||||
let expansion_size = if height > width {
|
Length::Shrink => f32::min(raw_size.height, full_size.height),
|
||||||
self.width
|
_ => raw_size.height,
|
||||||
} else {
|
},
|
||||||
self.height
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only calculate viewport sizes if the images are constrained to a limited space.
|
layout::Node::new(final_size)
|
||||||
// If they are Fill|Portion let them expand within their allotted space.
|
|
||||||
match expansion_size {
|
|
||||||
Length::Shrink | Length::Fixed(_) => {
|
|
||||||
let aspect_ratio = width as f32 / height as f32;
|
|
||||||
let viewport_aspect_ratio = size.width / size.height;
|
|
||||||
if viewport_aspect_ratio > aspect_ratio {
|
|
||||||
size.width = width as f32 * size.height / height as f32;
|
|
||||||
} else {
|
|
||||||
size.height = height as f32 * size.width / width as f32;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Length::Fill | Length::FillPortion(_) => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
layout::Node::new(size)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(
|
fn on_event(
|
||||||
|
|
@ -184,12 +182,7 @@ where
|
||||||
})
|
})
|
||||||
.clamp(self.min_scale, self.max_scale);
|
.clamp(self.min_scale, self.max_scale);
|
||||||
|
|
||||||
let image_size = image_size(
|
let image_size = image_size(renderer, &self.handle, state, bounds.size(), self.content_fit);
|
||||||
renderer,
|
|
||||||
&self.handle,
|
|
||||||
state,
|
|
||||||
bounds.size(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let factor = state.scale / previous_scale - 1.0;
|
let factor = state.scale / previous_scale - 1.0;
|
||||||
|
|
||||||
|
|
@ -231,7 +224,7 @@ where
|
||||||
}
|
}
|
||||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
||||||
let state = tree.state.downcast_mut::<State>();
|
let state = tree.state.downcast_mut::<State>();
|
||||||
|
|
||||||
if state.cursor_grabbed_at.is_some() {
|
if state.cursor_grabbed_at.is_some() {
|
||||||
state.cursor_grabbed_at = None;
|
state.cursor_grabbed_at = None;
|
||||||
|
|
||||||
|
|
@ -242,15 +235,9 @@ where
|
||||||
}
|
}
|
||||||
Event::Mouse(mouse::Event::CursorMoved { position }) => {
|
Event::Mouse(mouse::Event::CursorMoved { position }) => {
|
||||||
let state = tree.state.downcast_mut::<State>();
|
let state = tree.state.downcast_mut::<State>();
|
||||||
|
|
||||||
if let Some(origin) = state.cursor_grabbed_at {
|
if let Some(origin) = state.cursor_grabbed_at {
|
||||||
let image_size = image_size(
|
let image_size = image_size(renderer, &self.handle, state, bounds.size(), self.content_fit);
|
||||||
renderer,
|
|
||||||
&self.handle,
|
|
||||||
state,
|
|
||||||
bounds.size(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let hidden_width = (image_size.width - bounds.width / 2.0)
|
let hidden_width = (image_size.width - bounds.width / 2.0)
|
||||||
.max(0.0)
|
.max(0.0)
|
||||||
.round();
|
.round();
|
||||||
|
|
@ -321,32 +308,30 @@ 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 =
|
let image_size = image_size(renderer, &self.handle, state, bounds.size(), self.content_fit);
|
||||||
image_size(renderer, &self.handle, state, bounds.size());
|
|
||||||
|
|
||||||
let translation = {
|
let translation = {
|
||||||
let image_top_left = Vector::new(
|
let image_top_left = Vector::new(
|
||||||
bounds.width / 2.0 - image_size.width / 2.0,
|
(bounds.width - image_size.width).max(0.0) / 2.0,
|
||||||
bounds.height / 2.0 - image_size.height / 2.0,
|
(bounds.height - image_size.height).max(0.0) / 2.0,
|
||||||
);
|
);
|
||||||
|
|
||||||
image_top_left - state.offset(bounds, image_size)
|
image_top_left - state.offset(bounds, image_size)
|
||||||
};
|
};
|
||||||
|
|
||||||
renderer.with_layer(bounds, |renderer| {
|
let render = |renderer: &mut Renderer| {
|
||||||
renderer.with_translation(translation, |renderer| {
|
renderer.with_translation(translation, |renderer| {
|
||||||
image::Renderer::draw(
|
let drawing_bounds = Rectangle {
|
||||||
renderer,
|
width: image_size.width,
|
||||||
self.handle.clone(),
|
height: image_size.height,
|
||||||
self.filter_method,
|
..bounds
|
||||||
Rectangle {
|
};
|
||||||
x: bounds.x,
|
|
||||||
y: bounds.y,
|
renderer.draw(self.handle.clone(), self.filter_method, drawing_bounds);
|
||||||
..Rectangle::with_size(image_size)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
|
renderer.with_layer(bounds, render);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -417,27 +402,14 @@ pub fn image_size<Renderer>(
|
||||||
handle: &<Renderer as image::Renderer>::Handle,
|
handle: &<Renderer as image::Renderer>::Handle,
|
||||||
state: &State,
|
state: &State,
|
||||||
bounds: Size,
|
bounds: Size,
|
||||||
|
content_fit: ContentFit,
|
||||||
) -> Size
|
) -> Size
|
||||||
where
|
where
|
||||||
Renderer: image::Renderer,
|
Renderer: image::Renderer,
|
||||||
{
|
{
|
||||||
let Size { width, height } = renderer.dimensions(handle);
|
let size = renderer.dimensions(handle);
|
||||||
|
let size = Size::new(size.width as f32, size.height as f32);
|
||||||
|
let size = content_fit.fit(size, bounds);
|
||||||
|
|
||||||
let (width, height) = {
|
Size::new(size.width * state.scale, size.height * state.scale)
|
||||||
let dimensions = (width as f32, height as f32);
|
|
||||||
|
|
||||||
let width_ratio = bounds.width / dimensions.0;
|
|
||||||
let height_ratio = bounds.height / dimensions.1;
|
|
||||||
|
|
||||||
let ratio = width_ratio.min(height_ratio);
|
|
||||||
let scale = state.scale;
|
|
||||||
|
|
||||||
if ratio < 1.0 {
|
|
||||||
(dimensions.0 * ratio * scale, dimensions.1 * ratio * scale)
|
|
||||||
} else {
|
|
||||||
(dimensions.0 * scale, dimensions.1 * scale)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Size::new(width, height)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue