Move Async up into menu_gen.generate to keep order of files

This commit is contained in:
Michael Leuchtenburg 2023-06-13 18:01:34 -04:00
parent 0f8c00b758
commit ab21d2455a
2 changed files with 73 additions and 73 deletions

View File

@ -14,6 +14,9 @@ local pairs = pairs
local ipairs = ipairs local ipairs = ipairs
local table = table local table = table
local gdebug = require("gears.debug") local gdebug = require("gears.debug")
local lgi = require("lgi")
local gio = lgi.Gio
local protected_call = require("gears.protected_call")
local menu_gen = {} local menu_gen = {}
@ -22,7 +25,6 @@ local menu_gen = {}
--- Get the path to the directories where XDG menu applications are installed. --- Get the path to the directories where XDG menu applications are installed.
local function get_xdg_menu_dirs() local function get_xdg_menu_dirs()
local dirs = gfilesystem.get_xdg_data_dirs() local dirs = gfilesystem.get_xdg_data_dirs()
gdebug.dump(gfilesystem.get_xdg_data_home(), "xdg_data_home")
table.insert(dirs, 1, gfilesystem.get_xdg_data_home()) table.insert(dirs, 1, gfilesystem.get_xdg_data_home())
return gtable.map(function(dir) return dir .. 'applications/' end, dirs) return gtable.map(function(dir) return dir .. 'applications/' end, dirs)
end end
@ -77,6 +79,28 @@ local function get_category_name_and_usage_by_type(app_type)
end end
end end
local do_protected_call, call_callback
do
-- Lua 5.1 cannot yield across a protected call. Instead of hardcoding a
-- check, we check for this problem: The following coroutine yields true on
-- success (so resume() returns true, true). On failure, pcall returns
-- false and a message, so resume() returns true, false, message.
local _, has_yieldable_pcall = coroutine.resume(coroutine.create(function()
return pcall(coroutine.yield, true)
end))
if has_yieldable_pcall then
do_protected_call = protected_call.call
call_callback = function(callback, ...)
return callback(...)
end
else
do_protected_call = function(f, ...)
return f(...)
end
call_callback = protected_call.call
end
end
--- Generate an array of all visible menu entries. --- Generate an array of all visible menu entries.
-- @tparam function callback Will be fired when all menu entries were parsed -- @tparam function callback Will be fired when all menu entries were parsed
-- with the resulting list of menu entries as argument. -- with the resulting list of menu entries as argument.
@ -91,6 +115,7 @@ function menu_gen.generate(callback)
local unique_entries = {} local unique_entries = {}
local dirs_parsed = 0 local dirs_parsed = 0
gio.Async.start(do_protected_call)(function()
for _, dir in ipairs(menu_gen.all_menu_dirs) do for _, dir in ipairs(menu_gen.all_menu_dirs) do
utils.parse_dir(dir, function(entries) utils.parse_dir(dir, function(entries)
entries = entries or {} entries = entries or {}
@ -105,8 +130,7 @@ function menu_gen.generate(callback)
-- of the first category it finds. -- of the first category it finds.
if entry.categories then if entry.categories then
for _, category in pairs(entry.categories) do for _, category in pairs(entry.categories) do
local cat_key, cat_use = local cat_key, cat_use = get_category_name_and_usage_by_type(category)
get_category_name_and_usage_by_type(category)
if cat_key and cat_use then if cat_key and cat_use then
target_category = cat_key target_category = cat_key
break break
@ -117,10 +141,10 @@ function menu_gen.generate(callback)
local name = utils.rtrim(entry.Name) or "" local name = utils.rtrim(entry.Name) or ""
local cmdline = utils.rtrim(entry.cmdline) or "" local cmdline = utils.rtrim(entry.cmdline) or ""
local icon = entry.icon_path or nil local icon = entry.icon_path or nil
table.insert(result, { name = name, table.insert(
cmdline = cmdline, result,
icon = icon, { name = name, cmdline = cmdline, icon = icon, category = target_category }
category = target_category }) )
unique_entries[unique_key] = true unique_entries[unique_key] = true
else else
gdebug.dump(entry, "entry not included") gdebug.dump(entry, "entry not included")
@ -129,10 +153,11 @@ function menu_gen.generate(callback)
end end
dirs_parsed = dirs_parsed + 1 dirs_parsed = dirs_parsed + 1
if dirs_parsed == #menu_gen.all_menu_dirs then if dirs_parsed == #menu_gen.all_menu_dirs then
callback(result) call_callback(callback, result)
end end
end) end)
end end
end)
end end
return menu_gen return menu_gen

View File

@ -18,7 +18,6 @@ local gio = lgi.Gio
local glib = lgi.GLib local glib = lgi.GLib
local w_textbox = require("wibox.widget.textbox") local w_textbox = require("wibox.widget.textbox")
local gdebug = require("gears.debug") local gdebug = require("gears.debug")
local protected_call = require("gears.protected_call")
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1) local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
local utils = {} local utils = {}
@ -92,28 +91,6 @@ end
-- Private section -- Private section
local do_protected_call, call_callback
do
-- Lua 5.1 cannot yield across a protected call. Instead of hardcoding a
-- check, we check for this problem: The following coroutine yields true on
-- success (so resume() returns true, true). On failure, pcall returns
-- false and a message, so resume() returns true, false, message.
local _, has_yieldable_pcall = coroutine.resume(coroutine.create(function()
return pcall(coroutine.yield, true)
end))
if has_yieldable_pcall then
do_protected_call = protected_call.call
call_callback = function(callback, ...)
return callback(...)
end
else
do_protected_call = function(f, ...)
return f(...)
end
call_callback = protected_call.call
end
end
local all_icon_sizes = { local all_icon_sizes = {
'scalable', 'scalable',
'128x128', '128x128',
@ -404,7 +381,6 @@ function utils.parse_dir(dir_path, callback)
enum:async_close() enum:async_close()
end end
gio.Async.start(do_protected_call)(function()
local result = {} local result = {}
local f = gio.File.new_for_path(dir_path) local f = gio.File.new_for_path(dir_path)
parser(f, result) parser(f, result)
@ -414,8 +390,7 @@ function utils.parse_dir(dir_path, callback)
entry.desktop_file_id = string.gsub(relative_path, "/", "-") entry.desktop_file_id = string.gsub(relative_path, "/", "-")
result[i] = entry result[i] = entry
end end
call_callback(callback, result) callback(result)
end)
end end
-- luacov: disable -- luacov: disable