1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 10:44:57 +00:00

TextEditor: Go-to-line now shows line in middle of view (#4008)

This commit is contained in:
Jack Byrne 2020-11-10 08:53:50 +00:00 committed by GitHub
parent 940380c986
commit 1041ab88ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View file

@ -96,9 +96,10 @@ void TextEditor::create_actions()
"Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](auto&) {
String value;
if (InputBox::show(value, window(), "Line:", "Go to line") == InputBox::ExecOK) {
auto line_number = value.to_uint();
if (line_number.has_value())
set_cursor(line_number.value() - 1, 0);
auto line_target = value.to_uint();
if (line_target.has_value()) {
set_cursor_and_focus_line(line_target.value() - 1, 0);
}
}
},
this);
@ -1156,6 +1157,24 @@ Gfx::IntRect TextEditor::line_content_rect(size_t line_index) const
};
}
void TextEditor::set_cursor_and_focus_line(size_t line, size_t column)
{
u_int index_max = line_count() - 1;
set_cursor(line, column);
if (line > 1 && line < index_max) {
int headroom = frame_inner_rect().height() / 3;
do {
auto line_data = m_line_visual_data[line];
headroom -= line_data.visual_rect.height();
line--;
} while (line > 0 && headroom > 0);
Gfx::IntRect rect = { 0, line_content_rect(line).y(),
1, frame_inner_rect().height() };
scroll_into_view(rect, false, true);
}
}
void TextEditor::update_cursor()
{
update(line_widget_rect(m_cursor.line()));