2008-08-05 22:21:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
2008-08-05 22:26:34 +02:00
|
|
|
-- @author Damien Leone <damien.leone@gmail.com>
|
2008-08-05 22:21:22 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
2009-04-18 16:05:18 +02:00
|
|
|
-- @copyright 2008-2009 Damien Leone, Julien Danjou
|
2008-08-18 11:41:58 +02:00
|
|
|
-- @release @AWESOME_VERSION@
|
2008-08-05 22:21:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment
|
2009-04-18 16:05:18 +02:00
|
|
|
local os = os
|
2008-08-05 22:21:22 +02:00
|
|
|
local print = print
|
2009-08-21 21:26:50 +02:00
|
|
|
local pcall = pcall
|
2009-04-28 19:40:14 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local dofile = dofile
|
2008-08-05 22:21:22 +02:00
|
|
|
local setmetatable = setmetatable
|
2008-09-29 16:49:18 +02:00
|
|
|
local util = require("awful.util")
|
2012-05-27 19:20:34 +02:00
|
|
|
local lgi = require("lgi")
|
|
|
|
local cairo = lgi.cairo
|
|
|
|
local Pango = lgi.Pango
|
|
|
|
local PangoCairo = lgi.PangoCairo
|
2008-08-20 18:15:08 +02:00
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
screen = screen,
|
2012-05-27 19:20:34 +02:00
|
|
|
awesome = awesome
|
2008-08-20 18:15:08 +02:00
|
|
|
}
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2009-04-28 22:54:55 +02:00
|
|
|
--- Theme library.
|
2012-06-12 03:24:22 +02:00
|
|
|
local beautiful = { mt = {} }
|
2008-08-05 22:21:22 +02:00
|
|
|
|
|
|
|
-- Local data
|
2012-11-27 22:55:42 +01:00
|
|
|
local theme = {}
|
2011-04-14 22:40:56 +02:00
|
|
|
local descs = setmetatable({}, { __mode = 'k' })
|
|
|
|
local fonts = setmetatable({}, { __mode = 'v' })
|
2011-04-14 07:04:48 +02:00
|
|
|
local active_font
|
2010-10-06 14:18:11 +02:00
|
|
|
|
2011-04-14 07:04:48 +02:00
|
|
|
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
|
2012-05-27 19:20:34 +02:00
|
|
|
local desc = Pango.FontDescription.from_string(name)
|
2010-10-06 14:18:11 +02:00
|
|
|
|
2012-05-27 20:14:56 +02:00
|
|
|
local ctx = PangoCairo.font_map_get_default():create_context()
|
|
|
|
local layout = Pango.Layout.new(ctx)
|
2011-04-14 07:04:48 +02:00
|
|
|
layout:set_font_description(desc)
|
2010-10-06 14:18:11 +02:00
|
|
|
|
|
|
|
local width, height = layout:get_pixel_size()
|
2011-04-14 07:04:48 +02:00
|
|
|
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
|
|
|
|
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.get_font(name)
|
2011-04-14 07:04:48 +02:00
|
|
|
return load_font(name).description
|
|
|
|
end
|
|
|
|
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.get_font_height(name)
|
2011-04-14 07:04:48 +02:00
|
|
|
return load_font(name).height
|
2010-10-06 14:18:11 +02:00
|
|
|
end
|
2008-08-05 22:21:22 +02:00
|
|
|
|
|
|
|
--- Init function, should be runned at the beginning of configuration file.
|
|
|
|
-- @param path The theme file path.
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.init(path)
|
2008-08-05 22:21:22 +02:00
|
|
|
if path then
|
2009-08-21 21:26:50 +02:00
|
|
|
local success
|
|
|
|
success, theme = pcall(function() return dofile(path) end)
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2009-08-21 21:26:50 +02:00
|
|
|
if not success then
|
|
|
|
return print("E: beautiful: error loading theme file " .. theme)
|
|
|
|
elseif theme then
|
2009-04-28 19:40:14 +02:00
|
|
|
-- 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
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2010-10-06 14:18:11 +02:00
|
|
|
if theme.font then set_font(theme.font) end
|
2009-04-28 19:40:14 +02:00
|
|
|
else
|
2009-08-21 21:26:50 +02:00
|
|
|
return print("E: beautiful: error loading theme file " .. path)
|
2008-08-05 22:21:22 +02:00
|
|
|
end
|
2009-04-28 19:40:14 +02:00
|
|
|
else
|
2009-08-21 21:26:50 +02:00
|
|
|
return print("E: beautiful: error loading theme: no path specified")
|
2008-08-05 22:21:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-13 11:53:41 +01:00
|
|
|
--- Get the current theme.
|
|
|
|
-- @return The current theme table.
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.get()
|
2009-04-28 22:54:55 +02:00
|
|
|
return theme
|
2008-11-13 11:53:41 +01:00
|
|
|
end
|
|
|
|
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.mt:__index(k)
|
|
|
|
return theme[k]
|
|
|
|
end
|
|
|
|
|
2010-10-06 14:18:11 +02:00
|
|
|
-- Set the default font
|
|
|
|
set_font("sans 8")
|
|
|
|
|
2012-06-12 03:24:22 +02:00
|
|
|
return setmetatable(beautiful, beautiful.mt)
|
2008-08-06 15:39:51 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|