summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-11-29 22:39:23 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-11-29 22:41:36 +0100
commitb09ac2631146a2e8ed4fc34935784a35e43b0a16 (patch)
tree830e8dac0f9d3b28c0875f648783612e9db3bd5a
parent4e6cd541c9c5eca61d4c5d86776fd380f9c907ae (diff)
downloadserenity-b09ac2631146a2e8ed4fc34935784a35e43b0a16.zip
PaintBrush: Allow canceling a line by pressing the Escape key
Sometimes you change your mind mid-line, and just want to get out of the situation. You can now do that :^)
-rw-r--r--Applications/PaintBrush/LineTool.cpp9
-rw-r--r--Applications/PaintBrush/LineTool.h1
-rw-r--r--Applications/PaintBrush/PaintableWidget.cpp7
-rw-r--r--Applications/PaintBrush/PaintableWidget.h2
-rw-r--r--Applications/PaintBrush/Tool.h2
-rw-r--r--Applications/PaintBrush/main.cpp1
6 files changed, 21 insertions, 1 deletions
diff --git a/Applications/PaintBrush/LineTool.cpp b/Applications/PaintBrush/LineTool.cpp
index 9620164896..e6c867ca32 100644
--- a/Applications/PaintBrush/LineTool.cpp
+++ b/Applications/PaintBrush/LineTool.cpp
@@ -58,6 +58,15 @@ void LineTool::on_second_paint(GPaintEvent& event)
painter.draw_line(m_line_start_position, m_line_end_position, m_widget->color_for(m_drawing_button), m_thickness);
}
+void LineTool::on_keydown(GKeyEvent& event)
+{
+ if (event.key() == Key_Escape && m_drawing_button != GMouseButton::None) {
+ m_drawing_button = GMouseButton::None;
+ m_widget->update();
+ event.accept();
+ }
+}
+
void LineTool::on_contextmenu(GContextMenuEvent& event)
{
if (!m_context_menu) {
diff --git a/Applications/PaintBrush/LineTool.h b/Applications/PaintBrush/LineTool.h
index 5e37ca5bfa..4bfb589d28 100644
--- a/Applications/PaintBrush/LineTool.h
+++ b/Applications/PaintBrush/LineTool.h
@@ -15,6 +15,7 @@ public:
virtual void on_mouseup(GMouseEvent&) override;
virtual void on_contextmenu(GContextMenuEvent&) override;
virtual void on_second_paint(GPaintEvent&) override;
+ virtual void on_keydown(GKeyEvent&) override;
private:
virtual const char* class_name() const override { return "LineTool"; }
diff --git a/Applications/PaintBrush/PaintableWidget.cpp b/Applications/PaintBrush/PaintableWidget.cpp
index a4d3a35330..33b57deeb9 100644
--- a/Applications/PaintBrush/PaintableWidget.cpp
+++ b/Applications/PaintBrush/PaintableWidget.cpp
@@ -96,6 +96,13 @@ void PaintableWidget::second_paint_event(GPaintEvent& event)
GWidget::second_paint_event(event);
}
+void PaintableWidget::keydown_event(GKeyEvent& event)
+{
+ if (m_tool)
+ m_tool->on_keydown(event);
+ GWidget::keydown_event(event);
+}
+
void PaintableWidget::set_primary_color(Color color)
{
if (m_primary_color == color)
diff --git a/Applications/PaintBrush/PaintableWidget.h b/Applications/PaintBrush/PaintableWidget.h
index 76dadb4052..73279b296e 100644
--- a/Applications/PaintBrush/PaintableWidget.h
+++ b/Applications/PaintBrush/PaintableWidget.h
@@ -32,11 +32,13 @@ public:
Function<void(Color)> on_secondary_color_change;
private:
+ virtual bool accepts_focus() const override { return true; }
virtual void paint_event(GPaintEvent&) override;
virtual void second_paint_event(GPaintEvent&) override;
virtual void mousedown_event(GMouseEvent&) override;
virtual void mouseup_event(GMouseEvent&) override;
virtual void mousemove_event(GMouseEvent&) override;
+ virtual void keydown_event(GKeyEvent&) override;
RefPtr<GraphicsBitmap> m_bitmap;
diff --git a/Applications/PaintBrush/Tool.h b/Applications/PaintBrush/Tool.h
index 2d335a9051..737c4aa942 100644
--- a/Applications/PaintBrush/Tool.h
+++ b/Applications/PaintBrush/Tool.h
@@ -1,7 +1,6 @@
#pragma once
#include "PaintableWidget.h"
-class GMouseEvent;
class Tool {
public:
@@ -14,6 +13,7 @@ public:
virtual void on_mouseup(GMouseEvent&) {}
virtual void on_contextmenu(GContextMenuEvent&) {}
virtual void on_second_paint(GPaintEvent&) {}
+ virtual void on_keydown(GKeyEvent&) {}
void clear() { m_widget = nullptr; }
void setup(PaintableWidget& widget) { m_widget = widget.make_weak_ptr(); }
diff --git a/Applications/PaintBrush/main.cpp b/Applications/PaintBrush/main.cpp
index 3c56e0ce9d..384a2b65d8 100644
--- a/Applications/PaintBrush/main.cpp
+++ b/Applications/PaintBrush/main.cpp
@@ -33,6 +33,7 @@ int main(int argc, char** argv)
vertical_container->layout()->set_spacing(0);
auto paintable_widget = PaintableWidget::construct(vertical_container);
+ paintable_widget->set_focus(true);
PaletteWidget::construct(*paintable_widget, vertical_container);
window->show();