2008-08-05 22:21:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
2014-05-24 23:18:19 +02:00
|
|
|
--- Theme library.
|
|
|
|
--
|
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
|
2014-05-24 23:18:19 +02:00
|
|
|
-- @module beautiful
|
2008-08-05 22:21:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment
|
2009-04-18 16:05:18 +02:00
|
|
|
local os = os
|
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
|
2012-05-27 19:20:34 +02:00
|
|
|
local lgi = require("lgi")
|
|
|
|
local Pango = lgi.Pango
|
|
|
|
local PangoCairo = lgi.PangoCairo
|
2015-12-22 19:28:05 +01:00
|
|
|
local gears_debug = require("gears.debug")
|
2017-05-25 10:40:45 +02:00
|
|
|
local Gio = require("lgi").Gio
|
2016-02-28 12:28:55 +01:00
|
|
|
local protected_call = require("gears.protected_call")
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2015-05-14 22:57:22 +02:00
|
|
|
local xresources = require("beautiful.xresources")
|
2017-01-14 04:09:54 +01:00
|
|
|
local theme_assets = require("beautiful.theme_assets")
|
2018-06-26 16:43:20 +02:00
|
|
|
local gtk = require("beautiful.gtk")
|
2015-05-14 22:57:22 +02:00
|
|
|
|
2017-01-14 04:09:54 +01:00
|
|
|
local beautiful = {
|
|
|
|
xresources = xresources,
|
|
|
|
theme_assets = theme_assets,
|
2018-06-26 16:43:20 +02:00
|
|
|
gtk = gtk,
|
2017-01-14 04:09:54 +01:00
|
|
|
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
|
|
|
|
2016-08-12 08:34:03 +02:00
|
|
|
--- The default font.
|
|
|
|
-- @beautiful beautiful.font
|
|
|
|
|
|
|
|
-- The default background color.
|
|
|
|
-- @beautiful beautiful.bg_normal
|
|
|
|
|
|
|
|
-- The default focused element background color.
|
|
|
|
-- @beautiful beautiful.bg_focus
|
|
|
|
|
|
|
|
-- The default urgent element background color.
|
|
|
|
-- @beautiful beautiful.bg_urgent
|
|
|
|
|
|
|
|
-- The default minimized element background color.
|
|
|
|
-- @beautiful beautiful.bg_minimize
|
|
|
|
|
|
|
|
-- The system tray background color.
|
|
|
|
-- Please note that only solid colors are currently supported.
|
|
|
|
-- @beautiful beautiful.bg_systray
|
|
|
|
|
|
|
|
-- The default focused element foreground (text) color.
|
|
|
|
-- @beautiful beautiful.fg_normal
|
|
|
|
|
|
|
|
-- The default focused element foreground (text) color.
|
|
|
|
-- @beautiful beautiful.fg_focus
|
|
|
|
|
|
|
|
-- The default urgent element foreground (text) color.
|
|
|
|
-- @beautiful beautiful.fg_urgent
|
|
|
|
|
|
|
|
-- The default minimized element foreground (text) color.
|
|
|
|
-- @beautiful beautiful.fg_minimize
|
|
|
|
|
|
|
|
--- The gap between clients.
|
|
|
|
-- @beautiful beautiful.useless_gap
|
|
|
|
-- @param[opt=0] number
|
|
|
|
|
|
|
|
--- The client border width.
|
|
|
|
-- @beautiful beautiful.border_width
|
|
|
|
|
2018-07-12 06:30:46 +02:00
|
|
|
--- The default clients border color.
|
2016-08-12 08:34:03 +02:00
|
|
|
-- Note that only solid colors are supported.
|
|
|
|
-- @beautiful beautiful.border_normal
|
|
|
|
|
2018-07-12 06:30:46 +02:00
|
|
|
--- The focused client border color.
|
2016-08-12 08:34:03 +02:00
|
|
|
-- Note that only solid colors are supported.
|
|
|
|
-- @beautiful beautiful.border_focus
|
|
|
|
|
2018-07-12 06:30:46 +02:00
|
|
|
--- The marked clients border color.
|
2016-08-12 08:34:03 +02:00
|
|
|
-- Note that only solid colors are supported.
|
|
|
|
-- @beautiful beautiful.border_marked
|
|
|
|
|
|
|
|
--- The wallpaper path.
|
|
|
|
-- @beautiful beautiful.wallpaper
|
|
|
|
|
|
|
|
-- The icon theme name.
|
|
|
|
-- It has to be a directory in `/usr/share/icons` or an XDG icon folder.
|
|
|
|
-- @beautiful beautiful.icon_theme
|
|
|
|
|
|
|
|
--- The Awesome icon path.
|
|
|
|
-- @beautiful beautiful.awesome_icon
|
|
|
|
|
2017-05-25 10:40:45 +02:00
|
|
|
--- The current theme path (if any)
|
|
|
|
-- @tfield string beautiful.theme_path
|
|
|
|
|
2015-07-13 19:27:38 +02:00
|
|
|
--- Load a font from a string or a font description.
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2015-07-13 19:27:38 +02:00
|
|
|
-- @see https://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string
|
|
|
|
-- @tparam string|lgi.Pango.FontDescription name Font, which can be a
|
|
|
|
-- string or a lgi.Pango.FontDescription.
|
|
|
|
-- @treturn table A table with `name`, `description` and `height`.
|
2011-04-14 07:04:48 +02:00
|
|
|
local function load_font(name)
|
|
|
|
name = name or active_font
|
2015-07-13 19:27:38 +02:00
|
|
|
if name and type(name) ~= "string" then
|
|
|
|
if descs[name] then
|
|
|
|
name = descs[name]
|
|
|
|
else
|
|
|
|
name = name:to_string()
|
|
|
|
end
|
2011-04-14 07:04:48 +02:00
|
|
|
end
|
|
|
|
if fonts[name] then
|
|
|
|
return fonts[name]
|
|
|
|
end
|
2010-10-06 14:18:11 +02:00
|
|
|
|
2014-03-23 18:57:48 +01:00
|
|
|
-- Load new font
|
|
|
|
local desc = Pango.FontDescription.from_string(name)
|
2012-05-27 20:14:56 +02:00
|
|
|
local ctx = PangoCairo.font_map_get_default():create_context()
|
2015-05-14 22:57:22 +02:00
|
|
|
ctx:set_resolution(beautiful.xresources.get_dpi())
|
2010-10-06 14:18:11 +02:00
|
|
|
|
2014-03-23 18:57:48 +01:00
|
|
|
-- Apply default values from the context (e.g. a default font size)
|
|
|
|
desc:merge(ctx:get_font_description(), false)
|
|
|
|
|
2015-06-19 21:03:27 +02:00
|
|
|
-- Calculate font height.
|
2014-03-23 18:57:48 +01:00
|
|
|
local metrics = ctx:get_metrics(desc, nil)
|
|
|
|
local height = math.ceil((metrics:get_ascent() + metrics:get_descent()) / Pango.SCALE)
|
2017-05-14 20:14:18 +02:00
|
|
|
if height == 0 then
|
|
|
|
height = desc:get_size() / Pango.SCALE
|
|
|
|
gears_debug.print_warning(string.format(
|
|
|
|
"beautiful.load_font: could not get height for '%s' (likely missing font), using %d.",
|
|
|
|
name, height))
|
|
|
|
end
|
2014-03-23 18:57:48 +01:00
|
|
|
|
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
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Set an active font
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
|
|
|
-- @param name The font
|
2011-04-14 07:04:48 +02:00
|
|
|
local function set_font(name)
|
|
|
|
active_font = load_font(name).name
|
|
|
|
end
|
|
|
|
|
2015-07-13 19:27:38 +02:00
|
|
|
--- Get a font description.
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2015-07-24 01:56:36 +02:00
|
|
|
-- See https://developer.gnome.org/pango/stable/pango-Fonts.html#PangoFontDescription.
|
2015-07-13 19:27:38 +02:00
|
|
|
-- @tparam string|lgi.Pango.FontDescription name The name of the font.
|
|
|
|
-- @treturn lgi.Pango.FontDescription
|
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
|
|
|
|
|
2015-07-13 19:27:38 +02:00
|
|
|
--- Get a new font with merged attributes, based on another one.
|
|
|
|
--
|
2015-07-24 01:56:36 +02:00
|
|
|
-- See https://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string.
|
2015-07-13 19:27:38 +02:00
|
|
|
-- @tparam string|Pango.FontDescription name The base font.
|
|
|
|
-- @tparam string merge Attributes that should be merged, e.g. "bold".
|
|
|
|
-- @treturn lgi.Pango.FontDescription
|
|
|
|
function beautiful.get_merged_font(name, merge)
|
|
|
|
local font = beautiful.get_font(name)
|
2016-02-07 14:29:40 +01:00
|
|
|
merge = Pango.FontDescription.from_string(merge)
|
2015-07-13 19:27:38 +02:00
|
|
|
local merged = font:copy_static()
|
|
|
|
merged:merge(merge, true)
|
|
|
|
return beautiful.get_font(merged:to_string())
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the height of a font.
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
|
|
|
-- @param name Name of the font
|
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
|
|
|
|
2018-08-14 13:47:41 +02:00
|
|
|
--- Init function, should be run at the beginning of configuration file.
|
2014-05-25 14:24:52 +02:00
|
|
|
-- @tparam string|table config The theme to load. It can be either the path to
|
2015-10-14 00:21:29 +02:00
|
|
|
-- the theme file (returning a table) or directly the table
|
|
|
|
-- containing all the theme values.
|
2015-01-20 15:37:35 +01:00
|
|
|
function beautiful.init(config)
|
|
|
|
if config then
|
2014-03-24 01:10:45 +01:00
|
|
|
local homedir = os.getenv("HOME")
|
|
|
|
|
2016-03-06 14:46:13 +01:00
|
|
|
-- If `config` is the path to a theme file, run this file,
|
|
|
|
-- otherwise if it is a theme table, save it.
|
2015-01-20 15:37:35 +01:00
|
|
|
if type(config) == 'string' then
|
|
|
|
-- Expand the '~' $HOME shortcut
|
|
|
|
config = config:gsub("^~/", homedir .. "/")
|
2017-05-25 10:40:45 +02:00
|
|
|
local dir = Gio.File.new_for_path(config):get_parent()
|
|
|
|
beautiful.theme_path = dir and (dir:get_path().."/") or nil
|
2016-02-28 12:28:55 +01:00
|
|
|
theme = protected_call(dofile, config)
|
2015-01-20 15:37:35 +01:00
|
|
|
elseif type(config) == 'table' then
|
|
|
|
theme = config
|
|
|
|
end
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2016-02-28 12:28:55 +01:00
|
|
|
if theme then
|
2009-04-28 19:40:14 +02:00
|
|
|
-- expand '~'
|
|
|
|
if homedir then
|
|
|
|
for k, v in pairs(theme) do
|
2014-03-24 01:10:45 +01:00
|
|
|
if type(v) == "string" then theme[k] = v:gsub("^~/", homedir .. "/") end
|
2009-04-28 19:40:14 +02:00
|
|
|
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
|
2017-01-25 23:06:15 +01:00
|
|
|
theme = {}
|
2015-12-22 19:28:05 +01:00
|
|
|
return gears_debug.print_error("beautiful: error loading theme file " .. config)
|
2008-08-05 22:21:22 +02:00
|
|
|
end
|
2009-04-28 19:40:14 +02:00
|
|
|
else
|
2015-12-22 19:28:05 +01:00
|
|
|
return gears_debug.print_error("beautiful: error loading theme: no theme specified")
|
2008-08-05 22:21:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-13 11:53:41 +01:00
|
|
|
--- Get the current theme.
|
2014-05-25 14:24:52 +02:00
|
|
|
--
|
|
|
|
-- @treturn table 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
|
|
|
|
|
2017-06-20 10:05:32 +02:00
|
|
|
function beautiful.mt:__newindex(k, v)
|
|
|
|
theme[k] = v
|
|
|
|
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
|