From d9832f8d0bf0fc6f0f5847e31a288751db808a1d Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Tue, 7 Sep 2021 14:14:18 -0400 Subject: [PATCH] PixelPaint: Add `Fit Image To View` action This is a feature I missed from Photoshop: it sets the scale and position so that the image fits (it's longest dimension) into the editor view. There's a 5% border left around the image to provide context. This is just arbitrary seemed like the right amount after some trial and error. --- Userland/Applications/PixelPaint/ImageEditor.cpp | 12 ++++++++++++ Userland/Applications/PixelPaint/ImageEditor.h | 1 + Userland/Applications/PixelPaint/MainWidget.cpp | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index 2bb4bd0bd5..d0e98279d6 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -425,6 +425,18 @@ void ImageEditor::scale_by(float scale_delta) } } +void ImageEditor::fit_image_to_view() +{ + 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(); + m_scale = border_ratio * min(height_ratio, width_ratio); + + m_pan_origin = Gfx::FloatPoint(0, 0); + relayout(); +} + void ImageEditor::reset_scale_and_position() { if (m_scale != 1.0f) diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h index 343e72e5e0..edd8dac642 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.h +++ b/Userland/Applications/PixelPaint/ImageEditor.h @@ -52,6 +52,7 @@ public: float scale() const { return m_scale; } void scale_centered_on_position(Gfx::IntPoint const&, float); + void fit_image_to_view(); void reset_scale_and_position(); void scale_by(float); diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index 5306e3fc54..aebcb1ef37 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -342,6 +342,11 @@ void MainWidget::initialize_menubar(GUI::Window& window) view_menu.add_action(*m_zoom_in_action); view_menu.add_action(*m_zoom_out_action); view_menu.add_action(*m_reset_zoom_action); + view_menu.add_action(GUI::Action::create( + "&Fit Image To View", [&](auto&) { + if (auto* editor = current_image_editor()) + editor->fit_image_to_view(); + })); view_menu.add_separator(); view_menu.add_action(*m_add_guide_action); view_menu.add_action(*m_show_guides_action);