radical/widgets/underlay.lua

111 lines
3.5 KiB
Lua
Raw Normal View History

2014-01-16 06:25:50 +01:00
local type = type
2014-01-17 06:34:45 +01:00
local ipairs = ipairs
2014-01-16 06:25:50 +01:00
local color = require("gears.color")
local gsurface = require("gears.surface")
local cairo = require("lgi").cairo
local pango = require("lgi").Pango
local pangocairo = require("lgi").PangoCairo
local beautiful = require("beautiful")
local shape = require("gears.shape")
2014-01-16 06:25:50 +01:00
local module = {}
local pango_l,pango_crx = {},{}
--TODO using the current cairo path extents, it would be possible to merge the 2 function
-- and have an abstract underlay that work with most shapes
2014-01-17 06:34:45 +01:00
local function draw_item(cr,x,y,width,height,padding,args)
cr:save()
cr:rectangle(x,y,width+padding,height+padding)
cr:clip()
local s = shape.transform(shape.rounded_bar) : translate(x + padding, y + padding/4)
s(cr, width - 2*padding, height - padding/2)
2015-01-11 05:59:51 +01:00
cr:set_source(color(args.bg or beautiful.bg_underlay or beautiful.bg_alternate))
2014-01-17 06:34:45 +01:00
cr:fill()
2014-01-17 06:34:45 +01:00
cr:set_source(color(args.fg or beautiful.bg_normal))
cr:set_operator(cairo.Operator.CLEAR)
cr:move_to(x+height/2 + 2,y+padding/4 + (args.margins or 0)-(args.padding or 0)/2)
cr:show_layout(pango_l[height])
cr:restore()
end
2014-03-12 05:31:50 +01:00
function module.draw_arrow(cr,x,y,width,height,padding,args)
cr:save()
cr:rectangle(x,y,width+padding,height+padding)
cr:clip()
padding=padding/2
local mid = (height-2*padding)/2
local s = shape.transform(shape.hexagon) : translate(x, padding)
s(cr, width, height - 2*padding)
2015-01-11 05:59:51 +01:00
cr:set_source(color(args.bg or beautiful.bg_underlay or beautiful.bg_alternate))
2014-03-12 05:31:50 +01:00
cr:fill()
cr:set_source(color(args.fg or beautiful.bg_normal))
cr:set_operator(cairo.Operator.CLEAR)
cr:move_to(x+height/2+2,y+padding/2)
cr:show_layout(pango_l[height])
cr:restore()
end
function module.fit(text, args, context)
local context = context or {}
2014-01-16 06:25:50 +01:00
local args = args or {}
local height = args.height or (beautiful.menu_height)
2014-03-12 05:31:50 +01:00
local padding = height/4--beautiful.default_height/3
2014-01-17 06:34:45 +01:00
local dpi = context.dpi or beautiful.xresources.get_dpi(1)
2014-01-17 06:34:45 +01:00
-- Get text height
2014-01-16 06:25:50 +01:00
if not pango_l[height] then
local pango_crx = pangocairo.font_map_get_default():create_context()
pango_l[height] = pango.Layout.new(pango_crx)
local desc = pango.FontDescription()
desc:set_family("Verdana")
desc:set_weight(pango.Weight.BOLD)
desc:set_size((height-padding*2) * pango.SCALE)
-- desc:set_variant(pango.Variant.SMALL_CAPS)
2014-01-16 06:25:50 +01:00
pango_l[height]:set_font_description(desc)
end
2014-01-17 06:34:45 +01:00
local pango_layout = pango_l[height]
pango_layout:get_context():set_resolution(dpi)
pango_layout:context_changed()
2014-01-17 06:34:45 +01:00
-- Compute full width
local ret,full_width = {},0
for k,v in ipairs(type(text) == "table" and text or {text}) do
pango_layout.text = v
ret[k] = pango_layout:get_pixel_extents().width + height + padding
2014-01-17 06:34:45 +01:00
full_width = full_width + ret[k]
end
2014-06-01 05:45:46 +02:00
return full_width,ret
end
function module.draw(text, args, context)
2014-06-01 05:45:46 +02:00
local args = args or {}
local height = args.height or (beautiful.menu_height)
local padding = height/4--beautiful.default_height/3
local full_width,ret = module.fit(text, args, context)
2014-01-17 06:34:45 +01:00
local img = cairo.ImageSurface.create(cairo.Format.ARGB32, full_width+(args.padding_right or 0), height+padding)
2014-01-16 06:25:50 +01:00
cr = cairo.Context(img)
2014-01-17 06:34:45 +01:00
-- Draw every item
local x = 0
for k,v in ipairs(type(text) == "table" and text or {text}) do
pango_l[height].text = v
2014-03-12 05:31:50 +01:00
local draw_f = args.style or draw_item
draw_f(cr,x,0,ret[k],height,padding,args)
2014-01-17 06:34:45 +01:00
x = x + ret[k]
end
2014-01-16 06:25:50 +01:00
return img
end
return setmetatable(module, { __call = function(_, ...) return new(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;