summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorMarcus Nilsson <brainbomb@gmail.com>2021-07-01 15:51:47 +0200
committerAndreas Kling <kling@serenityos.org>2021-07-05 20:39:30 +0200
commite1906d74b80b7c8c9bc128f0ead210b6eac543b2 (patch)
tree45588ee5534e7bec6b5e03a5f4ab54eb7ce7262a /Userland/Applications
parent8d205ae62ee1637168efa19b1166e92071c9ac89 (diff)
downloadserenity-e1906d74b80b7c8c9bc128f0ead210b6eac543b2.zip
PixelPaint: Make move_selection() cycle through layers
Previously move_selection() did not work as expected. Instead store the selected layer index in a member variable and continue to cycle through the layers when you come to the start/end. Also use it to scroll into view. Lastly rename the function to cycle_through_selection() to make it clearer what it does.
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/PixelPaint/LayerListWidget.cpp21
-rw-r--r--Userland/Applications/PixelPaint/LayerListWidget.h4
-rw-r--r--Userland/Applications/PixelPaint/main.cpp4
3 files changed, 23 insertions, 6 deletions
diff --git a/Userland/Applications/PixelPaint/LayerListWidget.cpp b/Userland/Applications/PixelPaint/LayerListWidget.cpp
index 5b31ab2cc4..7f231ea124 100644
--- a/Userland/Applications/PixelPaint/LayerListWidget.cpp
+++ b/Userland/Applications/PixelPaint/LayerListWidget.cpp
@@ -140,6 +140,7 @@ void LayerListWidget::mousedown_event(GUI::MouseEvent& event)
return;
}
m_moving_gadget_index = gadget_index;
+ m_selected_layer_index = gadget_index.value();
m_moving_event_origin = translated_event_point;
auto& gadget = m_gadgets[m_moving_gadget_index.value()];
auto& layer = m_image->layer(gadget_index.value());
@@ -190,6 +191,7 @@ void LayerListWidget::context_menu_event(GUI::ContextMenuEvent& event)
auto gadget_index = gadget_at(translated_event_point);
if (gadget_index.has_value()) {
auto& layer = m_image->layer(gadget_index.value());
+ m_selected_layer_index = gadget_index.value();
set_selected_layer(&layer);
}
@@ -244,6 +246,7 @@ void LayerListWidget::select_bottom_layer()
{
if (!m_image || !m_image->layer_count())
return;
+ m_selected_layer_index = 0;
set_selected_layer(&m_image->layer(0));
}
@@ -251,15 +254,25 @@ void LayerListWidget::select_top_layer()
{
if (!m_image || !m_image->layer_count())
return;
+ m_selected_layer_index = m_image->layer_count() - 1;
set_selected_layer(&m_image->layer(m_image->layer_count() - 1));
}
-void LayerListWidget::move_selection(int delta)
+void LayerListWidget::cycle_through_selection(int delta)
{
if (!m_image || !m_image->layer_count())
return;
- int new_layer_index = min(max(0, (int)m_image->layer_count() + delta), (int)m_image->layer_count() - 1);
- set_selected_layer(&m_image->layer(new_layer_index));
+
+ int selected_layer_index = static_cast<int>(m_selected_layer_index);
+ selected_layer_index += delta;
+
+ if (selected_layer_index < 0)
+ selected_layer_index = m_image->layer_count() - 1;
+ if (selected_layer_index > static_cast<int>(m_image->layer_count()) - 1)
+ selected_layer_index = 0;
+
+ m_selected_layer_index = selected_layer_index;
+ set_selected_layer(&m_image->layer(m_selected_layer_index));
}
void LayerListWidget::relayout_gadgets()
@@ -295,6 +308,8 @@ void LayerListWidget::set_selected_layer(Layer* layer)
m_image->layer(i).set_selected(layer == &m_image->layer(i));
if (on_layer_select)
on_layer_select(layer);
+
+ scroll_into_view(m_gadgets[m_selected_layer_index].rect, false, true);
update();
}
diff --git a/Userland/Applications/PixelPaint/LayerListWidget.h b/Userland/Applications/PixelPaint/LayerListWidget.h
index e1d2008c95..b23806d04c 100644
--- a/Userland/Applications/PixelPaint/LayerListWidget.h
+++ b/Userland/Applications/PixelPaint/LayerListWidget.h
@@ -27,7 +27,7 @@ public:
void select_bottom_layer();
void select_top_layer();
- void move_selection(int delta);
+ void cycle_through_selection(int delta);
private:
explicit LayerListWidget();
@@ -66,6 +66,8 @@ private:
Optional<size_t> m_moving_gadget_index;
Gfx::IntPoint m_moving_event_origin;
+
+ size_t m_selected_layer_index { 0 };
};
}
diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp
index 78e6a910d3..057d43062b 100644
--- a/Userland/Applications/PixelPaint/main.cpp
+++ b/Userland/Applications/PixelPaint/main.cpp
@@ -350,12 +350,12 @@ int main(int argc, char** argv)
layer_menu.add_separator();
layer_menu.add_action(GUI::Action::create(
"Select &Previous Layer", { 0, Key_PageUp }, [&](auto&) {
- layer_list_widget.move_selection(1);
+ layer_list_widget.cycle_through_selection(1);
},
window));
layer_menu.add_action(GUI::Action::create(
"Select &Next Layer", { 0, Key_PageDown }, [&](auto&) {
- layer_list_widget.move_selection(-1);
+ layer_list_widget.cycle_through_selection(-1);
},
window));
layer_menu.add_action(GUI::Action::create(