From f890f8529ae882795b7358fab5cdc524dc87da7e Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sat, 11 Sep 2021 14:31:41 -0400 Subject: [PATCH] PixelPaint: Make `Fit Image To View` account for rulers Because of the way rulers are implemented in the ImageEditor currently, the `Fit Image To View` action doesn't work correctly with them enabled. This patch makes them adjust to the effective viewport area and account for the rulers. This is a bit of a hack, but the correct way to deal with this would be to put the rulers in a new widget so they don't interfere with the actual viewport rect (which is being used all over). --- .../Applications/PixelPaint/ImageEditor.cpp | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index 9c23d4f065..d34f7b3d51 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -562,13 +562,27 @@ void ImageEditor::scale_by(float scale_delta) void ImageEditor::fit_image_to_view() { + auto viewport_rect = rect(); + m_pan_origin = Gfx::FloatPoint(0, 0); + + if (m_show_rulers) { + viewport_rect = { + viewport_rect.x() + m_ruler_thickness, + viewport_rect.y() + m_ruler_thickness, + viewport_rect.width() - m_ruler_thickness, + viewport_rect.height() - m_ruler_thickness + }; + } + const float border_ratio = 0.95f; auto image_size = image().size(); - auto height_ratio = rect().height() / (float)image_size.height(); - auto width_ratio = rect().width() / (float)image_size.width(); + auto height_ratio = viewport_rect.height() / (float)image_size.height(); + auto width_ratio = viewport_rect.width() / (float)image_size.width(); m_scale = border_ratio * min(height_ratio, width_ratio); - m_pan_origin = Gfx::FloatPoint(0, 0); + float offset = m_show_rulers ? -m_ruler_thickness / (m_scale * 2.0f) : 0.0f; + m_pan_origin = Gfx::FloatPoint(offset, offset); + relayout(); }