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

PixelPaint: Make LayerListWidget scrollable

Previously only a couple of layers would fit in the layer widget, this
makes it scrollable and also tweaks some sizes and coordinates.
This commit is contained in:
Marcus Nilsson 2021-07-01 01:08:52 +02:00 committed by Andreas Kling
parent 13e526de43
commit 36abb38f26
3 changed files with 24 additions and 9 deletions

View file

@ -17,6 +17,8 @@ namespace PixelPaint {
LayerListWidget::LayerListWidget() LayerListWidget::LayerListWidget()
{ {
set_should_hide_unnecessary_scrollbars(false);
horizontal_scrollbar().set_visible(false);
} }
LayerListWidget::~LayerListWidget() LayerListWidget::~LayerListWidget()
@ -71,6 +73,8 @@ void LayerListWidget::paint_event(GUI::PaintEvent& event)
auto& layer = m_image->layer(gadget.layer_index); auto& layer = m_image->layer(gadget.layer_index);
auto adjusted_rect = gadget.rect; 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) { if (gadget.is_moving) {
adjusted_rect.translate_by(0, gadget.movement_delta.y()); adjusted_rect.translate_by(0, gadget.movement_delta.y());
@ -87,6 +91,7 @@ void LayerListWidget::paint_event(GUI::PaintEvent& event)
Gfx::IntRect thumbnail_rect { adjusted_rect.x(), adjusted_rect.y(), adjusted_rect.height(), adjusted_rect.height() }; Gfx::IntRect thumbnail_rect { adjusted_rect.x(), adjusted_rect.y(), adjusted_rect.height(), adjusted_rect.height() };
thumbnail_rect.shrink(8, 8); thumbnail_rect.shrink(8, 8);
painter.draw_scaled_bitmap(thumbnail_rect, layer.bitmap(), layer.bitmap().rect()); painter.draw_scaled_bitmap(thumbnail_rect, layer.bitmap(), layer.bitmap().rect());
painter.draw_rect(thumbnail_rect, Color::Black);
Gfx::IntRect text_rect { thumbnail_rect.right() + 10, adjusted_rect.y(), adjusted_rect.width(), adjusted_rect.height() }; Gfx::IntRect text_rect { thumbnail_rect.right() + 10, adjusted_rect.y(), adjusted_rect.width(), adjusted_rect.height() };
text_rect.intersect(adjusted_rect); text_rect.intersect(adjusted_rect);
@ -101,6 +106,8 @@ void LayerListWidget::paint_event(GUI::PaintEvent& event)
if (m_moving_gadget_index.has_value()) if (m_moving_gadget_index.has_value())
paint_gadget(m_gadgets[m_moving_gadget_index.value()]); paint_gadget(m_gadgets[m_moving_gadget_index.value()]);
Gfx::StylePainter::paint_frame(painter, rect(), palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Sunken, 2);
} }
Optional<size_t> LayerListWidget::gadget_at(Gfx::IntPoint const& position) Optional<size_t> LayerListWidget::gadget_at(Gfx::IntPoint const& position)
@ -118,14 +125,17 @@ void LayerListWidget::mousedown_event(GUI::MouseEvent& event)
return; return;
if (event.button() != GUI::MouseButton::Left) if (event.button() != GUI::MouseButton::Left)
return; return;
auto gadget_index = gadget_at(event.position());
Gfx::IntPoint translated_event_point = { 0, vertical_scrollbar().value() + event.y() };
auto gadget_index = gadget_at(translated_event_point);
if (!gadget_index.has_value()) { if (!gadget_index.has_value()) {
if (on_layer_select) if (on_layer_select)
on_layer_select(nullptr); on_layer_select(nullptr);
return; return;
} }
m_moving_gadget_index = gadget_index; m_moving_gadget_index = gadget_index;
m_moving_event_origin = event.position(); m_moving_event_origin = translated_event_point;
auto& gadget = m_gadgets[m_moving_gadget_index.value()]; auto& gadget = m_gadgets[m_moving_gadget_index.value()];
auto& layer = m_image->layer(gadget_index.value()); auto& layer = m_image->layer(gadget_index.value());
set_selected_layer(&layer); set_selected_layer(&layer);
@ -141,7 +151,9 @@ void LayerListWidget::mousemove_event(GUI::MouseEvent& event)
if (!m_moving_gadget_index.has_value()) if (!m_moving_gadget_index.has_value())
return; return;
auto delta = event.position() - m_moving_event_origin; Gfx::IntPoint translated_event_point = { 0, vertical_scrollbar().value() + event.y() };
auto delta = translated_event_point - m_moving_event_origin;
auto& gadget = m_gadgets[m_moving_gadget_index.value()]; auto& gadget = m_gadgets[m_moving_gadget_index.value()];
VERIFY(gadget.is_moving); VERIFY(gadget.is_moving);
gadget.movement_delta = delta; gadget.movement_delta = delta;
@ -197,8 +209,8 @@ void LayerListWidget::image_did_modify_layer_stack()
rebuild_gadgets(); rebuild_gadgets();
} }
static constexpr int gadget_height = 30; static constexpr int gadget_height = 40;
static constexpr int gadget_spacing = 1; static constexpr int gadget_spacing = -1;
static constexpr int vertical_step = gadget_height + gadget_spacing; static constexpr int vertical_step = gadget_height + gadget_spacing;
size_t LayerListWidget::hole_index_during_move() const size_t LayerListWidget::hole_index_during_move() const
@ -245,11 +257,14 @@ void LayerListWidget::relayout_gadgets()
continue; continue;
if (hole_index.has_value() && index == hole_index.value()) if (hole_index.has_value() && index == hole_index.value())
y += vertical_step; y += vertical_step;
gadget.rect = { 0, y, width(), gadget_height }; gadget.rect = { 0, y, widget_inner_rect().width(), gadget_height };
y += vertical_step; y += vertical_step;
++index; ++index;
} }
auto total_gadget_height = static_cast<int>(m_gadgets.size()) * vertical_step;
set_content_size({ widget_inner_rect().width(), total_gadget_height });
vertical_scrollbar().set_range(0, max(total_gadget_height - height(), 0));
update(); update();
} }

View file

@ -7,12 +7,12 @@
#pragma once #pragma once
#include "Image.h" #include "Image.h"
#include <LibGUI/Widget.h> #include <LibGUI/AbstractScrollableWidget.h>
namespace PixelPaint { namespace PixelPaint {
class LayerListWidget final class LayerListWidget final
: public GUI::Widget : public GUI::AbstractScrollableWidget
, ImageClient { , ImageClient {
C_OBJECT(LayerListWidget); C_OBJECT(LayerListWidget);

View file

@ -51,7 +51,7 @@
@GUI::GroupBox { @GUI::GroupBox {
title: "Layers" title: "Layers"
layout: @GUI::VerticalBoxLayout { layout: @GUI::VerticalBoxLayout {
margins: [4, 16, 4, 8] margins: [6, 16, 6, 6]
} }
@PixelPaint::LayerListWidget { @PixelPaint::LayerListWidget {