conky-config/drawing/static.lua

122 lines
3.7 KiB
Lua
Raw Normal View History

2022-07-10 16:25:12 -04:00
local common = require 'common'
local pure = require 'pure'
local geom = require 'geom'
local fill_rect = require 'fill_rect'
local reduce_modules_y = function(modlist, init_x, width, acc, new)
2022-07-16 00:00:06 -04:00
if type(new) == "number" then
acc.next_y = acc.next_y + new
else
local r = modlist[new](width, geom.make_point(init_x, acc.next_y))
2022-07-16 00:00:06 -04:00
table.insert(acc.fgroups, {update = r.update, static = r.static, dynamic = r.dynamic})
acc.next_x = math.max(acc.next_x, r.next_x)
acc.next_y = r.next_y
end
return acc
end
2022-07-17 12:07:38 -04:00
local reduce_modules_x = function(modlist, init_y, acc, x_mods)
2022-07-16 00:00:06 -04:00
if type(x_mods) == "number" then
acc.next_x = acc.next_x + x_mods
else
local r = pure.reduce(
pure.partial(reduce_modules_y, modlist, acc.next_x, x_mods.width),
2022-07-16 00:00:06 -04:00
{next_x = acc.next_x, next_y = init_y, fgroups = acc.fgroups},
x_mods.blocks
2022-07-16 00:00:06 -04:00
)
acc.fgroups = r.fgroups
acc.next_x = r.next_x
acc.next_y = math.max(acc.next_y, r.next_y)
end
return acc
end
2022-07-17 12:07:38 -04:00
local arrange_panel_modules = function(modlist, point, mods)
2022-07-16 00:00:06 -04:00
local r = pure.reduce(
2022-07-17 12:07:38 -04:00
pure.partial(reduce_modules_x, modlist, point.y),
2022-07-16 00:00:06 -04:00
{next_x = point.x, next_y = point.y, fgroups = {}},
mods
)
return {
point_ul = point,
width = r.next_x - point.x,
height = r.next_y - point.y,
update = pure.map_keys('update', r.fgroups),
static = pure.map_keys('static', r.fgroups),
dynamic = pure.map_keys('dynamic', r.fgroups),
}
end
2022-07-16 00:00:06 -04:00
local build_surface = function(box, fs)
local panel_line_thickness = 1
-- move over by half a pixel so the lines don't need to be antialiased
local _x = box.corner.x + 0.5
local _y = box.corner.y + 0.5
local panel = common.make_panel(_x, _y, box.width, box.height, panel_line_thickness)
local cs_x = _x - panel_line_thickness * 0.5
local cs_y = _y - panel_line_thickness * 0.5
local cs_w = box.width + panel_line_thickness
local cs_h = box.height + panel_line_thickness
2022-07-16 00:00:06 -04:00
local cs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, cs_w, cs_h)
local cr = cairo_create(cs)
2022-07-16 00:00:06 -04:00
cairo_translate(cr, -cs_x, -cs_y)
2022-07-16 00:00:06 -04:00
fill_rect.draw(panel, cr)
for i = 1, #fs do
fs[i](cr)
end
2022-07-16 00:00:06 -04:00
cairo_destroy(cr)
return {x = cs_x, y = cs_y, s = cs}
end
local reduce_static = function(mods, y, acc, panel_mods)
2022-07-16 00:00:06 -04:00
if type(panel_mods) == "number" then
acc.next_x = acc.next_x + panel_mods
else
local margins = panel_mods.margins
local margin_x = margins[1]
local margin_y = margins[2]
local mpoint = geom.make_point(acc.next_x + margin_x, y + margin_y)
local r = arrange_panel_modules(mods, mpoint, panel_mods.columns)
local w = r.width + margin_x * 2
local h = r.height + margin_y * 2
2022-07-16 00:00:06 -04:00
local pbox = geom.make_box(acc.next_x, y, w, h)
acc.next_x = acc.next_x + w
acc.static = pure.flatten({acc.static, {build_surface(pbox, r.static)}})
acc.update = pure.flatten({acc.update, r.update})
acc.dynamic = pure.flatten({acc.dynamic, r.dynamic})
end
return acc
end
2022-07-17 12:07:38 -04:00
return function(point, mods, module_sets)
2022-07-16 00:00:06 -04:00
local __cairo_set_source_surface = cairo_set_source_surface
local __cairo_paint = cairo_paint
2022-07-16 00:00:06 -04:00
local r = pure.reduce(
pure.partial(
reduce_static,
2022-07-17 12:07:38 -04:00
mods,
point.y
2022-07-16 00:00:06 -04:00
),
{next_x = point.x, static = {}, update = {}, dynamic = {}},
module_sets
)
2022-07-16 00:00:06 -04:00
local cs = r.static
return {
static = function(cr)
for i = 1, #cs do
local c = cs[i]
__cairo_set_source_surface(cr, c.s, c.x, c.y)
__cairo_paint(cr)
end
end,
update = pure.sequence(table.unpack(r.update)),
dynamic = pure.sequence(table.unpack(r.dynamic)),
}
end