Skip to content

Commit

Permalink
feat(ui): press Home/End key to select first/last mount in list
Browse files Browse the repository at this point in the history
  • Loading branch information
exochron committed Nov 5, 2023
1 parent e28b26e commit f0f7e77
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions UI/Hotkeys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local _, ADDON = ...
-- Keyboard Shortcuts:
-- UP: Select Previous Mount
-- DOWN: Select Next Mount
-- HOME: Select first Mount
-- END: Select last Mount

local function FetchCurrentSelectedIndex()
local target = MountJournal.selectedMountID
Expand All @@ -13,24 +15,27 @@ local function FetchCurrentSelectedIndex()
end
end

local function JumpTo(index)
MountJournal.ScrollBox:ScrollToElementDataIndex(index)
local frame = MountJournal.ScrollBox:FindFrame(MountJournal.ScrollBox:Find(index))
if frame then
frame:Click()
end
end

local function Select(step, totalDisplayed)
local currentIndex = FetchCurrentSelectedIndex()
local index
if currentIndex == nil then
index = 1
else
index = currentIndex + step
if index < 1 then
index = 1
elseif index > totalDisplayed then
index = totalDisplayed
end
return JumpTo(1)
end

MountJournal.ScrollBox:ScrollToElementDataIndex(index)
local frame = MountJournal.ScrollBox:FindFrame(MountJournal.ScrollBox:Find(index))
if frame then
frame:Click()
local index = currentIndex + step
if index < 1 then
JumpTo(1)
elseif index > totalDisplayed then
JumpTo(totalDisplayed)
else
JumpTo(index)
end
end

Expand All @@ -44,14 +49,18 @@ ADDON.Events:RegisterCallback("loadUI", function()
scrollFrame:EnableKeyboard(true)
scrollFrame:HookScript("OnKeyDown", function(self, key)
local totalDisplayed
if (key == "DOWN" or key == "UP") and ADDON.settings.ui.enableCursorKeys and not IsModifierKeyDown() then
if (key == "DOWN" or key == "UP" or key == "HOME" or key == "END") and ADDON.settings.ui.enableCursorKeys and not IsModifierKeyDown() then
totalDisplayed = ADDON.Api:GetDataProvider():GetSize()
if totalDisplayed > 0 then
local step = 1
if key == "UP" then
step = -1
if key == "END" then
JumpTo(totalDisplayed)
elseif key == "HOME" then
JumpTo(1)
elseif key == "UP" then
Select(-1, totalDisplayed)
elseif key == "DOWN" then
Select(1, totalDisplayed)
end
Select(step, totalDisplayed)

self:SetPropagateKeyboardInput(false)
return
Expand Down

0 comments on commit f0f7e77

Please sign in to comment.