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")
-- Notification library
local naughty = require("naughty")
require("menubar")
local menubar = require("menubar")
-- {{{ Error handling
-- 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 common = require("awful.widget.common")
local theme = require("beautiful")
local menu_gen = require("menubar.menu_gen")
local menu_utils = require("menubar.utils")
local wibox = require("wibox")
-- Standard lua
@ -26,7 +24,10 @@ local string = string
local math = math
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:
-- <p><ul>
@ -49,19 +50,19 @@ module("menubar")
-- When true the .desktop files will be reparsed only when the
-- extension is initialized. Use this if menubar takes much time to
-- open.
cache_entries = true
local cache_entries = true
-- When true the categories will be shown alongside application
-- entries.
show_categories = true
local show_categories = true
-- 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
-- geometry. However, missing height is replaced by the font size.
geometry = { width = nil,
height = nil,
x = nil,
y = nil }
local geometry = { width = nil,
height = nil,
x = nil,
y = nil }
-- Private section
local current_item = 1
@ -202,7 +203,7 @@ local function initialize()
end
--- Refresh menubar's cache by reloading .desktop files.
function refresh()
function menubar.refresh()
menu_entries = menu_gen.generate()
end
@ -241,7 +242,7 @@ local function prompt_keypressed_callback(mod, key, comm)
current_item = #shownitems
if mod.Mod1 then
-- 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
end
end
@ -252,13 +253,13 @@ end
--- Show the menubar on the given screen.
-- @param scr Screen number.
function show(scr)
function menubar.show(scr)
if not instance.wibox then
initialize()
elseif instance.wibox.visible then -- Menu already shown, exit
return
elseif not cache_entries then
refresh()
menubar.refresh()
end
-- Set position and size
@ -276,23 +277,23 @@ function show(scr)
function(s) end, -- exe_callback function set to do nothing
awful.completion.shell, -- completion_callback
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
end
--- Hide the menubar.
function hide()
function menubar.hide()
instance.wibox.visible = false
end
--- Get a menubar wibox.
-- @return menubar wibox.
function get()
function menubar.get()
if app_folders then
menu_gen.all_menu_dirs = app_folders
end
refresh()
menubar.refresh()
-- Add to each category the name of its key in all_categories
for k, v in pairs(menu_gen.all_categories) do
v.key = k
@ -300,6 +301,10 @@ function get()
return common_args.w
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

View File

@ -12,18 +12,19 @@ local string = string
local table = table
-- Menu generation module for menubar
module("menubar.menu_gen")
-- menubar.menu_gen
local menu_gen = {}
-- Options section
-- Specifies all directories where menubar should look for .desktop
-- 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
-- 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.
all_categories = {
menu_gen.all_categories = {
multimedia = { app_type = "AudioVideo", name = "Multimedia",
icon_name = "applications-multimedia.png", use = true },
development = { app_type = "Development", name = "Development",
@ -47,8 +48,8 @@ all_categories = {
}
--- Find icons for category entries.
function lookup_category_icons()
for _, v in pairs(all_categories) do
function menu_gen.lookup_category_icons()
for _, v in pairs(menu_gen.all_categories) do
v.icon = utils.lookup_icon(v.icon_name)
end
end
@ -57,7 +58,7 @@ end
-- @param app_type Application category as written in .desktop file.
-- @return category key name in all_categories, whether the category is used
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
return k, v.use
end
@ -76,13 +77,13 @@ end
--- Generate an array of all visible menu entries.
-- @return all menu entries.
function generate()
function menu_gen.generate()
-- Update icons for category entries
lookup_category_icons()
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)
for _, program in ipairs(entries) do
-- Check whether to include program in the menu
@ -116,4 +117,6 @@ function generate()
return result
end
return menu_gen
-- 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")
-- Utility module for menubar
module("menubar.utils")
-- menubar.utils
local utils = {}
-- NOTE: This icons/desktop files module was written according to the
-- following freedesktop.org specifications:
@ -23,14 +24,14 @@ module("menubar.utils")
-- Options section
-- 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
-- their .desktop files.
default_icon = nil
local default_icon = nil
-- Name of the WM for the OnlyShownIn entry in the .desktop file.
wm_name = "awesome"
local wm_name = "awesome"
-- Private section
@ -66,7 +67,7 @@ end
--- Lookup an icon in different folders of the filesystem.
-- @param icon_file Short or 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
return default_icon
end
@ -121,7 +122,7 @@ end
--- Parse a .desktop file.
-- @param file The .desktop file.
-- @return A table with file entries.
function parse(file)
function utils.parse(file)
local program = { show = true, file = file }
for line in io.lines(file) do
for key, value in line:gmatch("(%w+)=(.+)") do
@ -166,7 +167,7 @@ function parse(file)
cmdline = cmdline:gsub('%%i', '')
end
if program.Terminal == "true" then
cmdline = terminal .. ' -e ' .. cmdline
cmdline = utils.terminal .. ' -e ' .. cmdline
end
program.cmdline = cmdline
end
@ -178,7 +179,7 @@ end
-- @param dir The directory.
-- @param icons_size, The icons sizes, optional.
-- @return A table with all .desktop entries.
function parse_dir(dir)
function utils.parse_dir(dir)
local programs = {}
local files = io.popen('find '.. dir ..' -maxdepth 1 -name "*.desktop"')
for file in files:lines() do
@ -187,4 +188,6 @@ function parse_dir(dir)
return programs
end
return utils
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80