Less garbage: reuse window stack entries in Begin, scratch rects in UpdateWindowManualResize/GetResizeBorderRect

This commit is contained in:
4DBug committed 2026-10-17 08:49:45 -05:00
1 parent 5029525f2a
commit 25a7387642
5 files changed
+101 -62

No files matched your search

+33 -20
View File
@@ -4872,26 +4872,33 @@ local ImGuiResizeBorderDef = {
--- @param thickness float
--- @return ImRect
--- @nodiscard
local function GetResizeBorderRect(window, border_n, perp_padding, thickness)
local rect = window:Rect()
-- [Luau] out: optional ImRect written in place (no allocation)
local function GetResizeBorderRect(window, border_n, perp_padding, thickness, out)
local x0, y0 = window.Pos.x, window.Pos.y
local x1, y1 = x0 + window.Size.x, y0 + window.Size.y
if thickness == 0.0 then
ImVec2_CopyV(rect.Max, rect.Max.x - 1, rect.Max.y - 1)
x1, y1 = x1 - 1, y1 - 1
end
local a, b, c, d
if border_n == ImGuiDir.Left then
return ImRect(rect.Min.x - thickness, rect.Min.y + perp_padding, rect.Min.x + thickness, rect.Max.y - perp_padding)
a, b, c, d = x0 - thickness, y0 + perp_padding, x0 + thickness, y1 - perp_padding
elseif border_n == ImGuiDir.Right then
a, b, c, d = x1 - thickness, y0 + perp_padding, x1 + thickness, y1 - perp_padding
elseif border_n == ImGuiDir.Up then
a, b, c, d = x0 + perp_padding, y0 - thickness, x1 - perp_padding, y0 + thickness
elseif border_n == ImGuiDir.Down then
a, b, c, d = x0 + perp_padding, y1 - thickness, x1 - perp_padding, y1 + thickness
else
IM_ASSERT(false)
a, b, c, d = 0, 0, 0, 0
end
if border_n == ImGuiDir.Right then
return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding)
if out then
out.Min.x, out.Min.y, out.Max.x, out.Max.y = a, b, c, d
return out
end
if border_n == ImGuiDir.Up then
return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness)
end
if border_n == ImGuiDir.Down then
return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness)
end
IM_ASSERT(false)
return ImRect()
return ImRect(a, b, c, d)
end
local UpdateWindowManualResize_rect = ImRect() -- [Luau] scratch grip/border rect: ItemAdd() copies, ButtonBehavior() reads
-- Known size hash
-- It is ok to call ImHashData on a string with known length but the ### operator won't be supported.
@@ -10173,10 +10180,12 @@ local function UpdateWindowManualResize(window, resize_grip_count, resize_grip_c
local corner_pos = def.CornerPosN
local inner_dir = def.InnerDir
local corner = ImVec2(window.Pos.x + corner_pos.x * window.Size.x, window.Pos.y + corner_pos.y * window.Size.y)
local corner_x, corner_y = window.Pos.x + corner_pos.x * window.Size.x, window.Pos.y + corner_pos.y * window.Size.y
-- Using the FlattenChilds button flag we make the resize button accessible even if we are hovering over a child window
local resize_rect = ImRect(corner - inner_dir * grip_hover_outer_size, corner + inner_dir * grip_hover_inner_size)
local resize_rect = UpdateWindowManualResize_rect -- [Luau] scratch, scalar math
resize_rect.Min.x, resize_rect.Min.y = corner_x - inner_dir.x * grip_hover_outer_size, corner_y - inner_dir.y * grip_hover_outer_size
resize_rect.Max.x, resize_rect.Max.y = corner_x + inner_dir.x * grip_hover_inner_size, corner_y + inner_dir.y * grip_hover_inner_size
if resize_rect.Min.x > resize_rect.Max.x then resize_rect.Min.x, resize_rect.Max.x = resize_rect.Max.x, resize_rect.Min.x end
if resize_rect.Min.y > resize_rect.Max.y then resize_rect.Min.y, resize_rect.Max.y = resize_rect.Max.y, resize_rect.Min.y end
@@ -10249,7 +10258,7 @@ local function UpdateWindowManualResize(window, resize_grip_count, resize_grip_c
axis = ImGuiAxis.Y
end
local border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, g.WindowsBorderHoverPadding)
local border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, g.WindowsBorderHoverPadding, UpdateWindowManualResize_rect)
local border_id = window:GetID(border_n + 4) -- == GetWindowResizeBorderID()
ImGui.ItemAdd(border_rect, border_id, nil, ImGuiItemFlags.NoNav)
local _, hovered, held = ImGui.ButtonBehavior(border_rect, border_id, bit32.bor(ImGuiButtonFlags.FlattenChildren, ImGuiButtonFlags.NoNavFocus))
@@ -10278,7 +10287,7 @@ local function UpdateWindowManualResize(window, resize_grip_count, resize_grip_c
-- FIXME: May want to generalize this idiom at lower-level, so more widgets can use it!
local just_scrolled_manually_while_resizing = (g.WheelingWindow ~= nil and g.WheelingWindowScrolledFrame == g.FrameCount and ImGui.IsWindowChildOf(window, g.WheelingWindow, false, true))
if g.ActiveIdIsJustActivated or just_scrolled_manually_while_resizing then
g.WindowResizeBorderExpectedRect = border_rect
g.WindowResizeBorderExpectedRect = ImRect(border_rect.Min, border_rect.Max) -- [Luau] copy: border_rect is scratch
g.WindowResizeRelativeMode = false
end
@@ -12169,13 +12178,17 @@ function ImGui.Begin(name, open, flags)
-- Add to stack
g.CurrentWindow = window
g.CurrentWindowStack:resize(g.CurrentWindowStack.Size + 1)
g.CurrentWindowStack.Data[g.CurrentWindowStack.Size] = ImGuiWindowStackData()
-- [Luau] reuse the slot's table left by pop_back() (C++ reuses the vector storage); every field is set below
local window_stack_data = g.CurrentWindowStack.Data[g.CurrentWindowStack.Size]
if type(window_stack_data) ~= "table" or window_stack_data.ParentLastItemDataBackup == nil then
window_stack_data = ImGuiWindowStackData()
g.CurrentWindowStack.Data[g.CurrentWindowStack.Size] = window_stack_data
end
window_stack_data.Window = window
ImGuiLastItemData_Copy(window_stack_data.ParentLastItemDataBackup, g.LastItemData)
window_stack_data.DisabledOverrideReenable = (bit32.band(flags, ImGuiWindowFlags.Tooltip) ~= 0) and (bit32.band(g.CurrentItemFlags, ImGuiItemFlags.Disabled) ~= 0)
window_stack_data.DisabledOverrideReenableAlphaBackup = 0.0
window_stack_data.StackSizesInBegin = ImGuiErrorRecoveryState()
window_stack_data.StackSizesInBegin = window_stack_data.StackSizesInBegin or ImGuiErrorRecoveryState() -- [Luau] reused, all fields stored
ImGui.ErrorRecoveryStoreState(window_stack_data.StackSizesInBegin)
g.StackSizesInBeginForCurrentWindow = window_stack_data.StackSizesInBegin
if bit32.band(flags, ImGuiWindowFlags.ChildMenu) ~= 0 then