Ported menubar to lua 5.2

Tested with lua 5.1: all good

Signed-off-by: Arvydas Sidorenko <asido4@gmail.com>
This commit is contained in:
Arvydas Sidorenko 2012-06-12 10:36:28 +02:00 committed by Uli Schlachter
parent e789cfaf66
commit a75112160f
4 changed files with 46 additions and 35 deletions

View File

@ -8,7 +8,7 @@ require("wibox")
local beautiful = require("beautiful") local beautiful = require("beautiful")
-- Notification library -- Notification library
local naughty = require("naughty") local naughty = require("naughty")
require("menubar") local menubar = require("menubar")
-- {{{ Error handling -- {{{ Error handling
-- Check if awesome encountered an error during startup and fell back to -- Check if awesome encountered an error during startup and fell back to

View File

@ -13,8 +13,6 @@ local capi = {
local awful = require("awful") local awful = require("awful")
local common = require("awful.widget.common") local common = require("awful.widget.common")
local theme = require("beautiful") local theme = require("beautiful")
local menu_gen = require("menubar.menu_gen")
local menu_utils = require("menubar.utils")
local wibox = require("wibox") local wibox = require("wibox")
-- Standard lua -- Standard lua
@ -26,7 +24,10 @@ local string = string
local math = math local math = math
local setmetatable = setmetatable local setmetatable = setmetatable
module("menubar") -- menubar
local menubar = { mt = {} }
menubar.menu_gen = require("menubar.menu_gen")
menubar.utils = require("menubar.utils")
--- List of menubar keybindings: --- List of menubar keybindings:
-- <p><ul> -- <p><ul>
@ -49,19 +50,19 @@ module("menubar")
-- When true the .desktop files will be reparsed only when the -- When true the .desktop files will be reparsed only when the
-- extension is initialized. Use this if menubar takes much time to -- extension is initialized. Use this if menubar takes much time to
-- open. -- open.
cache_entries = true local cache_entries = true
-- When true the categories will be shown alongside application -- When true the categories will be shown alongside application
-- entries. -- entries.
show_categories = true local show_categories = true
-- Specifies the geometry of the menubar. This is a table with the keys -- Specifies the geometry of the menubar. This is a table with the keys
-- x, y, width and height. Missing values are replaced via the screen's -- x, y, width and height. Missing values are replaced via the screen's
-- geometry. However, missing height is replaced by the font size. -- geometry. However, missing height is replaced by the font size.
geometry = { width = nil, local geometry = { width = nil,
height = nil, height = nil,
x = nil, x = nil,
y = nil } y = nil }
-- Private section -- Private section
local current_item = 1 local current_item = 1
@ -202,7 +203,7 @@ local function initialize()
end end
--- Refresh menubar's cache by reloading .desktop files. --- Refresh menubar's cache by reloading .desktop files.
function refresh() function menubar.refresh()
menu_entries = menu_gen.generate() menu_entries = menu_gen.generate()
end end
@ -241,7 +242,7 @@ local function prompt_keypressed_callback(mod, key, comm)
current_item = #shownitems current_item = #shownitems
if mod.Mod1 then if mod.Mod1 then
-- add a terminal to the cmdline -- add a terminal to the cmdline
shownitems[current_item].cmdline = menu_utils.terminal shownitems[current_item].cmdline = menubar.utils.terminal
.. " -e " .. shownitems[current_item].cmdline .. " -e " .. shownitems[current_item].cmdline
end end
end end
@ -252,13 +253,13 @@ end
--- Show the menubar on the given screen. --- Show the menubar on the given screen.
-- @param scr Screen number. -- @param scr Screen number.
function show(scr) function menubar.show(scr)
if not instance.wibox then if not instance.wibox then
initialize() initialize()
elseif instance.wibox.visible then -- Menu already shown, exit elseif instance.wibox.visible then -- Menu already shown, exit
return return
elseif not cache_entries then elseif not cache_entries then
refresh() menubar.refresh()
end end
-- Set position and size -- Set position and size
@ -276,23 +277,23 @@ function show(scr)
function(s) end, -- exe_callback function set to do nothing function(s) end, -- exe_callback function set to do nothing
awful.completion.shell, -- completion_callback awful.completion.shell, -- completion_callback
awful.util.getdir("cache") .. "/history_menu", awful.util.getdir("cache") .. "/history_menu",
nil, hide, menulist_update, prompt_keypressed_callback nil, menubar.hide, menulist_update, prompt_keypressed_callback
) )
instance.wibox.visible = true instance.wibox.visible = true
end end
--- Hide the menubar. --- Hide the menubar.
function hide() function menubar.hide()
instance.wibox.visible = false instance.wibox.visible = false
end end
--- Get a menubar wibox. --- Get a menubar wibox.
-- @return menubar wibox. -- @return menubar wibox.
function get() function menubar.get()
if app_folders then if app_folders then
menu_gen.all_menu_dirs = app_folders menu_gen.all_menu_dirs = app_folders
end end
refresh() menubar.refresh()
-- Add to each category the name of its key in all_categories -- Add to each category the name of its key in all_categories
for k, v in pairs(menu_gen.all_categories) do for k, v in pairs(menu_gen.all_categories) do
v.key = k v.key = k
@ -300,6 +301,10 @@ function get()
return common_args.w return common_args.w
end end
setmetatable(_M, { __call = function(_, ...) return get(...) end }) function menubar.mt:__call(...)
return menubar.get(...)
end
return setmetatable(menubar, menubar.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -12,18 +12,19 @@ local string = string
local table = table local table = table
-- Menu generation module for menubar -- Menu generation module for menubar
module("menubar.menu_gen") -- menubar.menu_gen
local menu_gen = {}
-- Options section -- Options section
-- Specifies all directories where menubar should look for .desktop -- Specifies all directories where menubar should look for .desktop
-- files. The search is not recursive. -- files. The search is not recursive.
all_menu_dirs = { '/usr/share/applications/' } menu_gen.all_menu_dirs = { '/usr/share/applications/' }
-- Specify the mapping of .desktop Categories section to the -- Specify the mapping of .desktop Categories section to the
-- categories in the menubar. If "use" flag is set to false then any of -- categories in the menubar. If "use" flag is set to false then any of
-- the applications that fall only to this category will not be shown. -- the applications that fall only to this category will not be shown.
all_categories = { menu_gen.all_categories = {
multimedia = { app_type = "AudioVideo", name = "Multimedia", multimedia = { app_type = "AudioVideo", name = "Multimedia",
icon_name = "applications-multimedia.png", use = true }, icon_name = "applications-multimedia.png", use = true },
development = { app_type = "Development", name = "Development", development = { app_type = "Development", name = "Development",
@ -47,8 +48,8 @@ all_categories = {
} }
--- Find icons for category entries. --- Find icons for category entries.
function lookup_category_icons() function menu_gen.lookup_category_icons()
for _, v in pairs(all_categories) do for _, v in pairs(menu_gen.all_categories) do
v.icon = utils.lookup_icon(v.icon_name) v.icon = utils.lookup_icon(v.icon_name)
end end
end end
@ -57,7 +58,7 @@ end
-- @param app_type Application category as written in .desktop file. -- @param app_type Application category as written in .desktop file.
-- @return category key name in all_categories, whether the category is used -- @return category key name in all_categories, whether the category is used
local function get_category_name_and_usage_by_type(app_type) local function get_category_name_and_usage_by_type(app_type)
for k, v in pairs(all_categories) do for k, v in pairs(menu_gen.all_categories) do
if app_type == v.app_type then if app_type == v.app_type then
return k, v.use return k, v.use
end end
@ -76,13 +77,13 @@ end
--- Generate an array of all visible menu entries. --- Generate an array of all visible menu entries.
-- @return all menu entries. -- @return all menu entries.
function generate() function menu_gen.generate()
-- Update icons for category entries -- Update icons for category entries
lookup_category_icons() lookup_category_icons()
local result = {} local result = {}
for _, dir in ipairs(all_menu_dirs) do for _, dir in ipairs(menu_gen.all_menu_dirs) do
local entries = utils.parse_dir(dir) local entries = utils.parse_dir(dir)
for _, program in ipairs(entries) do for _, program in ipairs(entries) do
-- Check whether to include program in the menu -- Check whether to include program in the menu
@ -116,4 +117,6 @@ function generate()
return result return result
end end
return menu_gen
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -13,7 +13,8 @@ local awful_util = require("awful.util")
local theme = require("beautiful") local theme = require("beautiful")
-- Utility module for menubar -- Utility module for menubar
module("menubar.utils") -- menubar.utils
local utils = {}
-- NOTE: This icons/desktop files module was written according to the -- NOTE: This icons/desktop files module was written according to the
-- following freedesktop.org specifications: -- following freedesktop.org specifications:
@ -23,14 +24,14 @@ module("menubar.utils")
-- Options section -- Options section
-- Terminal which applications that need terminal would open in. -- Terminal which applications that need terminal would open in.
terminal = 'xterm' utils.terminal = 'xterm'
-- The default icon for applications that don't provide any icon in -- The default icon for applications that don't provide any icon in
-- their .desktop files. -- their .desktop files.
default_icon = nil local default_icon = nil
-- Name of the WM for the OnlyShownIn entry in the .desktop file. -- Name of the WM for the OnlyShownIn entry in the .desktop file.
wm_name = "awesome" local wm_name = "awesome"
-- Private section -- Private section
@ -66,7 +67,7 @@ end
--- Lookup an icon in different folders of the filesystem. --- Lookup an icon in different folders of the filesystem.
-- @param icon_file Short or full name of the icon. -- @param icon_file Short or full name of the icon.
-- @return full name of the icon. -- @return full name of the icon.
function lookup_icon(icon_file) function utils.lookup_icon(icon_file)
if not icon_file or icon_file == "" then if not icon_file or icon_file == "" then
return default_icon return default_icon
end end
@ -121,7 +122,7 @@ end
--- Parse a .desktop file. --- Parse a .desktop file.
-- @param file The .desktop file. -- @param file The .desktop file.
-- @return A table with file entries. -- @return A table with file entries.
function parse(file) function utils.parse(file)
local program = { show = true, file = file } local program = { show = true, file = file }
for line in io.lines(file) do for line in io.lines(file) do
for key, value in line:gmatch("(%w+)=(.+)") do for key, value in line:gmatch("(%w+)=(.+)") do
@ -166,7 +167,7 @@ function parse(file)
cmdline = cmdline:gsub('%%i', '') cmdline = cmdline:gsub('%%i', '')
end end
if program.Terminal == "true" then if program.Terminal == "true" then
cmdline = terminal .. ' -e ' .. cmdline cmdline = utils.terminal .. ' -e ' .. cmdline
end end
program.cmdline = cmdline program.cmdline = cmdline
end end
@ -178,7 +179,7 @@ end
-- @param dir The directory. -- @param dir The directory.
-- @param icons_size, The icons sizes, optional. -- @param icons_size, The icons sizes, optional.
-- @return A table with all .desktop entries. -- @return A table with all .desktop entries.
function parse_dir(dir) function utils.parse_dir(dir)
local programs = {} local programs = {}
local files = io.popen('find '.. dir ..' -maxdepth 1 -name "*.desktop"') local files = io.popen('find '.. dir ..' -maxdepth 1 -name "*.desktop"')
for file in files:lines() do for file in files:lines() do
@ -187,4 +188,6 @@ function parse_dir(dir)
return programs return programs
end end
return utils
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80