Merge pull request #144 from actionless/menubar-paginated-scroll
feat(lib: menubar): add paginated scrolling
This commit is contained in:
commit
015e3e985b
|
@ -16,9 +16,10 @@ local theme = require("beautiful")
|
||||||
local wibox = require("wibox")
|
local wibox = require("wibox")
|
||||||
|
|
||||||
-- menubar
|
-- menubar
|
||||||
local menubar = { mt = {} }
|
local menubar = { mt = {}, menu_entries = {} }
|
||||||
menubar.menu_gen = require("menubar.menu_gen")
|
menubar.menu_gen = require("menubar.menu_gen")
|
||||||
menubar.utils = require("menubar.utils")
|
menubar.utils = require("menubar.utils")
|
||||||
|
local compute_text_width = menubar.utils.compute_text_width
|
||||||
|
|
||||||
--- List of menubar keybindings:
|
--- List of menubar keybindings:
|
||||||
-- <p><ul>
|
-- <p><ul>
|
||||||
|
@ -55,6 +56,15 @@ menubar.geometry = { width = nil,
|
||||||
x = nil,
|
x = nil,
|
||||||
y = nil }
|
y = nil }
|
||||||
|
|
||||||
|
--- 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 = "◀◀"
|
||||||
|
|
||||||
--- Allows user to specify custom parameters for prompt.run function
|
--- Allows user to specify custom parameters for prompt.run function
|
||||||
-- (like colors).
|
-- (like colors).
|
||||||
menubar.prompt_args = {}
|
menubar.prompt_args = {}
|
||||||
|
@ -110,12 +120,51 @@ local function perform_action(o)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Cut item list to return only current page
|
||||||
|
-- @tparam table all_item All items list
|
||||||
|
-- @tparam str query Search query.
|
||||||
|
-- @return table List of items for current page.
|
||||||
|
local function get_current_page(all_items, query)
|
||||||
|
if not instance.prompt.width then
|
||||||
|
instance.prompt.width = compute_text_width(instance.prompt.prompt)
|
||||||
|
end
|
||||||
|
if not menubar.left_label_width then
|
||||||
|
menubar.left_label_width = compute_text_width(menubar.left_label)
|
||||||
|
end
|
||||||
|
if not menubar.right_label_width then
|
||||||
|
menubar.right_label_width = compute_text_width(menubar.right_label)
|
||||||
|
end
|
||||||
|
local available_space = instance.geometry.width - menubar.right_margin -
|
||||||
|
menubar.right_label_width - menubar.left_label_width -
|
||||||
|
compute_text_width(query) - instance.prompt.width
|
||||||
|
|
||||||
|
local width_sum = 0
|
||||||
|
local current_page = {}
|
||||||
|
for i, item in ipairs(all_items) do
|
||||||
|
item.width = item.width or
|
||||||
|
compute_text_width(" " .. item.name) +
|
||||||
|
(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
|
||||||
|
|
||||||
-- Update the menubar according to the command entered by user.
|
-- Update the menubar according to the command entered by user.
|
||||||
-- @param query The text to filter entries by.
|
-- @param query The text to filter entries by.
|
||||||
local function menulist_update(query)
|
local function menulist_update(query)
|
||||||
local query = query or ""
|
query = query or ""
|
||||||
local pattern = awful.util.query_to_pattern(query)
|
|
||||||
shownitems = {}
|
shownitems = {}
|
||||||
|
local pattern = awful.util.query_to_pattern(query)
|
||||||
local match_inside = {}
|
local match_inside = {}
|
||||||
|
|
||||||
-- First we add entries which names match the command from the
|
-- First we add entries which names match the command from the
|
||||||
|
@ -139,7 +188,7 @@ local function menulist_update(query)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add the applications according to their name and cmdline
|
-- Add the applications according to their name and cmdline
|
||||||
for i, v in ipairs(menu_entries) do
|
for i, v in ipairs(menubar.menu_entries) do
|
||||||
v.focused = false
|
v.focused = false
|
||||||
if not current_category or v.category == current_category then
|
if not current_category or v.category == current_category then
|
||||||
if string.match(v.name, pattern)
|
if string.match(v.name, pattern)
|
||||||
|
@ -173,7 +222,7 @@ local function menulist_update(query)
|
||||||
|
|
||||||
common.list_update(common_args.w, nil, label,
|
common.list_update(common_args.w, nil, label,
|
||||||
common_args.data,
|
common_args.data,
|
||||||
shownitems)
|
get_current_page(shownitems, query))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create the menubar wibox and widgets.
|
-- Create the menubar wibox and widgets.
|
||||||
|
@ -190,7 +239,7 @@ end
|
||||||
|
|
||||||
--- Refresh menubar's cache by reloading .desktop files.
|
--- Refresh menubar's cache by reloading .desktop files.
|
||||||
function menubar.refresh()
|
function menubar.refresh()
|
||||||
menu_entries = menubar.menu_gen.generate()
|
menubar.menu_entries = menubar.menu_gen.generate()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Awful.prompt keypressed callback to be used when the user presses a key.
|
-- Awful.prompt keypressed callback to be used when the user presses a key.
|
||||||
|
@ -252,10 +301,11 @@ function menubar.show(scr)
|
||||||
scr = scr or capi.mouse.screen or 1
|
scr = scr or capi.mouse.screen or 1
|
||||||
local scrgeom = capi.screen[scr].workarea
|
local scrgeom = capi.screen[scr].workarea
|
||||||
local geometry = menubar.geometry
|
local geometry = menubar.geometry
|
||||||
instance.wibox:geometry({x = geometry.x or scrgeom.x,
|
instance.geometry = {x = geometry.x or scrgeom.x,
|
||||||
y = geometry.y or scrgeom.y,
|
y = geometry.y or scrgeom.y,
|
||||||
height = geometry.height or theme.get_font_height() * 1.5,
|
height = geometry.height or theme.get_font_height() * 1.5,
|
||||||
width = geometry.width or scrgeom.width})
|
width = geometry.width or scrgeom.width}
|
||||||
|
instance.wibox:geometry(instance.geometry)
|
||||||
|
|
||||||
current_item = 1
|
current_item = 1
|
||||||
current_category = nil
|
current_category = nil
|
||||||
|
@ -280,9 +330,6 @@ end
|
||||||
--- Get a menubar wibox.
|
--- Get a menubar wibox.
|
||||||
-- @return menubar wibox.
|
-- @return menubar wibox.
|
||||||
function menubar.get()
|
function menubar.get()
|
||||||
if app_folders then
|
|
||||||
menubar.menu_gen.all_menu_dirs = app_folders
|
|
||||||
end
|
|
||||||
menubar.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(menubar.menu_gen.all_categories) do
|
for k, v in pairs(menubar.menu_gen.all_categories) do
|
||||||
|
|
|
@ -12,6 +12,7 @@ local string = string
|
||||||
local awful_util = require("awful.util")
|
local awful_util = require("awful.util")
|
||||||
local theme = require("beautiful")
|
local theme = require("beautiful")
|
||||||
local glib = require("lgi").GLib
|
local glib = require("lgi").GLib
|
||||||
|
local wibox = require("wibox")
|
||||||
|
|
||||||
-- Utility module for menubar
|
-- Utility module for menubar
|
||||||
-- menubar.utils
|
-- menubar.utils
|
||||||
|
@ -218,6 +219,14 @@ function utils.parse_dir(dir)
|
||||||
return programs
|
return programs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Compute text width.
|
||||||
|
-- @tparam str text Text.
|
||||||
|
-- @treturn int Text width.
|
||||||
|
function utils.compute_text_width(text)
|
||||||
|
local _, logical = wibox.widget.textbox(awful_util.escape(text))._layout:get_pixel_extents()
|
||||||
|
return logical.width
|
||||||
|
end
|
||||||
|
|
||||||
return utils
|
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
|
||||||
|
|
Loading…
Reference in New Issue