Skip to content

Commit 3b8afcd

Browse files
aaronfrankekitbdev
andcommitted
Implement fit content width in TextEdit
Co-authored-by: Kit Bishop <kitbdev@gmail.com>
1 parent 3978628 commit 3b8afcd

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

doc/classes/TextEdit.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,10 @@
13271327
Text shown when the [TextEdit] is empty. It is [b]not[/b] the [TextEdit]'s default value (see [member text]).
13281328
</member>
13291329
<member name="scroll_fit_content_height" type="bool" setter="set_fit_content_height_enabled" getter="is_fit_content_height_enabled" default="false">
1330-
If [code]true[/code], [TextEdit] will disable vertical scroll and fit minimum height to the number of visible lines.
1330+
If [code]true[/code], [TextEdit] will disable vertical scroll and fit minimum height to the number of visible lines. When both this property and [member scroll_fit_content_width] are [code]true[/code], no scrollbars will be displayed.
1331+
</member>
1332+
<member name="scroll_fit_content_width" type="bool" setter="set_fit_content_width_enabled" getter="is_fit_content_width_enabled" default="false">
1333+
If [code]true[/code], [TextEdit] will disable horizontal scroll and fit minimum width to the widest line in the text. When both this property and [member scroll_fit_content_height] are [code]true[/code], no scrollbars will be displayed.
13311334
</member>
13321335
<member name="scroll_horizontal" type="int" setter="set_h_scroll" getter="get_h_scroll" default="0">
13331336
If there is a horizontal scrollbar, this determines the current horizontal scroll value in pixels.

scene/gui/text_edit.cpp

+37-18
Original file line numberDiff line numberDiff line change
@@ -2928,7 +2928,10 @@ void TextEdit::_update_ime_text() {
29282928
Size2 TextEdit::get_minimum_size() const {
29292929
Size2 size = theme_cache.style_normal->get_minimum_size();
29302930
if (fit_content_height) {
2931-
size.y += content_height_cache;
2931+
size.y += content_size_cache.y;
2932+
}
2933+
if (fit_content_width) {
2934+
size.x += content_size_cache.x;
29322935
}
29332936
return size;
29342937
}
@@ -3098,7 +3101,7 @@ void TextEdit::apply_ime() {
30983101
insert_text_at_caret(insert_ime_text);
30993102
}
31003103

3101-
void TextEdit::set_editable(const bool p_editable) {
3104+
void TextEdit::set_editable(bool p_editable) {
31023105
if (editable == p_editable) {
31033106
return;
31043107
}
@@ -3223,7 +3226,7 @@ bool TextEdit::is_indent_wrapped_lines() const {
32233226
}
32243227

32253228
// User controls
3226-
void TextEdit::set_overtype_mode_enabled(const bool p_enabled) {
3229+
void TextEdit::set_overtype_mode_enabled(bool p_enabled) {
32273230
if (overtype_mode == p_enabled) {
32283231
return;
32293232
}
@@ -4486,7 +4489,7 @@ TextEdit::CaretType TextEdit::get_caret_type() const {
44864489
return caret_type;
44874490
}
44884491

4489-
void TextEdit::set_caret_blink_enabled(const bool p_enabled) {
4492+
void TextEdit::set_caret_blink_enabled(bool p_enabled) {
44904493
if (caret_blink_enabled == p_enabled) {
44914494
return;
44924495
}
@@ -4528,15 +4531,15 @@ bool TextEdit::is_drawing_caret_when_editable_disabled() const {
45284531
return draw_caret_when_editable_disabled;
45294532
}
45304533

4531-
void TextEdit::set_move_caret_on_right_click_enabled(const bool p_enabled) {
4534+
void TextEdit::set_move_caret_on_right_click_enabled(bool p_enabled) {
45324535
move_caret_on_right_click = p_enabled;
45334536
}
45344537

45354538
bool TextEdit::is_move_caret_on_right_click_enabled() const {
45364539
return move_caret_on_right_click;
45374540
}
45384541

4539-
void TextEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) {
4542+
void TextEdit::set_caret_mid_grapheme_enabled(bool p_enabled) {
45404543
caret_mid_grapheme_enabled = p_enabled;
45414544
}
45424545

@@ -4646,7 +4649,7 @@ void TextEdit::add_caret_at_carets(bool p_below) {
46464649
for (int i = 0; i < num_carets; i++) {
46474650
const int caret_line = get_caret_line(i);
46484651
const int caret_column = get_caret_column(i);
4649-
const bool is_selected = has_selection(i) || carets[i].last_fit_x != carets[i].selection.origin_last_fit_x;
4652+
bool is_selected = has_selection(i) || carets[i].last_fit_x != carets[i].selection.origin_last_fit_x;
46504653
const int selection_origin_line = get_selection_origin_line(i);
46514654
const int selection_origin_column = get_selection_origin_column(i);
46524655
const int caret_wrap_index = get_caret_wrap_index(i);
@@ -5111,7 +5114,7 @@ String TextEdit::get_word_under_caret(int p_caret) const {
51115114
}
51125115

51135116
/* Selection. */
5114-
void TextEdit::set_selecting_enabled(const bool p_enabled) {
5117+
void TextEdit::set_selecting_enabled(bool p_enabled) {
51155118
if (selecting_enabled == p_enabled) {
51165119
return;
51175120
}
@@ -5127,7 +5130,7 @@ bool TextEdit::is_selecting_enabled() const {
51275130
return selecting_enabled;
51285131
}
51295132

5130-
void TextEdit::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
5133+
void TextEdit::set_deselect_on_focus_loss_enabled(bool p_enabled) {
51315134
if (deselect_on_focus_loss_enabled == p_enabled) {
51325135
return;
51335136
}
@@ -5142,7 +5145,7 @@ bool TextEdit::is_deselect_on_focus_loss_enabled() const {
51425145
return deselect_on_focus_loss_enabled;
51435146
}
51445147

5145-
void TextEdit::set_drag_and_drop_selection_enabled(const bool p_enabled) {
5148+
void TextEdit::set_drag_and_drop_selection_enabled(bool p_enabled) {
51465149
drag_and_drop_selection_enabled = p_enabled;
51475150
}
51485151

@@ -5702,7 +5705,7 @@ Vector<String> TextEdit::get_line_wrapped_text(int p_line) const {
57025705

57035706
/* Viewport */
57045707
// Scrolling.
5705-
void TextEdit::set_smooth_scroll_enabled(const bool p_enabled) {
5708+
void TextEdit::set_smooth_scroll_enabled(bool p_enabled) {
57065709
v_scroll->set_smooth_scroll_enabled(p_enabled);
57075710
smooth_scroll_enabled = p_enabled;
57085711
}
@@ -5711,7 +5714,7 @@ bool TextEdit::is_smooth_scroll_enabled() const {
57115714
return smooth_scroll_enabled;
57125715
}
57135716

5714-
void TextEdit::set_scroll_past_end_of_file_enabled(const bool p_enabled) {
5717+
void TextEdit::set_scroll_past_end_of_file_enabled(bool p_enabled) {
57155718
if (scroll_past_end_of_file_enabled == p_enabled) {
57165719
return;
57175720
}
@@ -5765,7 +5768,7 @@ float TextEdit::get_v_scroll_speed() const {
57655768
return v_scroll_speed;
57665769
}
57675770

5768-
void TextEdit::set_fit_content_height_enabled(const bool p_enabled) {
5771+
void TextEdit::set_fit_content_height_enabled(bool p_enabled) {
57695772
if (fit_content_height == p_enabled) {
57705773
return;
57715774
}
@@ -5777,6 +5780,18 @@ bool TextEdit::is_fit_content_height_enabled() const {
57775780
return fit_content_height;
57785781
}
57795782

5783+
void TextEdit::set_fit_content_width_enabled(bool p_enabled) {
5784+
if (fit_content_width == p_enabled) {
5785+
return;
5786+
}
5787+
fit_content_width = p_enabled;
5788+
update_minimum_size();
5789+
}
5790+
5791+
bool TextEdit::is_fit_content_width_enabled() const {
5792+
return fit_content_width;
5793+
}
5794+
57805795
double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
57815796
ERR_FAIL_INDEX_V(p_line, text.size(), 0);
57825797
ERR_FAIL_COND_V(p_wrap_index < 0, 0);
@@ -6330,7 +6345,7 @@ bool TextEdit::is_highlight_current_line_enabled() const {
63306345
return highlight_current_line;
63316346
}
63326347

6333-
void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
6348+
void TextEdit::set_highlight_all_occurrences(bool p_enabled) {
63346349
if (highlight_all_occurrences == p_enabled) {
63356350
return;
63366351
}
@@ -6748,6 +6763,9 @@ void TextEdit::_bind_methods() {
67486763
ClassDB::bind_method(D_METHOD("set_fit_content_height_enabled", "enabled"), &TextEdit::set_fit_content_height_enabled);
67496764
ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &TextEdit::is_fit_content_height_enabled);
67506765

6766+
ClassDB::bind_method(D_METHOD("set_fit_content_width_enabled", "enabled"), &TextEdit::set_fit_content_width_enabled);
6767+
ClassDB::bind_method(D_METHOD("is_fit_content_width_enabled"), &TextEdit::is_fit_content_width_enabled);
6768+
67516769
ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0));
67526770

67536771
// Visible lines.
@@ -6872,6 +6890,7 @@ void TextEdit::_bind_methods() {
68726890
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:lines"), "set_v_scroll", "get_v_scroll");
68736891
ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
68746892
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_height"), "set_fit_content_height_enabled", "is_fit_content_height_enabled");
6893+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_width"), "set_fit_content_width_enabled", "is_fit_content_width_enabled");
68756894

68766895
ADD_GROUP("Minimap", "minimap_");
68776896
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "set_draw_minimap", "is_drawing_minimap");
@@ -7859,8 +7878,8 @@ void TextEdit::_update_scrollbars() {
78597878
total_width += minimap_width;
78607879
}
78617880

7862-
content_height_cache = MAX(total_rows, 1) * get_line_height();
7863-
if (fit_content_height) {
7881+
content_size_cache = Vector2i(total_width + 10, MAX(total_rows, 1) * get_line_height());
7882+
if (fit_content_height || fit_content_width) {
78647883
update_minimum_size();
78657884
}
78667885

@@ -8067,7 +8086,7 @@ void TextEdit::_update_minimap_hover() {
80678086
const Point2 mp = get_local_mouse_pos();
80688087
const int xmargin_end = get_size().width - theme_cache.style_normal->get_margin(SIDE_RIGHT);
80698088

8070-
const bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end;
8089+
bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end;
80718090
if (!hovering_sidebar) {
80728091
if (hovering_minimap) {
80738092
// Only redraw if the hovering status changed.
@@ -8081,7 +8100,7 @@ void TextEdit::_update_minimap_hover() {
80818100

80828101
const int row = get_minimap_line_at_pos(mp);
80838102

8084-
const bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line();
8103+
bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line();
80858104
if (new_hovering_minimap != hovering_minimap) {
80868105
// Only redraw if the hovering status changed.
80878106
hovering_minimap = new_hovering_minimap;

scene/gui/text_edit.h

+17-13
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,9 @@ class TextEdit : public Control {
505505
HScrollBar *h_scroll = nullptr;
506506
VScrollBar *v_scroll = nullptr;
507507

508-
float content_height_cache = 0.0;
508+
Vector2i content_size_cache;
509509
bool fit_content_height = false;
510+
bool fit_content_width = false;
510511
bool scroll_past_end_of_file_enabled = false;
511512

512513
// Smooth scrolling.
@@ -734,7 +735,7 @@ class TextEdit : public Control {
734735
void cancel_ime();
735736
void apply_ime();
736737

737-
void set_editable(const bool p_editable);
738+
void set_editable(bool p_editable);
738739
bool is_editable() const;
739740

740741
void set_text_direction(TextDirection p_text_direction);
@@ -755,7 +756,7 @@ class TextEdit : public Control {
755756
bool is_indent_wrapped_lines() const;
756757

757758
// User controls
758-
void set_overtype_mode_enabled(const bool p_enabled);
759+
void set_overtype_mode_enabled(bool p_enabled);
759760
bool is_overtype_mode_enabled() const;
760761

761762
void set_context_menu_enabled(bool p_enabled);
@@ -862,7 +863,7 @@ class TextEdit : public Control {
862863
void set_caret_type(CaretType p_type);
863864
CaretType get_caret_type() const;
864865

865-
void set_caret_blink_enabled(const bool p_enabled);
866+
void set_caret_blink_enabled(bool p_enabled);
866867
bool is_caret_blink_enabled() const;
867868

868869
void set_caret_blink_interval(const float p_interval);
@@ -871,10 +872,10 @@ class TextEdit : public Control {
871872
void set_draw_caret_when_editable_disabled(bool p_enable);
872873
bool is_drawing_caret_when_editable_disabled() const;
873874

874-
void set_move_caret_on_right_click_enabled(const bool p_enabled);
875+
void set_move_caret_on_right_click_enabled(bool p_enabled);
875876
bool is_move_caret_on_right_click_enabled() const;
876877

877-
void set_caret_mid_grapheme_enabled(const bool p_enabled);
878+
void set_caret_mid_grapheme_enabled(bool p_enabled);
878879
bool is_caret_mid_grapheme_enabled() const;
879880

880881
void set_multiple_carets_enabled(bool p_enabled);
@@ -910,13 +911,13 @@ class TextEdit : public Control {
910911
String get_word_under_caret(int p_caret = -1) const;
911912

912913
/* Selection. */
913-
void set_selecting_enabled(const bool p_enabled);
914+
void set_selecting_enabled(bool p_enabled);
914915
bool is_selecting_enabled() const;
915916

916-
void set_deselect_on_focus_loss_enabled(const bool p_enabled);
917+
void set_deselect_on_focus_loss_enabled(bool p_enabled);
917918
bool is_deselect_on_focus_loss_enabled() const;
918919

919-
void set_drag_and_drop_selection_enabled(const bool p_enabled);
920+
void set_drag_and_drop_selection_enabled(bool p_enabled);
920921
bool is_drag_and_drop_selection_enabled() const;
921922

922923
void set_selection_mode(SelectionMode p_mode);
@@ -965,10 +966,10 @@ class TextEdit : public Control {
965966

966967
/* Viewport. */
967968
// Scrolling.
968-
void set_smooth_scroll_enabled(const bool p_enabled);
969+
void set_smooth_scroll_enabled(bool p_enabled);
969970
bool is_smooth_scroll_enabled() const;
970971

971-
void set_scroll_past_end_of_file_enabled(const bool p_enabled);
972+
void set_scroll_past_end_of_file_enabled(bool p_enabled);
972973
bool is_scroll_past_end_of_file_enabled() const;
973974

974975
VScrollBar *get_v_scroll_bar() const;
@@ -983,9 +984,12 @@ class TextEdit : public Control {
983984
void set_v_scroll_speed(float p_speed);
984985
float get_v_scroll_speed() const;
985986

986-
void set_fit_content_height_enabled(const bool p_enabled);
987+
void set_fit_content_height_enabled(bool p_enabled);
987988
bool is_fit_content_height_enabled() const;
988989

990+
void set_fit_content_width_enabled(bool p_enabled);
991+
bool is_fit_content_width_enabled() const;
992+
989993
double get_scroll_pos_for_line(int p_line, int p_wrap_index = 0) const;
990994

991995
// Visible lines.
@@ -1071,7 +1075,7 @@ class TextEdit : public Control {
10711075
void set_highlight_current_line(bool p_enabled);
10721076
bool is_highlight_current_line_enabled() const;
10731077

1074-
void set_highlight_all_occurrences(const bool p_enabled);
1078+
void set_highlight_all_occurrences(bool p_enabled);
10751079
bool is_highlight_all_occurrences_enabled() const;
10761080

10771081
void set_draw_control_chars(bool p_enabled);

0 commit comments

Comments
 (0)