Panel.SizeToChildren not working

Hi, I’m making a gamemode to play with my friends.

I’m scripting some kind of tooltip that’s supposed to appear when you hover over an inventory item. This hover panel contains a title, subtitle and a description. Since the length of these labels can vary, I need the panel to automatically detect the correct size in order to make sure everything fits. I’m using Panel:Dock, DLabel:SetAutoStretchVertical and Panel:SizeToChildren.

This is the code:

local function ItemHover( x, y )

	local title = "TITLE"
	local subtitle = "SUBTITLE"
	local desc = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Cursus euismod quis viverra nibh cras pulvinar mattis nunc sed."
	local color = Color( 201, 145, 99 )

	local w = ScrScaleX(350)
	local h = ScrScaleY(250)
	if x + w >= ScrW() then x = ScrW() - w end
	if y + h >= ScrH() then y = ScrH() - h end

	local textInset = 10

	local mainPanel = vgui.Create( "DPanel" )
	mainPanel:SetSize( w, h )
	mainPanel:SetPos( x, y )
	mainPanel:SetBackgroundColor( color )
	mainPanel:MakePopup()
	mainPanel:SetKeyboardInputEnabled( false )
	mainPanel:SetMouseInputEnabled( false )
	mainPanel:MoveToFront()

	local titlePanel = vgui.Create( "DPanel", mainPanel )
	titlePanel:SetTall( ScrScaleY(70) )
	titlePanel:Dock( TOP )
	titlePanel:SetBackgroundColor( Color( 0, 0, 0, 175 ) )
	titlePanel.PaintOver = function( self, w, h )

		local anim = math.abs(math.cos( CurTime() * 1.5 ))

		surface.SetMaterial( mat_gradient_r )
		surface.SetDrawColor( 0, 0, 0, 135 )
		surface.DrawTexturedRect( 0, 0, w, h )
		surface.SetMaterial( mat_gradient_l )
		surface.SetDrawColor( Color(color.r, color.g, color.b, anim * 55) )
		surface.DrawTexturedRect( 0, 0, w, h )

	end

	local titleLabel = vgui.Create( "DLabel", titlePanel )
	titleLabel:SetText( string.upper( title ) )
	titleLabel:SetFont( "GillSansMT_medium_bold" )
	titleLabel:SetTextColor( Color( 55, 55, 55, 175 ) )
	titleLabel:Dock( TOP )
	titleLabel:DockMargin( textInset, 2, textInset, -textInset )
	titleLabel:SetAutoStretchVertical( true )

	local subtitleLabel = vgui.Create( "DLabel", titlePanel )
	subtitleLabel:SetText( string.upper( subtitle ) )
	subtitleLabel:SetFont( "GillSansMT_medium_light" )
	subtitleLabel:SetTextColor( Color( 55, 55, 55, 175 ) )
	subtitleLabel:Dock( TOP )
	subtitleLabel:DockMargin( textInset, 5, textInset, -textInset )
	subtitleLabel:SetAutoStretchVertical( true )

	titleLabel:InvalidateLayout(true)
	titleLabel:SizeToChildren(false, true)

	local descLabel = vgui.Create( "DLabel", mainPanel )
	descLabel:SetText( desc )
	descLabel:SetFont( "GillSansMT_medium_small" )
	descLabel:SetTextColor( Color( 255, 255, 255, 155 ) )
	descLabel:SetContentAlignment( 7 )
	descLabel:SetWrap( true )
	descLabel:Dock( TOP )
	descLabel:DockMargin( textInset, textInset, textInset, textInset )
	descLabel:SetAutoStretchVertical( true )
	
	mainPanel:InvalidateLayout(true)
	mainPanel:SizeToChildren(true, true)

	return mainPanel
end

And this is the result:

Notice how the main panel doesn’t stretch to fit the lorem ipsum label entirely.

How to fix?