在很棒的 wm 3.5 中,您可以使用 cairo 创建自定义小部件来绘制其视觉效果。我想要一个显示单色 PNG 图标的小部件(就像 wibox.widget.imagebox 一样)并允许快速更改其颜色。我尝试在 wibox.widget.imagebox 的绘制函数中修改几行
local cairo = require("lgi").cairo
--- Draw an imagebox with the given cairo context in the given geometry.
function imagebox:draw(wibox, cr, width, height)
if not self._image then return end
if width == 0 or height == 0 then return end
cr:save()
if not self.resize_forbidden then
-- Let's scale the image so that it fits into (width, height)
local w = self._image:get_width()
local h = self._image:get_height()
local aspect = width / w
local aspect_h = height / h
if aspect > aspect_h then aspect = aspect_h end
cr:scale(aspect, aspect)
end
-- Here is my modifications
cr:set_source_surface(self._image, 0, 0)
cr:paint()
cr:set_operator(cairo.Operator.IN)
cr:set_source_rgba(0, 0, 1, 0.5)
cr:paint()
-- End of my my modifications
-- This is original draw code how it was
--cr:set_source_surface(self._image, 0, 0)
--cr:paint()
cr:restore()
end
但它不起作用。我尝试设置其他几个 cairo 的合成运算符,但大多数都没有按预期工作。错误的重叠区域和黑色区域,而不是 wibox 背景颜色。SOURCE 和 OVER 只能正常工作。我在哪里犯错了?