Skip to content

Commit

Permalink
Cache menu items to make the dialog snappier (at the cost of possibly…
Browse files Browse the repository at this point in the history
… having stale menu items)
  • Loading branch information
Brian Hartin committed Aug 31, 2017
1 parent 6b6c98a commit 000f4b7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions menupenny.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
menuItemCache = {}

function concatTables(t1, t2)
for k,v in pairs(t2) do
table.insert(t1, v)
Expand Down Expand Up @@ -79,7 +81,6 @@ end

function init()
local current = hs.application.frontmostApplication()

local chooser = hs.chooser.new(function(chosen)
current:activate()
if chosen ~= nil then
Expand All @@ -90,10 +91,14 @@ function init()
chooser:searchSubText(false)
chooser:show()

local menu = current:getMenuItems(current)
local menuItemPaths = menuItemsToPaths(menu, nil)

chooser:queryChangedCallback(function(query)
local menuItemPaths
local title = current:title()
if menuItemCache[title] == nil then
local menuItems = current:getMenuItems(current)
menuItemCache[title] = menuItemsToPaths(menuItems, nil)
end
menuItemPaths = menuItemCache[title]
chooser:choices(menuItemsToChoices(searchMenuItems(menuItemPaths, query)))
end)
end
Expand Down

0 comments on commit 000f4b7

Please sign in to comment.