Debian Codegraph @

freedesktop/menu.lua

raw symbols SCIP
1
2 --[[
3
4 Awesome-Freedesktop
5 Freedesktop.org compliant desktop entries and menu
6
7 Menu section
8
9 Licensed under GNU General Public License v2
10 * (c) 2016, Luke Bonham
11 * (c) 2014, Harvey Mittens
12
13 --]]
14
15 local Gio = require("lgi").Gio
16 local awful_menu = require("awful.menu")
17 local menu_gen = require("menubar.menu_gen")
18 local menu_utils = require("menubar.utils")
19
20 local io, pairs, string, table, os = io, pairs, string, table, os
21
22 -- Expecting a wm_name of awesome omits too many applications and tools
23 menu_utils.wm_name = ""
24
25 -- Menu
26 -- freedesktop.menu
27 local menu = {}
28
29 -- Check if a path is a directory.
30 -- @tparam string path The directory path
31 -- @treturn boolean True if path exists and is a directory
32 function menu.is_dir(path)
33 return Gio.File.new_for_path(path):query_file_type({}) == "DIRECTORY"
34 end
35
36 -- Remove non existent paths in order to avoid issues
37 local existent_paths = {}
38 for k,v in pairs(menu_gen.all_menu_dirs) do
39 if menu.is_dir(v) then
40 table.insert(existent_paths, v)
41 end
42 end
43 menu_gen.all_menu_dirs = existent_paths
44
45 -- Determines whether an table includes a certain element
46 -- @param tab a given table
47 -- @param val the element to search for
48 -- @return true if the given string is found within the search table; otherwise, false if not
49 function menu.has_value (tab, val)
50 for index, value in pairs(tab) do
51 if val:find(value) then
52 return true
53 end
54 end
55 return false
56 end
57
58 -- Use MenuBar parsing utils to build a menu for Awesome
59 -- @return awful.menu
60 function menu.build(args)
61 local args = args or {}
62 local before = args.before or {}
63 local after = args.after or {}
64 local skip_items = args.skip_items or {}
65 local sub_menu = args.sub_menu or false
66
67 local result = {}
68 local _menu = awful_menu({ items = before })
69
70 menu_gen.generate(function(entries)
71 -- Add category icons
72 for k, v in pairs(menu_gen.all_categories) do
73 table.insert(result, { k, {}, v.icon })
74 end
75
76 -- Get items table
77 for k, v in pairs(entries) do
78 for _, cat in pairs(result) do
79 if cat[1] == v.category then
80 if not menu.has_value(skip_items, v.name) then
81 table.insert(cat[2], { v.name, v.cmdline, v.icon })
82 end
83 break
84 end
85 end
86 end
87
88 -- Cleanup things a bit
89 for i = #result, 1, -1 do
90 local v = result[i]
91 if #v[2] == 0 then
92 -- Remove unused categories
93 table.remove(result, i)
94 else
95 --Sort entries alphabetically (by name)
96 table.sort(v[2], function (a, b) return string.byte(a[1]) < string.byte(b[1]) end)
97 -- Replace category name with nice name
98 v[1] = menu_gen.all_categories[v[1]].name
99 end
100 end
101
102 -- Sort categories alphabetically also
103 table.sort(result, function(a, b) return string.byte(a[1]) < string.byte(b[1]) end)
104
105 -- Add menu item to hold the generated menu
106 if sub_menu then
107 result = {{sub_menu, result}}
108 end
109
110 -- Add items to menu
111 for _, v in pairs(result) do _menu:add(v) end
112 for _, v in pairs(after) do _menu:add(v) end
113 end)
114
115 -- Hold the menu in the module
116 menu.menu = _menu
117
118 return _menu
119 end
120
121 return menu