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,48 +115,49 @@ function menu_gen.generate(callback)
local unique_entries = {} local unique_entries = {}
local dirs_parsed = 0 local dirs_parsed = 0
for _, dir in ipairs(menu_gen.all_menu_dirs) do gio.Async.start(do_protected_call)(function()
utils.parse_dir(dir, function(entries) for _, dir in ipairs(menu_gen.all_menu_dirs) do
entries = entries or {} utils.parse_dir(dir, function(entries)
for _, entry in ipairs(entries) do entries = entries or {}
-- Check whether to include program in the menu for _, entry in ipairs(entries) do
if entry.show and entry.Name and entry.cmdline then -- Check whether to include program in the menu
local unique_key = entry.desktop_file_id if entry.show and entry.Name and entry.cmdline then
if not unique_entries[unique_key] then local unique_key = entry.desktop_file_id
local target_category = nil if not unique_entries[unique_key] then
-- Check if the program falls into at least one of the local target_category = nil
-- usable categories. Set target_category to be the id -- Check if the program falls into at least one of the
-- of the first category it finds. -- usable categories. Set target_category to be the id
if entry.categories then -- of the first category it finds.
for _, category in pairs(entry.categories) do if entry.categories then
local cat_key, cat_use = for _, category in pairs(entry.categories) do
get_category_name_and_usage_by_type(category) local cat_key, cat_use = 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
end
end end
end end
end
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")
end
end end
end 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 call_callback(callback, result)
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,18 +381,16 @@ 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) for i, entry in ipairs(result) do
for i, entry in ipairs(result) do local target = gio.File.new_for_path(entry.file)
local target = gio.File.new_for_path(entry.file) local relative_path = f:get_relative_path(target)
local relative_path = f:get_relative_path(target) entry.desktop_file_id = string.gsub(relative_path, "/", "-")
entry.desktop_file_id = string.gsub(relative_path, "/", "-") result[i] = entry
result[i] = entry end
end callback(result)
call_callback(callback, result)
end)
end end
-- luacov: disable -- luacov: disable