2013-05-11 21:02:42 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local beautiful = require( "beautiful" )
|
|
|
|
local color = require( "gears.color" )
|
|
|
|
local cairo = require( "lgi" ).cairo
|
|
|
|
local base = require( "radical.base" )
|
2014-03-24 00:00:43 +01:00
|
|
|
local glib = require("lgi").GLib
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
local module = {
|
|
|
|
margins = {
|
|
|
|
BOTTOM = 10,
|
|
|
|
TOP = 10,
|
|
|
|
LEFT = 0 ,
|
|
|
|
RIGHT = 0 ,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local function rotate(img, geometry, angle,swap_size)
|
|
|
|
geometry = swap_size and {width = geometry.height, height=geometry.width} or geometry
|
|
|
|
local matrix,pattern,img2 = cairo.Matrix(),cairo.Pattern.create_for_surface(img),cairo.ImageSurface(cairo.Format.ARGB32, geometry.width, geometry.height)
|
|
|
|
cairo.Matrix.init_rotate(matrix,angle)
|
|
|
|
matrix:translate((angle == math.pi/2) and 0 or -geometry.width, (angle == 3*(math.pi/2)) and 0 or -geometry.height)
|
|
|
|
pattern:set_matrix(matrix)
|
|
|
|
local cr2 = cairo.Context(img2)
|
|
|
|
cr2:set_source(pattern)
|
|
|
|
cr2:paint()
|
|
|
|
return img2
|
|
|
|
end
|
|
|
|
|
|
|
|
local function do_gen_menu_top(data, width, height, radius,padding,args)
|
|
|
|
local img = cairo.ImageSurface(cairo.Format.ARGB32, width,height)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
local no_arrow = data.arrow_type == base.arrow_type.NONE
|
|
|
|
local top_padding = (data.arrow_type == base.arrow_type.NONE) and 0 or 13
|
2014-03-27 04:21:42 +01:00
|
|
|
local arrow_x = data._arrow_x or 20
|
2013-05-11 21:02:42 +02:00
|
|
|
cr:set_operator(cairo.Operator.SOURCE)
|
|
|
|
cr:set_source( color(args.bg) )
|
|
|
|
cr:paint()
|
|
|
|
cr:set_source( color(args.fg) )
|
|
|
|
cr:rectangle(10, top_padding+padding, width - 20 +1 , 10)
|
|
|
|
if not no_arrow then
|
|
|
|
for i=1,13 do
|
2014-03-27 04:21:42 +01:00
|
|
|
cr:rectangle((arrow_x) + 13 - i, i+padding , 2*i , 1)
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
end
|
2014-03-27 04:21:42 +01:00
|
|
|
cr:rectangle(arrow_x+padding,top_padding+padding,26+padding,10)
|
2013-05-11 21:02:42 +02:00
|
|
|
cr:rectangle(padding or 0,no_arrow and 10 or 23, width-2*padding, height-33 + (no_arrow and 13 or 0))
|
|
|
|
cr:rectangle(10+padding-1,height-10, width-20, 10-padding)
|
|
|
|
cr:fill()
|
|
|
|
cr:arc(10,10+top_padding,(radius-padding),0,2*math.pi)
|
|
|
|
cr:arc(width-10, 10+top_padding + (pdding or 0),(radius-padding),0,2*math.pi)
|
|
|
|
cr:arc(10,height-(radius-padding)-padding,(radius-padding),0,2*math.pi)
|
|
|
|
cr:arc(width-10,height-(radius-padding)-padding,(radius-padding),0,2*math.pi)
|
|
|
|
cr:fill()
|
|
|
|
return img
|
|
|
|
end
|
|
|
|
|
2014-06-01 05:45:46 +02:00
|
|
|
local function gen_arrow_x(data,direction)
|
2014-03-27 04:21:42 +01:00
|
|
|
local at = data.arrow_type
|
|
|
|
local par_center_x = data.parent_geometry and (data.parent_geometry.x + data.parent_geometry.width/2) or -1
|
|
|
|
local par_center_y = data.parent_geometry and (data.parent_geometry.y + data.parent_geometry.height/2) or -1
|
|
|
|
local menu_beg_x = data.x
|
|
|
|
local menu_end_x = data.x + data.width
|
|
|
|
|
|
|
|
if at == base.arrow_type.PRETTY or not at then
|
|
|
|
if direction == "left" then
|
2014-06-01 05:45:46 +02:00
|
|
|
data._arrow_x = data._internal.w.height -20 - (data.arrow_x_orig or 20)
|
2014-03-27 04:21:42 +01:00
|
|
|
elseif direction == "right" then
|
|
|
|
--TODO
|
|
|
|
elseif direction == "bottom" then
|
2014-06-01 05:45:46 +02:00
|
|
|
data._arrow_x = data.width -20 - (data.arrow_x_orig or 20)
|
2014-03-27 04:21:42 +01:00
|
|
|
if par_center_x >= menu_beg_x then
|
|
|
|
data._arrow_x = data.width - (par_center_x - menu_beg_x) - 13
|
|
|
|
end
|
|
|
|
elseif direction == "top" then
|
|
|
|
--TODO
|
|
|
|
end
|
|
|
|
elseif at == base.arrow_type.CENTERED then
|
|
|
|
data._arrow_x = data.width/2 - 13
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-24 00:00:43 +01:00
|
|
|
local function _set_direction(data,direction)
|
2014-06-01 05:45:46 +02:00
|
|
|
local height,width = data.height,data.width
|
|
|
|
local hash = height*1000+width
|
2014-07-28 01:20:04 +02:00
|
|
|
print("DIR",direction,hash,data._arrow_x,data._internal.last_size)
|
2014-06-01 05:45:46 +02:00
|
|
|
|
|
|
|
-- Try not to waste time for nothing
|
|
|
|
if data._internal._last_direction == direction..(hash) then return end
|
|
|
|
|
|
|
|
-- Avoid recomputing the arrow_x value
|
|
|
|
if not data._arrow_x or data._internal.last_size ~= hash then
|
|
|
|
gen_arrow_x(data,direction)
|
|
|
|
data._internal.last_size = hash
|
2014-05-01 22:25:46 +02:00
|
|
|
end
|
2014-06-01 05:45:46 +02:00
|
|
|
|
|
|
|
local geometry = (direction == "left" or direction == "right") and {width = height, height = width} or {height = height, width = width}
|
2013-07-13 07:05:46 +02:00
|
|
|
local top_clip_surface = do_gen_menu_top(data,geometry.width,geometry.height,10,data.border_width,{bg=beautiful.fg_normal or "#0000ff",fg=data.bg or "#00ffff"})
|
2013-05-11 21:02:42 +02:00
|
|
|
local top_bounding_surface = do_gen_menu_top(data,geometry.width,geometry.height,10,0,{bg="#00000000",fg="#ffffffff"})
|
|
|
|
|
|
|
|
local arr_margin,angle,mar_func = (data.arrow_type == base.arrow_type.NONE) and 0 or 13,0
|
|
|
|
if direction == "bottom" then
|
|
|
|
angle,swap = math.pi,false
|
|
|
|
elseif direction == "left" then
|
|
|
|
angle,swap = math.pi/2,true
|
|
|
|
elseif direction == "right" then
|
|
|
|
angle,swap = 3*math.pi/2,true
|
|
|
|
end
|
|
|
|
if angle ~= 0 then
|
|
|
|
top_bounding_surface = rotate(top_bounding_surface,geometry,angle,swap)
|
|
|
|
top_clip_surface = rotate(top_clip_surface,geometry,angle,swap)
|
|
|
|
end
|
|
|
|
data.wibox.shape_bounding = top_bounding_surface._native
|
|
|
|
data.wibox:set_bg(cairo.Pattern.create_for_surface(top_clip_surface))
|
2014-03-24 00:00:43 +01:00
|
|
|
data._internal._need_direction_reload = false
|
2014-06-01 05:45:46 +02:00
|
|
|
data._internal._last_direction = direction..(hash)
|
2014-03-24 00:00:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Try to avoid useless repaint, this function is heavy
|
|
|
|
local function set_direction(data,direction)
|
2014-03-24 05:04:19 +01:00
|
|
|
data._internal._need_direction = direction
|
2014-06-01 05:45:46 +02:00
|
|
|
if not data._internal._need_direction_reload and data._internal._last_direction ~= direction..(data.height*1000+data.width) then
|
2014-03-24 05:04:19 +01:00
|
|
|
glib.idle_add(glib.PRIORITY_HIGH_IDLE, function() _set_direction(data,data._internal._need_direction) end)
|
2014-03-24 00:00:43 +01:00
|
|
|
data._internal._need_direction_reload = true
|
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
|
2014-06-01 05:45:46 +02:00
|
|
|
local function get_arrow_x(data)
|
|
|
|
local height,width = data.height,data.width
|
|
|
|
local hash = height*1000+width
|
|
|
|
if not data._arrow_x or data._internal.last_size ~= hash then
|
|
|
|
gen_arrow_x(data,direction)
|
2014-07-28 01:20:04 +02:00
|
|
|
-- data._internal.last_size = hash
|
2014-06-01 05:45:46 +02:00
|
|
|
end
|
|
|
|
return data._arrow_x
|
|
|
|
end
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
local function draw(data,args)
|
|
|
|
local args = args or {}
|
|
|
|
local direction = data.direction or "top"
|
2014-06-01 05:45:46 +02:00
|
|
|
if not data.get_arrow_x then
|
|
|
|
rawset(data,"arrow_x_orig",data.arrow_x)
|
|
|
|
rawset(data,"arrow_x_orig",nil)
|
|
|
|
data.get_arrow_x = get_arrow_x
|
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
set_direction(data,direction)
|
2014-02-02 22:53:09 +01:00
|
|
|
-- data._internal.set_position(data) --TODO DEAD CODE?
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
local margins = data.margins
|
|
|
|
local margin = data._internal.margin
|
|
|
|
for k,v in pairs(margins) do
|
|
|
|
margin["set_"..k](margin,v)
|
|
|
|
end
|
|
|
|
return w,w2
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(module, { __call = function(_, ...) return draw(...) end })
|
|
|
|
-- kate: space-indent on; indent-width 2; replace-tabs on;
|