Merge branch 'replace-popen-to-async' of https://github.com/actionless/awesome

This commit is contained in:
Uli Schlachter 2016-04-30 09:11:50 +02:00
commit a1e340d118
3 changed files with 102 additions and 50 deletions

View File

@ -236,9 +236,10 @@ local function menulist_update(query, scr)
end
--- Create the menubar wibox and widgets.
local function initialize()
-- @tparam[opt] screen scr Screen.
local function initialize(scr)
instance.wibox = wibox({})
instance.widget = menubar.get()
instance.widget = menubar.get(scr)
instance.wibox.ontop = true
instance.prompt = awful.widget.prompt()
local layout = wibox.layout.fixed.horizontal()
@ -248,8 +249,12 @@ local function initialize()
end
--- Refresh menubar's cache by reloading .desktop files.
function menubar.refresh()
menubar.menu_entries = menubar.menu_gen.generate()
-- @tparam[opt] screen scr Screen.
function menubar.refresh(scr)
menubar.menu_gen.generate(function(entries)
menubar.menu_entries = entries
menulist_update(nil, scr)
end)
end
--- Awful.prompt keypressed callback to be used when the user presses a key.
@ -300,11 +305,11 @@ end
-- @param scr Screen.
function menubar.show(scr)
if not instance.wibox then
initialize()
initialize(scr)
elseif instance.wibox.visible then -- Menu already shown, exit
return
elseif not menubar.cache_entries then
menubar.refresh()
menubar.refresh(scr)
end
-- Set position and size
@ -341,9 +346,10 @@ function menubar.hide()
end
--- Get a menubar wibox.
-- @tparam[opt] screen scr Screen.
-- @return menubar wibox.
function menubar.get()
menubar.refresh()
function menubar.get(scr)
menubar.refresh(scr)
-- Add to each category the name of its key in all_categories
for k, v in pairs(menubar.menu_gen.all_categories) do
v.key = k
@ -351,7 +357,7 @@ function menubar.get()
return common_args.w
end
function menubar.mt:__call(...)
function menubar.mt.__call(_, ...)
return menubar.get(...)
end

View File

@ -83,15 +83,21 @@ local function trim(s)
end
--- Generate an array of all visible menu entries.
-- @treturn table All menu entries.
function menu_gen.generate()
-- @tparam function callback Will be fired when all menu entries were parsed
-- with the resulting list of menu entries as argument.
-- @tparam table callback.entries All menu entries.
function menu_gen.generate(callback)
-- Update icons for category entries
menu_gen.lookup_category_icons()
local result = {}
local unique_entries = {}
local dirs_parsed = 0
for _, dir in ipairs(menu_gen.all_menu_dirs) do
for _, entry in ipairs(utils.parse_dir(dir)) do
utils.parse_dir(dir, function(entries)
entries = entries or {}
for _, entry in ipairs(entries) do
-- Check whether to include program in the menu
if entry.show and entry.Name and entry.cmdline then
local unique_key = entry.Name .. '\0' .. entry.cmdline
@ -123,8 +129,12 @@ function menu_gen.generate()
end
end
end
dirs_parsed = dirs_parsed + 1
if dirs_parsed == #menu_gen.all_menu_dirs then
callback(result)
end
end)
end
return result
end
return menu_gen

View File

@ -15,8 +15,11 @@ local string = string
local screen = screen
local awful_util = require("awful.util")
local theme = require("beautiful")
local glib = require("lgi").GLib
local lgi = require("lgi")
local gio = lgi.Gio
local glib = lgi.GLib
local wibox = require("wibox")
local debug = require("gears.debug")
local utils = {}
@ -242,18 +245,51 @@ function utils.parse_desktop_file(file)
end
--- Parse a directory with .desktop files recursively.
-- @tparam string dir The directory.
-- @treturn table Paths of found .desktop files.
function utils.parse_dir(dir)
local programs = {}
local files = io.popen('find '.. dir .." -name '*.desktop' 2>/dev/null")
for file in files:lines() do
local program = utils.parse_desktop_file(file)
-- @tparam string dir_path The directory path.
-- @tparam function callback Will be fired when all the files were parsed
-- with the resulting list of menu entries as argument.
-- @tparam table callback.programs Paths of found .desktop files.
function utils.parse_dir(dir_path, callback)
local function parser(dir, programs)
local f = gio.File.new_for_path(dir)
-- Except for "NONE" there is also NOFOLLOW_SYMLINKS
local enum, err = f:async_enumerate_children("standard::name", gio.FileQueryInfoFlags.NONE)
if not enum then
debug.print_error(err)
return
end
local files_per_call = 100 -- Actual value is not that important
while true do
local list, enum_err = enum:async_next_files(files_per_call)
if enum_err then
debug.print_error(enum_err)
return
end
for _, info in ipairs(list) do
local file_type = info:get_file_type()
local file_path = enum:get_child(info):get_path()
if file_type == 'REGULAR' then
local program = utils.parse_desktop_file(file_path)
if program then
table.insert(programs, program)
end
elseif file_type == 'DIRECTORY' then
parser(file_path, programs)
end
return programs
end
if #list == 0 then
break
end
end
enum:async_close()
end
gio.Async.start(function()
local result = {}
parser(dir_path, result)
callback(result)
end)()
end
--- Compute textbox width.