Skip to content

Commit 1c0bff5

Browse files
committed
Merge pull request godotengine#93792 from kitbdev/fix-center-viewport-horizontal
Fix center viewport not working horizontally
2 parents a366f23 + 46fa858 commit 1c0bff5

File tree

2 files changed

+70
-94
lines changed

2 files changed

+70
-94
lines changed

scene/gui/text_edit.cpp

Lines changed: 68 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6004,7 +6004,7 @@ int TextEdit::get_total_visible_line_count() const {
60046004
void TextEdit::adjust_viewport_to_caret(int p_caret) {
60056005
ERR_FAIL_INDEX(p_caret, carets.size());
60066006

6007-
// Make sure Caret is visible on the screen.
6007+
// Move viewport so the caret is visible on the screen vertically.
60086008
scrolling = false;
60096009
minimap_clicked = false;
60106010

@@ -6024,105 +6024,19 @@ void TextEdit::adjust_viewport_to_caret(int p_caret) {
60246024
set_line_as_last_visible(cur_line, cur_wrap);
60256025
}
60266026

6027-
int visible_width = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding;
6028-
if (draw_minimap) {
6029-
visible_width -= minimap_width;
6030-
}
6031-
if (v_scroll->is_visible_in_tree()) {
6032-
visible_width -= v_scroll->get_combined_minimum_size().width;
6033-
}
6034-
visible_width -= 20; // Give it a little more space.
6035-
6036-
if (visible_width <= 0) {
6037-
// Not resized yet.
6038-
return;
6039-
}
6040-
6041-
Vector2i caret_pos;
6042-
6043-
// Get position of the start of caret.
6044-
if (has_ime_text() && ime_selection.x != 0) {
6045-
caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x, get_caret_line(p_caret), get_caret_column(p_caret) + ime_selection.x);
6046-
} else {
6047-
caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
6048-
}
6049-
6050-
// Get position of the end of caret.
6051-
if (has_ime_text()) {
6052-
if (ime_selection.y > 0) {
6053-
caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x + ime_selection.y, get_caret_line(p_caret), get_caret_column(p_caret) + ime_selection.x + ime_selection.y);
6054-
} else {
6055-
caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_text.length(), get_caret_line(p_caret), get_caret_column(p_caret) + ime_text.length());
6056-
}
6057-
} else {
6058-
caret_pos.y = caret_pos.x;
6059-
}
6060-
6061-
if (MAX(caret_pos.x, caret_pos.y) > (first_visible_col + visible_width)) {
6062-
first_visible_col = MAX(caret_pos.x, caret_pos.y) - visible_width + 1;
6063-
}
6064-
6065-
if (MIN(caret_pos.x, caret_pos.y) < first_visible_col) {
6066-
first_visible_col = MIN(caret_pos.x, caret_pos.y);
6067-
}
6068-
h_scroll->set_value(first_visible_col);
6069-
6070-
queue_redraw();
6027+
_adjust_viewport_to_caret_horizontally(p_caret);
60716028
}
60726029

60736030
void TextEdit::center_viewport_to_caret(int p_caret) {
60746031
ERR_FAIL_INDEX(p_caret, carets.size());
60756032

6076-
// Move viewport so the caret is in the center of the screen.
6033+
// Move viewport so the caret is in the center of the screen vertically.
60776034
scrolling = false;
60786035
minimap_clicked = false;
60796036

60806037
set_line_as_center_visible(get_caret_line(p_caret), get_caret_wrap_index(p_caret));
6081-
int visible_width = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding;
6082-
if (draw_minimap) {
6083-
visible_width -= minimap_width;
6084-
}
6085-
if (v_scroll->is_visible_in_tree()) {
6086-
visible_width -= v_scroll->get_combined_minimum_size().width;
6087-
}
6088-
visible_width -= 20; // Give it a little more space.
6089-
6090-
if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
6091-
// Center x offset.
6092-
6093-
Vector2i caret_pos;
60946038

6095-
// Get position of the start of caret.
6096-
if (has_ime_text() && ime_selection.x != 0) {
6097-
caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x, get_caret_line(p_caret), get_caret_column(p_caret) + ime_selection.x);
6098-
} else {
6099-
caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
6100-
}
6101-
6102-
// Get position of the end of caret.
6103-
if (has_ime_text()) {
6104-
if (ime_selection.y > 0) {
6105-
caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x + ime_selection.y, get_caret_line(p_caret), get_caret_column(p_caret) + ime_selection.x + ime_selection.y);
6106-
} else {
6107-
caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_text.length(), get_caret_line(p_caret), get_caret_column(p_caret) + ime_text.length());
6108-
}
6109-
} else {
6110-
caret_pos.y = caret_pos.x;
6111-
}
6112-
6113-
if (MAX(caret_pos.x, caret_pos.y) > (first_visible_col + visible_width)) {
6114-
first_visible_col = MAX(caret_pos.x, caret_pos.y) - visible_width + 1;
6115-
}
6116-
6117-
if (MIN(caret_pos.x, caret_pos.y) < first_visible_col) {
6118-
first_visible_col = MIN(caret_pos.x, caret_pos.y);
6119-
}
6120-
} else {
6121-
first_visible_col = 0;
6122-
}
6123-
h_scroll->set_value(first_visible_col);
6124-
6125-
queue_redraw();
6039+
_adjust_viewport_to_caret_horizontally(p_caret);
61266040
}
61276041

61286042
/* Minimap */
@@ -8212,6 +8126,70 @@ void TextEdit::_scroll_lines_down() {
82128126
merge_overlapping_carets();
82138127
}
82148128

8129+
void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret) {
8130+
if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
8131+
first_visible_col = 0;
8132+
h_scroll->set_value(first_visible_col);
8133+
queue_redraw();
8134+
return;
8135+
}
8136+
8137+
int visible_width = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding;
8138+
if (draw_minimap) {
8139+
visible_width -= minimap_width;
8140+
}
8141+
if (v_scroll->is_visible_in_tree()) {
8142+
visible_width -= v_scroll->get_combined_minimum_size().width;
8143+
}
8144+
visible_width -= 20; // Give it a little more space.
8145+
8146+
if (visible_width <= 0) {
8147+
// Not resized yet.
8148+
return;
8149+
}
8150+
8151+
int caret_start_pos;
8152+
int caret_end_pos;
8153+
bool prioritize_end = true;
8154+
8155+
// Get start and end position of the caret.
8156+
if (has_ime_text()) {
8157+
// Use the size of the IME.
8158+
int ime_start_column = get_caret_column(p_caret) + ime_selection.x;
8159+
caret_start_pos = _get_column_x_offset_for_line(ime_start_column, get_caret_line(p_caret), ime_start_column);
8160+
int ime_end_column = get_caret_column(p_caret) + (ime_selection.y > 0 ? ime_selection.x + ime_selection.y : ime_text.length());
8161+
caret_end_pos = _get_column_x_offset_for_line(ime_end_column, get_caret_line(p_caret), ime_end_column);
8162+
prioritize_end = false;
8163+
} else if (has_selection(p_caret) && get_selection_from_line(p_caret) == get_selection_to_line(p_caret)) {
8164+
// Use selection if it is on one line.
8165+
caret_start_pos = _get_column_x_offset_for_line(get_selection_from_column(p_caret), get_caret_line(p_caret), get_selection_from_column(p_caret));
8166+
caret_end_pos = _get_column_x_offset_for_line(get_selection_to_column(p_caret), get_caret_line(p_caret), get_selection_to_column(p_caret));
8167+
prioritize_end = is_caret_after_selection_origin();
8168+
} else {
8169+
caret_start_pos = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
8170+
caret_end_pos = caret_start_pos;
8171+
}
8172+
8173+
if (caret_start_pos > caret_end_pos) {
8174+
// For RTL text.
8175+
SWAP(caret_start_pos, caret_end_pos);
8176+
prioritize_end = !prioritize_end;
8177+
}
8178+
8179+
if (!prioritize_end && caret_end_pos > first_visible_col + visible_width) {
8180+
first_visible_col = caret_end_pos - visible_width + 1;
8181+
}
8182+
if (caret_start_pos < first_visible_col) {
8183+
first_visible_col = caret_start_pos;
8184+
}
8185+
if (prioritize_end && caret_end_pos > first_visible_col + visible_width) {
8186+
first_visible_col = caret_end_pos - visible_width + 1;
8187+
}
8188+
8189+
h_scroll->set_value(first_visible_col);
8190+
queue_redraw();
8191+
}
8192+
82158193
// Minimap
82168194

82178195
void TextEdit::_update_minimap_hover() {

scene/gui/text_edit.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ class TextEdit : public Control {
191191
int gutter_count = 0;
192192
bool indent_wrapped_lines = false;
193193

194-
void _calculate_line_height() const;
195-
void _calculate_max_line_width() const;
196-
197194
public:
198195
void set_tab_size(int p_tab_size);
199196
int get_tab_size() const;
@@ -216,7 +213,6 @@ class TextEdit : public Control {
216213
void set_use_custom_word_separators(bool p_enabled);
217214
bool is_custom_word_separators_enabled() const;
218215

219-
void set_word_separators(const String &p_separators);
220216
void set_custom_word_separators(const String &p_separators);
221217
String get_enabled_word_separators() const;
222218
String get_custom_word_separators() const;
@@ -538,6 +534,8 @@ class TextEdit : public Control {
538534
void _scroll_lines_up();
539535
void _scroll_lines_down();
540536

537+
void _adjust_viewport_to_caret_horizontally(int p_caret = 0);
538+
541539
// Minimap.
542540
bool draw_minimap = false;
543541

0 commit comments

Comments
 (0)