Fizxing bizzare tooltip behaviour

Godot Version

4.7.2

Question

I am using _make_custom_tooltip in order to replace the standard tooltip with a RichTextLabel. That has worked fine, but the tooltip sometimes appeared partially outside the screen

I was able to fix this, but only by doing something that feels clearly unintented: grabbing the tooltip popup by connecting the RichTextLabel.ready to a custom function, forcing a popup() on it, and then updating the RichTextLabel.size to match it’s minimum size

Updating it’s size to match the minimum before calling popup() doesn’t work, as the minimum size returned at that point will be incorrect. Updating it’s size while the RichTextLabel is created also doesn’t work, as it’s get_minimum_size() does not seem to work properly at all unless the RichTextLabel is inside the scene tree

I was also able to fix it by using await get_tree.process_frame to wait for the tooltip to place itself, and then manually adjusting it’ s position, but that was even worse as the tooltip would visibly change places on the screen

The final code looks like this:

extends Control

const max_size :int = 400
var current_label :RichTextLabel

func _make_custom_tooltip(for_text: String) -> Object:
	if for_text:
		var l :RichTextLabel = RichTextLabel.new()
		l.text = for_text
		l.bbcode_enabled = true
		l.fit_content = true
		l.autowrap_mode = TextServer.AUTOWRAP_WORD
		l.custom_minimum_size.x = max_size
		l.ready.connect(on_tooltip_ready)
		current_label = l
		return l
	else:
		return null

func on_tooltip_ready() -> void:
	current_label.get_parent().popup()
	current_label.size = current_label.custom_minimum_size.max(current_label.get_minimum_size())

Is there a more elegant way to solve this problem other than creating my own tooltip entierly using the mouse_entered/mouse_exited signals?

And on that topic, does it require any more complexity than adding a RichTextLabel to a popup and calling it’s popup() on a position relative to the mouse?

Thank you in advance for the help

Looks like it’s a good way to solve the issue. I’m not aware of any other way to do it while using the built-in tooltip system.

A custom tooltip system is more involved. If you are happy with your current solution I wouldn’t bother with it.

While I do want to create my own tooltip, the pragmatic part of me has to concede that the current system is working fine, even if it’s inelegant

Also small update, I made use of the get_multiline_string_size function from the font class to determine if enabling line breaking is even necessary, and to make the horizontal size is as small as necessary without adding any new lines

But it doesn’t quite work. The tooltip always ended up slightly bigger than it should, and I found out that each time I changed the size of the popup’s children (including the internal pannel) the popup’s get_contents_minimum_size() would return a value 8 pixels bigger in each direction than before, even if the children are now smaller, and if their minimum sizes (either the custom or the get_minimum_size) did not change

Again, the pragmatic thing to do seems to be to just leave it as is, since it’s working fine and no one but me is ever gonna notice the tooltip is sometimes slightly larger than it needs to be, but this still baffles me

The code in the end ended up looking like this:

extends Control

const max_size :int = 380
var current_label :RichTextLabel

func _make_custom_tooltip(for_text: String) -> Object:
	if for_text:
		var l :RichTextLabel = RichTextLabel.new()
		l.text = for_text
		l.bbcode_enabled = true
		l.fit_content = true
		var parsed_text :String = l.get_parsed_text()
		if get_string_size(parsed_text).x > max_size:
			l.autowrap_mode = TextServer.AUTOWRAP_WORD
			l.custom_minimum_size.x = get_string_size(parsed_text, max_size).x
		else:
			l.autowrap_mode = TextServer.AUTOWRAP_OFF
		l.ready.connect(on_tooltip_ready)
		current_label = l
		return l
	else:
		return null

func get_string_size(string :String, widith :float = -1) -> Vector2:
	var font :Font = get_theme_font("normal_font", "RichTextLabel")
	var font_size :int = get_theme_font_size("normal_font", "RichTextLabel")
	return font.get_multiline_string_size(string, HORIZONTAL_ALIGNMENT_LEFT, widith, font_size)

func on_tooltip_ready() -> void:
	var popup :Popup = current_label.get_parent()
	popup.popup()
	var sz :Vector2 = current_label.custom_minimum_size.max(current_label.get_minimum_size())
	for c in popup.get_children(true):
		c.size = sz