2013-05-11 21:02:42 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local math = math
|
2014-01-08 06:59:57 +01:00
|
|
|
local base = require( "radical.base" )
|
2013-05-11 21:02:42 +02:00
|
|
|
local color = require( "gears.color" )
|
|
|
|
local cairo = require( "lgi" ).cairo
|
2015-12-29 11:19:23 +01:00
|
|
|
local wibox = require("wibox" )
|
2013-05-11 21:02:42 +02:00
|
|
|
local print = print
|
|
|
|
|
|
|
|
local module = {
|
|
|
|
margins = {
|
|
|
|
TOP = 4,
|
|
|
|
BOTTOM = 4,
|
|
|
|
RIGHT = 4,
|
|
|
|
LEFT = 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:01:54 +02:00
|
|
|
local state_cache = {}
|
2013-05-11 21:02:42 +02:00
|
|
|
|
2015-01-11 05:59:51 +01:00
|
|
|
local function rect(cr,width,height,x,y)
|
2015-01-02 23:52:36 +01:00
|
|
|
local radius = 3
|
2014-12-25 07:10:55 +01:00
|
|
|
cr:move_to(0,radius)
|
|
|
|
cr:arc(radius,radius,radius,math.pi,3*(math.pi/2))
|
|
|
|
cr:arc(width-radius,radius,radius,3*(math.pi/2),math.pi*2)
|
|
|
|
cr:arc(width-radius,height-radius,radius,math.pi*2,math.pi/2)
|
|
|
|
cr:arc(radius,height-radius,radius,math.pi/2,math.pi)
|
|
|
|
cr:close_path()
|
2015-01-11 05:59:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function gen(width,height,bg_color,border_color,item,shadow)
|
|
|
|
local extra = shadow and 5 or 0
|
|
|
|
local img = cairo.ImageSurface(cairo.Format.ARGB32, width+extra,height+extra)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
|
|
|
rect(cr,width,height,0,0)
|
|
|
|
|
|
|
|
cr:set_source(color(bg_color))
|
2015-01-02 23:52:36 +01:00
|
|
|
if item.item_border_color then
|
|
|
|
cr:fill_preserve()
|
|
|
|
cr:set_line_width(item.border_width or 1)
|
|
|
|
cr:set_source(color(item.item_border_color))
|
|
|
|
cr:stroke()
|
|
|
|
else
|
|
|
|
cr:fill()
|
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
return cairo.Pattern.create_for_surface(img)
|
|
|
|
end
|
|
|
|
|
2015-12-29 11:19:23 +01:00
|
|
|
local function widget_draw(self, context, cr, width, height,shadow)
|
2015-01-02 23:52:36 +01:00
|
|
|
local item = self._item
|
|
|
|
local state = item.state or {}
|
2014-10-06 00:01:54 +02:00
|
|
|
local current_state = state._current_key or ""
|
|
|
|
if not state_cache[current_state] then
|
|
|
|
state_cache[current_state] = {}
|
|
|
|
end
|
|
|
|
local cache = state_cache[current_state]
|
|
|
|
local hash = width+1234*height
|
|
|
|
|
|
|
|
local cached = cache[hash]
|
|
|
|
|
|
|
|
--Generate the pixmap
|
|
|
|
if not cached then
|
|
|
|
local state_name = current_state == "" and "bg" or "bg_"..(base.colors_by_id[current_state] or "")
|
2015-01-11 05:59:51 +01:00
|
|
|
cached = gen(width,height,item[state_name],bc,item,shadow)
|
2014-10-06 00:01:54 +02:00
|
|
|
cache[hash] = cached
|
|
|
|
end
|
|
|
|
|
|
|
|
if current_state ~= self._last_state then
|
|
|
|
self:set_bg(cached)
|
|
|
|
self._last_state = current_state
|
|
|
|
end
|
|
|
|
|
2015-12-29 11:19:23 +01:00
|
|
|
if wibox.widget.background.draw then
|
|
|
|
wibox.widget.background.draw(self, context, cr, width, height)
|
|
|
|
end
|
2014-02-15 05:35:53 +01:00
|
|
|
end
|
|
|
|
|
2015-12-29 11:19:23 +01:00
|
|
|
local function draw_width_shadow(self, context, cr, width, height)
|
2014-02-15 05:35:53 +01:00
|
|
|
|
2015-01-11 05:59:51 +01:00
|
|
|
cr:save()
|
|
|
|
cr:reset_clip()
|
|
|
|
cr:set_source_rgba(0,0,0,.1)
|
|
|
|
cr:set_line_width(1)
|
|
|
|
cr:translate(3,3)
|
|
|
|
for i=1,3 do
|
|
|
|
cr:translate(-1,-1)
|
|
|
|
rect(cr,width,height)
|
|
|
|
cr:fill()
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2015-01-11 05:59:51 +01:00
|
|
|
cr:restore()
|
|
|
|
|
2015-12-29 11:19:23 +01:00
|
|
|
widget_draw(self, context, cr, width, height,true)
|
2015-01-11 05:59:51 +01:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
|
2015-01-11 05:59:51 +01:00
|
|
|
local function common(item,args)
|
2014-02-21 04:29:40 +01:00
|
|
|
local state = item.state or {}
|
2014-02-22 04:22:13 +01:00
|
|
|
local current_state = state._current_key or nil
|
|
|
|
local state_name = base.colors_by_id[current_state]
|
2014-02-21 04:29:40 +01:00
|
|
|
|
2014-02-22 04:22:13 +01:00
|
|
|
if current_state == base.item_flags.SELECTED or (item._tmp_menu) then
|
2014-03-02 22:28:30 +01:00
|
|
|
item.widget:set_fg(item["fg_focus"])
|
2014-10-06 00:01:54 +02:00
|
|
|
elseif state_name then
|
2014-03-02 22:28:30 +01:00
|
|
|
item.widget:set_fg( item["fg_"..state_name])
|
2013-05-11 21:02:42 +02:00
|
|
|
else
|
2014-03-02 22:28:30 +01:00
|
|
|
item.widget:set_fg(item["fg_normal"])
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-11 05:59:51 +01:00
|
|
|
local function draw(item,args)
|
|
|
|
local args = args or {}
|
|
|
|
|
2015-12-29 11:19:23 +01:00
|
|
|
item.widget.draw = widget_draw
|
|
|
|
|
2015-01-11 05:59:51 +01:00
|
|
|
if not item.widget._overlay_init then
|
|
|
|
item.widget._overlay_init = true
|
|
|
|
item.widget._item = item
|
|
|
|
end
|
|
|
|
|
|
|
|
common(item,args)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Same as the default, but also draw a shadow
|
|
|
|
local function shadow(item,args)
|
|
|
|
local args = args or {}
|
|
|
|
|
2015-12-29 11:19:23 +01:00
|
|
|
item.widget.draw = draw_width_shadow
|
|
|
|
|
2015-01-11 05:59:51 +01:00
|
|
|
if not item.widget._overlay_init then
|
|
|
|
item.widget._overlay_init = true
|
|
|
|
item.widget._item = item
|
|
|
|
end
|
|
|
|
|
|
|
|
common(item,args)
|
|
|
|
end
|
|
|
|
module.shadow = {}
|
|
|
|
setmetatable(module.shadow, { __call = function(_, ...) return shadow(...) end })
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
return setmetatable(module, { __call = function(_, ...) return draw(...) end })
|
|
|
|
-- kate: space-indent on; indent-width 2; replace-tabs on;
|