icons: move icon lookup in a seperate module

This commit is contained in:
Vincent Bernat 2012-07-15 22:22:38 +02:00
parent 52090d90ea
commit 335b80262d
8 changed files with 104 additions and 26 deletions

View file

@ -6,6 +6,10 @@ local tonumber = tonumber
local string = string
local os = os
-- A bit odd, but...
require("lib/icons")
local icons = package.loaded["vbe/icons"]
module("vbe/brightness")
local nid = nil
@ -15,7 +19,8 @@ local function change(what)
if not out then return end
out = tonumber(out)
local icon = "/usr/share/icons/HighContrast/32x32/status/display-brightness.png"
local icon = icons.lookup({name = "display-brightness",
type = "status"})
nid = naughty.notify({ text = string.format("%3d %%", out),
icon = icon,

63
lib/icons.lua Normal file
View file

@ -0,0 +1,63 @@
-- Lookup function for icons
local paths = {
"/usr/share/icons/gnome-wine",
"/usr/share/icons/gnome",
"/usr/share/icons/hicolor",
"/usr/share/icons/HighContrast",
"/usr/share/fvwm-crystal/fvwm/icons/Default",
}
local sizes = {
'32x32',
'24x24',
'22x22',
'16x16',
}
local types = {
'apps',
'actions',
'devices',
'status',
}
local formats = {
".png",
".xpm"
}
local assert = assert
local type = type
local pairs = pairs
local awful = require("awful")
module("vbe/icons")
-- Lookup for an icon. Return full path.
function lookup(arg)
local inames = assert(arg.name)
local isizes = arg.size or sizes
local itypes = arg.type or types
local ipaths = paths
local iformats = formats
if type(isizes) ~= "table" then isizes = { isizes } end
if type(itypes) ~= "table" then itypes = { itypes } end
if type(inames) ~= "table" then inames = { inames } end
for _, path in pairs(ipaths) do
for _, size in pairs(isizes) do
for _, t in pairs(itypes) do
for _, name in pairs(inames) do
if name then
for _, name in pairs({name, name:lower()}) do
for _, ext in pairs(iformats) do
local icon = path .. "/" .. size .. "/" .. t .. "/" .. name .. ext
if awful.util.file_readable(icon) then
return icon
end
end
end
end
end
end
end
end
end

View file

@ -6,6 +6,10 @@ local tonumber = tonumber
local string = string
local os = os
-- A bit odd, but...
require("lib/icons")
local icons = package.loaded["vbe/icons"]
module("vbe/volume")
local volid = nil
@ -25,7 +29,8 @@ local function change(what)
elseif vol < 60 then
icon = "medium"
end
icon = "/usr/share/icons/gnome/32x32/status/audio-volume-" .. icon .. ".png"
icon = icons.lookup({name = "audio-volume-" .. icon,
type = "status"})
volid = naughty.notify({ text = string.format("%3d %%", vol),
icon = icon,

View file

@ -1,5 +1,7 @@
-- Keyboard configuration with kbdd
local icons = loadrc("icons", "vbe/icons")
-- Global configuration
if config.hostname == "guybrush" then
os.execute("setxkbmap us,fr '' compose:rctrl ctrl:nocaps")
@ -38,7 +40,8 @@ dbus.add_signal("ru.gentoo.kbdd",
nid = naughty.notify({ title = "Keyboard layout changed",
text = "New layout is <i>" .. layout .. "</i>",
replaces_id = nid,
icon = "/usr/share/icons/gnome/32x32/devices/keyboard.png",
icon = icons.lookup({ name = "keyboard",
type = "devices" }),
screen = client.focus.screen }).id
end)

View file

@ -1,3 +1,5 @@
local icons = loadrc("icons", "vbe/icons")
-- Signal function to execute when a new client appears.
client.add_signal("manage",
function (c, startup)
@ -9,6 +11,15 @@ client.add_signal("manage",
client.focus = c
end
end)
-- Setup icon if none exists
if not c.icon then
local icon = icons.lookup({ name = { c.class, c.instance },
type = "apps" })
if icon then
c.icon = image(icon)
end
end
if not startup then
-- Put windows in a smart way, only if they does not set an initial position.

View file

@ -1,6 +1,7 @@
-- Widgets
require("vicious")
local icons = loadrc("icons", "vbe/icons")
-- Separator
local separator = widget({ type = "textbox" })
@ -38,8 +39,8 @@ if config.hostname == "guybrush" then
text = "Battery level is currently " ..
current .. "%.\n" .. args[3] ..
" left before running out of power.",
icon = "/usr/share/icons/gnome/32x32" ..
"/status/battery-caution.png",
icon = icons.lookup({name = "battery-caution",
type = "status"}),
replaces_id = batwidget.lastid }).id
batwidget.lastwarn = current
end
@ -174,27 +175,11 @@ for s = 1, screen.count() do
fn = awful.widget.tasklist.label.alltags
end
local title, color, _, icon = fn(c, s)
-- Try to search for an alternative icon if none is available
for _, name in pairs({c.class, c.instance}) do
if not icon and title and name then
for _, n in pairs({name, name:lower()}) do
icon = awful.util.geticonpath(name,
nil,
{"/usr/share/fvwm-crystal/fvwm/icons/Default/16x16/apps/",
"/usr/share/fvwm-crystal/fvwm/icons/Default/22x22/apps/",
"/usr/share/icons/hicolor/16x16/apps/"})
if icon then
-- This is our new icon. And we set it for the client to not search again
icon = image(icon)
c.icon = icon
end
end
end
end
-- Use our icon and don't set the status image.
if screen.count() > 1 then
-- title, color and icon
return title, color, nil, icon
elseif icon then
-- just color and icon
return "", color, nil, icon
end
end, tasklist.buttons)

View file

@ -1,8 +1,12 @@
-- Lockscreen
local icons = loadrc("icons", "vbe/icons")
xrun("xautolock",
"xautolock -notify 10 -notifier " ..
" 'notify-send Lock\\ screen -i /usr/share/icons/gnome/32x32/actions/system-lock-screen.png -t 10000 " ..
" 'notify-send Lock\\ screen -i " ..
icons.lookup({name = "system-lock-screen", type = "actions" }) ..
" -t 10000 " ..
" Lock\\ screen\\ will\\ be\\ started\\ in\\ 10\\ seconds...' " ..
" -time 3 -locker " ..
" 'i3lock -n -i " .. awful.util.getdir("cache") .. "/current-wallpaper.png'")

View file

@ -1,5 +1,7 @@
-- Menu with xrandr choices
local icons = loadrc("icons", "vbe/icons")
-- Get active outputs
local function outputs()
local outputs = {}
@ -76,7 +78,7 @@ local function menu()
menu[#menu + 1] = { label,
cmd,
"/usr/share/icons/gnome/32x32/devices/display.png" }
icons.lookup({ name = "display", type = "devices" }) }
end
return menu
@ -103,7 +105,7 @@ local function xrandr()
local next = state.iterator()
local label, action, icon
if not next then
label, icon = "Keep the current configuration", "/usr/share/icons/gnome/32x32/devices/display.png"
label, icon = "Keep the current configuration", icons.lookup({ name = "display", type = "devices" })
state.iterator = nil
else
label, action, icon = unpack(next)