common: Make overlay support available to all widgets

This is the first step toward removing the underlay/overlay from
all item.style and item.layout and use the common implementation.

This will simplify the code a lot.
This commit is contained in:
Emmanuel Lepage Vallee 2015-12-29 08:36:06 -05:00
parent 1229da75c7
commit 7c0425fdfd
2 changed files with 74 additions and 18 deletions

View File

@ -35,28 +35,75 @@ local function set_menu(self,menu,button)
return bt
end
local function _underlay_draw(self, context, cr, width, height)
local function layer_draw_common(self, context, cr, width, height, typename)
cr:save()
local udl = underlay.draw(self._underlay,{height=height,style = self._underlay_style,bg=self._underlay_color})
local udl = underlay.draw(self["_"..typename], {
height = height,
style = self["_"..typename.."_style"],
bg = self["_"..typename.."_color"]
},
context
)
cr:set_source_surface(udl,width-udl:get_width()-3)
cr:paint_with_alpha(self._underlay_alpha or beautiful.underlay_alpha or 0.7)
cr:paint_with_alpha(self["_"..typename.."_alpha"] or beautiful[typename.."_alpha"] or 0.7)
cr:restore()
self._draw_underlay(self, context, cr, width, height)
end
local function set_underlay(self,udl,args)
local args = args or {}
if not self._draw_underlay then
self._draw_underlay = self.draw
self.draw = _underlay_draw
local function draw_underlay(self, context, cr, width, height)
layer_draw_common(self, context, cr, width, height, "underlay")
if self._draw_original then
self._draw_original(self, context, cr, width, height)
end
self._underlay = udl
self._underlay_style = args.style
self._underlay_alpha = args.alpha
self._underlay_color = args.color
end
local function draw_overlay(self, context, cr, width, height)
if self._draw_original then
self._draw_original(self, context, cr, width, height)
end
layer_draw_common(self, context, cr, width, height, "overlay")
end
local function set_layer_common(typename, self ,udl ,args)
local args = args or {}
if self.draw and not self._draw_original then
self._draw_original = self.draw
if typename == "underlay" then
self.draw = draw_underlay
elseif typename == "overlay" then
self.draw = draw_overlay
end
elseif typename == "underlay" then
self.before_draw_children = draw_underlay
elseif typename == "overlay" then
self.after_draw_children = draw_overlay
end
-- TODO detect if it is a Radical item and get those properties,
-- then, delete item.layout implementations
self["_"..typename ] = udl
self["_"..typename.."_style"] = args.style
self["_"..typename.."_alpha"] = args.alpha
self["_"..typename.."_color"] = args.color
self:emit_signal("widget::updated")
end
local function set_underlay(...)
set_layer_common("underlay",...)
end
local function set_overlay(...)
set_layer_common("overlay",...)
end
local function get_preferred_size(self, context, width, height)
local context = context or 1
@ -76,6 +123,7 @@ base.make_widget = function(...)
ret.set_tooltip = set_tooltip
ret.set_menu = set_menu
ret.set_underlay = set_underlay
ret.set_overlay = set_overlay
-- Textboxes already have it
if not ret.get_preferred_size then

View File

@ -51,11 +51,14 @@ function module.draw_arrow(cr,x,y,width,height,padding,args)
cr:restore()
end
function module.fit(text,args)
function module.fit(text, args, context)
local context = context or {}
local args = args or {}
local height = args.height or (beautiful.menu_height)
local padding = height/4--beautiful.default_height/3
local dpi = context.dpi or beautiful.xresources.get_dpi(1)
-- Get text height
if not pango_l[height] then
local pango_crx = pangocairo.font_map_get_default():create_context()
@ -68,21 +71,26 @@ function module.fit(text,args)
pango_l[height]:set_font_description(desc)
end
local pango_layout = pango_l[height]
pango_layout:get_context():set_resolution(dpi)
pango_layout:context_changed()
-- Compute full width
local ret,full_width = {},0
for k,v in ipairs(type(text) == "table" and text or {text}) do
pango_l[height].text = v
ret[k] = pango_l[height]:get_pixel_extents().width + height + padding
pango_layout.text = v
ret[k] = pango_layout:get_pixel_extents().width + height + padding
full_width = full_width + ret[k]
end
return full_width,ret
end
function module.draw(text,args)
function module.draw(text, args, context)
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)
local full_width,ret = module.fit(text, args, context)
local img = cairo.ImageSurface.create(cairo.Format.ARGB32, full_width+(args.padding_right or 0), height+padding)
cr = cairo.Context(img)