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.
This commit is contained in:
parent
8d205ae62e
commit
e1906d74b8
Notes:
sideshowbarker
2024-07-18 10:21:07 +09:00
Author: https://github.com/metmo Commit: https://github.com/SerenityOS/serenity/commit/e1906d74b80 Pull-request: https://github.com/SerenityOS/serenity/pull/8365
3 changed files with 23 additions and 6 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Reference in a new issue