From 7ca4d045bd67a6830c23e4091a7fa8fcf3d9f4b0 Mon Sep 17 00:00:00 2001 From: Marcus Nilsson Date: Sun, 9 Jan 2022 11:59:32 +0100 Subject: [PATCH] PixelPaint: Make PaletteWidget::set_image_editor take a ImageEditor* After closing the last open ImageEditor, selecting a color would try to dereference it causing a crash. Instead make set_image_editor() take a pointer to it and set it to nullptr when closing the last tab like we do with LayerListWidget and LayerPropertiesWidget. --- .../Applications/PixelPaint/MainWidget.cpp | 3 ++- .../Applications/PixelPaint/PaletteWidget.cpp | 21 ++++++++++++------- .../Applications/PixelPaint/PaletteWidget.h | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index 88a0353c55..10435a08d6 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -71,6 +71,7 @@ MainWidget::MainWidget() if (m_tab_widget->children().size() == 0) { m_layer_list_widget->set_image(nullptr); m_layer_properties_widget->set_layer(nullptr); + m_palette_widget->set_image_editor(nullptr); } }); } @@ -78,7 +79,7 @@ MainWidget::MainWidget() m_tab_widget->on_change = [&](auto& widget) { auto& image_editor = verify_cast(widget); - m_palette_widget->set_image_editor(image_editor); + m_palette_widget->set_image_editor(&image_editor); m_layer_list_widget->set_image(&image_editor.image()); m_layer_properties_widget->set_layer(image_editor.active_layer()); if (auto* active_tool = m_toolbox->active_tool()) diff --git a/Userland/Applications/PixelPaint/PaletteWidget.cpp b/Userland/Applications/PixelPaint/PaletteWidget.cpp index 688f71df3b..d4bab4b9e1 100644 --- a/Userland/Applications/PixelPaint/PaletteWidget.cpp +++ b/Userland/Applications/PixelPaint/PaletteWidget.cpp @@ -143,17 +143,20 @@ PaletteWidget::PaletteWidget() display_color_list(result.value()); } -void PaletteWidget::set_image_editor(ImageEditor& editor) +void PaletteWidget::set_image_editor(ImageEditor* editor) { - m_editor = &editor; - set_primary_color(editor.primary_color()); - set_secondary_color(editor.secondary_color()); + m_editor = editor; + if (!m_editor) + return; - editor.on_primary_color_change = [this](Color color) { + set_primary_color(editor->primary_color()); + set_secondary_color(editor->secondary_color()); + + editor->on_primary_color_change = [this](Color color) { set_primary_color(color); }; - editor.on_secondary_color_change = [this](Color color) { + editor->on_secondary_color_change = [this](Color color) { set_secondary_color(color); }; } @@ -164,13 +167,15 @@ PaletteWidget::~PaletteWidget() void PaletteWidget::set_primary_color(Color color) { - m_editor->set_primary_color(color); + if (m_editor) + m_editor->set_primary_color(color); m_primary_color_widget->set_background_color(color); } void PaletteWidget::set_secondary_color(Color color) { - m_editor->set_secondary_color(color); + if (m_editor) + m_editor->set_secondary_color(color); m_secondary_color_widget->set_background_color(color); } diff --git a/Userland/Applications/PixelPaint/PaletteWidget.h b/Userland/Applications/PixelPaint/PaletteWidget.h index 8c9eb824a4..ce129864d7 100644 --- a/Userland/Applications/PixelPaint/PaletteWidget.h +++ b/Userland/Applications/PixelPaint/PaletteWidget.h @@ -35,7 +35,7 @@ public: static Result save_palette_fd_and_close(Vector, int); static Vector fallback_colors(); - void set_image_editor(ImageEditor&); + void set_image_editor(ImageEditor*); private: static Result, String> load_palette_file(Core::File&);