2012-03-08 20:24:21 +01:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-19 13:37:13 +02:00
|
|
|
--- Menubar module, which aims to provide a freedesktop menu alternative
|
|
|
|
--
|
|
|
|
-- List of menubar keybindings:
|
|
|
|
-- ---
|
|
|
|
--
|
|
|
|
-- * "Left" | "C-j" select an item on the left
|
|
|
|
-- * "Right" | "C-k" select an item on the right
|
|
|
|
-- * "Backspace" exit the current category if we are in any
|
|
|
|
-- * "Escape" exit the current directory or exit menubar
|
|
|
|
-- * "Home" select the first item
|
|
|
|
-- * "End" select the last
|
|
|
|
-- * "Return" execute the entry
|
2015-09-30 00:05:56 +02:00
|
|
|
-- * "C-Return" execute the command with awful.spawn
|
2014-05-19 13:37:13 +02:00
|
|
|
-- * "C-M-Return" execute the command in a terminal
|
|
|
|
--
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @author Alexander Yakushev <yakushev.alex@gmail.com>
|
|
|
|
-- @copyright 2011-2012 Alexander Yakushev
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-19 13:37:13 +02:00
|
|
|
-- @module menubar
|
2012-03-08 20:24:21 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
2012-05-05 00:06:53 +02:00
|
|
|
local capi = {
|
|
|
|
client = client,
|
|
|
|
mouse = mouse,
|
|
|
|
screen = screen
|
|
|
|
}
|
|
|
|
local awful = require("awful")
|
|
|
|
local common = require("awful.widget.common")
|
|
|
|
local theme = require("beautiful")
|
|
|
|
local wibox = require("wibox")
|
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
-- menubar
|
2015-02-19 22:11:38 +01:00
|
|
|
local menubar = { mt = {}, menu_entries = {} }
|
2012-06-12 10:36:28 +02:00
|
|
|
menubar.menu_gen = require("menubar.menu_gen")
|
|
|
|
menubar.utils = require("menubar.utils")
|
2015-02-19 22:11:38 +01:00
|
|
|
local compute_text_width = menubar.utils.compute_text_width
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
-- Options section
|
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- When true the .desktop files will be reparsed only when the
|
2012-03-08 20:24:21 +01:00
|
|
|
-- extension is initialized. Use this if menubar takes much time to
|
|
|
|
-- open.
|
2012-09-04 01:48:03 +02:00
|
|
|
menubar.cache_entries = true
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- When true the categories will be shown alongside application
|
2012-03-08 20:24:21 +01:00
|
|
|
-- entries.
|
2012-09-04 01:48:03 +02:00
|
|
|
menubar.show_categories = true
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- Specifies the geometry of the menubar. This is a table with the keys
|
2012-03-08 20:24:21 +01:00
|
|
|
-- x, y, width and height. Missing values are replaced via the screen's
|
|
|
|
-- geometry. However, missing height is replaced by the font size.
|
2012-09-04 01:48:03 +02:00
|
|
|
menubar.geometry = { width = nil,
|
|
|
|
height = nil,
|
|
|
|
x = nil,
|
|
|
|
y = nil }
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2015-02-19 22:11:38 +01:00
|
|
|
--- Width of blank space left in the right side.
|
|
|
|
menubar.right_margin = 50
|
|
|
|
|
|
|
|
--- Label used for "Next page", default "▶▶".
|
|
|
|
menubar.right_label = "▶▶"
|
|
|
|
|
|
|
|
--- Label used for "Previous page", default "◀◀".
|
|
|
|
menubar.left_label = "◀◀"
|
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- Allows user to specify custom parameters for prompt.run function
|
2012-09-04 01:45:18 +02:00
|
|
|
-- (like colors).
|
|
|
|
menubar.prompt_args = {}
|
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
-- Private section
|
|
|
|
local current_item = 1
|
|
|
|
local previous_item = nil
|
|
|
|
local current_category = nil
|
|
|
|
local shownitems = nil
|
|
|
|
local instance = { prompt = nil,
|
|
|
|
widget = nil,
|
|
|
|
wibox = nil }
|
|
|
|
|
|
|
|
local common_args = { w = wibox.layout.fixed.horizontal(),
|
|
|
|
data = setmetatable({}, { __mode = 'kv' }) }
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Wrap the text with the color span tag.
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @param s The text.
|
|
|
|
-- @param c The desired text color.
|
|
|
|
-- @return the text wrapped in a span tag.
|
|
|
|
local function colortext(s, c)
|
2015-07-11 00:11:45 +02:00
|
|
|
return "<span color='" .. awful.util.ensure_pango_color(c) .. "'>" .. s .. "</span>"
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Get how the menu item should be displayed.
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @param o The menu item.
|
|
|
|
-- @return item name, item background color, background image, item icon.
|
|
|
|
local function label(o)
|
|
|
|
if o.focused then
|
2015-07-11 00:11:45 +02:00
|
|
|
return colortext(o.name, theme.fg_focus), theme.bg_focus, nil, o.icon
|
2012-03-08 20:24:21 +01:00
|
|
|
else
|
|
|
|
return o.name, theme.bg_normal, nil, o.icon
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Perform an action for the given menu item.
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @param o The menu item.
|
|
|
|
-- @return if the function processed the callback, new awful.prompt command, new awful.prompt prompt text.
|
|
|
|
local function perform_action(o)
|
2012-11-24 17:57:27 +01:00
|
|
|
if not o then return end
|
2012-03-08 20:24:21 +01:00
|
|
|
if o.key then
|
|
|
|
current_category = o.key
|
|
|
|
local new_prompt = shownitems[current_item].name .. ": "
|
|
|
|
previous_item = current_item
|
|
|
|
current_item = 1
|
|
|
|
return true, "", new_prompt
|
|
|
|
elseif shownitems[current_item].cmdline then
|
2015-09-30 00:05:56 +02:00
|
|
|
awful.spawn(shownitems[current_item].cmdline)
|
2012-05-08 00:16:31 +02:00
|
|
|
-- Let awful.prompt execute dummy exec_callback and
|
|
|
|
-- done_callback to stop the keygrabber properly.
|
|
|
|
return false
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Cut item list to return only current page.
|
2015-02-26 12:05:59 +01:00
|
|
|
-- @tparam table all_items All items list.
|
2015-02-19 22:11:38 +01:00
|
|
|
-- @tparam str query Search query.
|
2015-12-22 18:31:48 +01:00
|
|
|
-- @tparam number scr Screen number
|
2015-02-19 22:11:38 +01:00
|
|
|
-- @return table List of items for current page.
|
2015-11-20 13:49:26 +01:00
|
|
|
local function get_current_page(all_items, query, scr)
|
2015-02-19 22:11:38 +01:00
|
|
|
if not instance.prompt.width then
|
2015-11-20 13:49:26 +01:00
|
|
|
instance.prompt.width = compute_text_width(instance.prompt.prompt, scr)
|
2015-02-19 22:11:38 +01:00
|
|
|
end
|
|
|
|
if not menubar.left_label_width then
|
2015-11-20 13:49:26 +01:00
|
|
|
menubar.left_label_width = compute_text_width(menubar.left_label, scr)
|
2015-02-19 22:11:38 +01:00
|
|
|
end
|
|
|
|
if not menubar.right_label_width then
|
2015-11-20 13:49:26 +01:00
|
|
|
menubar.right_label_width = compute_text_width(menubar.right_label, scr)
|
2015-02-19 22:11:38 +01:00
|
|
|
end
|
|
|
|
local available_space = instance.geometry.width - menubar.right_margin -
|
|
|
|
menubar.right_label_width - menubar.left_label_width -
|
2015-11-20 13:49:26 +01:00
|
|
|
compute_text_width(query, scr) - instance.prompt.width
|
2015-02-19 22:11:38 +01:00
|
|
|
|
|
|
|
local width_sum = 0
|
|
|
|
local current_page = {}
|
|
|
|
for i, item in ipairs(all_items) do
|
|
|
|
item.width = item.width or
|
2015-11-20 13:49:26 +01:00
|
|
|
compute_text_width(" " .. item.name, scr) +
|
2015-02-19 22:11:38 +01:00
|
|
|
(item.icon and instance.geometry.height or 0)
|
|
|
|
if width_sum + item.width > available_space then
|
|
|
|
if current_item < i then
|
|
|
|
table.insert(current_page, { name = menubar.right_label, icon = nil })
|
|
|
|
break
|
|
|
|
end
|
|
|
|
current_page = { { name = menubar.left_label, icon = nil }, item, }
|
|
|
|
width_sum = item.width
|
|
|
|
else
|
|
|
|
table.insert(current_page, item)
|
|
|
|
width_sum = width_sum + item.width
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return current_page
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Update the menubar according to the command entered by user.
|
|
|
|
-- @tparam str query Search query.
|
2015-12-22 18:31:48 +01:00
|
|
|
-- @tparam number scr Screen number
|
2015-11-20 13:49:26 +01:00
|
|
|
local function menulist_update(query, scr)
|
2015-02-19 22:11:38 +01:00
|
|
|
query = query or ""
|
2012-03-08 20:24:21 +01:00
|
|
|
shownitems = {}
|
2015-02-19 22:11:38 +01:00
|
|
|
local pattern = awful.util.query_to_pattern(query)
|
2012-03-08 20:24:21 +01:00
|
|
|
local match_inside = {}
|
|
|
|
|
|
|
|
-- First we add entries which names match the command from the
|
|
|
|
-- beginning to the table shownitems, and the ones that contain
|
|
|
|
-- command in the middle to the table match_inside.
|
|
|
|
|
|
|
|
-- Add the categories
|
2012-09-04 01:48:03 +02:00
|
|
|
if menubar.show_categories then
|
2012-06-30 21:51:11 +02:00
|
|
|
for _, v in pairs(menubar.menu_gen.all_categories) do
|
2012-03-08 20:24:21 +01:00
|
|
|
v.focused = false
|
|
|
|
if not current_category and v.use then
|
2015-01-22 01:30:13 +01:00
|
|
|
if string.match(v.name, pattern) then
|
|
|
|
if string.match(v.name, "^" .. pattern) then
|
2012-03-08 20:24:21 +01:00
|
|
|
table.insert(shownitems, v)
|
|
|
|
else
|
|
|
|
table.insert(match_inside, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-04 23:51:09 +02:00
|
|
|
-- Add the applications according to their name and cmdline
|
2015-02-19 22:11:38 +01:00
|
|
|
for i, v in ipairs(menubar.menu_entries) do
|
2012-03-08 20:24:21 +01:00
|
|
|
v.focused = false
|
|
|
|
if not current_category or v.category == current_category then
|
2015-01-22 01:30:13 +01:00
|
|
|
if string.match(v.name, pattern)
|
|
|
|
or string.match(v.cmdline, pattern) then
|
|
|
|
if string.match(v.name, "^" .. pattern)
|
|
|
|
or string.match(v.cmdline, "^" .. pattern) then
|
2012-03-08 20:24:21 +01:00
|
|
|
table.insert(shownitems, v)
|
|
|
|
else
|
|
|
|
table.insert(match_inside, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Now add items from match_inside to shownitems
|
|
|
|
for i, v in ipairs(match_inside) do
|
|
|
|
table.insert(shownitems, v)
|
|
|
|
end
|
|
|
|
|
|
|
|
if #shownitems > 0 then
|
2012-05-05 00:07:51 +02:00
|
|
|
-- Insert a run item value as the last choice
|
|
|
|
table.insert(shownitems, { name = "Exec: " .. query, cmdline = query, icon = nil })
|
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
if current_item > #shownitems then
|
|
|
|
current_item = #shownitems
|
|
|
|
end
|
|
|
|
shownitems[current_item].focused = true
|
|
|
|
else
|
2012-05-05 00:07:51 +02:00
|
|
|
table.insert(shownitems, { name = "", cmdline = query, icon = nil })
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
common.list_update(common_args.w, nil, label,
|
|
|
|
common_args.data,
|
2015-11-20 13:49:26 +01:00
|
|
|
get_current_page(shownitems, query, scr))
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Create the menubar wibox and widgets.
|
2012-03-08 20:24:21 +01:00
|
|
|
local function initialize()
|
|
|
|
instance.wibox = wibox({})
|
2012-06-12 20:13:09 +02:00
|
|
|
instance.widget = menubar.get()
|
2012-03-08 20:24:21 +01:00
|
|
|
instance.wibox.ontop = true
|
|
|
|
instance.prompt = awful.widget.prompt()
|
|
|
|
local layout = wibox.layout.fixed.horizontal()
|
|
|
|
layout:add(instance.prompt)
|
|
|
|
layout:add(instance.widget)
|
|
|
|
instance.wibox:set_widget(layout)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Refresh menubar's cache by reloading .desktop files.
|
2012-06-12 10:36:28 +02:00
|
|
|
function menubar.refresh()
|
2015-02-19 22:11:38 +01:00
|
|
|
menubar.menu_entries = menubar.menu_gen.generate()
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Awful.prompt keypressed callback to be used when the user presses a key.
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @param mod Table of key combination modifiers (Control, Shift).
|
|
|
|
-- @param key The key that was pressed.
|
|
|
|
-- @param comm The current command in the prompt.
|
|
|
|
-- @return if the function processed the callback, new awful.prompt command, new awful.prompt prompt text.
|
|
|
|
local function prompt_keypressed_callback(mod, key, comm)
|
|
|
|
if key == "Left" or (mod.Control and key == "j") then
|
|
|
|
current_item = math.max(current_item - 1, 1)
|
|
|
|
return true
|
|
|
|
elseif key == "Right" or (mod.Control and key == "k") then
|
|
|
|
current_item = current_item + 1
|
|
|
|
return true
|
|
|
|
elseif key == "BackSpace" then
|
|
|
|
if comm == "" and current_category then
|
|
|
|
current_category = nil
|
|
|
|
current_item = previous_item
|
2012-05-06 00:43:33 +02:00
|
|
|
return true, nil, "Run: "
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
elseif key == "Escape" then
|
|
|
|
if current_category then
|
|
|
|
current_category = nil
|
|
|
|
current_item = previous_item
|
2012-05-06 00:43:33 +02:00
|
|
|
return true, nil, "Run: "
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
2012-05-05 00:08:45 +02:00
|
|
|
elseif key == "Home" then
|
|
|
|
current_item = 1
|
|
|
|
return true
|
|
|
|
elseif key == "End" then
|
|
|
|
current_item = #shownitems
|
|
|
|
return true
|
2013-01-02 11:41:35 +01:00
|
|
|
elseif key == "Return" or key == "KP_Enter" then
|
2012-05-05 00:08:45 +02:00
|
|
|
if mod.Control then
|
|
|
|
current_item = #shownitems
|
|
|
|
if mod.Mod1 then
|
|
|
|
-- add a terminal to the cmdline
|
2012-06-12 10:36:28 +02:00
|
|
|
shownitems[current_item].cmdline = menubar.utils.terminal
|
2012-05-05 00:08:45 +02:00
|
|
|
.. " -e " .. shownitems[current_item].cmdline
|
|
|
|
end
|
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
return perform_action(shownitems[current_item])
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Show the menubar on the given screen.
|
|
|
|
-- @param scr Screen number.
|
2012-06-12 10:36:28 +02:00
|
|
|
function menubar.show(scr)
|
2012-03-08 20:24:21 +01:00
|
|
|
if not instance.wibox then
|
|
|
|
initialize()
|
|
|
|
elseif instance.wibox.visible then -- Menu already shown, exit
|
|
|
|
return
|
2012-09-04 01:48:03 +02:00
|
|
|
elseif not menubar.cache_entries then
|
2012-06-12 10:36:28 +02:00
|
|
|
menubar.refresh()
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Set position and size
|
2015-01-31 16:40:46 +01:00
|
|
|
scr = scr or awful.screen.focused() or 1
|
2012-03-08 20:24:21 +01:00
|
|
|
local scrgeom = capi.screen[scr].workarea
|
2012-09-04 01:48:03 +02:00
|
|
|
local geometry = menubar.geometry
|
2015-02-19 22:11:38 +01:00
|
|
|
instance.geometry = {x = geometry.x or scrgeom.x,
|
2012-03-08 20:24:21 +01:00
|
|
|
y = geometry.y or scrgeom.y,
|
2015-08-29 20:47:10 +02:00
|
|
|
height = geometry.height or awful.util.round(theme.get_font_height() * 1.5),
|
2015-02-19 22:11:38 +01:00
|
|
|
width = geometry.width or scrgeom.width}
|
|
|
|
instance.wibox:geometry(instance.geometry)
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
current_item = 1
|
|
|
|
current_category = nil
|
2015-11-20 13:49:26 +01:00
|
|
|
menulist_update(nil, scr)
|
2012-09-04 01:45:18 +02:00
|
|
|
|
|
|
|
local prompt_args = menubar.prompt_args or {}
|
|
|
|
prompt_args.prompt = "Run: "
|
|
|
|
awful.prompt.run(prompt_args, instance.prompt.widget,
|
2012-05-05 00:09:03 +02:00
|
|
|
function(s) end, -- exe_callback function set to do nothing
|
|
|
|
awful.completion.shell, -- completion_callback
|
|
|
|
awful.util.getdir("cache") .. "/history_menu",
|
2015-11-20 13:49:26 +01:00
|
|
|
nil,
|
|
|
|
menubar.hide, function(query) menulist_update(query, scr) end,
|
|
|
|
prompt_keypressed_callback
|
2012-05-05 00:09:03 +02:00
|
|
|
)
|
2012-03-08 20:24:21 +01:00
|
|
|
instance.wibox.visible = true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Hide the menubar.
|
2012-06-12 10:36:28 +02:00
|
|
|
function menubar.hide()
|
2012-03-08 20:24:21 +01:00
|
|
|
instance.wibox.visible = false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get a menubar wibox.
|
|
|
|
-- @return menubar wibox.
|
2012-06-12 10:36:28 +02:00
|
|
|
function menubar.get()
|
|
|
|
menubar.refresh()
|
2012-03-08 20:24:21 +01:00
|
|
|
-- Add to each category the name of its key in all_categories
|
2012-06-30 21:51:11 +02:00
|
|
|
for k, v in pairs(menubar.menu_gen.all_categories) do
|
2012-03-08 20:24:21 +01:00
|
|
|
v.key = k
|
|
|
|
end
|
|
|
|
return common_args.w
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
function menubar.mt:__call(...)
|
|
|
|
return menubar.get(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(menubar, menubar.mt)
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|