awesome/lib/beautiful.lua.in

125 lines
3.6 KiB
Lua
Raw Normal View History

----------------------------------------------------------------------------
-- @author Damien Leone <damien.leone@gmail.com>
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2008-2009 Damien Leone, Julien Danjou
-- @release @AWESOME_VERSION@
----------------------------------------------------------------------------
-- Grab environment
local os = os
local print = print
local pcall = pcall
local pairs = pairs
local type = type
local dofile = dofile
local setmetatable = setmetatable
local util = require("awful.util")
local capi =
{
screen = screen,
awesome = awesome,
oocairo = oocairo,
oopango = oopango
}
--- Theme library.
module("beautiful")
-- Local data
local theme
local descs = setmetatable({}, { __mode = 'k' })
local fonts = setmetatable({}, { __mode = 'v' })
local active_font
local function load_font(name)
name = name or active_font
if name and type(name) ~= "string" and descs[name] then
name = descs[name]
end
if fonts[name] then
return fonts[name]
end
-- load new font
local desc = capi.oopango.font_description_from_string(name)
-- Create a temporary surface that we need for computing the size :(
local surface = capi.oocairo.image_surface_create("argb32", 1, 1)
local cr = capi.oocairo.context_create(surface)
local layout
-- Api breakage in oopango
if capi.oopango.cairo_layout_create then
layout = capi.oopango.cairo_layout_create(cr)
else
layout = capi.oopango.cairo.layout_create(cr)
end
layout:set_font_description(desc)
local width, height = layout:get_pixel_size()
local font = { name = name, description = desc, height = height }
fonts[name] = font
descs[desc] = name
return font
end
local function set_font(name)
active_font = load_font(name).name
end
function get_font(name)
return load_font(name).description
end
function get_font_height(name)
return load_font(name).height
end
--- Init function, should be runned at the beginning of configuration file.
-- @param path The theme file path.
function init(path)
if path then
local success
success, theme = pcall(function() return dofile(path) end)
if not success then
return print("E: beautiful: error loading theme file " .. theme)
elseif theme then
-- try and grab user's $HOME directory
local homedir = os.getenv("HOME")
-- expand '~'
if homedir then
for k, v in pairs(theme) do
if type(v) == "string" then theme[k] = v:gsub("~", homedir) end
end
end
-- setup wallpaper
if theme.wallpaper_cmd then
for s = 1, capi.screen.count() do
util.spawn(theme.wallpaper_cmd[util.cycle(#theme.wallpaper_cmd, s)], false, s)
end
end
if theme.font then set_font(theme.font) end
if theme.fg_normal then capi.awesome.fg = theme.fg_normal end
if theme.bg_normal then capi.awesome.bg = theme.bg_normal end
else
return print("E: beautiful: error loading theme file " .. path)
end
else
return print("E: beautiful: error loading theme: no path specified")
end
end
--- Get the current theme.
-- @return The current theme table.
function get()
return theme
end
-- Set the default font
set_font("sans 8")
setmetatable(_M, { __index = function(t, k) return theme[k] end })
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80