1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:37:36 +00:00

PixelPaint: Make paste action create new image if no editor is opened

This matches GIMP behaviour.
This commit is contained in:
Maciej 2021-11-26 21:09:22 +01:00 committed by Brian Gianforcaro
parent 41d02bd5e6
commit 28ea412251
2 changed files with 24 additions and 15 deletions

View file

@ -119,20 +119,7 @@ void MainWidget::initialize_menubar(GUI::Window& window)
m_new_image_from_clipboard_action = GUI::Action::create( m_new_image_from_clipboard_action = GUI::Action::create(
"&New Image from Clipboard", { Mod_Ctrl | Mod_Shift, Key_V }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) { "&New Image from Clipboard", { Mod_Ctrl | Mod_Shift, Key_V }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
auto bitmap = GUI::Clipboard::the().fetch_data_and_type().as_bitmap(); create_image_from_clipboard();
if (!bitmap) {
GUI::MessageBox::show(&window, "There is no image in a clipboard to paste.", "PixelPaint", GUI::MessageBox::Type::Warning);
return;
}
auto image = PixelPaint::Image::try_create_with_size(bitmap->size()).release_value_but_fixme_should_propagate_errors();
auto layer = PixelPaint::Layer::try_create_with_bitmap(image, *bitmap, "Pasted layer").release_value_but_fixme_should_propagate_errors();
image->add_layer(*layer);
image->set_title("Untitled");
create_new_editor(*image);
m_layer_list_widget->set_image(image);
m_layer_list_widget->set_selected_layer(layer);
}); });
m_open_image_action = GUI::CommonActions::make_open_action([&](auto&) { m_open_image_action = GUI::CommonActions::make_open_action([&](auto&) {
@ -241,8 +228,11 @@ void MainWidget::initialize_menubar(GUI::Window& window)
m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) { m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
auto* editor = current_image_editor(); auto* editor = current_image_editor();
if (!editor) if (!editor) {
create_image_from_clipboard();
return; return;
}
auto bitmap = GUI::Clipboard::the().fetch_data_and_type().as_bitmap(); auto bitmap = GUI::Clipboard::the().fetch_data_and_type().as_bitmap();
if (!bitmap) if (!bitmap)
return; return;
@ -716,6 +706,24 @@ void MainWidget::create_default_image()
editor.set_active_layer(bg_layer); editor.set_active_layer(bg_layer);
} }
void MainWidget::create_image_from_clipboard()
{
auto bitmap = GUI::Clipboard::the().fetch_data_and_type().as_bitmap();
if (!bitmap) {
GUI::MessageBox::show(window(), "There is no image in a clipboard to paste.", "PixelPaint", GUI::MessageBox::Type::Warning);
return;
}
auto image = PixelPaint::Image::try_create_with_size(bitmap->size()).release_value_but_fixme_should_propagate_errors();
auto layer = PixelPaint::Layer::try_create_with_bitmap(image, *bitmap, "Pasted layer").release_value_but_fixme_should_propagate_errors();
image->add_layer(*layer);
image->set_title("Untitled");
create_new_editor(*image);
m_layer_list_widget->set_image(image);
m_layer_list_widget->set_selected_layer(layer);
}
bool MainWidget::request_close() bool MainWidget::request_close()
{ {
if (m_tab_widget->children().is_empty()) if (m_tab_widget->children().is_empty())

View file

@ -43,6 +43,7 @@ private:
ImageEditor* current_image_editor(); ImageEditor* current_image_editor();
ImageEditor& create_new_editor(NonnullRefPtr<Image>); ImageEditor& create_new_editor(NonnullRefPtr<Image>);
void create_image_from_clipboard();
virtual void drop_event(GUI::DropEvent&) override; virtual void drop_event(GUI::DropEvent&) override;