| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
local awful = require("awful") |
| 15 |
local theme = require("beautiful") |
| 16 |
local utils = require("menubar.utils") |
| 17 |
local wibox = require("wibox") |
| 18 |
|
| 19 |
local io = io |
| 20 |
local ipairs = ipairs |
| 21 |
local mouse = mouse |
| 22 |
local os = os |
| 23 |
local string = string |
| 24 |
local screen = screen |
| 25 |
local table = table |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
local desktop = { |
| 30 |
|
| 31 |
baseicons = { |
| 32 |
[1] = { |
| 33 |
label = "This PC", |
| 34 |
icon = "computer", |
| 35 |
onclick = "computer://" |
| 36 |
}, |
| 37 |
[2] = { |
| 38 |
label = "Home", |
| 39 |
icon = "user-home", |
| 40 |
onclick = os.getenv("HOME") |
| 41 |
}, |
| 42 |
[3] = { |
| 43 |
label = "Trash", |
| 44 |
icon = "user-trash", |
| 45 |
onclick = "trash://" |
| 46 |
} |
| 47 |
}, |
| 48 |
|
| 49 |
iconsize = { width = 48, height = 48 }, |
| 50 |
labelsize = { width = 140, height = 20 }, |
| 51 |
margin = { x = 20, y = 20 }, |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
local mime_types = {} |
| 56 |
|
| 57 |
|
| 58 |
desktop.current_pos = {} |
| 59 |
|
| 60 |
|
| 61 |
local function pipelines(...) |
| 62 |
local f = assert(io.popen(...)) |
| 63 |
return function () |
| 64 |
local data = f:read() |
| 65 |
if data == nil then f:close() end |
| 66 |
return data |
| 67 |
end |
| 68 |
end |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
function desktop.add_single_icon(args, label, icon, onclick) |
| 76 |
local s = args.screen |
| 77 |
local dcp = desktop.current_pos |
| 78 |
|
| 79 |
|
| 80 |
if not dcp[s] then |
| 81 |
dcp[s] = { x = (screen[s].geometry.x + args.iconsize.width + args.margin.x), y = screen[s].geometry.y + 20 + args.margin.y } |
| 82 |
end |
| 83 |
|
| 84 |
local tot_height = (icon and args.iconsize.height or 0) + (label and args.labelsize.height or 0) |
| 85 |
if tot_height == 0 then return end |
| 86 |
|
| 87 |
if dcp[s].y + tot_height > screen[s].geometry.y + screen[s].geometry.height - 20 - args.margin.y then |
| 88 |
dcp[s].x = dcp[s].x + args.labelsize.width + args.iconsize.width + args.margin.x |
| 89 |
dcp[s].y = 20 + args.margin.y |
| 90 |
end |
| 91 |
|
| 92 |
local common = { screen = s, bg = "#00000000", visible = true, type = "desktop" } |
| 93 |
|
| 94 |
|
| 95 |
if icon then |
| 96 |
common.width = args.iconsize.width |
| 97 |
common.height = args.iconsize.height |
| 98 |
common.x = dcp[s].x |
| 99 |
common.y = dcp[s].y |
| 100 |
|
| 101 |
icon = wibox.widget { |
| 102 |
image = icon, |
| 103 |
resize = false, |
| 104 |
widget = wibox.widget.imagebox |
| 105 |
} |
| 106 |
|
| 107 |
icon:buttons(awful.button({ }, 1, nil, onclick)) |
| 108 |
|
| 109 |
icon_container = wibox(common) |
| 110 |
icon_container:set_widget(icon) |
| 111 |
|
| 112 |
dcp[s].y = dcp[s].y + args.iconsize.height + 5 |
| 113 |
end |
| 114 |
|
| 115 |
|
| 116 |
if label then |
| 117 |
common.width = args.labelsize.width |
| 118 |
common.height = args.labelsize.height |
| 119 |
common.x = dcp[s].x - (args.labelsize.width/2) + args.iconsize.width/2 |
| 120 |
common.y = dcp[s].y |
| 121 |
|
| 122 |
caption = wibox.widget { |
| 123 |
text = label, |
| 124 |
align = "center", |
| 125 |
forced_width = common.width, |
| 126 |
forced_height = common.height, |
| 127 |
ellipsize = "middle", |
| 128 |
widget = wibox.widget.textbox |
| 129 |
} |
| 130 |
|
| 131 |
caption:buttons(awful.button({ }, 1, onclick)) |
| 132 |
caption_container = wibox(common) |
| 133 |
caption_container:set_widget(caption) |
| 134 |
end |
| 135 |
|
| 136 |
dcp[s].y = dcp[s].y + args.labelsize.height + args.margin.y |
| 137 |
|
| 138 |
desktop.current_pos = dcp |
| 139 |
|
| 140 |
return dcp |
| 141 |
end |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
function desktop.add_base_icons(args) |
| 146 |
for _,base in ipairs(args.baseicons) do |
| 147 |
desktop.add_single_icon(args, base.label, utils.lookup_icon(base.icon), function() |
| 148 |
awful.spawn(string.format("%s '%s'", args.open_with, base.onclick)) |
| 149 |
end) |
| 150 |
end |
| 151 |
end |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
function desktop.lookup_file_icon(filename) |
| 157 |
|
| 158 |
if #mime_types == 0 then |
| 159 |
for line in io.lines("/etc/mime.types") do |
| 160 |
if not line:find("^#") then |
| 161 |
local parsed = {} |
| 162 |
for w in line:gmatch("[^%s]+") do |
| 163 |
table.insert(parsed, w) |
| 164 |
end |
| 165 |
if #parsed > 1 then |
| 166 |
for i = 2, #parsed do |
| 167 |
mime_types[parsed[i]] = parsed[1]:gsub("/", "-") |
| 168 |
end |
| 169 |
end |
| 170 |
end |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
|
| 175 |
local extension = filename:match("%a+$") |
| 176 |
local mime = mime_types[extension] or "" |
| 177 |
local mime_family = mime:match("^%a+") or "" |
| 178 |
|
| 179 |
local possible_filenames = { |
| 180 |
mime, "gnome-mime-" .. mime, |
| 181 |
mime_family, "gnome-mime-" .. mime_family, |
| 182 |
extension |
| 183 |
} |
| 184 |
|
| 185 |
for i, filename in ipairs(possible_filenames) do |
| 186 |
local icon = utils.lookup_icon(filename) |
| 187 |
if icon then return icon end |
| 188 |
end |
| 189 |
|
| 190 |
|
| 191 |
return utils.lookup_icon("text-x-generic") |
| 192 |
end |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
function desktop.parse_dirs_and_files(dir) |
| 198 |
local files = {} |
| 199 |
local paths = pipelines('find '..dir..' -maxdepth 1 -type d |sort|tail -n +1') |
| 200 |
for path in paths do |
| 201 |
if path:match("[^/]+$") then |
| 202 |
local file = {} |
| 203 |
file.filename = path:match("[^/]+$") |
| 204 |
file.path = path |
| 205 |
file.show = true |
| 206 |
file.icon = utils.lookup_icon("folder") |
| 207 |
table.insert(files, file) |
| 208 |
end |
| 209 |
end |
| 210 |
local paths = pipelines('find '..dir..' -maxdepth 1 -type f') |
| 211 |
for path in paths do |
| 212 |
if not path:find("%.desktop$") then |
| 213 |
local file = {} |
| 214 |
file.filename = path:match("[^/]+$") |
| 215 |
file.path = path |
| 216 |
file.show = true |
| 217 |
file.icon = desktop.lookup_file_icon(file.filename) |
| 218 |
table.insert(files, file) |
| 219 |
end |
| 220 |
end |
| 221 |
return files |
| 222 |
end |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
function desktop.add_dirs_and_files_icons(args) |
| 227 |
for _, file in ipairs(desktop.parse_dirs_and_files(args.dir)) do |
| 228 |
if file.show then |
| 229 |
local label = args.showlabels and file.filename or nil |
| 230 |
local onclick = function () awful.spawn(string.format("%s '%s'", args.open_with, file.path)) end |
| 231 |
desktop.add_single_icon(args, label, file.icon, onclick) |
| 232 |
end |
| 233 |
end |
| 234 |
end |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
function desktop.add_icons(args) |
| 239 |
args = args or {} |
| 240 |
args.screen = args.screen or mouse.screen |
| 241 |
args.dir = args.dir or os.getenv("HOME") .. "/Desktop" |
| 242 |
args.showlabels = args.showlabel or true |
| 243 |
args.open_with = args.open_with or "xdg_open" |
| 244 |
args.baseicons = args.baseicons or desktop.baseicons |
| 245 |
args.iconsize = args.iconsize or desktop.iconsize |
| 246 |
args.labelsize = args.labelsize or desktop.labelsize |
| 247 |
args.margin = args.margin or desktop.margin |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if not theme.icon_theme then |
| 252 |
theme.icon_theme = args.icon_theme or "Adwaita" |
| 253 |
end |
| 254 |
|
| 255 |
desktop.add_base_icons(args) |
| 256 |
desktop.add_dirs_and_files_icons(args) |
| 257 |
end |
| 258 |
|
| 259 |
return desktop |