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.
This commit is contained in:
Andreas Kling 2020-01-23 21:27:27 +01:00
parent a33259483a
commit 1c34348b0c
Notes: sideshowbarker 2024-07-19 09:51:43 +09:00
2 changed files with 15 additions and 11 deletions

View file

@ -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();

View file

@ -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<GAction> m_copy_action;
RefPtr<GAction> m_paste_action;
RefPtr<GAction> m_delete_action;
RefPtr<GAction> m_go_to_line_action;
CElapsedTimer m_triple_click_timer;
NonnullRefPtrVector<GAction> m_custom_context_menu_actions;