Skip to content

Commit d27d19f

Browse files
krkkAtkinsSJ
authored andcommitted
PixelPaint: Set Tool on_*_color_change logic using virtual functions
Previously, we were rewriting the on_primary_color_change in the Text Tool and Gradient, which made the palette widget no longer update after picking a color from an image. Additionally, it also crashed the program after leaving the Gradient tool and trying to change color.
1 parent cb96c89 commit d27d19f

File tree

7 files changed

+23
-18
lines changed

7 files changed

+23
-18
lines changed

Userland/Applications/PixelPaint/MainWidget.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,17 @@ ImageEditor& MainWidget::create_new_editor(NonnullRefPtr<Image> image)
12291229
},
12301230
100);
12311231

1232+
image_editor.on_primary_color_change = [&](Color color) {
1233+
m_palette_widget->set_primary_color(color);
1234+
if (image_editor.active_tool())
1235+
image_editor.active_tool()->on_primary_color_change(color);
1236+
};
1237+
image_editor.on_secondary_color_change = [&](Color color) {
1238+
m_palette_widget->set_secondary_color(color);
1239+
if (image_editor.active_tool())
1240+
image_editor.active_tool()->on_secondary_color_change(color);
1241+
};
1242+
12321243
if (image->layer_count())
12331244
image_editor.set_active_layer(&image->layer(0));
12341245

Userland/Applications/PixelPaint/PaletteWidget.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,6 @@ void PaletteWidget::set_image_editor(ImageEditor* editor)
150150

151151
set_primary_color(editor->primary_color());
152152
set_secondary_color(editor->secondary_color());
153-
154-
editor->on_primary_color_change = [this](Color color) {
155-
set_primary_color(color);
156-
};
157-
158-
editor->on_secondary_color_change = [this](Color color) {
159-
set_secondary_color(color);
160-
};
161153
}
162154

163155
void PaletteWidget::set_primary_color(Color color)

Userland/Applications/PixelPaint/Tools/GradientTool.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@ void GradientTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
163163
draw_gradient(painter, true, m_editor->content_to_frame_position(Gfx::IntPoint(0, 0)), m_editor->scale(), m_editor->content_rect());
164164
}
165165

166-
void GradientTool::on_tool_activation()
166+
void GradientTool::on_primary_color_change(Color)
167167
{
168-
m_editor->on_primary_color_change = [this](Color) {
169-
if (m_gradient_end.has_value())
170-
m_editor->update();
171-
};
168+
if (m_gradient_end.has_value())
169+
m_editor->update();
170+
}
172171

172+
void GradientTool::on_tool_activation()
173+
{
173174
reset();
174175
}
175176

Userland/Applications/PixelPaint/Tools/GradientTool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class GradientTool : public Tool {
2020
virtual void on_mouseup(Layer*, MouseEvent&) override;
2121
virtual bool on_keydown(GUI::KeyEvent&) override;
2222
virtual void on_keyup(GUI::KeyEvent&) override;
23+
virtual void on_primary_color_change(Color) override;
2324
virtual void on_tool_activation() override;
2425
virtual GUI::Widget* get_properties_widget() override;
2526

Userland/Applications/PixelPaint/Tools/TextTool.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ TextTool::TextTool()
4141
}).release_value_but_fixme_should_propagate_errors();
4242
}
4343

44-
void TextTool::on_tool_activation()
44+
void TextTool::on_primary_color_change(Color color)
4545
{
46-
m_editor->on_primary_color_change = [this](auto color) {
47-
m_text_color = color;
48-
};
46+
m_text_color = color;
4947
}
5048

5149
void TextTool::on_tool_deactivation()

Userland/Applications/PixelPaint/Tools/TextTool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TextTool final : public Tool {
3737
virtual void on_mousedown(Layer*, MouseEvent&) override;
3838
virtual bool on_keydown(GUI::KeyEvent&) override;
3939
virtual void on_second_paint(Layer const*, GUI::PaintEvent&) override;
40-
virtual void on_tool_activation() override;
40+
virtual void on_primary_color_change(Color) override;
4141
virtual void on_tool_deactivation() override;
4242
virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> cursor() override;
4343
virtual GUI::Widget* get_properties_widget() override;

Userland/Applications/PixelPaint/Tools/Tool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class Tool {
6363
virtual void on_second_paint(Layer const*, GUI::PaintEvent&) { }
6464
virtual bool on_keydown(GUI::KeyEvent&);
6565
virtual void on_keyup(GUI::KeyEvent&) { }
66+
virtual void on_primary_color_change(Color) { }
67+
virtual void on_secondary_color_change(Color) { }
6668
virtual void on_tool_activation() { }
6769
virtual void on_tool_deactivation() { }
6870
virtual GUI::Widget* get_properties_widget() { return nullptr; }

0 commit comments

Comments
 (0)