2012-03-08 20:24:21 +01:00
|
|
|
---------------------------------------------------------------------------
|
2019-11-21 15:43:15 +01:00
|
|
|
--- Menubar module, which aims to provide a freedesktop menu alternative.
|
2014-05-19 13:37:13 +02:00
|
|
|
--
|
|
|
|
-- List of menubar keybindings:
|
|
|
|
-- ---
|
|
|
|
--
|
2019-06-08 01:08:05 +02:00
|
|
|
-- <table class='widget_list' border=1>
|
|
|
|
-- <tr style='font-weight: bold;'>
|
|
|
|
-- <th align='center'>Keybinding</th>
|
|
|
|
-- <th align='center'>Description</th>
|
|
|
|
-- </tr> </td></tr>
|
|
|
|
-- <tr><td><kbd>Left</kbd><kbd>C-j</kbd></td><td> select an item on the left </td></tr>
|
|
|
|
-- <tr><td><kbd>Right</kbd><kbd>C-k</kbd></td><td> select an item on the right </td></tr>
|
|
|
|
-- <tr><td><kbd>Backspace </kbd></td><td> exit the current category if we are in any </td></tr>
|
|
|
|
-- <tr><td><kbd>Escape </kbd></td><td> exit the current directory or exit menubar </td></tr>
|
|
|
|
-- <tr><td><kbd>Home </kbd></td><td> select the first item </td></tr>
|
|
|
|
-- <tr><td><kbd>End </kbd></td><td> select the last </td></tr>
|
|
|
|
-- <tr><td><kbd>Return </kbd></td><td> execute the entry </td></tr>
|
|
|
|
-- <tr><td><kbd>C-Return </kbd></td><td> execute the command with awful.spawn </td></tr>
|
|
|
|
-- <tr><td><kbd>C-M-Return </kbd></td><td> execute the command in a terminal </td></tr>
|
|
|
|
-- </table>
|
2014-05-19 13:37:13 +02:00
|
|
|
--
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @author Alexander Yakushev <yakushev.alex@gmail.com>
|
|
|
|
-- @copyright 2011-2012 Alexander Yakushev
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @popupmod 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
|
|
|
|
}
|
2017-03-06 17:11:05 +01:00
|
|
|
local gmath = require("gears.math")
|
2012-05-05 00:06:53 +02:00
|
|
|
local awful = require("awful")
|
2017-02-14 00:16:45 +01:00
|
|
|
local gfs = require("gears.filesystem")
|
2012-05-05 00:06:53 +02:00
|
|
|
local common = require("awful.widget.common")
|
|
|
|
local theme = require("beautiful")
|
|
|
|
local wibox = require("wibox")
|
2017-03-03 00:02:03 +01:00
|
|
|
local gcolor = require("gears.color")
|
2017-03-12 00:57:32 +01:00
|
|
|
local gstring = require("gears.string")
|
2017-08-10 12:18:54 +02:00
|
|
|
local gdebug = require("gears.debug")
|
2012-05-05 00:06:53 +02:00
|
|
|
|
2016-02-26 18:25:55 +01:00
|
|
|
local function get_screen(s)
|
|
|
|
return s and capi.screen[s]
|
|
|
|
end
|
|
|
|
|
2018-09-28 06:35:10 +02:00
|
|
|
|
|
|
|
--- Menubar normal text color.
|
|
|
|
-- @beautiful beautiful.menubar_fg_normal
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2018-09-28 06:35:10 +02:00
|
|
|
|
|
|
|
--- Menubar normal background color.
|
|
|
|
-- @beautiful beautiful.menubar_bg_normal
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2018-09-28 06:35:10 +02:00
|
|
|
|
|
|
|
--- Menubar border width.
|
|
|
|
-- @beautiful beautiful.menubar_border_width
|
|
|
|
-- @tparam[opt=0] number menubar_border_width
|
|
|
|
|
|
|
|
--- Menubar border color.
|
|
|
|
-- @beautiful beautiful.menubar_border_color
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2018-09-28 06:35:10 +02:00
|
|
|
|
|
|
|
--- Menubar selected item text color.
|
2020-02-27 11:19:09 +01:00
|
|
|
-- @beautiful beautiful.menubar_fg_focus
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2018-09-28 06:35:10 +02:00
|
|
|
|
|
|
|
--- Menubar selected item background color.
|
2020-02-27 11:19:09 +01:00
|
|
|
-- @beautiful beautiful.menubar_bg_focus
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2018-09-28 06:35:10 +02:00
|
|
|
|
2020-12-18 15:40:40 +01:00
|
|
|
--- Menubar font.
|
|
|
|
-- @beautiful beautiful.menubar_font
|
|
|
|
-- @param[opt=beautiful.font] font
|
|
|
|
|
2018-09-28 06:35:10 +02:00
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
-- menubar
|
2017-03-03 17:20:56 +01:00
|
|
|
local menubar = { menu_entries = {} }
|
2012-06-12 10:36:28 +02:00
|
|
|
menubar.menu_gen = require("menubar.menu_gen")
|
|
|
|
menubar.utils = require("menubar.utils")
|
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.
|
2016-09-11 07:48:45 +02:00
|
|
|
-- @tfield[opt=true] boolean cache_entries
|
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.
|
2016-09-11 07:48:45 +02:00
|
|
|
-- @tfield[opt=true] boolean show_categories
|
2012-09-04 01:48:03 +02:00
|
|
|
menubar.show_categories = true
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2020-04-21 17:44:23 +02:00
|
|
|
--- When false will hide results if the current query is empty
|
|
|
|
-- @tfield[opt=true] boolean match_empty
|
|
|
|
menubar.match_empty = true
|
|
|
|
|
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.
|
2016-09-11 07:48:45 +02:00
|
|
|
-- @table geometry
|
|
|
|
-- @tfield number geometry.x A forced horizontal position
|
|
|
|
-- @tfield number geometry.y A forced vertical position
|
|
|
|
-- @tfield number geometry.width A forced width
|
|
|
|
-- @tfield number geometry.height A forced height
|
2012-09-04 01:48:03 +02:00
|
|
|
menubar.geometry = { width = nil,
|
|
|
|
height = nil,
|
|
|
|
x = nil,
|
|
|
|
y = nil }
|
2016-09-11 07:48:45 +02:00
|
|
|
|
2015-02-19 22:11:38 +01:00
|
|
|
--- Width of blank space left in the right side.
|
2016-09-11 07:48:45 +02:00
|
|
|
-- @tfield number right_margin
|
2016-03-02 02:22:08 +01:00
|
|
|
menubar.right_margin = theme.xresources.apply_dpi(8)
|
2015-02-19 22:11:38 +01:00
|
|
|
|
|
|
|
--- Label used for "Next page", default "▶▶".
|
2016-09-11 07:48:45 +02:00
|
|
|
-- @tfield[opt="▶▶"] string right_label
|
2015-02-19 22:11:38 +01:00
|
|
|
menubar.right_label = "▶▶"
|
|
|
|
|
|
|
|
--- Label used for "Previous page", default "◀◀".
|
2016-09-11 07:48:45 +02:00
|
|
|
-- @tfield[opt="◀◀"] string left_label
|
2015-02-19 22:11:38 +01:00
|
|
|
menubar.left_label = "◀◀"
|
|
|
|
|
2020-12-18 15:40:40 +01:00
|
|
|
-- awful.widget.common.list_update adds spacing of dpi(4) between items.
|
|
|
|
-- @tfield number list_spacing
|
|
|
|
local list_spacing = theme.xresources.apply_dpi(4)
|
2016-03-02 02:22:08 +01:00
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- Allows user to specify custom parameters for prompt.run function
|
2020-04-22 22:03:28 +02:00
|
|
|
-- (like colors). This will merge with the default parameters, overriding affected values.
|
2016-09-11 07:48:45 +02:00
|
|
|
-- @see awful.prompt
|
2012-09-04 01:45:18 +02:00
|
|
|
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
|
2017-01-02 17:20:41 +01:00
|
|
|
local instance = nil
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
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)
|
2017-03-03 00:02:03 +01:00
|
|
|
return "<span color='" .. gcolor.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.
|
2020-12-18 15:40:40 +01:00
|
|
|
-- @return item name, item background color, background image, item icon, item args.
|
2012-03-08 20:24:21 +01:00
|
|
|
local function label(o)
|
2018-09-28 06:35:10 +02:00
|
|
|
local fg_color = theme.menubar_fg_normal or theme.menu_fg_normal or theme.fg_normal
|
|
|
|
local bg_color = theme.menubar_bg_normal or theme.menu_bg_normal or theme.bg_normal
|
2012-03-08 20:24:21 +01:00
|
|
|
if o.focused then
|
2018-09-28 06:35:10 +02:00
|
|
|
fg_color = theme.menubar_fg_focus or theme.menu_fg_focus or theme.fg_focus
|
|
|
|
bg_color = theme.menubar_bg_focus or theme.menu_bg_focus or theme.bg_focus
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
2018-12-31 21:35:52 +01:00
|
|
|
return colortext(gstring.xml_escape(o.name), fg_color),
|
2018-08-26 18:35:14 +02:00
|
|
|
bg_color,
|
|
|
|
nil,
|
2020-12-18 15:40:40 +01:00
|
|
|
o.icon,
|
|
|
|
o.icon and {icon_size=instance.geometry.height}
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
2016-07-09 19:23:36 +02:00
|
|
|
local function load_count_table()
|
2017-02-10 14:14:32 +01:00
|
|
|
if instance.count_table then
|
|
|
|
return instance.count_table
|
|
|
|
end
|
|
|
|
instance.count_table = {}
|
2018-03-11 16:07:36 +01:00
|
|
|
local count_file_name = gfs.get_cache_dir() .. "/menu_count_file"
|
2017-02-10 13:35:46 +01:00
|
|
|
local count_file = io.open (count_file_name, "r")
|
2016-07-09 19:23:36 +02:00
|
|
|
if count_file then
|
2017-02-10 13:35:46 +01:00
|
|
|
for line in count_file:lines() do
|
2016-07-09 19:23:36 +02:00
|
|
|
local name, count = string.match(line, "([^;]+);([^;]+)")
|
|
|
|
if name ~= nil and count ~= nil then
|
2017-02-10 14:14:32 +01:00
|
|
|
instance.count_table[name] = count
|
2016-07-09 19:23:36 +02:00
|
|
|
end
|
|
|
|
end
|
2017-02-10 11:05:48 +01:00
|
|
|
count_file:close()
|
2016-07-09 19:23:36 +02:00
|
|
|
end
|
2017-02-10 14:14:32 +01:00
|
|
|
return instance.count_table
|
2016-07-09 19:23:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function write_count_table(count_table)
|
2017-02-10 14:14:32 +01:00
|
|
|
count_table = count_table or instance.count_table
|
2018-03-11 16:07:36 +01:00
|
|
|
local count_file_name = gfs.get_cache_dir() .. "/menu_count_file"
|
2017-04-02 18:58:03 +02:00
|
|
|
local count_file = assert(io.open(count_file_name, "w"))
|
2017-02-10 13:35:46 +01:00
|
|
|
for name, count in pairs(count_table) do
|
|
|
|
local str = string.format("%s;%d\n", name, count)
|
|
|
|
count_file:write(str)
|
2016-07-09 19:23:36 +02:00
|
|
|
end
|
2017-02-10 13:35:46 +01:00
|
|
|
count_file:close()
|
2016-07-09 19:23:36 +02:00
|
|
|
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)
|
2016-07-09 19:23:36 +02:00
|
|
|
-- load count_table from cache file
|
|
|
|
local count_table = load_count_table()
|
|
|
|
-- increase count
|
|
|
|
local curname = shownitems[current_item].name
|
2017-02-10 14:14:32 +01:00
|
|
|
count_table[curname] = (count_table[curname] or 0) + 1
|
2016-07-09 19:23:36 +02:00
|
|
|
-- write updated count table to cache file
|
|
|
|
write_count_table(count_table)
|
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
|
|
|
|
|
2016-07-09 19:23:36 +02: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.
|
2016-02-26 18:25:55 +01:00
|
|
|
-- @tparam number|screen scr Screen
|
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)
|
2020-12-18 15:40:40 +01:00
|
|
|
|
|
|
|
local compute_text_width = function(text, s)
|
|
|
|
return wibox.widget.textbox.get_markup_geometry(text, s, instance.font)['width']
|
|
|
|
end
|
|
|
|
|
2016-02-26 18:25:55 +01:00
|
|
|
scr = get_screen(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
|
2020-12-18 15:40:40 +01:00
|
|
|
local border_width = theme.menubar_border_width or theme.menu_border_width or 0
|
2015-02-19 22:11:38 +01:00
|
|
|
local available_space = instance.geometry.width - menubar.right_margin -
|
|
|
|
menubar.right_label_width - menubar.left_label_width -
|
2020-12-18 15:40:40 +01:00
|
|
|
compute_text_width(query..' ', scr) - instance.prompt.width - border_width * 2
|
|
|
|
-- space character is added as input cursor placeholder
|
2015-02-19 22:11:38 +01:00
|
|
|
|
|
|
|
local width_sum = 0
|
|
|
|
local current_page = {}
|
|
|
|
for i, item in ipairs(all_items) do
|
2020-12-18 15:40:40 +01:00
|
|
|
item.width = item.width or (
|
|
|
|
compute_text_width(label(item), scr) +
|
|
|
|
(item.icon and (instance.geometry.height + list_spacing) or 0) + list_spacing * 2
|
|
|
|
)
|
2015-02-19 22:11:38 +01:00
|
|
|
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.
|
2016-02-26 18:25:55 +01:00
|
|
|
-- @tparam number|screen scr Screen
|
2017-02-11 01:16:57 +01:00
|
|
|
local function menulist_update(scr)
|
|
|
|
local query = instance.query or ""
|
2012-03-08 20:24:21 +01:00
|
|
|
shownitems = {}
|
2017-03-12 00:57:32 +01:00
|
|
|
local pattern = gstring.query_to_pattern(query)
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2016-07-17 17:23:35 +02:00
|
|
|
-- All entries are added to a list that will be sorted
|
|
|
|
-- according to the priority (first) and weight (second) of its
|
|
|
|
-- entries.
|
|
|
|
-- If categories are used in the menu, we add the entries matching
|
|
|
|
-- the current query with high priority as to ensure they are
|
|
|
|
-- displayed first. Afterwards the non-category entries are added.
|
|
|
|
-- All entries are weighted according to the number of times they
|
|
|
|
-- have been executed previously (stored in count_table).
|
2016-07-17 12:11:10 +02:00
|
|
|
local count_table = load_count_table()
|
|
|
|
local command_list = {}
|
|
|
|
|
|
|
|
local PRIO_NONE = 0
|
2016-07-17 17:23:35 +02:00
|
|
|
local PRIO_CATEGORY_MATCH = 2
|
2016-07-17 12:11:10 +02:00
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
-- 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
|
2016-07-17 17:23:35 +02:00
|
|
|
|
|
|
|
-- check if current query matches a category
|
2015-01-22 01:30:13 +01:00
|
|
|
if string.match(v.name, pattern) then
|
2016-07-17 17:23:35 +02:00
|
|
|
|
|
|
|
v.weight = 0
|
|
|
|
v.prio = PRIO_CATEGORY_MATCH
|
|
|
|
|
|
|
|
-- get use count from count_table if present
|
|
|
|
-- and use it as weight
|
|
|
|
if string.len(pattern) > 0 and count_table[v.name] ~= nil then
|
|
|
|
v.weight = tonumber(count_table[v.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check for prefix match
|
2015-01-22 01:30:13 +01:00
|
|
|
if string.match(v.name, "^" .. pattern) then
|
2016-07-17 17:23:35 +02:00
|
|
|
-- increase default priority
|
|
|
|
v.prio = PRIO_CATEGORY_MATCH + 1
|
|
|
|
else
|
|
|
|
v.prio = PRIO_CATEGORY_MATCH
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
2016-07-17 17:23:35 +02:00
|
|
|
|
|
|
|
table.insert (command_list, v)
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-04 23:51:09 +02:00
|
|
|
-- Add the applications according to their name and cmdline
|
2020-04-21 17:47:41 +02:00
|
|
|
local add_entry = function(entry)
|
|
|
|
entry.focused = false
|
|
|
|
if not current_category or entry.category == current_category then
|
2016-07-17 17:23:35 +02:00
|
|
|
|
|
|
|
-- check if the query matches either the name or the commandline
|
|
|
|
-- of some entry
|
2020-04-21 17:47:41 +02:00
|
|
|
if string.match(entry.name, pattern)
|
|
|
|
or string.match(entry.cmdline, pattern) then
|
2016-07-09 19:23:36 +02:00
|
|
|
|
2020-04-21 17:47:41 +02:00
|
|
|
entry.weight = 0
|
|
|
|
entry.prio = PRIO_NONE
|
2016-07-17 12:11:10 +02:00
|
|
|
|
2016-07-17 17:23:35 +02:00
|
|
|
-- get use count from count_table if present
|
|
|
|
-- and use it as weight
|
2020-04-21 17:47:41 +02:00
|
|
|
if string.len(pattern) > 0 and count_table[entry.name] ~= nil then
|
|
|
|
entry.weight = tonumber(count_table[entry.name])
|
2016-07-17 12:11:10 +02:00
|
|
|
end
|
2016-07-09 19:23:36 +02:00
|
|
|
|
2016-07-17 17:23:35 +02:00
|
|
|
-- check for prefix match
|
2020-04-21 17:47:41 +02:00
|
|
|
if string.match(entry.name, "^" .. pattern)
|
|
|
|
or string.match(entry.cmdline, "^" .. pattern) then
|
2016-07-17 17:23:35 +02:00
|
|
|
-- increase default priority
|
2020-04-21 17:47:41 +02:00
|
|
|
entry.prio = PRIO_NONE + 1
|
2016-07-17 12:11:10 +02:00
|
|
|
else
|
2020-04-21 17:47:41 +02:00
|
|
|
entry.prio = PRIO_NONE
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
2016-07-17 12:11:10 +02:00
|
|
|
|
2020-04-21 17:47:41 +02:00
|
|
|
table.insert (command_list, entry)
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 17:44:23 +02:00
|
|
|
-- Add entries if required
|
|
|
|
if query ~= "" or menubar.match_empty then
|
|
|
|
for _, v in ipairs(menubar.menu_entries) do
|
|
|
|
add_entry(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-07-17 17:23:35 +02:00
|
|
|
local function compare_counts(a, b)
|
|
|
|
if a.prio == b.prio then
|
|
|
|
return a.weight > b.weight
|
2016-07-17 12:11:10 +02:00
|
|
|
end
|
2016-07-17 17:23:35 +02:00
|
|
|
return a.prio > b.prio
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
2016-07-17 17:23:35 +02:00
|
|
|
-- sort command_list by weight (highest first)
|
2016-07-09 19:23:36 +02:00
|
|
|
table.sort(command_list, compare_counts)
|
|
|
|
-- copy into showitems
|
|
|
|
shownitems = command_list
|
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
--- Refresh menubar's cache by reloading .desktop files.
|
2016-03-01 03:17:13 +01:00
|
|
|
-- @tparam[opt] screen scr Screen.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct menubar.refresh
|
2016-03-01 03:17:13 +01:00
|
|
|
function menubar.refresh(scr)
|
2017-10-08 14:53:58 +02:00
|
|
|
scr = get_screen(scr or awful.screen.focused() or 1)
|
2016-03-01 03:17:13 +01:00
|
|
|
menubar.menu_gen.generate(function(entries)
|
|
|
|
menubar.menu_entries = entries
|
2017-01-02 17:23:20 +01:00
|
|
|
if instance then
|
2017-02-11 01:16:57 +01:00
|
|
|
menulist_update(scr)
|
2017-01-02 17:23:20 +01:00
|
|
|
end
|
2016-03-01 03:17:13 +01:00
|
|
|
end)
|
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.
|
2017-10-08 14:53:58 +02:00
|
|
|
-- @param[opt] scr Screen.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct menubar.show
|
2012-06-12 10:36:28 +02:00
|
|
|
function menubar.show(scr)
|
2017-10-08 14:53:58 +02:00
|
|
|
scr = get_screen(scr or awful.screen.focused() or 1)
|
2018-09-28 06:35:10 +02:00
|
|
|
local fg_color = theme.menubar_fg_normal or theme.menu_fg_normal or theme.fg_normal
|
|
|
|
local bg_color = theme.menubar_bg_normal or theme.menu_bg_normal or theme.bg_normal
|
|
|
|
local border_width = theme.menubar_border_width or theme.menu_border_width or 0
|
|
|
|
local border_color = theme.menubar_border_color or theme.menu_border_color
|
2020-12-18 15:40:40 +01:00
|
|
|
local font = theme.menubar_font or theme.font or "Monospace 10"
|
2018-09-28 06:35:10 +02:00
|
|
|
|
2017-01-02 17:20:41 +01:00
|
|
|
if not instance then
|
2017-03-03 17:20:56 +01:00
|
|
|
-- 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
|
|
|
|
end
|
|
|
|
|
2017-03-03 17:26:15 +01:00
|
|
|
if menubar.cache_entries then
|
|
|
|
menubar.refresh(scr)
|
|
|
|
end
|
|
|
|
|
2017-01-02 17:20:41 +01:00
|
|
|
instance = {
|
2018-08-26 18:35:14 +02:00
|
|
|
wibox = wibox{
|
|
|
|
ontop = true,
|
|
|
|
bg = bg_color,
|
|
|
|
fg = fg_color,
|
2018-09-28 06:35:10 +02:00
|
|
|
border_width = border_width,
|
|
|
|
border_color = border_color,
|
2020-12-18 15:40:40 +01:00
|
|
|
font = font,
|
2018-08-26 18:35:14 +02:00
|
|
|
},
|
2017-03-03 17:20:56 +01:00
|
|
|
widget = common_args.w,
|
2017-01-02 17:20:41 +01:00
|
|
|
prompt = awful.widget.prompt(),
|
2017-02-05 22:06:29 +01:00
|
|
|
query = nil,
|
2017-02-10 14:14:32 +01:00
|
|
|
count_table = nil,
|
2020-12-18 15:40:40 +01:00
|
|
|
font = font,
|
2017-01-02 17:20:41 +01:00
|
|
|
}
|
|
|
|
local layout = wibox.layout.fixed.horizontal()
|
|
|
|
layout:add(instance.prompt)
|
|
|
|
layout:add(instance.widget)
|
|
|
|
instance.wibox:set_widget(layout)
|
|
|
|
end
|
|
|
|
|
|
|
|
if instance.wibox.visible then -- Menu already shown, exit
|
2012-03-08 20:24:21 +01:00
|
|
|
return
|
2012-09-04 01:48:03 +02:00
|
|
|
elseif not menubar.cache_entries then
|
2016-03-01 03:17:13 +01:00
|
|
|
menubar.refresh(scr)
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Set position and size
|
2016-09-11 07:49:57 +02:00
|
|
|
local scrgeom = 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,
|
2020-12-18 15:40:40 +01:00
|
|
|
height = geometry.height or gmath.round(theme.get_font_height(font) * 1.5),
|
2018-09-28 06:35:10 +02:00
|
|
|
width = (geometry.width or scrgeom.width) - border_width * 2}
|
2015-02-19 22:11:38 +01:00
|
|
|
instance.wibox:geometry(instance.geometry)
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
current_item = 1
|
|
|
|
current_category = nil
|
2017-02-11 01:16:57 +01:00
|
|
|
menulist_update(scr)
|
2012-09-04 01:45:18 +02:00
|
|
|
|
2020-04-21 16:45:40 +02:00
|
|
|
local default_prompt_args = {
|
2016-10-12 00:20:42 +02:00
|
|
|
prompt = "Run: ",
|
|
|
|
textbox = instance.prompt.widget,
|
|
|
|
completion_callback = awful.completion.shell,
|
2017-02-14 00:16:45 +01:00
|
|
|
history_path = gfs.get_cache_dir() .. "/history_menu",
|
2016-10-12 00:20:42 +02:00
|
|
|
done_callback = menubar.hide,
|
2017-02-05 22:06:29 +01:00
|
|
|
changed_callback = function(query)
|
|
|
|
instance.query = query
|
2017-02-11 01:16:57 +01:00
|
|
|
menulist_update(scr)
|
2017-02-05 22:06:29 +01:00
|
|
|
end,
|
2016-10-12 00:20:42 +02:00
|
|
|
keypressed_callback = prompt_keypressed_callback
|
2020-04-21 16:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
awful.prompt.run(setmetatable(menubar.prompt_args, {__index=default_prompt_args}))
|
|
|
|
|
2016-10-12 00:20:42 +02:00
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
instance.wibox.visible = true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Hide the menubar.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct menubar.hide
|
2012-06-12 10:36:28 +02:00
|
|
|
function menubar.hide()
|
2017-01-02 17:21:10 +01:00
|
|
|
if instance then
|
|
|
|
instance.wibox.visible = false
|
2017-02-11 01:16:57 +01:00
|
|
|
instance.query = nil
|
2017-01-02 17:21:10 +01:00
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Get a menubar wibox.
|
2016-03-01 03:17:13 +01:00
|
|
|
-- @tparam[opt] screen scr Screen.
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @return menubar wibox.
|
2017-03-18 10:43:55 +01:00
|
|
|
-- @deprecated get
|
2016-03-01 03:17:13 +01:00
|
|
|
function menubar.get(scr)
|
2017-08-10 12:18:54 +02:00
|
|
|
gdebug.deprecate("Use menubar.show() instead", { deprecated_in = 5 })
|
2016-03-01 03:17:13 +01:00
|
|
|
menubar.refresh(scr)
|
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
|
|
|
|
|
2017-03-03 17:20:56 +01:00
|
|
|
local mt = {}
|
|
|
|
function mt.__call(_, ...)
|
2012-06-12 10:36:28 +02:00
|
|
|
return menubar.get(...)
|
|
|
|
end
|
|
|
|
|
2017-03-03 17:20:56 +01:00
|
|
|
return setmetatable(menubar, mt)
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|