From 1c34348b0cac714c2b5e775eccf3f9fd14afaf16 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 23 Jan 2020 21:27:27 +0100 Subject: [PATCH] GTextEditor: Create the "go to line" action eagerly Previously it was created the first time you requested a context menu for the GTextEditor by right-clicking in it. That meant it wasn't possible to use Ctrl+L to "go to line" before you had first right-clicked the editor. --- Libraries/LibGUI/GTextEditor.cpp | 24 +++++++++++++----------- Libraries/LibGUI/GTextEditor.h | 2 ++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Libraries/LibGUI/GTextEditor.cpp b/Libraries/LibGUI/GTextEditor.cpp index 0ff02f1da9c..ef041c5ca90 100644 --- a/Libraries/LibGUI/GTextEditor.cpp +++ b/Libraries/LibGUI/GTextEditor.cpp @@ -83,6 +83,18 @@ void GTextEditor::create_actions() m_copy_action = GCommonActions::make_copy_action([&](auto&) { copy(); }, this); m_paste_action = GCommonActions::make_paste_action([&](auto&) { paste(); }, this); m_delete_action = GCommonActions::make_delete_action([&](auto&) { do_delete(); }, this); + m_go_to_line_action = GAction::create( + "Go to line...", { Mod_Ctrl, Key_L }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](auto&) { + auto input_box = GInputBox::construct("Line:", "Go to line", window()); + auto result = input_box->exec(); + if (result == GInputBox::ExecOK) { + bool ok; + auto line_number = input_box->text_value().to_uint(ok); + if (ok) + set_cursor(line_number - 1, 0); + } + }, + this); } void GTextEditor::set_text(const StringView& text) @@ -1223,17 +1235,7 @@ void GTextEditor::context_menu_event(GContextMenuEvent& event) m_context_menu->add_action(delete_action()); if (is_multi_line()) { m_context_menu->add_separator(); - m_context_menu->add_action(GAction::create( - "Go to line...", { Mod_Ctrl, Key_L }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](auto&) { - auto input_box = GInputBox::construct("Line:", "Go to line", window()); - auto result = input_box->exec(); - if (result == GInputBox::ExecOK) { - bool ok; - auto line_number = input_box->text_value().to_uint(ok); - if (ok) - set_cursor(line_number - 1, 0); - } - })); + m_context_menu->add_action(go_to_line_action()); } if (!m_custom_context_menu_actions.is_empty()) { m_context_menu->add_separator(); diff --git a/Libraries/LibGUI/GTextEditor.h b/Libraries/LibGUI/GTextEditor.h index a845569fb2d..b278a49a617 100644 --- a/Libraries/LibGUI/GTextEditor.h +++ b/Libraries/LibGUI/GTextEditor.h @@ -124,6 +124,7 @@ public: GAction& copy_action() { return *m_copy_action; } GAction& paste_action() { return *m_paste_action; } GAction& delete_action() { return *m_delete_action; } + GAction& go_to_line_action() { return *m_go_to_line_action; } void add_custom_context_menu_action(GAction&); @@ -230,6 +231,7 @@ private: RefPtr m_copy_action; RefPtr m_paste_action; RefPtr m_delete_action; + RefPtr m_go_to_line_action; CElapsedTimer m_triple_click_timer; NonnullRefPtrVector m_custom_context_menu_actions;