Skip to content

Commit 0a4c7ad

Browse files
fixed major headache with sectors
1 parent 0979358 commit 0a4c7ad

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

TimMenu/Layout/Sector.lua

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ local Common = require("TimMenu.Common") -- EndSector might need this for DrawLi
44

55
local Sector = {}
66

7-
-- Forward declaration for helper usage if needed, though likely not for static helpers
7+
-- Spacing between consecutive sectors (larger than internal padding for visual separation)
8+
local SECTOR_SPACING = Globals.Defaults.WINDOW_CONTENT_PADDING
89

910
--[[----------------------------------------------------------------------------
1011
-- Private Helper Functions for Sector.End
@@ -89,16 +90,15 @@ local function _enqueueBorderDraw(win, sector_data, depth, persistentWidth, pers
8990
end
9091

9192
local function _finalizeCursorAndLayout(win, sector_data, width, height, pad)
92-
win.cursorX = sector_data.startX + width + pad
93-
-- Keep cursorY at the top of this sector for side-by-side placement
94-
-- The window's own lineHeight tracking will handle vertical wrapping
93+
-- Treat sector as a single widget occupying the computed width/height
94+
local horizontalSpacing = Globals.Defaults.ITEM_SPACING or 0
95+
win.cursorX = sector_data.startX + width + horizontalSpacing
9596
win.cursorY = sector_data.startY
96-
win.lineHeight = math.max(win.lineHeight or 0, height)
97+
win.lineHeight = math.max(sector_data.preLineHeight or 0, height)
9798

9899
if #win._sectorStack > 0 then
99100
local parentSector = win._sectorStack[#win._sectorStack]
100101
parentSector.maxX = math.max(parentSector.maxX, sector_data.startX + width)
101-
-- Use actual bottom of child sector, not reset cursor position
102102
parentSector.maxY = math.max(parentSector.maxY, sector_data.startY + height)
103103
end
104104
end
@@ -113,10 +113,10 @@ function Sector.Begin(win, label)
113113
-- persistent storage for sector sizes
114114
win._sectorSizes = win._sectorSizes or {}
115115

116-
-- Increase padding inside sectors by 5 pixels vertically for better spacing
116+
-- Internal padding for content inside sector borders
117117
local pad = Globals.Defaults.WINDOW_CONTENT_PADDING
118-
-- capture current cursor as sector origin (shifted down by pad)
119-
local startX, startY = win.cursorX, win.cursorY + pad
118+
-- Sector starts at current cursor position
119+
local startX, startY = win.cursorX, win.cursorY
120120

121121
-- Start with current cursor position as initial extents (no stored sizes)
122122
local sector_data = {
@@ -127,9 +127,13 @@ function Sector.Begin(win, label)
127127
label = label,
128128
padding = pad,
129129
origAdd = win.AddWidget, -- Store original AddWidget
130+
preLineHeight = win.lineHeight,
130131
}
131132
table.insert(win._sectorStack, sector_data)
132133

134+
-- Reset lineHeight for clean sector start
135+
win.lineHeight = 0
136+
133137
-- override AddWidget & NextLine to track extents within this sector
134138
sector_data.origNext = win.NextLine -- Store original NextLine
135139
win.AddWidget = function(self, w, h)
@@ -141,16 +145,12 @@ function Sector.Begin(win, label)
141145
end
142146

143147
win.NextLine = function(self, spacing)
144-
-- Directly update cursor within sector boundaries without calling origNext
145-
local extra = 5 -- add 5px more between lines inside sector
148+
-- Advance to next line within sector
146149
local baseSpacing = spacing or Globals.Defaults.WINDOW_CONTENT_PADDING
147-
-- Update cursorY manually to avoid origNext resetting cursorX to window padding
148-
self.cursorY = self.cursorY + self.lineHeight + baseSpacing + extra
149-
-- Keep cursorX constrained to sector's indented start position
150+
self.cursorY = self.cursorY + self.lineHeight + baseSpacing
151+
-- Keep cursor aligned to sector's left edge
150152
self.cursorX = sector_data.startX + sector_data.padding
151-
-- Don't update maxY here - let AddWidget expand bounds only when widgets are actually added
152-
-- This prevents empty space when conditionals hide content after NextLine
153-
-- Reset lineHeight for the new line (matches Window:NextLine behavior)
153+
-- Reset lineHeight for new line
154154
self.lineHeight = 0
155155
end
156156

test_sector_fix.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--[[ Test script to demonstrate the sector positioning fix ]]
2+
3+
local TimMenu = require("TimMenu")
4+
5+
local function TestSectorFix()
6+
if not TimMenu.Begin("Sector Position Test") then
7+
return
8+
end
9+
10+
-- Test 1: Two sectors side by side (this was broken before the fix)
11+
TimMenu.BeginSector("Left Sector")
12+
TimMenu.Checkbox("Option 1", false)
13+
TimMenu.Checkbox("Option 2", true)
14+
TimMenu.EndSector(true) -- true = stay on same line
15+
16+
TimMenu.BeginSector("Right Sector")
17+
TimMenu.Checkbox("Setting 1", false)
18+
TimMenu.Checkbox("Setting 2", true)
19+
TimMenu.EndSector() -- false = move to next line (default)
20+
21+
-- Test 2: Three sectors in one row
22+
TimMenu.BeginSector("First")
23+
TimMenu.Checkbox("A", false)
24+
TimMenu.EndSector(true)
25+
26+
TimMenu.BeginSector("Second")
27+
TimMenu.Checkbox("B", true)
28+
TimMenu.EndSector(true)
29+
30+
TimMenu.BeginSector("Third")
31+
TimMenu.Checkbox("C", false)
32+
TimMenu.EndSector() -- Move to next line after third
33+
34+
-- Test 3: Backwards compatibility - sectors without parameter work as before
35+
TimMenu.BeginSector("Standalone")
36+
TimMenu.Checkbox("Solo Option", false)
37+
TimMenu.EndSector() -- No parameter = old behavior (move to next line)
38+
39+
TimMenu.End()
40+
end
41+
42+
-- Register the test
43+
callbacks.Register("Draw", "sector_test", TestSectorFix)
44+
45+
print("Sector positioning fix test loaded!")
46+
print("Use TimMenu.EndSector(true) to keep sectors on same line")
47+
print("Use TimMenu.EndSector() or TimMenu.EndSector(false) to move to next line")

0 commit comments

Comments
 (0)