From 3d9ba870772fcaba9ec5772b267f0f2a60133316 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 28 Jan 2023 07:00:44 +0000 Subject: [PATCH] PixelPaint: Enable more text tool keyboard shortcuts This commit allows the text tool's internal TextEditor component to handle keyboard shortcuts that would normally be handled by menu actions. The shortcuts that can now be used are: cut, copy, paste, undo, redo and select all. --- .../Applications/PixelPaint/Tools/TextTool.cpp | 15 +++++++++++++++ Userland/Applications/PixelPaint/Tools/TextTool.h | 1 + 2 files changed, 16 insertions(+) diff --git a/Userland/Applications/PixelPaint/Tools/TextTool.cpp b/Userland/Applications/PixelPaint/Tools/TextTool.cpp index df62724f45..4bad020fb4 100644 --- a/Userland/Applications/PixelPaint/Tools/TextTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/TextTool.cpp @@ -30,6 +30,12 @@ void TextToolEditor::handle_keyevent(Badge, GUI::KeyEvent& event) TextEditor::keydown_event(event); } +NonnullRefPtrVector TextToolEditor::actions() +{ + static NonnullRefPtrVector actions = { cut_action(), copy_action(), paste_action(), undo_action(), redo_action(), select_all_action() }; + return actions; +} + TextTool::TextTool() { m_text_editor = TextToolEditor::construct(); @@ -282,6 +288,15 @@ bool TextTool::on_keydown(GUI::KeyEvent& event) return true; } + // Pass key events that would normally be handled by menu shortcuts to our TextEditor subclass. + for (auto& action : m_text_editor->actions()) { + auto const& shortcut = action.shortcut(); + if (event.key() == shortcut.key() && event.modifiers() == shortcut.modifiers()) { + action.activate(m_text_editor); + return true; + } + } + // Pass the key event off to our TextEditor subclass which handles all text entry features like // caret navigation, backspace/delete, etc. m_text_editor->handle_keyevent({}, event); diff --git a/Userland/Applications/PixelPaint/Tools/TextTool.h b/Userland/Applications/PixelPaint/Tools/TextTool.h index 9f80be38b8..1396290d30 100644 --- a/Userland/Applications/PixelPaint/Tools/TextTool.h +++ b/Userland/Applications/PixelPaint/Tools/TextTool.h @@ -22,6 +22,7 @@ class TextToolEditor : public GUI::TextEditor { public: virtual ~TextToolEditor() override = default; virtual void handle_keyevent(Badge, GUI::KeyEvent&); + NonnullRefPtrVector actions(); protected: TextToolEditor();