From d0459725593ee45a7d2a52d3a1c8ac37701db16c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 13 May 2020 14:22:10 +0200 Subject: [PATCH] PaintBrush: Add keyboard shortcuts for all tools I've used the shortcuts from GIMP for the most part, since that's what I'm used to. We can definitely iterate on these to find better options as the app develops. :^) --- Applications/PaintBrush/ToolboxWidget.cpp | 50 ++++++++++++++++------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/Applications/PaintBrush/ToolboxWidget.cpp b/Applications/PaintBrush/ToolboxWidget.cpp index cff7b21cda..1890c8e0f3 100644 --- a/Applications/PaintBrush/ToolboxWidget.cpp +++ b/Applications/PaintBrush/ToolboxWidget.cpp @@ -34,6 +34,8 @@ #include "PickerTool.h" #include "RectangleTool.h" #include "SprayTool.h" +#include +#include #include #include @@ -42,15 +44,32 @@ namespace PaintBrush { class ToolButton final : public GUI::Button { C_OBJECT(ToolButton) public: - ToolButton(const String& name, OwnPtr&& tool) - : m_tool(move(tool)) + ToolButton(ToolboxWidget& toolbox, const String& name, const GUI::Shortcut& shortcut, OwnPtr&& tool) + : m_toolbox(toolbox) + , m_tool(move(tool)) { - set_tooltip(name); + StringBuilder builder; + builder.append(name); + builder.append(" ("); + builder.append(shortcut.to_string()); + builder.append(")"); + set_tooltip(builder.to_string()); + + m_action = GUI::Action::create_checkable(name, shortcut, [this](auto& action) { + if (action.is_checked()) + m_toolbox.on_tool_selection(m_tool); + else + m_toolbox.on_tool_selection(nullptr); + }); + + set_action(*m_action); } const Tool& tool() const { return *m_tool; } Tool& tool() { return *m_tool; } + virtual bool is_uncheckable() const override { return false; } + virtual void context_menu_event(GUI::ContextMenuEvent& event) override { set_checked(true); @@ -58,7 +77,9 @@ public: } private: + ToolboxWidget& m_toolbox; OwnPtr m_tool; + RefPtr m_action; }; ToolboxWidget::ToolboxWidget() @@ -75,13 +96,12 @@ ToolboxWidget::ToolboxWidget() set_layout(); layout()->set_margins({ 4, 4, 4, 4 }); - auto add_tool = [&](const StringView& name, const StringView& icon_name, OwnPtr&& tool) { - auto& button = add(name, move(tool)); + auto add_tool = [&](const StringView& name, const StringView& icon_name, const GUI::Shortcut& shortcut, NonnullOwnPtr tool) { + auto& button = add(*this, name, shortcut, move(tool)); button.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); button.set_preferred_size(0, 32); button.set_checkable(true); button.set_exclusive(true); - button.set_icon(Gfx::Bitmap::load_from_file(String::format("/res/icons/paintbrush/%s.png", icon_name.to_string().characters()))); button.on_checked = [this, button = &button](auto checked) { @@ -92,15 +112,15 @@ ToolboxWidget::ToolboxWidget() }; }; - add_tool("Move", "move", make()); - add_tool("Pen", "pen", make()); - add_tool("Bucket Fill", "bucket", make()); - add_tool("Spray", "spray", make()); - add_tool("Color Picker", "picker", make()); - add_tool("Erase", "eraser", make()); - add_tool("Line", "line", make()); - add_tool("Rectangle", "rectangle", make()); - add_tool("Ellipse", "circle", make()); + add_tool("Move", "move", { 0, Key_M }, make()); + add_tool("Pen", "pen", { 0, Key_N }, make()); + add_tool("Bucket Fill", "bucket", { Mod_Shift, Key_B }, make()); + add_tool("Spray", "spray", { Mod_Shift, Key_S }, make()); + add_tool("Color Picker", "picker", { 0, Key_O }, make()); + add_tool("Erase", "eraser", { Mod_Shift, Key_E }, make()); + add_tool("Line", "line", { Mod_Ctrl | Mod_Shift, Key_L }, make()); + add_tool("Rectangle", "rectangle", { Mod_Ctrl | Mod_Shift, Key_R }, make()); + add_tool("Ellipse", "circle", { Mod_Ctrl | Mod_Shift, Key_E }, make()); } ToolboxWidget::~ToolboxWidget()