2013-05-11 21:02:42 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs,ipairs = pairs, ipairs
|
2014-02-06 03:43:06 +01:00
|
|
|
local table = table
|
2014-01-16 06:25:50 +01:00
|
|
|
local beautiful = require( "beautiful" )
|
|
|
|
local util = require( "awful.util" )
|
|
|
|
local aw_key = require( "awful.key" )
|
|
|
|
local object = require( "radical.object" )
|
2014-02-23 05:59:03 +01:00
|
|
|
local theme = require( "radical.theme" )
|
|
|
|
local item_mod = require( "radical.item" )
|
2016-02-21 08:34:05 +01:00
|
|
|
local common = require( "radical.common" )
|
2013-05-11 21:02:42 +02:00
|
|
|
|
2014-01-15 05:19:49 +01:00
|
|
|
local capi = { mouse = mouse, screen = screen , keygrabber = keygrabber, root=root, }
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
local module = {
|
|
|
|
arrow_type = {
|
2016-03-06 09:30:30 +01:00
|
|
|
NONE = 0, --TODO move to theme.state or something
|
2013-05-11 21:02:42 +02:00
|
|
|
PRETTY = 1,
|
|
|
|
CENTERED = 2,
|
2014-01-04 06:49:20 +01:00
|
|
|
},
|
2014-02-06 04:48:26 +01:00
|
|
|
event ={
|
2014-01-04 07:27:52 +01:00
|
|
|
NEVER = 0,
|
|
|
|
BUTTON1 = 1,
|
|
|
|
BUTTON2 = 2,
|
|
|
|
BUTTON3 = 3,
|
|
|
|
SELECTED = 100,
|
2014-02-06 04:48:26 +01:00
|
|
|
HOVER = 1000,
|
|
|
|
LEAVE = 1001,
|
2014-01-04 07:27:52 +01:00
|
|
|
},
|
2014-01-08 06:59:57 +01:00
|
|
|
item_flags = {
|
2014-02-22 04:22:13 +01:00
|
|
|
NONE = 999999 ,
|
|
|
|
DISABLED = 1 , -- Cannot be interacted with
|
|
|
|
URGENT = 2 , -- Need attention
|
|
|
|
SELECTED = 3 , -- Single item selected [[FOCUS]]
|
|
|
|
PRESSED = 4 , -- Mouse pressed
|
2014-03-13 06:14:52 +01:00
|
|
|
HOVERED = -1 , -- Mouse hover
|
2014-03-02 22:28:30 +01:00
|
|
|
CHANGED = 6 , -- The item changed, need attention
|
|
|
|
USED = 7 , -- Common flag
|
|
|
|
CHECKED = 8 , -- When checkbox isn't enough
|
|
|
|
ALTERNATE = 9 ,
|
|
|
|
HIGHLIGHT = 10 ,
|
|
|
|
HEADER = 11,
|
2014-02-09 06:54:59 +01:00
|
|
|
|
|
|
|
-- Implementation defined flags
|
|
|
|
USR1 = 101,
|
|
|
|
USR2 = 102,
|
|
|
|
USR3 = 103,
|
|
|
|
USR4 = 104,
|
|
|
|
USR5 = 105,
|
|
|
|
USR6 = 106,
|
|
|
|
USR7 = 107,
|
|
|
|
USR8 = 108,
|
|
|
|
USR9 = 109,
|
|
|
|
USR10 = 110,
|
2014-02-22 04:22:13 +01:00
|
|
|
},
|
2014-02-23 05:59:03 +01:00
|
|
|
colors_by_id = theme.colors_by_id
|
2014-01-04 07:27:52 +01:00
|
|
|
}
|
2013-05-11 21:02:42 +02:00
|
|
|
|
2016-03-06 09:30:30 +01:00
|
|
|
--TODO move to theme/init.lua
|
2014-02-23 05:59:03 +01:00
|
|
|
theme.register_color(module.item_flags.DISABLED , "disabled" , "disabled" , true )
|
|
|
|
theme.register_color(module.item_flags.URGENT , "urgent" , "urgent" , true )
|
|
|
|
theme.register_color(module.item_flags.SELECTED , "focus" , "focus" , true )
|
|
|
|
theme.register_color(module.item_flags.PRESSED , "pressed" , "pressed" , true )
|
|
|
|
theme.register_color(module.item_flags.HOVERED , "hover" , "hover" , true )
|
2014-03-02 22:28:30 +01:00
|
|
|
theme.register_color(module.item_flags.CHANGED , "changed" , "changed" , true )
|
2014-02-23 05:59:03 +01:00
|
|
|
theme.register_color(module.item_flags.USED , "used" , "used" , true )
|
|
|
|
theme.register_color(module.item_flags.CHECKED , "checked" , "checked" , true )
|
|
|
|
theme.register_color(module.item_flags.ALTERNATE , "alternate" , "alternate" , true )
|
|
|
|
theme.register_color(module.item_flags.HIGHLIGHT , "highlight" , "highlight" , true )
|
2014-02-22 04:22:13 +01:00
|
|
|
|
|
|
|
|
2013-07-02 00:18:25 +02:00
|
|
|
local function filter(data)
|
2016-06-24 06:44:43 +02:00
|
|
|
local max_items = data.max_items or 9999999
|
|
|
|
|
|
|
|
local fs = data.filter_string ~= "" and data.filter_string:lower() or nil
|
|
|
|
|
|
|
|
local start_at, visible_counter = data._start_at or -1, 0
|
|
|
|
|
2016-06-28 23:58:25 +02:00
|
|
|
local exit_soon = false
|
|
|
|
|
2016-06-24 06:44:43 +02:00
|
|
|
-- There is 2 factors to consider to display the item:
|
|
|
|
--
|
|
|
|
-- * Is the item within range
|
|
|
|
-- * Is the item matching the filter
|
|
|
|
--
|
|
|
|
for k,v in ipairs(data.items) do
|
2016-06-25 08:13:56 +02:00
|
|
|
-- Do not count widgets as item
|
|
|
|
if not v._private_data.is_widget then
|
|
|
|
v.widget.visible = (not fs or v.text and v.text:lower():find(fs) ~= nil)
|
|
|
|
and k >= start_at and visible_counter < max_items
|
|
|
|
|
|
|
|
if v.widget.visible then
|
|
|
|
visible_counter = visible_counter + 1
|
|
|
|
v.f_key = visible_counter
|
|
|
|
end
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2016-06-25 08:13:56 +02:00
|
|
|
-- Don't waste CPU
|
2016-06-28 23:58:25 +02:00
|
|
|
if visible_counter >= max_items then
|
|
|
|
if exit_soon then break end
|
|
|
|
|
|
|
|
exit_soon = true
|
|
|
|
end
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2013-07-18 06:03:19 +02:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2016-06-25 08:13:56 +02:00
|
|
|
local changed = data._internal.visible_item_count ~= visible_counter
|
|
|
|
|
2016-06-24 06:44:43 +02:00
|
|
|
data._internal.visible_item_count = visible_counter
|
2016-02-21 08:34:05 +01:00
|
|
|
|
2014-06-01 05:45:46 +02:00
|
|
|
-- Make sure to select an item
|
2016-06-24 06:44:43 +02:00
|
|
|
if data._current_item and not data._current_item.widget.visible then
|
2014-06-01 05:45:46 +02:00
|
|
|
local n = data.next_item
|
|
|
|
if n then
|
|
|
|
n.selected = true
|
|
|
|
end
|
|
|
|
end
|
2016-06-25 08:13:56 +02:00
|
|
|
|
|
|
|
if changed then
|
|
|
|
data:emit_signal("visible_item_count::changed", visible_counter)
|
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
|
2014-10-05 23:59:06 +02:00
|
|
|
-- Get the number of visible rows
|
|
|
|
local function get_visible_row_count(data)
|
2016-06-24 06:44:43 +02:00
|
|
|
if not data._internal.visible_item_count then
|
|
|
|
filter(data)
|
2014-10-05 23:59:06 +02:00
|
|
|
end
|
2016-06-24 06:44:43 +02:00
|
|
|
|
|
|
|
return data._internal.visible_item_count
|
2014-10-05 23:59:06 +02:00
|
|
|
end
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
------------------------------------KEYBOARD HANDLING-----------------------------------
|
|
|
|
local function activateKeyboard(data)
|
|
|
|
if not data then return end
|
2016-06-24 08:02:40 +02:00
|
|
|
|
2013-08-07 21:32:44 +02:00
|
|
|
if (not (data._internal.private_data.enable_keyboard == false)) and data.visible == true then
|
2013-05-11 21:02:42 +02:00
|
|
|
capi.keygrabber.run(function(mod, key, event)
|
2013-12-20 06:25:54 +01:00
|
|
|
for k,v in pairs(data._internal.filter_hooks or {}) do --TODO modkeys
|
2014-04-04 05:25:35 +02:00
|
|
|
if (k.key == "Mod4" or k.key == "Mod1") and (key == "End" or key == "Super_L" or key == "Alt_L") then
|
2016-06-24 08:02:40 +02:00
|
|
|
for _,v3 in ipairs(mod) do
|
|
|
|
for _,v4 in ipairs({"Mod4","Mod1"})do
|
2014-04-04 05:25:35 +02:00
|
|
|
if v3 == v4 and event == k.event then
|
2016-06-24 08:02:40 +02:00
|
|
|
local _,self = v(data,mod)
|
2014-04-04 05:25:35 +02:00
|
|
|
if self and type(self) == "table" then
|
|
|
|
data = self
|
|
|
|
end
|
2013-12-20 06:25:54 +01:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2013-12-20 06:25:54 +01:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2013-12-20 06:25:54 +01:00
|
|
|
if k.key == key and k.event == event then
|
|
|
|
local retval, self = v(data,mod)
|
|
|
|
if self and type(self) == "table" then
|
|
|
|
data = self
|
|
|
|
end
|
2014-03-23 05:14:25 +01:00
|
|
|
if retval == false then
|
|
|
|
data.visible = false
|
|
|
|
capi.keygrabber.stop()
|
|
|
|
end
|
2013-12-20 06:25:54 +01:00
|
|
|
return retval
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2013-12-20 06:25:54 +01:00
|
|
|
end
|
|
|
|
if event == "release" then
|
|
|
|
return true
|
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
|
2013-12-20 06:25:54 +01:00
|
|
|
if (key == 'Return') and data._current_item and data._current_item.button1 then
|
2014-04-04 05:25:35 +02:00
|
|
|
data._current_item.button1(data,data._current_item)
|
2016-02-21 08:34:05 +01:00
|
|
|
if data.sub_menu_on ~= module.event.BUTTON1 then
|
|
|
|
data.visible = false
|
|
|
|
end
|
2013-12-20 06:25:54 +01:00
|
|
|
elseif key == 'Escape' or (key == 'Tab' and data.filter_string == "") then
|
|
|
|
data.visible = false
|
|
|
|
capi.keygrabber.stop()
|
|
|
|
elseif (key == 'BackSpace') and data.filter_string ~= "" and data.filter == true then
|
|
|
|
data.filter_string = data.filter_string:sub(1,-2)
|
|
|
|
elseif data.filter == true and key:len() == 1 then
|
|
|
|
data.filter_string = data.filter_string .. key:lower()
|
|
|
|
else
|
|
|
|
data.visible = false
|
|
|
|
capi.keygrabber.stop()
|
|
|
|
end
|
|
|
|
return true
|
2013-05-11 21:02:42 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---------------------------------ITEM HANDLING----------------------------------
|
2013-07-04 07:20:46 +02:00
|
|
|
local function add_item(data,args)
|
2014-02-23 05:59:03 +01:00
|
|
|
local item = item_mod(data,args)
|
2014-01-12 07:27:46 +01:00
|
|
|
data._internal.setup_item(data,item,args)
|
2014-01-21 06:24:51 +01:00
|
|
|
if args.selected == true then
|
|
|
|
item.selected = true
|
|
|
|
end
|
2016-02-21 08:34:05 +01:00
|
|
|
|
|
|
|
-- Show sub-menu
|
|
|
|
if item._private_data.sub_menu_f or item._private_data.sub_menu_m then
|
|
|
|
if data.sub_menu_on == module.event.SELECTED then
|
|
|
|
item.widget:set_menu(
|
|
|
|
function()
|
|
|
|
local sub_menu = item._private_data.sub_menu_m
|
|
|
|
|
|
|
|
if not sub_menu and item._private_data.sub_menu_f then
|
|
|
|
sub_menu = item._private_data.sub_menu_f(item.widget)
|
|
|
|
end
|
|
|
|
|
|
|
|
if sub_menu and (item._private_data.sub_menu_f or sub_menu.rowcount > 0) then
|
|
|
|
sub_menu.arrow_type = module.arrow_type.NONE
|
|
|
|
sub_menu.parent_item = item
|
|
|
|
item._tmp_menu = sub_menu
|
|
|
|
data._tmp_menu = sub_menu
|
|
|
|
end
|
|
|
|
|
|
|
|
return sub_menu
|
|
|
|
end,
|
|
|
|
"mouse::enter"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-02 22:28:30 +01:00
|
|
|
item.index = data.rowcount
|
2014-03-05 06:12:48 +01:00
|
|
|
data:emit_signal("item::added",item)
|
2016-06-24 06:44:43 +02:00
|
|
|
|
|
|
|
-- Keep the filter up-to-date
|
|
|
|
if data.max_items and data.rowcount - (data._start_at or 0) > data.max_items then
|
|
|
|
--FIXME and data._internal.visible_item_count < data.max_items then
|
|
|
|
filter(data)
|
|
|
|
end
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
return item
|
|
|
|
end
|
|
|
|
|
2014-12-29 04:23:59 +01:00
|
|
|
local function add_items(data,items)
|
|
|
|
local ret = {}
|
|
|
|
for k,item in ipairs(items) do
|
|
|
|
ret[k] = data:add_item(item)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
|
2013-07-04 07:20:46 +02:00
|
|
|
local function add_widget(data,widget,args)
|
|
|
|
args = args or {}
|
|
|
|
data._internal.has_widget = true
|
|
|
|
widget._fit = widget.fit
|
2015-12-29 11:19:23 +01:00
|
|
|
widget.fit = function(self,context,width,height)
|
|
|
|
local w,h = widget._fit(self, context, width or 1, height or 1)
|
2013-07-04 07:20:46 +02:00
|
|
|
return args.width or w,args.height or h
|
|
|
|
end
|
|
|
|
|
2014-03-05 06:12:48 +01:00
|
|
|
local item,private_data = object({
|
2013-07-04 07:20:46 +02:00
|
|
|
private_data = {
|
|
|
|
widget = widget,
|
2016-06-25 08:13:56 +02:00
|
|
|
is_widget = true,
|
2013-07-04 07:20:46 +02:00
|
|
|
selected = false,
|
|
|
|
},
|
|
|
|
force_private = {
|
|
|
|
visible = true,
|
|
|
|
selected = true,
|
|
|
|
},
|
|
|
|
autogen_getmap = true,
|
|
|
|
autogen_setmap = true,
|
|
|
|
autogen_signals = true,
|
|
|
|
})
|
|
|
|
item._private_data = private_data
|
2014-03-05 06:12:48 +01:00
|
|
|
item._internal = {}
|
2013-07-23 08:12:19 +02:00
|
|
|
|
2013-07-04 07:20:46 +02:00
|
|
|
data._internal.widgets[#data._internal.widgets+1] = item
|
2014-03-26 05:21:42 +01:00
|
|
|
data._internal.items[#data._internal.items+1] = item
|
2014-03-12 05:31:50 +01:00
|
|
|
data:emit_signal("widget::added",item,widget)
|
2013-07-04 07:20:46 +02:00
|
|
|
end
|
|
|
|
|
2014-12-29 04:23:59 +01:00
|
|
|
local function add_widgets(data,widgets)
|
2016-06-24 08:02:40 +02:00
|
|
|
for _,item in ipairs(widgets) do
|
2014-12-29 04:23:59 +01:00
|
|
|
data:add_widget(item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-12 01:49:49 +01:00
|
|
|
local function add_prefix_widget(data,widget,args)
|
|
|
|
data:emit_signal("prefix_widget::added",widget,args)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function add_suffix_widget(data,widget,args)
|
|
|
|
data:emit_signal("suffix_widget::added",widget,args)
|
|
|
|
end
|
|
|
|
|
2014-10-05 23:59:06 +02:00
|
|
|
-- Sum all widgets height and width
|
|
|
|
local function get_widget_fit_sum(data)
|
|
|
|
local h,w = 0,0
|
2015-12-29 11:19:23 +01:00
|
|
|
-- TODO query this from the layout itself
|
2016-06-24 08:02:40 +02:00
|
|
|
for _,v in ipairs(data._internal.widgets) do
|
2015-12-29 12:21:13 +01:00
|
|
|
local fw,fh = v.widget:get_preferred_size()
|
2014-10-05 23:59:06 +02:00
|
|
|
w,h = w + fw,h + fh
|
|
|
|
end
|
|
|
|
return w,h
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_widget_fit_height_sum(data)
|
2015-12-29 11:19:23 +01:00
|
|
|
-- TODO query this from the layout itself
|
2016-06-24 08:02:40 +02:00
|
|
|
local _,h = get_widget_fit_sum(data)
|
2014-10-05 23:59:06 +02:00
|
|
|
return h
|
|
|
|
end
|
|
|
|
|
2013-07-04 07:20:46 +02:00
|
|
|
local function add_embeded_menu(data,menu)
|
2013-07-21 10:15:56 +02:00
|
|
|
add_widget(data,menu._internal.layout)
|
2013-12-31 22:13:11 +01:00
|
|
|
menu._embeded_parent = data
|
2013-07-04 07:20:46 +02:00
|
|
|
end
|
|
|
|
|
2014-12-30 23:27:57 +01:00
|
|
|
local function add_colors_namespace(data,namespace)
|
|
|
|
theme.add_colors_from_namespace(data,namespace)
|
|
|
|
end
|
|
|
|
|
2014-01-15 05:19:49 +01:00
|
|
|
local function add_key_binding(data,mod,key,func)
|
|
|
|
capi.root.keys(util.table.join(capi.root.keys(),aw_key(mod or {}, key, func and func() or function ()
|
|
|
|
data.visible = not data.visible
|
|
|
|
end)))
|
|
|
|
end
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
---------------------------------MENU HANDLING----------------------------------
|
|
|
|
local function new(args)
|
2016-06-24 08:02:40 +02:00
|
|
|
args = args or {}
|
2014-10-05 23:59:06 +02:00
|
|
|
local internal = args.internal or {}
|
2013-05-11 21:02:42 +02:00
|
|
|
if not internal.items then internal.items = {} end
|
2013-07-04 07:20:46 +02:00
|
|
|
if not internal.widgets then internal.widgets = {} end
|
2016-01-12 09:33:38 +01:00
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
-- All the magic in the universe
|
2014-03-05 06:12:48 +01:00
|
|
|
local data,private_data = object({
|
2013-05-11 21:02:42 +02:00
|
|
|
private_data = {
|
|
|
|
-- Default settings
|
|
|
|
bg = args.bg or beautiful.menu_bg_normal or beautiful.bg_normal or "#000000",
|
|
|
|
fg = args.fg or beautiful.menu_fg_normal or beautiful.fg_normal or "#ffffff",
|
2013-08-03 07:03:43 +02:00
|
|
|
bg_header = args.bg_header or beautiful.menu_bg_header or beautiful.fg_normal,
|
2014-02-03 05:01:31 +01:00
|
|
|
bg_prefix = args.bg_prefix or nil,
|
2013-05-11 21:02:42 +02:00
|
|
|
border_color = args.border_color or beautiful.menu_border_color or beautiful.border_color or "#333333",
|
|
|
|
border_width = args.border_width or beautiful.menu_border_width or beautiful.border_width or 3,
|
2014-01-05 00:51:07 +01:00
|
|
|
separator_color = args.separator_color or beautiful.menu_separator_color or args.border_color or beautiful.menu_border_color or beautiful.border_color or "#333333",
|
2013-05-11 21:02:42 +02:00
|
|
|
item_height = args.item_height or beautiful.menu_height or 30,
|
2013-05-27 02:49:51 +02:00
|
|
|
item_width = args.item_width or nil,
|
2014-07-28 01:20:04 +02:00
|
|
|
width = args.width or args.menu_width or beautiful.menu_width or 130,
|
|
|
|
default_width = args.width or args.menu_width or beautiful.menu_width or 130,
|
2013-05-27 02:49:51 +02:00
|
|
|
icon_size = args.icon_size or nil,
|
2013-05-11 21:02:42 +02:00
|
|
|
auto_resize = args.auto_resize or true,
|
|
|
|
parent_geometry = args.parent or nil,
|
|
|
|
arrow_type = args.arrow_type or beautiful.menu_arrow_type or module.arrow_type.PRETTY,
|
|
|
|
visible = args.visible or false,
|
|
|
|
direction = args.direction or "top",
|
|
|
|
has_changed = false,
|
|
|
|
row = args.row or nil,
|
|
|
|
column = args.column or nil,
|
|
|
|
layout = args.layout or nil,
|
|
|
|
screen = args.screen or nil,
|
|
|
|
style = args.style or nil,
|
2016-02-11 07:36:50 +01:00
|
|
|
item_style = args.item_style or beautiful.menu_default_item_style or require("radical.item.style.basic"),
|
2014-04-09 05:38:31 +02:00
|
|
|
item_layout = args.item_layout or nil,
|
2013-07-18 06:03:19 +02:00
|
|
|
filter = args.filter ~= false,
|
2013-05-11 21:02:42 +02:00
|
|
|
show_filter = args.show_filter or false,
|
|
|
|
filter_string = args.filter_string or "",
|
|
|
|
suffix_widget = args.suffix_widget or nil,
|
|
|
|
prefix_widget = args.prefix_widget or nil,
|
|
|
|
fkeys_prefix = args.fkeys_prefix or false,
|
2016-01-12 09:33:38 +01:00
|
|
|
overlay_draw = args.overlay_draw or nil,
|
2014-03-13 03:56:56 +01:00
|
|
|
filter_underlay = args.filter_underlay or nil,
|
2013-07-07 06:39:17 +02:00
|
|
|
filter_prefix = args.filter_prefix or "Filter:",
|
2013-08-07 21:32:44 +02:00
|
|
|
enable_keyboard = (args.enable_keyboard ~= false),
|
2013-07-07 06:39:17 +02:00
|
|
|
max_items = args.max_items or nil,
|
2013-10-01 04:19:38 +02:00
|
|
|
disable_markup = args.disable_markup or false,
|
2013-08-07 06:35:49 +02:00
|
|
|
x = args.x or 0,
|
|
|
|
y = args.y or 0,
|
2016-03-06 09:30:30 +01:00
|
|
|
shape = args.shape or nil,
|
|
|
|
item_shape = args.item_shape,
|
2014-02-06 04:48:26 +01:00
|
|
|
sub_menu_on = args.sub_menu_on or module.event.SELECTED,
|
|
|
|
select_on = args.select_on or module.event.HOVER,
|
2014-02-18 04:57:04 +01:00
|
|
|
opacity = args.opacity or beautiful.menu_opacity or 1,
|
2016-02-21 08:34:05 +01:00
|
|
|
spacing = args.spacing or nil,
|
|
|
|
default_margins = args.default_margins or beautiful.menu_default_margins or {},
|
2014-12-25 07:10:55 +01:00
|
|
|
icon_per_state = args.icon_per_state or false,
|
2014-10-18 05:52:26 +02:00
|
|
|
default_item_margins = args.default_item_margins or {},
|
|
|
|
icon_transformation = args.icon_transformation or nil,
|
2014-03-13 03:56:56 +01:00
|
|
|
filter_underlay_style = args.filter_underlay_style or nil,
|
|
|
|
filter_underlay_color = args.filter_underlay_color,
|
|
|
|
filter_placeholder = args.filter_placeholder or "",
|
2014-12-25 07:10:55 +01:00
|
|
|
disable_submenu_icon = args.disable_submenu_icon or false,
|
2016-02-11 07:36:50 +01:00
|
|
|
item_border_color = args.item_border_color or beautiful.menu_item_border_color
|
|
|
|
or args.border_color or beautiful.menu_border_color or beautiful.border_color or nil,
|
|
|
|
item_border_width = args.item_border_width or beautiful.menu_item_border_width or nil,
|
2013-05-11 21:02:42 +02:00
|
|
|
},
|
|
|
|
force_private = {
|
|
|
|
parent = true,
|
|
|
|
visible = true,
|
|
|
|
},
|
|
|
|
always_handle = {
|
|
|
|
width = true,
|
|
|
|
height = true,
|
|
|
|
},
|
|
|
|
autogen_getmap = true,
|
|
|
|
autogen_setmap = true,
|
|
|
|
autogen_signals = true,
|
|
|
|
})
|
2014-03-05 06:12:48 +01:00
|
|
|
internal.private_data = private_data
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2014-12-30 23:27:57 +01:00
|
|
|
-- Methods
|
2014-01-15 05:19:49 +01:00
|
|
|
data.add_item,data.add_widget,data.add_embeded_menu,data._internal,data.add_key_binding = add_item,add_widget,add_embeded_menu,internal,add_key_binding
|
2014-12-29 04:23:59 +01:00
|
|
|
data.add_prefix_widget,data.add_suffix_widget,data.add_items,data.add_widgets=add_prefix_widget,add_suffix_widget,add_items,add_widgets
|
2014-12-30 23:27:57 +01:00
|
|
|
data.add_colors_namespace = add_colors_namespace
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2014-12-30 23:27:57 +01:00
|
|
|
-- Load colors
|
2014-02-23 05:59:03 +01:00
|
|
|
theme.setup_colors(data,args)
|
2014-03-05 06:12:48 +01:00
|
|
|
|
|
|
|
-- Getters
|
2014-10-05 23:59:06 +02:00
|
|
|
data.get_is_menu = function(_) return true end
|
2016-06-24 08:02:40 +02:00
|
|
|
data.get_margin = function(_) return {top=0,bottom=0,right=0,left=0} end
|
2014-10-05 23:59:06 +02:00
|
|
|
data.get_items = function(_) return internal.items end
|
|
|
|
data.get_rowcount = function(_) return #internal.items end
|
|
|
|
data.get_visible_row_count = get_visible_row_count
|
2016-06-24 06:44:43 +02:00
|
|
|
data.get_widget_fit_height_sum = get_widget_fit_height_sum --TODO remove
|
2014-03-05 06:12:48 +01:00
|
|
|
|
|
|
|
-- Setters
|
|
|
|
data.set_auto_resize = function(_,val) private_data[""] = val end
|
|
|
|
data.set_parent_geometry = function(_,value)
|
2016-02-21 08:34:05 +01:00
|
|
|
private_data.parent_geometry = value --TODO delete
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
|
2014-03-05 06:12:48 +01:00
|
|
|
data.set_visible = function(_,value)
|
2013-07-04 07:51:01 +02:00
|
|
|
private_data.visible = value
|
2016-02-21 08:34:05 +01:00
|
|
|
if data._tmp_menu and data._current_item then
|
2013-07-18 06:03:19 +02:00
|
|
|
data._current_item._tmp_menu = nil
|
2014-03-02 22:28:30 +01:00
|
|
|
data.item_style(data._current_item,{})
|
2013-07-04 07:20:46 +02:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
if internal.has_changed and data.style then
|
2016-03-15 08:55:20 +01:00
|
|
|
data.style(data)
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2016-02-21 08:34:05 +01:00
|
|
|
if internal.set_position then
|
2013-05-11 21:02:42 +02:00
|
|
|
internal.set_position(data)
|
2016-02-21 08:34:05 +01:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
if internal.set_visible then
|
|
|
|
internal:set_visible(value)
|
|
|
|
end
|
|
|
|
if value and not capi.keygrabber.isrunning() then
|
|
|
|
activateKeyboard(data)
|
2013-12-19 06:22:49 +01:00
|
|
|
elseif data.parent_geometry and not data.parent_geometry.is_menu and data.enable_keyboard then
|
2013-05-11 21:02:42 +02:00
|
|
|
capi.keygrabber.stop()
|
|
|
|
end
|
2016-02-21 08:34:05 +01:00
|
|
|
|
|
|
|
-- Hide the sub menus when hiding
|
|
|
|
if data._tmp_menu and not value then
|
|
|
|
data._tmp_menu.visible = false
|
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2014-03-05 06:12:48 +01:00
|
|
|
|
2016-06-24 08:02:40 +02:00
|
|
|
data.add_colors_group = function(_,section)
|
2014-12-31 06:30:52 +01:00
|
|
|
theme.add_section(data,section,args)
|
|
|
|
end
|
|
|
|
|
2014-03-05 06:12:48 +01:00
|
|
|
data.set_layout = function(_,value)
|
2013-05-11 21:02:42 +02:00
|
|
|
if value then
|
2016-02-21 08:34:05 +01:00
|
|
|
local f = value.setup_key_hooks or common.setup_key_hooks
|
|
|
|
f(value, data)
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
private_data.layout = value
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set_map.auto_resize = function(value)
|
|
|
|
-- for k,v in ipairs(internal.items) do
|
|
|
|
-- TODO check all items size, ajustthe fit and global width
|
|
|
|
-- end
|
|
|
|
-- end
|
|
|
|
|
2014-03-05 06:12:48 +01:00
|
|
|
data.get_current_index = function(_)
|
2013-05-11 21:02:42 +02:00
|
|
|
if data._current_item then
|
|
|
|
for k,v in ipairs(internal.items) do --rows
|
2014-03-22 22:35:43 +01:00
|
|
|
if data._current_item == v then
|
|
|
|
return k
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-05 06:12:48 +01:00
|
|
|
data.get_previous_item = function(_)
|
2014-01-21 06:24:51 +01:00
|
|
|
local candidate,idx = internal.items[(data.current_index or 0)-1],(data.current_index or 0)-1
|
2016-06-24 06:44:43 +02:00
|
|
|
while candidate and (candidate.widget.visible == false) and idx > 0 do
|
2014-01-21 06:24:51 +01:00
|
|
|
candidate,idx = internal.items[idx - 1],idx-1
|
|
|
|
end
|
2014-03-22 22:35:43 +01:00
|
|
|
return (candidate or internal.items[data.rowcount])
|
2014-01-21 06:24:51 +01:00
|
|
|
end
|
2014-03-05 06:12:48 +01:00
|
|
|
data.get_next_item = function(_)
|
2014-01-21 06:24:51 +01:00
|
|
|
local candidate,idx = internal.items[(data.current_index or 0)+1],(data.current_index or 0)+1
|
2016-06-24 06:44:43 +02:00
|
|
|
while candidate and (candidate.widget.visible == false) and idx <= data.rowcount do
|
2014-01-21 06:24:51 +01:00
|
|
|
candidate,idx = internal.items[idx + 1],idx+1
|
|
|
|
end
|
2014-03-23 05:14:25 +01:00
|
|
|
return (candidate or internal.items[1])
|
2014-01-21 06:24:51 +01:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
--Repaint when appearance properties change
|
2016-06-24 08:02:40 +02:00
|
|
|
for _,v in ipairs({"bg","fg","border_color","border_width","item_height","width","arrow_type"}) do
|
2013-05-11 21:02:42 +02:00
|
|
|
data:connect_signal(v.."::changed",function()
|
2016-06-24 08:02:40 +02:00
|
|
|
if not (data.visible and data.style) then
|
2013-05-11 21:02:42 +02:00
|
|
|
data.has_changed = true
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2016-06-24 06:44:43 +02:00
|
|
|
-- Make sure the filter doesn't get outdated
|
|
|
|
data:connect_signal("max_items::changed" , filter)
|
|
|
|
data:connect_signal("filter_string::changed", filter)
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
function data:add_key_hook(mod, key, event, func)
|
|
|
|
if key and event and func then
|
|
|
|
internal.filter_hooks = internal.filter_hooks or {}
|
|
|
|
internal.filter_hooks[{key = key, event = event, mod = mod}] = func
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-15 05:39:24 +01:00
|
|
|
function data:remove_key_hook(key) --TODO broken?
|
2014-01-05 07:04:40 +01:00
|
|
|
for k,v in pairs(internal.filter_hooks or {}) do
|
2013-07-05 06:57:30 +02:00
|
|
|
if k.key == key then
|
|
|
|
internal.filter_hooks[k] = nil
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-16 21:18:55 +02:00
|
|
|
function data:clear()
|
|
|
|
internal.items = {}
|
|
|
|
data:emit_signal("clear::menu")
|
|
|
|
end
|
|
|
|
|
2016-06-24 06:44:43 +02:00
|
|
|
function data:swap(item1,item2) --TODO can item.index be used?
|
2014-02-08 06:52:42 +01:00
|
|
|
if not item1 or not item2 then return end
|
2014-02-05 07:03:25 +01:00
|
|
|
if not item1 or not item2 and item1 ~= item2 then return end
|
|
|
|
local idx1,idx2
|
|
|
|
for k,v in ipairs(internal.items) do --rows
|
2016-06-24 06:44:43 +02:00
|
|
|
if item2 == v then
|
|
|
|
idx2 = k
|
|
|
|
end
|
|
|
|
|
|
|
|
if item1 == v then
|
|
|
|
idx1 = k
|
2014-02-05 07:03:25 +01:00
|
|
|
end
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2014-02-05 07:03:25 +01:00
|
|
|
if idx1 and idx2 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if idx1 and idx2 then
|
|
|
|
internal.items[idx1],internal.items[idx2] = internal.items[idx2],internal.items[idx1]
|
2014-03-02 22:28:30 +01:00
|
|
|
item1.index,item2.index = idx2,idx1
|
2014-02-05 07:03:25 +01:00
|
|
|
data:emit_signal("item::swapped",item1,item2,idx1,idx2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-06 03:43:06 +01:00
|
|
|
function data:move(item,idx)
|
2014-02-08 06:52:42 +01:00
|
|
|
if not item or not idx then return end
|
2014-02-06 03:43:06 +01:00
|
|
|
local idx1 = nil
|
|
|
|
for k,v in ipairs(internal.items) do --rows
|
2014-03-22 22:35:43 +01:00
|
|
|
if item == v then
|
2014-02-06 03:43:06 +01:00
|
|
|
idx1 = k
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if idx1 then
|
|
|
|
-- if idx < idx1 then
|
|
|
|
-- idx = idx + 1
|
|
|
|
-- end
|
|
|
|
if idx1 > #internal.items + 1 then
|
|
|
|
idx1 = #internal.items + 1
|
|
|
|
end
|
|
|
|
if idx ~= idx1 then
|
|
|
|
table.insert(internal.items,idx1,table.remove(internal.items,idx))
|
2014-03-02 22:28:30 +01:00
|
|
|
item.index = idx
|
2014-02-06 03:43:06 +01:00
|
|
|
data:emit_signal("item::moved",item,idx,idx1)
|
2014-03-02 22:28:30 +01:00
|
|
|
for i=idx,idx1 do
|
2014-03-22 22:35:43 +01:00
|
|
|
internal.items[i].index = i
|
2014-03-02 22:28:30 +01:00
|
|
|
end
|
2014-02-06 03:43:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function data:remove(item)
|
2014-02-08 06:52:42 +01:00
|
|
|
if not item then return end
|
2014-02-06 03:43:06 +01:00
|
|
|
local idx1 = nil
|
|
|
|
for k,v in ipairs(internal.items) do --rows
|
2014-03-22 22:35:43 +01:00
|
|
|
if item == v then
|
2014-02-06 03:43:06 +01:00
|
|
|
idx1 = k
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if idx1 then
|
|
|
|
table.remove(internal.items,idx1)
|
|
|
|
data:emit_signal("item::removed",item,idx1)
|
2014-03-02 22:28:30 +01:00
|
|
|
for i=idx1,#internal.items do
|
2014-03-22 22:35:43 +01:00
|
|
|
internal.items[i].index = i
|
2014-03-02 22:28:30 +01:00
|
|
|
end
|
2014-02-06 03:43:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-07 05:47:24 +01:00
|
|
|
function data:append(item)
|
2014-02-08 06:52:42 +01:00
|
|
|
if not item then return end
|
2014-03-26 05:21:42 +01:00
|
|
|
internal.items[#internal.items + 1] = item
|
2014-02-07 05:47:24 +01:00
|
|
|
data:emit_signal("item::appended",item)
|
|
|
|
end
|
|
|
|
|
2013-07-08 00:18:55 +02:00
|
|
|
function data:scroll_up()
|
|
|
|
if data.max_items ~= nil and data.rowcount >= data.max_items and (data._start_at or 1) > 1 then
|
2014-03-22 04:20:50 +01:00
|
|
|
local current_item = data._current_item
|
|
|
|
if current_item then
|
2014-03-22 05:11:03 +01:00
|
|
|
current_item:set_selected(false,true)
|
2014-03-22 04:20:50 +01:00
|
|
|
end
|
2013-07-08 00:18:55 +02:00
|
|
|
data._start_at = (data._start_at or 1) - 1
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2013-08-09 08:07:27 +02:00
|
|
|
filter(data)
|
2016-02-11 07:36:50 +01:00
|
|
|
|
2013-07-08 00:18:55 +02:00
|
|
|
end
|
|
|
|
end
|
2013-12-20 06:25:54 +01:00
|
|
|
|
2013-07-08 00:18:55 +02:00
|
|
|
function data:scroll_down()
|
|
|
|
if data.max_items ~= nil and data.rowcount >= data.max_items and (data._start_at or 1)+data.max_items <= data.rowcount then
|
2014-03-22 04:20:50 +01:00
|
|
|
local current_item = data._current_item
|
|
|
|
if current_item then
|
2014-03-22 05:11:03 +01:00
|
|
|
current_item:set_selected(false,true)
|
2014-03-22 04:20:50 +01:00
|
|
|
end
|
2013-07-08 00:18:55 +02:00
|
|
|
data._start_at = (data._start_at or 1) + 1
|
2016-06-24 06:44:43 +02:00
|
|
|
|
2013-08-09 08:07:27 +02:00
|
|
|
filter(data)
|
2016-02-11 07:36:50 +01:00
|
|
|
|
2013-07-08 00:18:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-03 00:04:47 +02:00
|
|
|
function data:hide()
|
|
|
|
data.visible = false
|
2016-02-21 08:34:05 +01:00
|
|
|
|
|
|
|
if data.parent_geometry and data.parent_geometry.parent_menu then
|
|
|
|
local parent = data.parent_geometry.parent_menu
|
2014-08-03 00:04:47 +02:00
|
|
|
while parent do
|
|
|
|
parent.visible = false
|
|
|
|
parent = parent.parent_geometry and parent.parent_geometry.is_menu and parent.parent_geometry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
return data
|
|
|
|
end
|
|
|
|
return setmetatable(module, { __call = function(_, ...) return new(...) end })
|
|
|
|
-- kate: space-indent on; indent-width 2; replace-tabs on;
|