Browse Source

Allow to disable TextEdit vertical scroll

kobewi 3 years ago
parent
commit
41f6e7c50e
3 changed files with 37 additions and 4 deletions
  1. 4 1
      doc/classes/TextEdit.xml
  2. 28 3
      scene/gui/text_edit.cpp
  3. 5 0
      scene/gui/text_edit.h

+ 4 - 1
doc/classes/TextEdit.xml

@@ -998,13 +998,16 @@
 		<member name="placeholder_text" type="String" setter="set_placeholder" getter="get_placeholder" default="&quot;&quot;">
 		<member name="placeholder_text" type="String" setter="set_placeholder" getter="get_placeholder" default="&quot;&quot;">
 			Text shown when the [TextEdit] is empty. It is [b]not[/b] the [TextEdit]'s default value (see [member text]).
 			Text shown when the [TextEdit] is empty. It is [b]not[/b] the [TextEdit]'s default value (see [member text]).
 		</member>
 		</member>
+		<member name="scroll_fit_content_height" type="bool" setter="set_fit_content_height_enabled" getter="is_fit_content_height_enabled" default="false">
+			If [code]true[/code], [TextEdit] will disable vertical scroll and fit minimum height to the number of visible lines.
+		</member>
 		<member name="scroll_horizontal" type="int" setter="set_h_scroll" getter="get_h_scroll" default="0">
 		<member name="scroll_horizontal" type="int" setter="set_h_scroll" getter="get_h_scroll" default="0">
 			If there is a horizontal scrollbar, this determines the current horizontal scroll value in pixels.
 			If there is a horizontal scrollbar, this determines the current horizontal scroll value in pixels.
 		</member>
 		</member>
 		<member name="scroll_past_end_of_file" type="bool" setter="set_scroll_past_end_of_file_enabled" getter="is_scroll_past_end_of_file_enabled" default="false">
 		<member name="scroll_past_end_of_file" type="bool" setter="set_scroll_past_end_of_file_enabled" getter="is_scroll_past_end_of_file_enabled" default="false">
 			Allow scrolling past the last line into "virtual" space.
 			Allow scrolling past the last line into "virtual" space.
 		</member>
 		</member>
-		<member name="scroll_smooth" type="bool" setter="set_smooth_scroll_enable" getter="is_smooth_scroll_enabled" default="false">
+		<member name="scroll_smooth" type="bool" setter="set_smooth_scroll_enabled" getter="is_smooth_scroll_enabled" default="false">
 			Scroll smoothly over the text rather then jumping to the next location.
 			Scroll smoothly over the text rather then jumping to the next location.
 		</member>
 		</member>
 		<member name="scroll_v_scroll_speed" type="float" setter="set_v_scroll_speed" getter="get_v_scroll_speed" default="80.0">
 		<member name="scroll_v_scroll_speed" type="float" setter="set_v_scroll_speed" getter="get_v_scroll_speed" default="80.0">

+ 28 - 3
scene/gui/text_edit.cpp

@@ -2668,7 +2668,11 @@ void TextEdit::_update_caches() {
 
 
 /* General overrides. */
 /* General overrides. */
 Size2 TextEdit::get_minimum_size() const {
 Size2 TextEdit::get_minimum_size() const {
-	return style_normal->get_minimum_size();
+	Size2 size = style_normal->get_minimum_size();
+	if (fit_content_height) {
+		size.y += content_height_cache;
+	}
+	return size;
 }
 }
 
 
 bool TextEdit::is_text_field() const {
 bool TextEdit::is_text_field() const {
@@ -4499,6 +4503,18 @@ float TextEdit::get_v_scroll_speed() const {
 	return v_scroll_speed;
 	return v_scroll_speed;
 }
 }
 
 
+void TextEdit::set_fit_content_height_enabled(const bool p_enabled) {
+	if (fit_content_height == p_enabled) {
+		return;
+	}
+	fit_content_height = p_enabled;
+	update_minimum_size();
+}
+
+bool TextEdit::is_fit_content_height_enabled() const {
+	return fit_content_height;
+}
+
 double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
 double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
 	ERR_FAIL_INDEX_V(p_line, text.size(), 0);
 	ERR_FAIL_INDEX_V(p_line, text.size(), 0);
 	ERR_FAIL_COND_V(p_wrap_index < 0, 0);
 	ERR_FAIL_COND_V(p_wrap_index < 0, 0);
@@ -5297,7 +5313,7 @@ void TextEdit::_bind_methods() {
 
 
 	/* Viewport. */
 	/* Viewport. */
 	// Scrolling.
 	// Scrolling.
-	ClassDB::bind_method(D_METHOD("set_smooth_scroll_enable", "enable"), &TextEdit::set_smooth_scroll_enabled);
+	ClassDB::bind_method(D_METHOD("set_smooth_scroll_enabled", "enable"), &TextEdit::set_smooth_scroll_enabled);
 	ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
 	ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
 
 
 	ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
 	ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
@@ -5312,6 +5328,9 @@ void TextEdit::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
 	ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
 	ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
 	ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
 
 
+	ClassDB::bind_method(D_METHOD("set_fit_content_height_enabled"), &TextEdit::set_fit_content_height_enabled);
+	ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &TextEdit::is_fit_content_height_enabled);
+
 	ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0));
 	ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0));
 
 
 	// Visible lines.
 	// Visible lines.
@@ -5431,11 +5450,12 @@ void TextEdit::_bind_methods() {
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE), "set_syntax_highlighter", "get_syntax_highlighter");
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE), "set_syntax_highlighter", "get_syntax_highlighter");
 
 
 	ADD_GROUP("Scroll", "scroll_");
 	ADD_GROUP("Scroll", "scroll_");
-	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_smooth"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled");
+	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_smooth"), "set_smooth_scroll_enabled", "is_smooth_scroll_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_v_scroll_speed", PROPERTY_HINT_NONE, "suffix:px/s"), "set_v_scroll_speed", "get_v_scroll_speed");
 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_v_scroll_speed", PROPERTY_HINT_NONE, "suffix:px/s"), "set_v_scroll_speed", "get_v_scroll_speed");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_past_end_of_file"), "set_scroll_past_end_of_file_enabled", "is_scroll_past_end_of_file_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_past_end_of_file"), "set_scroll_past_end_of_file_enabled", "is_scroll_past_end_of_file_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
+	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_height"), "set_fit_content_height_enabled", "is_fit_content_height_enabled");
 
 
 	ADD_GROUP("Minimap", "minimap_");
 	ADD_GROUP("Minimap", "minimap_");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "set_draw_minimap", "is_drawing_minimap");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "set_draw_minimap", "is_drawing_minimap");
@@ -6197,6 +6217,11 @@ void TextEdit::_update_scrollbars() {
 		total_width += minimap_width;
 		total_width += minimap_width;
 	}
 	}
 
 
+	content_height_cache = MAX(total_rows, 1) * get_line_height();
+	if (fit_content_height) {
+		update_minimum_size();
+	}
+
 	updating_scrolls = true;
 	updating_scrolls = true;
 
 
 	if (total_rows > visible_rows) {
 	if (total_rows > visible_rows) {

+ 5 - 0
scene/gui/text_edit.h

@@ -456,6 +456,8 @@ private:
 	HScrollBar *h_scroll = nullptr;
 	HScrollBar *h_scroll = nullptr;
 	VScrollBar *v_scroll = nullptr;
 	VScrollBar *v_scroll = nullptr;
 
 
+	float content_height_cache = 0.0;
+	bool fit_content_height = false;
 	bool scroll_past_end_of_file_enabled = false;
 	bool scroll_past_end_of_file_enabled = false;
 
 
 	// Smooth scrolling.
 	// Smooth scrolling.
@@ -851,6 +853,9 @@ public:
 	void set_v_scroll_speed(float p_speed);
 	void set_v_scroll_speed(float p_speed);
 	float get_v_scroll_speed() const;
 	float get_v_scroll_speed() const;
 
 
+	void set_fit_content_height_enabled(const bool p_enabled);
+	bool is_fit_content_height_enabled() const;
+
 	double get_scroll_pos_for_line(int p_line, int p_wrap_index = 0) const;
 	double get_scroll_pos_for_line(int p_line, int p_wrap_index = 0) const;
 
 
 	// Visible lines.
 	// Visible lines.