Browse Source

Update Tab hover status when adding or removing tabs, issue 22890

Paulb23 7 years ago
parent
commit
5c04535e10
2 changed files with 49 additions and 34 deletions
  1. 47 34
      scene/gui/tabs.cpp
  2. 2 0
      scene/gui/tabs.h

+ 47 - 34
scene/gui/tabs.cpp

@@ -106,41 +106,8 @@ void Tabs::_gui_input(const Ref<InputEvent> &p_event) {
 			}
 			}
 		}
 		}
 
 
-		// test hovering to display right or close button
-		int hover_now = -1;
-		int hover_buttons = -1;
-		for (int i = 0; i < tabs.size(); i++) {
-
-			if (i < offset)
-				continue;
-
-			Rect2 rect = get_tab_rect(i);
-			if (rect.has_point(pos)) {
-				hover_now = i;
-			}
-			if (tabs[i].rb_rect.has_point(pos)) {
-				rb_hover = i;
-				cb_hover = -1;
-				hover_buttons = i;
-				break;
-			} else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
-				cb_hover = i;
-				rb_hover = -1;
-				hover_buttons = i;
-				break;
-			}
-		}
-		if (hover != hover_now) {
-			hover = hover_now;
-			emit_signal("tab_hover", hover);
-		}
-
-		if (hover_buttons == -1) { // no hover
-			rb_hover = hover_buttons;
-			cb_hover = hover_buttons;
-		}
+		_update_hover();
 		update();
 		update();
-
 		return;
 		return;
 	}
 	}
 
 
@@ -522,6 +489,48 @@ Ref<Texture> Tabs::get_tab_right_button(int p_tab) const {
 	return tabs[p_tab].right_button;
 	return tabs[p_tab].right_button;
 }
 }
 
 
+void Tabs::_update_hover() {
+
+	if (!is_inside_tree()) {
+		return;
+	}
+
+	const Point2 &pos = get_local_mouse_position();
+	// test hovering to display right or close button
+	int hover_now = -1;
+	int hover_buttons = -1;
+	for (int i = 0; i < tabs.size(); i++) {
+
+		if (i < offset)
+			continue;
+
+		Rect2 rect = get_tab_rect(i);
+		if (rect.has_point(pos)) {
+			hover_now = i;
+		}
+		if (tabs[i].rb_rect.has_point(pos)) {
+			rb_hover = i;
+			cb_hover = -1;
+			hover_buttons = i;
+			break;
+		} else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
+			cb_hover = i;
+			rb_hover = -1;
+			hover_buttons = i;
+			break;
+		}
+	}
+	if (hover != hover_now) {
+		hover = hover_now;
+		emit_signal("tab_hover", hover);
+	}
+
+	if (hover_buttons == -1) { // no hover
+		rb_hover = hover_buttons;
+		cb_hover = hover_buttons;
+	}
+}
+
 void Tabs::_update_cache() {
 void Tabs::_update_cache() {
 	Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
 	Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
 	Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
 	Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
@@ -597,6 +606,7 @@ void Tabs::add_tab(const String &p_str, const Ref<Texture> &p_icon) {
 
 
 	tabs.push_back(t);
 	tabs.push_back(t);
 	_update_cache();
 	_update_cache();
+	call_deferred("_update_hover");
 	update();
 	update();
 	minimum_size_changed();
 	minimum_size_changed();
 }
 }
@@ -604,6 +614,7 @@ void Tabs::add_tab(const String &p_str, const Ref<Texture> &p_icon) {
 void Tabs::clear_tabs() {
 void Tabs::clear_tabs() {
 	tabs.clear();
 	tabs.clear();
 	current = 0;
 	current = 0;
+	call_deferred("_update_hover");
 	update();
 	update();
 }
 }
 
 
@@ -614,6 +625,7 @@ void Tabs::remove_tab(int p_idx) {
 	if (current >= p_idx)
 	if (current >= p_idx)
 		current--;
 		current--;
 	_update_cache();
 	_update_cache();
+	call_deferred("_update_hover");
 	update();
 	update();
 	minimum_size_changed();
 	minimum_size_changed();
 
 
@@ -931,6 +943,7 @@ bool Tabs::get_select_with_rmb() const {
 void Tabs::_bind_methods() {
 void Tabs::_bind_methods() {
 
 
 	ClassDB::bind_method(D_METHOD("_gui_input"), &Tabs::_gui_input);
 	ClassDB::bind_method(D_METHOD("_gui_input"), &Tabs::_gui_input);
+	ClassDB::bind_method(D_METHOD("_update_hover"), &Tabs::_update_hover);
 	ClassDB::bind_method(D_METHOD("get_tab_count"), &Tabs::get_tab_count);
 	ClassDB::bind_method(D_METHOD("get_tab_count"), &Tabs::get_tab_count);
 	ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &Tabs::set_current_tab);
 	ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &Tabs::set_current_tab);
 	ClassDB::bind_method(D_METHOD("get_current_tab"), &Tabs::get_current_tab);
 	ClassDB::bind_method(D_METHOD("get_current_tab"), &Tabs::get_current_tab);

+ 2 - 0
scene/gui/tabs.h

@@ -97,6 +97,8 @@ private:
 
 
 	int get_tab_width(int p_idx) const;
 	int get_tab_width(int p_idx) const;
 	void _ensure_no_over_offset();
 	void _ensure_no_over_offset();
+
+	void _update_hover();
 	void _update_cache();
 	void _update_cache();
 
 
 protected:
 protected: