--[[ Feature: Add a toggle button to the left of "Add Address Manually" + global hotkey Ctrl+Shift+K Fix: Thoroughly fixed hotkey duplicate registration issue (using pcall + forced garbage collection) v1.0.2 ]] local mainForm = getMainForm() if not mainForm then return end local refButton = mainForm.ComponentByName['btnAddAddressManually'] if not refButton then return end local parent = refButton.Parent if not parent then return end -- ★★★★★ Thoroughly clean up old hotkey (using pcall to prevent errors) ★★★★★ if toggleHotkey ~= nil then pcall(function() toggleHotkey.destroy() end) toggleHotkey = nil end -- Force garbage collection to release any lingering references collectgarbage() -- Clear old button if btnToggleHide ~= nil then btnToggleHide.destroy() btnToggleHide = nil end -- Button dimensions local BTN_WIDTH = 250 local BTN_HEIGHT_OFFSET = 13 -- Create button (initially hidden) btnToggleHide = createButton(parent) btnToggleHide.Parent = parent btnToggleHide.Width = BTN_WIDTH btnToggleHide.Height = refButton.Height + BTN_HEIGHT_OFFSET btnToggleHide.Font.Size = refButton.Font.Size btnToggleHide.ShowHint = true btnToggleHide.Visible = false -- Marker constants local MARK_HIDE = "|moH" local MARK_ALWAYS = "|moAH" -- Check if currently in "expanded" state (any markers exist) local function hasMarkers() local addrList = getAddressList() if not addrList then return false end for i = 0, addrList.Count - 1 do local rec = addrList.getMemoryRecord(i) local desc = rec.Description or "" if desc:find(MARK_HIDE) or desc:find(MARK_ALWAYS) then return true end end return false end -- Update button caption local function updateButtonCaption() if hasMarkers() then btnToggleHide.Caption = "Collapse records" btnToggleHide.Hint = "Click to restore and collapse previously hidden records" else btnToggleHide.Caption = "Show records" btnToggleHide.Hint = "Click to expand all collapsed records (group headers with hidden children)" end end -- Positioning function local function positionButton() if not btnToggleHide or not refButton then return end local refLeft = refButton.Left local refTop = refButton.Top local newLeft = refLeft - btnToggleHide.Width - 5 if newLeft < 0 then newLeft = 0 end btnToggleHide.Left = newLeft btnToggleHide.Top = refTop btnToggleHide.Visible = true end -- Delayed creation local initTimer = createTimer(nil, false) initTimer.Interval = 200 initTimer.OnTimer = function() initTimer.Enabled = false initTimer.destroy() positionButton() updateButtonCaption() btnToggleHide.BringToFront() if parent.Repaint then parent.Repaint() end end initTimer.Enabled = true -- Window resize listener local oldResize = mainForm.OnResize mainForm.OnResize = function(sender) if oldResize then oldResize(sender) end positionButton() end -- ★★★ Core toggle function (with proper option concatenation) ★★★ local function toggleHideRecords() local addrList = getAddressList() if not addrList or addrList.Count == 0 then showMessage("Address list is empty.") return end if hasMarkers() then -- Markers exist → collapse for i = 0, addrList.Count - 1 do local rec = addrList.getMemoryRecord(i) local desc = rec.Description or "" local opt = rec.Options or "" if desc:find(MARK_HIDE) then rec.Description = desc:gsub(MARK_HIDE, "") if opt ~= "" then opt = opt .. "," end rec.Options = opt .. "moHideChildren" rec.Collapsed = true end if desc:find(MARK_ALWAYS) then rec.Description = desc:gsub(MARK_ALWAYS, "") if opt ~= "" then opt = opt .. "," end rec.Options = opt .. "moAlwaysHideChildren" rec.Collapsed = true end end --showMessage("Collapsed all hidden records.") else -- No markers → expand local count = 0 for i = 0, addrList.Count - 1 do local rec = addrList.getMemoryRecord(i) local opt = rec.Options or "" local desc = rec.Description or "" local modified = false if opt:find("moHideChildren") then rec.Description = desc .. MARK_HIDE rec.Options = opt:gsub("moHideChildren", "") rec.Collapsed = false modified = true end if opt:find("moAlwaysHideChildren") then rec.Description = desc .. MARK_ALWAYS rec.Options = rec.Options:gsub("moAlwaysHideChildren", "") rec.Collapsed = false modified = true end if modified then count = count + 1 end end --showMessage(string.format("Expanded %d hidden record(s).", count)) end updateButtonCaption() end btnToggleHide.OnClick = toggleHideRecords -- ★★★ Global hotkey Ctrl+Shift+K (created after thorough cleanup) ★★★ toggleHotkey = createHotkey( function() if btnToggleHide and btnToggleHide.OnClick then btnToggleHide.OnClick() end end, { VK_CONTROL, VK_SHIFT, 0x4B } -- 0x4B = 'K' ) --print("auto_hide_toggle loaded (with hotkey Ctrl+Shift+K, old hotkey thoroughly cleaned).")