GTextEditor: Move "Go to line" feature from HackStudio into GTextEditor

This is a handy feature for any multi-line GTextEditor, so let's just
have it in the base widget. :^)

Fixes #1122.
This commit is contained in:
Andreas Kling 2020-01-23 21:21:55 +01:00
parent ebfb3d91e2
commit a33259483a
2 changed files with 15 additions and 14 deletions

View file

@ -67,20 +67,6 @@ EditorWrapper::EditorWrapper(GWidget* parent)
m_editor->on_focus = [this] {
g_current_editor_wrapper = this;
};
m_editor->add_custom_context_menu_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) {
m_editor->set_cursor(line_number - 1, 0);
}
}
},
m_editor));
}
EditorWrapper::~EditorWrapper()

View file

@ -31,6 +31,7 @@
#include <LibGUI/GAction.h>
#include <LibGUI/GClipboard.h>
#include <LibGUI/GFontDatabase.h>
#include <LibGUI/GInputBox.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GScrollBar.h>
@ -1220,6 +1221,20 @@ void GTextEditor::context_menu_event(GContextMenuEvent& event)
m_context_menu->add_action(copy_action());
m_context_menu->add_action(paste_action());
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);
}
}));
}
if (!m_custom_context_menu_actions.is_empty()) {
m_context_menu->add_separator();
for (auto& action : m_custom_context_menu_actions) {