Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize DLabel autostretch behavior #2089

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions garrysmod/lua/includes/extensions/client/panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,30 @@ end
--[[---------------------------------------------------------
Name: SizeToContentsY (Only works on Labels)
-----------------------------------------------------------]]
function meta:SizeToContentsY( addval )
function meta:SizeToContentsY( addVal )

local w, h = self:GetContentSize()
if ( !w || !h ) then return end
if ( !w or !h ) then return end

self:SetTall( h + ( addval or 0 ) )
local newSize = h + ( addVal or 0 )
if ( newSize == self:GetTall() ) then return end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these checks actually do anything? They are also performed on the C side.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally added those checks to avoid constant PerformLayouts with certain DLabel panels that have autostretch enabled and thought that I could remove the variable to make the changes a bit simpler, but it seems like this change causes the same issue whereas using a variable for caching does not.

Here's an example that I'm using to test this behavior with vgui_visualizelayout 1:

    local testFrame = vgui.Create("DFrame")
    testFrame:SetSize(400, 400)
    testFrame:SetDeleteOnClose(true)
    local testLabel = vgui.Create("DLabel", testFrame)
    testLabel:Dock(FILL)
    testLabel:SetText("test")
    testLabel:SetAutoStretchVertical(true)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the proposed changes just make the function not work if the label's size gets changing by whatever means?

For example:

local testFrame = vgui.Create("DFrame")
testFrame:SetSize(400, 400)
testFrame:SetDeleteOnClose(true)
testFrame:MakePopup()

local testLabel = vgui.Create("DLabel", testFrame)
testLabel:Dock(FILL)
testLabel:SetText("test")
print( "Size 1: ", testLabel:GetTall())
testLabel:SizeToContentsY()
print( "Size 2: ", testLabel:GetTall())
testLabel:SetTall( 60 )
print( "Size 3: ", testLabel:GetTall())
testLabel:SizeToContentsY()
print( "Size 4: ", testLabel:GetTall()) -- Not sized to contents!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I believe I've come up with a solution that should both avoid the broken resizing you've shown and prevent any potential infinite layout loops in 1648df2.


self:SetTall( newSize )

end

--[[---------------------------------------------------------
Name: SizeToContentsX (Only works on Labels)
-----------------------------------------------------------]]
function meta:SizeToContentsX( addval )
function meta:SizeToContentsX( addVal )

local w, h = self:GetContentSize()
if ( !w || !h ) then return end
if ( !w or !h ) then return end

local newSize = w + ( addVal or 0 )
if ( newSize == self:GetWide() ) then return end

self:SetWide( w + ( addval or 0 ) )
self:SetWide( newSize )

end

Expand Down
10 changes: 3 additions & 7 deletions garrysmod/lua/vgui/dlabel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,16 @@ function PANEL:ApplySchemeSettings()

end

function PANEL:Think()
function PANEL:PerformLayout()

self:ApplySchemeSettings()

if ( self:GetAutoStretchVertical() ) then
self:SizeToContentsY()
end

end

function PANEL:PerformLayout()

self:ApplySchemeSettings()

end


function PANEL:OnCursorEntered()

Expand Down