From 285f0383d410a3f63da850a3eecdd2e401ddaf34 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 9 Jul 2021 21:27:21 +0200 Subject: [PATCH] PixelPaint: Move LayerListWidget gadget rect computation to a function This will allow functions other than paint_event() to use the various gadget rects (thumbnail, text, etc.) --- .../PixelPaint/LayerListWidget.cpp | 33 +++++++++++-------- .../Applications/PixelPaint/LayerListWidget.h | 1 + 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Userland/Applications/PixelPaint/LayerListWidget.cpp b/Userland/Applications/PixelPaint/LayerListWidget.cpp index fe3c987985..6ffddc9e34 100644 --- a/Userland/Applications/PixelPaint/LayerListWidget.cpp +++ b/Userland/Applications/PixelPaint/LayerListWidget.cpp @@ -57,6 +57,22 @@ void LayerListWidget::resize_event(GUI::ResizeEvent& event) relayout_gadgets(); } +void LayerListWidget::get_gadget_rects(Gadget const& gadget, Gfx::IntRect& outer_rect, Gfx::IntRect& thumbnail_rect, Gfx::IntRect& text_rect) +{ + outer_rect = gadget.rect; + outer_rect.translate_by(0, -vertical_scrollbar().value()); + outer_rect.translate_by(frame_thickness(), frame_thickness()); + if (gadget.is_moving) { + outer_rect.translate_by(0, gadget.movement_delta.y()); + } + + thumbnail_rect = { outer_rect.x(), outer_rect.y(), outer_rect.height(), outer_rect.height() }; + thumbnail_rect.shrink(8, 8); + + text_rect = { thumbnail_rect.right() + 10, outer_rect.y(), outer_rect.width(), outer_rect.height() }; + text_rect.intersect(outer_rect); +} + void LayerListWidget::paint_event(GUI::PaintEvent& event) { GUI::Painter painter(*this); @@ -72,13 +88,10 @@ void LayerListWidget::paint_event(GUI::PaintEvent& event) auto paint_gadget = [&](auto& gadget) { auto& layer = m_image->layer(gadget.layer_index); - auto adjusted_rect = gadget.rect; - adjusted_rect.translate_by(0, -vertical_scrollbar().value()); - adjusted_rect.translate_by(frame_thickness(), frame_thickness()); - - if (gadget.is_moving) { - adjusted_rect.translate_by(0, gadget.movement_delta.y()); - } + Gfx::IntRect adjusted_rect; + Gfx::IntRect thumbnail_rect; + Gfx::IntRect text_rect; + get_gadget_rects(gadget, adjusted_rect, thumbnail_rect, text_rect); if (gadget.is_moving) { painter.fill_rect(adjusted_rect, palette().selection().lightened(1.5f)); @@ -87,14 +100,8 @@ void LayerListWidget::paint_event(GUI::PaintEvent& event) } painter.draw_rect(adjusted_rect, palette().color(ColorRole::BaseText)); - - Gfx::IntRect thumbnail_rect { adjusted_rect.x(), adjusted_rect.y(), adjusted_rect.height(), adjusted_rect.height() }; - thumbnail_rect.shrink(8, 8); painter.draw_scaled_bitmap(thumbnail_rect, layer.bitmap(), layer.bitmap().rect()); - Gfx::IntRect text_rect { thumbnail_rect.right() + 10, adjusted_rect.y(), adjusted_rect.width(), adjusted_rect.height() }; - text_rect.intersect(adjusted_rect); - if (layer.is_visible()) { painter.draw_text(text_rect, layer.name(), Gfx::TextAlignment::CenterLeft, layer.is_selected() ? palette().selection_text() : palette().button_text()); painter.draw_rect(thumbnail_rect, palette().color(ColorRole::BaseText)); diff --git a/Userland/Applications/PixelPaint/LayerListWidget.h b/Userland/Applications/PixelPaint/LayerListWidget.h index 7364bf2d5f..dcc00b8ac5 100644 --- a/Userland/Applications/PixelPaint/LayerListWidget.h +++ b/Userland/Applications/PixelPaint/LayerListWidget.h @@ -56,6 +56,7 @@ private: Gfx::IntPoint movement_delta; }; + void get_gadget_rects(Gadget const&, Gfx::IntRect& outer_rect, Gfx::IntRect& thumbnail_rect, Gfx::IntRect& text_rect); bool is_moving_gadget() const { return m_moving_gadget_index.has_value(); } Optional gadget_at(Gfx::IntPoint const&);