1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

LibGUI+PixelPaint: Move fit_image_to_view to AbstractZoomPanWidget

We often want to zoom and fit the content of the widget into the
visible frame (or some rect within the frame), and it makes sense to
move this functionality into the AbstractZoomPanWidget to minimize
the amount of coordinate math derived classes need to do.

This commit moves the code that implements this functionality from
`PixelPaint::ImageEditor` into `AbstractZoomPanWidget` so that we
can also use it for other applications (such as ImageViewer!)
This commit is contained in:
Mustafa Quraish 2022-01-21 02:53:49 -05:00 committed by Andreas Kling
parent 12daecf72d
commit 5c763e9832
5 changed files with 39 additions and 30 deletions

View file

@ -180,4 +180,29 @@ void AbstractZoomPanWidget::set_scale_bounds(float min_scale, float max_scale)
m_max_scale = max_scale;
}
void AbstractZoomPanWidget::fit_content_to_rect(Gfx::IntRect const& viewport_rect, FitType type)
{
const float border_ratio = 0.95f;
auto image_size = m_original_rect.size();
auto height_ratio = floorf(border_ratio * viewport_rect.height()) / (float)image_size.height();
auto width_ratio = floorf(border_ratio * viewport_rect.width()) / (float)image_size.width();
float new_scale = 1.0f;
switch (type) {
case FitType::Width:
new_scale = width_ratio;
break;
case FitType::Height:
new_scale = height_ratio;
break;
case FitType::Both:
new_scale = min(height_ratio, width_ratio);
break;
}
auto const& offset = rect().center() - viewport_rect.center();
set_origin(Gfx::FloatPoint(offset.x(), offset.y()));
set_scale(new_scale);
}
}