diff options
author | ache <ache@ache.one> | 2017-03-13 23:17:19 +0100 |
---|---|---|
committer | ache <ache@ache.one> | 2017-03-13 23:17:19 +0100 |
commit | 22d656903563f75678f3634964731ccf93355dfd (patch) | |
tree | e3cb6279d95c9764093072d5e946566ea6533799 /lib/beautiful |
Init commit
Diffstat (limited to 'lib/beautiful')
-rw-r--r-- | lib/beautiful/init.lua | 217 | ||||
-rw-r--r-- | lib/beautiful/xresources.lua | 117 |
2 files changed, 334 insertions, 0 deletions
diff --git a/lib/beautiful/init.lua b/lib/beautiful/init.lua new file mode 100644 index 0000000..0e72cbc --- /dev/null +++ b/lib/beautiful/init.lua @@ -0,0 +1,217 @@ +---------------------------------------------------------------------------- +--- Theme library. +-- +-- @author Damien Leone <damien.leone@gmail.com> +-- @author Julien Danjou <julien@danjou.info> +-- @copyright 2008-2009 Damien Leone, Julien Danjou +-- @module beautiful +---------------------------------------------------------------------------- + +-- Grab environment +local os = os +local pairs = pairs +local type = type +local dofile = dofile +local setmetatable = setmetatable +local lgi = require("lgi") +local Pango = lgi.Pango +local PangoCairo = lgi.PangoCairo +local gears_debug = require("gears.debug") +local protected_call = require("gears.protected_call") + +local xresources = require("beautiful.xresources") + +local beautiful = { xresources = xresources, mt = {} } + +-- Local data +local theme = {} +local descs = setmetatable({}, { __mode = 'k' }) +local fonts = setmetatable({}, { __mode = 'v' }) +local active_font + +--- 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 + +--- The default clients border width. +-- Note that only solid colors are supported. +-- @beautiful beautiful.border_normal + +--- The focused client border width. +-- Note that only solid colors are supported. +-- @beautiful beautiful.border_focus + +--- The marked clients border width. +-- 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 + +--- Load a font from a string or a font description. +-- +-- @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`. +local function load_font(name) + name = name or active_font + if name and type(name) ~= "string" then + if descs[name] then + name = descs[name] + else + name = name:to_string() + end + end + if fonts[name] then + return fonts[name] + end + + -- Load new font + local desc = Pango.FontDescription.from_string(name) + local ctx = PangoCairo.font_map_get_default():create_context() + ctx:set_resolution(beautiful.xresources.get_dpi()) + + -- Apply default values from the context (e.g. a default font size) + desc:merge(ctx:get_font_description(), false) + + -- Calculate font height. + local metrics = ctx:get_metrics(desc, nil) + local height = math.ceil((metrics:get_ascent() + metrics:get_descent()) / Pango.SCALE) + + local font = { name = name, description = desc, height = height } + fonts[name] = font + descs[desc] = name + return font +end + +--- Set an active font +-- +-- @param name The font +local function set_font(name) + active_font = load_font(name).name +end + +--- Get a font description. +-- +-- See https://developer.gnome.org/pango/stable/pango-Fonts.html#PangoFontDescription. +-- @tparam string|lgi.Pango.FontDescription name The name of the font. +-- @treturn lgi.Pango.FontDescription +function beautiful.get_font(name) + return load_font(name).description +end + +--- Get a new font with merged attributes, based on another one. +-- +-- See https://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string. +-- @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) + merge = Pango.FontDescription.from_string(merge) + local merged = font:copy_static() + merged:merge(merge, true) + return beautiful.get_font(merged:to_string()) +end + +--- Get the height of a font. +-- +-- @param name Name of the font +function beautiful.get_font_height(name) + return load_font(name).height +end + +--- Init function, should be runned at the beginning of configuration file. +-- @tparam string|table config The theme to load. It can be either the path to +-- the theme file (returning a table) or directly the table +-- containing all the theme values. +function beautiful.init(config) + if config then + local homedir = os.getenv("HOME") + + -- If `config` is the path to a theme file, run this file, + -- otherwise if it is a theme table, save it. + if type(config) == 'string' then + -- Expand the '~' $HOME shortcut + config = config:gsub("^~/", homedir .. "/") + theme = protected_call(dofile, config) + elseif type(config) == 'table' then + theme = config + end + + if theme then + -- expand '~' + if homedir then + for k, v in pairs(theme) do + if type(v) == "string" then theme[k] = v:gsub("^~/", homedir .. "/") end + end + end + + if theme.font then set_font(theme.font) end + else + return gears_debug.print_error("beautiful: error loading theme file " .. config) + end + else + return gears_debug.print_error("beautiful: error loading theme: no theme specified") + end +end + +--- Get the current theme. +-- +-- @treturn table The current theme table. +function beautiful.get() + return theme +end + +function beautiful.mt:__index(k) + return theme[k] +end + +-- Set the default font +set_font("sans 8") + +return setmetatable(beautiful, beautiful.mt) + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/lib/beautiful/xresources.lua b/lib/beautiful/xresources.lua new file mode 100644 index 0000000..f0f5d78 --- /dev/null +++ b/lib/beautiful/xresources.lua @@ -0,0 +1,117 @@ +---------------------------------------------------------------------------- +--- Library for getting xrdb data. +-- +-- @author Yauhen Kirylau <yawghen@gmail.com> +-- @copyright 2015 Yauhen Kirylau +-- @module beautiful.xresources +---------------------------------------------------------------------------- + +-- Grab environment +local awesome = awesome +local screen = screen +local round = require("awful.util").round +local gears_debug = require("gears.debug") + +local xresources = {} + +local fallback = { + --black + color0 = '#000000', + color8 = '#465457', + --red + color1 = '#cb1578', + color9 = '#dc5e86', + --green + color2 = '#8ecb15', + color10 = '#9edc60', + --yellow + color3 = '#cb9a15', + color11 = '#dcb65e', + --blue + color4 = '#6f15cb', + color12 = '#7e5edc', + --purple + color5 = '#cb15c9', + color13 = '#b75edc', + --cyan + color6 = '#15b4cb', + color14 = '#5edcb4', + --white + color7 = '#888a85', + color15 = '#ffffff', + -- + background = '#0e0021', + foreground = '#bcbcbc', +} + +--- Get current base colorscheme from xrdb. +-- @treturn table Color table with keys 'background', 'foreground' and 'color0'..'color15' +function xresources.get_current_theme() + local keys = { 'background', 'foreground' } + for i=0,15 do table.insert(keys, "color"..i) end + local colors = {} + for _, key in ipairs(keys) do + colors[key] = awesome.xrdb_get_value("", key) + if not colors[key] then + gears_debug.print_warning("beautiful: can't get colorscheme from xrdb (using fallback).") + return fallback + end + if colors[key]:find("rgb:") then + colors[key] = "#"..colors[key]:gsub("[a]?rgb:", ""):gsub("/", "") + end + end + return colors +end + + +local dpi_per_screen = {} + +local function get_screen(s) + return s and screen[s] +end + +--- Get global or per-screen DPI value falling back to xrdb. +-- @tparam[opt] integer|screen s The screen. +-- @treturn number DPI value. +function xresources.get_dpi(s) + s = get_screen(s) + if dpi_per_screen[s] then + return dpi_per_screen[s] + end + if not xresources.dpi then + -- Might not be present when run under unit tests + if awesome and awesome.xrdb_get_value then + xresources.dpi = tonumber(awesome.xrdb_get_value("", "Xft.dpi")) + end + if not xresources.dpi then + xresources.dpi = 96 + end + end + return xresources.dpi +end + + +--- Set DPI for a given screen (defaults to global). +-- @tparam number dpi DPI value. +-- @tparam[opt] integer s Screen. +function xresources.set_dpi(dpi, s) + s = get_screen(s) + if not s then + xresources.dpi = dpi + else + dpi_per_screen[s] = dpi + end +end + + +--- Compute resulting size applying current DPI value (optionally per screen). +-- @tparam number size Size +-- @tparam[opt] integer|screen s The screen. +-- @treturn integer Resulting size (rounded to integer). +function xresources.apply_dpi(size, s) + return round(size / 96 * xresources.get_dpi(s)) +end + +return xresources + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 |