Skip to content

Commit 2be02bc

Browse files
committed
Resize columns with scroll wheel and middle click when hovering column header
1 parent 6c7bbf3 commit 2be02bc

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

src/Export/Classes/RowListControl.lua

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local t_insert = table.insert
99
local RowListClass = newClass("RowListControl", "ListControl", function(self, anchor, rect)
1010
self.ListControl(anchor, rect, 14, "HORIZONTAL", false, { })
1111
self.colLabels = true
12+
self._autoSizeToggleState = {} -- internal toggle memory, not saved to spec
1213
end)
1314

1415
function RowListClass:BuildRows(filter)
@@ -48,6 +49,7 @@ function RowListClass:BuildColumns()
4849
for _, specCol in ipairs(main.curDatFile.spec) do
4950
t_insert(self.colList, {
5051
width = specCol.width,
52+
specColRef = specCol, -- Link to the original data
5153
label = specCol.name,
5254
font = function() return IsKeyDown("CTRL") and "FIXED" or "VAR" end,
5355
sortable = true
@@ -236,3 +238,113 @@ function RowListClass:ReSort(colIndex)
236238
self.lastSortedCol = colIndex
237239
self.sortAsc = asc
238240
end
241+
242+
function RowListClass:OnKeyUp(key, doubleClick)
243+
if not self:IsShown() or not self:IsEnabled() then
244+
return
245+
end
246+
247+
local function isScrollKey(k)
248+
if k == "WHEELUP" then return true, -1, 10 end
249+
if k == "WHEELDOWN" then return true, 1, -10 end
250+
return false, 0, 0
251+
end
252+
253+
local mOverControl = self:GetMouseOverControl()
254+
if mOverControl and mOverControl.OnKeyDown then
255+
return mOverControl:OnKeyDown(key)
256+
end
257+
258+
if not self:IsMouseOver() and key:match("BUTTON") then
259+
return
260+
end
261+
262+
-- Get cursor info
263+
local x, y = self:GetPos()
264+
local cursorX, cursorY = GetCursorPos()
265+
local scrollOffsetH = self.controls.scrollBarH and self.controls.scrollBarH.offset or 0
266+
local relX = cursorX - (x + 2)
267+
local relY = cursorY - (y + 2)
268+
local adjustedRelX = relX + scrollOffsetH
269+
270+
-- Middle-click resets column width
271+
if key == "MIDDLEBUTTON" then
272+
for colIndex, column in ipairs(self.colList) do
273+
local colOffset = column._offset
274+
local colWidth = column.width or column._width
275+
if colOffset and colWidth then
276+
local mOver = adjustedRelX >= colOffset and adjustedRelX <= colOffset + colWidth and relY >= 0 and relY <= 18
277+
if mOver then
278+
-- Initialize if not present
279+
self._autoSizeToggleState[colIndex] = not self._autoSizeToggleState[colIndex]
280+
281+
local newWidth
282+
if self._autoSizeToggleState[colIndex] then
283+
-- First toggle: size to contents
284+
local maxWidth = 0
285+
for _, rowIndex in ipairs(self.list) do
286+
local val = self:GetRowValue(colIndex, nil, rowIndex)
287+
if val ~= nil then
288+
local width = DrawStringWidth(self.rowHeight, "FIXED", tostring(val))
289+
maxWidth = math.max(maxWidth, width)
290+
end
291+
end
292+
local labelWidth = DrawStringWidth(self.rowHeight, "FIXED", tostring(column.label or ""))
293+
newWidth = math.max(40, math.max(maxWidth, labelWidth) + 10)
294+
else
295+
-- Second toggle: reset to label or 150, whichever is greater
296+
local labelWidth = DrawStringWidth(self.rowHeight, "FIXED", tostring(column.label or ""))
297+
newWidth = math.max(150, labelWidth + 10)
298+
end
299+
300+
column.width = newWidth
301+
302+
if column.specColRef then
303+
column.specColRef.width = newWidth
304+
main.curSpecCol = column.specColRef
305+
main.controls.colWidth:SetText(newWidth)
306+
end
307+
308+
self:BuildColumns()
309+
return self
310+
end
311+
end
312+
end
313+
return self
314+
end
315+
-- Scroll behavior
316+
local isScroll, scrollStep, colDelta = isScrollKey(key)
317+
if isScroll then
318+
local overColumnHeader = false
319+
for _, column in ipairs(self.colList) do
320+
local colOffset = column._offset
321+
local colWidth = column.width or column._width
322+
if colOffset and colWidth then
323+
local mOver = adjustedRelX >= colOffset and adjustedRelX <= colOffset + colWidth and relY >= 0 and relY <= 18
324+
if mOver then
325+
-- Widen column if hovering over it
326+
overColumnHeader = true
327+
local newWidth = math.max(40, colWidth + colDelta)
328+
column.width = newWidth
329+
if column.specColRef then
330+
column.specColRef.width = newWidth
331+
main.curSpecCol = column.specColRef
332+
main.controls.colWidth:SetText(newWidth)
333+
end
334+
self:BuildColumns()
335+
break
336+
end
337+
end
338+
end
339+
-- Scroll vertically or horizontally if not resizing column
340+
if not overColumnHeader then
341+
if self.scroll and self.scrollH and IsKeyDown("SHIFT") then
342+
self.controls.scrollBarH:Scroll(scrollStep)
343+
else
344+
self.controls.scrollBarV:Scroll(scrollStep)
345+
end
346+
end
347+
return self
348+
end
349+
return self
350+
end

0 commit comments

Comments
 (0)