Skip to content

Commit 0ca1247

Browse files
Cnidariasawesomekling
authored andcommitted
PixelPaint: Propagate errors when creating images or layers
This fixes a dozen release_value_but_fixme_should_propage_errors
1 parent 25f2e49 commit 0ca1247

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

Userland/Applications/PixelPaint/MainWidget.cpp

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,26 @@ static constexpr int s_zoom_level_fit_height = 9;
145145
static constexpr int s_zoom_level_fit_image = 10;
146146
// Note: Update these together! ^
147147

148-
void MainWidget::initialize_menubar(GUI::Window& window)
148+
ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
149149
{
150150
auto& file_menu = window.add_menu("&File");
151151

152152
m_new_image_action = GUI::Action::create(
153153
"&New Image...", { Mod_Ctrl, Key_N }, g_icon_bag.filetype_pixelpaint, [&](auto&) {
154154
auto dialog = PixelPaint::CreateNewImageDialog::construct(&window);
155155
if (dialog->exec() == GUI::Dialog::ExecResult::OK) {
156-
auto image = PixelPaint::Image::try_create_with_size(dialog->image_size()).release_value_but_fixme_should_propagate_errors();
157-
auto bg_layer = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background").release_value_but_fixme_should_propagate_errors();
156+
auto image_result = PixelPaint::Image::try_create_with_size(dialog->image_size());
157+
if (image_result.is_error()) {
158+
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Failed to create image with size {}, error: {}", dialog->image_size(), image_result.error()));
159+
return;
160+
}
161+
auto image = image_result.release_value();
162+
auto bg_layer_result = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background");
163+
if (bg_layer_result.is_error()) {
164+
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Failed to create layer with size {}, error: {}", image->size(), bg_layer_result.error()));
165+
return;
166+
}
167+
auto bg_layer = bg_layer_result.release_value();
158168
image->add_layer(*bg_layer);
159169
auto background_color = dialog->background_color();
160170
if (background_color != Gfx::Color::Transparent)
@@ -174,7 +184,10 @@ void MainWidget::initialize_menubar(GUI::Window& window)
174184

175185
m_new_image_from_clipboard_action = GUI::Action::create(
176186
"&New Image from Clipboard", { Mod_Ctrl | Mod_Shift, Key_V }, g_icon_bag.new_clipboard, [&](auto&) {
177-
create_image_from_clipboard();
187+
auto result = create_image_from_clipboard();
188+
if (result.is_error()) {
189+
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Failed to create image from clipboard: {}", result.error()));
190+
}
178191
});
179192

180193
m_open_image_action = GUI::CommonActions::make_open_action([&](auto&) {
@@ -314,15 +327,23 @@ void MainWidget::initialize_menubar(GUI::Window& window)
314327
m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
315328
auto* editor = current_image_editor();
316329
if (!editor) {
317-
create_image_from_clipboard();
330+
auto result = create_image_from_clipboard();
331+
if (result.is_error()) {
332+
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Failed to create image from clipboard: {}", result.error()));
333+
}
318334
return;
319335
}
320336

321337
auto bitmap = GUI::Clipboard::the().fetch_data_and_type().as_bitmap();
322338
if (!bitmap)
323339
return;
324340

325-
auto layer = PixelPaint::Layer::try_create_with_bitmap(editor->image(), *bitmap, "Pasted layer").release_value_but_fixme_should_propagate_errors();
341+
auto layer_result = PixelPaint::Layer::try_create_with_bitmap(editor->image(), *bitmap, "Pasted layer");
342+
if (layer_result.is_error()) {
343+
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Could not create bitmap when pasting: {}", layer_result.error()));
344+
return;
345+
}
346+
auto layer = layer_result.release_value();
326347
editor->image().add_layer(*layer);
327348
editor->set_active_layer(layer);
328349
editor->image().selection().clear();
@@ -556,15 +577,15 @@ void MainWidget::initialize_menubar(GUI::Window& window)
556577
}));
557578
m_image_menu->add_separator();
558579

559-
m_image_menu->add_action(GUI::Action::create("Rotate Image &Counterclockwise", { Mod_Ctrl | Mod_Shift, Key_LessThan }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-ccw.png"sv).release_value_but_fixme_should_propagate_errors(),
580+
m_image_menu->add_action(GUI::Action::create("Rotate Image &Counterclockwise", { Mod_Ctrl | Mod_Shift, Key_LessThan }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-ccw.png"sv)),
560581
[&](auto&) {
561582
auto* editor = current_image_editor();
562583
VERIFY(editor);
563584
editor->image().rotate(Gfx::RotationDirection::CounterClockwise);
564585
editor->did_complete_action("Rotate Image Counterclockwise"sv);
565586
}));
566587

567-
m_image_menu->add_action(GUI::Action::create("Rotate Image Clock&wise", { Mod_Ctrl | Mod_Shift, Key_GreaterThan }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-cw.png"sv).release_value_but_fixme_should_propagate_errors(),
588+
m_image_menu->add_action(GUI::Action::create("Rotate Image Clock&wise", { Mod_Ctrl | Mod_Shift, Key_GreaterThan }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-cw.png"sv)),
568589
[&](auto&) {
569590
auto* editor = current_image_editor();
570591
VERIFY(editor);
@@ -750,7 +771,12 @@ void MainWidget::initialize_menubar(GUI::Window& window)
750771
auto& next_active_layer = editor->image().layer(active_layer_index > 0 ? active_layer_index - 1 : 0);
751772
editor->set_active_layer(&next_active_layer);
752773
} else {
753-
auto layer = PixelPaint::Layer::try_create_with_size(editor->image(), editor->image().size(), "Background").release_value_but_fixme_should_propagate_errors();
774+
auto layer_result = PixelPaint::Layer::try_create_with_size(editor->image(), editor->image().size(), "Background");
775+
if (layer_result.is_error()) {
776+
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Failed to create layer with size {}, error: {}", editor->image().size(), layer_result.error()));
777+
return;
778+
}
779+
auto layer = layer_result.release_value();
754780
editor->image().add_layer(move(layer));
755781
editor->layers_did_change();
756782
m_layer_list_widget->select_top_layer();
@@ -822,7 +848,7 @@ void MainWidget::initialize_menubar(GUI::Window& window)
822848
}));
823849
m_layer_menu->add_separator();
824850

825-
m_layer_menu->add_action(GUI::Action::create("Rotate Layer &Counterclockwise", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-ccw.png"sv).release_value_but_fixme_should_propagate_errors(),
851+
m_layer_menu->add_action(GUI::Action::create("Rotate Layer &Counterclockwise", TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-ccw.png"sv)),
826852
[&](auto&) {
827853
auto* editor = current_image_editor();
828854
VERIFY(editor);
@@ -833,7 +859,7 @@ void MainWidget::initialize_menubar(GUI::Window& window)
833859
editor->did_complete_action("Rotate Layer Counterclockwise"sv);
834860
}));
835861

836-
m_layer_menu->add_action(GUI::Action::create("Rotate Layer Clock&wise", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-cw.png"sv).release_value_but_fixme_should_propagate_errors(),
862+
m_layer_menu->add_action(GUI::Action::create("Rotate Layer Clock&wise", TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-rotate-cw.png"sv)),
837863
[&](auto&) {
838864
auto* editor = current_image_editor();
839865
VERIFY(editor);
@@ -967,6 +993,8 @@ void MainWidget::initialize_menubar(GUI::Window& window)
967993

968994
toolbar.add_separator();
969995
toolbar.add_action(*m_levels_dialog_action);
996+
997+
return {};
970998
}
971999

9721000
void MainWidget::set_actions_enabled(bool enabled)
@@ -1006,11 +1034,11 @@ void MainWidget::open_image(Core::File& file)
10061034
m_layer_list_widget->set_image(&image);
10071035
}
10081036

1009-
void MainWidget::create_default_image()
1037+
ErrorOr<void> MainWidget::create_default_image()
10101038
{
1011-
auto image = Image::try_create_with_size({ 510, 356 }).release_value_but_fixme_should_propagate_errors();
1039+
auto image = TRY(Image::try_create_with_size({ 510, 356 }));
10121040

1013-
auto bg_layer = Layer::try_create_with_size(*image, image->size(), "Background").release_value_but_fixme_should_propagate_errors();
1041+
auto bg_layer = TRY(Layer::try_create_with_size(*image, image->size(), "Background"));
10141042
image->add_layer(*bg_layer);
10151043
bg_layer->content_bitmap().fill(Color::Transparent);
10161044

@@ -1020,25 +1048,27 @@ void MainWidget::create_default_image()
10201048
editor.set_title("Untitled");
10211049
editor.set_active_layer(bg_layer);
10221050
editor.set_unmodified();
1051+
1052+
return {};
10231053
}
10241054

1025-
void MainWidget::create_image_from_clipboard()
1055+
ErrorOr<void> MainWidget::create_image_from_clipboard()
10261056
{
10271057
auto bitmap = GUI::Clipboard::the().fetch_data_and_type().as_bitmap();
10281058
if (!bitmap) {
1029-
GUI::MessageBox::show(window(), "There is no image in a clipboard to paste."sv, "PixelPaint"sv, GUI::MessageBox::Type::Warning);
1030-
return;
1059+
return Error::from_string_view("There is no image in a clipboard to paste."sv);
10311060
}
10321061

1033-
auto image = PixelPaint::Image::try_create_with_size(bitmap->size()).release_value_but_fixme_should_propagate_errors();
1034-
auto layer = PixelPaint::Layer::try_create_with_bitmap(image, *bitmap, "Pasted layer").release_value_but_fixme_should_propagate_errors();
1062+
auto image = TRY(PixelPaint::Image::try_create_with_size(bitmap->size()));
1063+
auto layer = TRY(PixelPaint::Layer::try_create_with_bitmap(image, *bitmap, "Pasted layer"));
10351064
image->add_layer(*layer);
10361065

10371066
auto& editor = create_new_editor(*image);
10381067
editor.set_title("Untitled");
10391068

10401069
m_layer_list_widget->set_image(image);
10411070
m_layer_list_widget->set_selected_layer(layer);
1071+
return {};
10421072
}
10431073

10441074
bool MainWidget::request_close()

Userland/Applications/PixelPaint/MainWidget.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class MainWidget : public GUI::Widget {
3838
public:
3939
virtual ~MainWidget() {};
4040

41-
void initialize_menubar(GUI::Window&);
41+
ErrorOr<void> initialize_menubar(GUI::Window&);
4242

4343
void open_image(Core::File&);
44-
void create_default_image();
44+
ErrorOr<void> create_default_image();
4545

4646
bool request_close();
4747

@@ -50,7 +50,7 @@ class MainWidget : public GUI::Widget {
5050

5151
ImageEditor* current_image_editor();
5252
ImageEditor& create_new_editor(NonnullRefPtr<Image>);
53-
void create_image_from_clipboard();
53+
ErrorOr<void> create_image_from_clipboard();
5454

5555
void image_editor_did_update_undo_stack();
5656

Userland/Applications/PixelPaint/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
5050

5151
auto main_widget = TRY(window->try_set_main_widget<PixelPaint::MainWidget>());
5252

53-
main_widget->initialize_menubar(*window);
53+
TRY(main_widget->initialize_menubar(*window));
5454

5555
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
5656
if (main_widget->request_close())
@@ -79,7 +79,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
7979
return 1;
8080
main_widget->open_image(response.value());
8181
} else {
82-
main_widget->create_default_image();
82+
TRY(main_widget->create_default_image());
8383
}
8484

8585
return app->exec();

0 commit comments

Comments
 (0)