diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index ba2131baf6..fff196c25f 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -384,6 +384,34 @@ void Image::flatten_all_layers() select_layer(&bottom_layer); } +void Image::merge_visible_layers() +{ + if (m_layers.size() < 2) + return; + + size_t index = 0; + + while (index < m_layers.size()) { + if (m_layers.at(index).is_visible()) { + auto& bottom_layer = m_layers.at(index); + GUI::Painter painter(bottom_layer.bitmap()); + paint_into(painter, { 0, 0, m_size.width(), m_size.height() }); + select_layer(&bottom_layer); + index++; + break; + } + index++; + } + while (index < m_layers.size()) { + if (m_layers.at(index).is_visible()) { + auto& layer = m_layers.at(index); + remove_layer(layer); + } else { + index++; + } + } +} + void Image::select_layer(Layer* layer) { for (auto* client : m_clients) diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index 1268b8d9d0..f24cbf75e2 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -69,6 +69,7 @@ public: void remove_layer(Layer&); void select_layer(Layer*); void flatten_all_layers(); + void merge_visible_layers(); void add_client(ImageClient&); void remove_client(ImageClient&); diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index 5c6bb7abbc..db2ffaceaf 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -419,6 +419,16 @@ int main(int argc, char** argv) }, window)); + layer_menu.add_action(GUI::Action::create( + "&Merge Visible", { Mod_Ctrl, Key_M }, [&](auto&) { + auto* editor = current_image_editor(); + if (!editor) + return; + editor->image().merge_visible_layers(); + editor->did_complete_action(); + }, + window)); + auto& filter_menu = menubar->add_menu("&Filter"); auto& spatial_filters_menu = filter_menu.add_submenu("&Spatial");