Add new 'item_layout' concept to fight code duplication between layouts

This commit is contained in:
Emmanuel Lepage Vallee 2014-02-02 16:53:09 -05:00
parent ffcace786f
commit 0376cfaf31
4 changed files with 171 additions and 153 deletions

92
bar.lua
View File

@ -11,6 +11,7 @@ local button = require( "awful.button" )
local checkbox = require( "radical.widgets.checkbox" ) local checkbox = require( "radical.widgets.checkbox" )
local item_style = require( "radical.item_style.arrow_single" ) local item_style = require( "radical.item_style.arrow_single" )
local vertical = require( "radical.layout.vertical" ) local vertical = require( "radical.layout.vertical" )
local item_layout= require( "radical.item_layout.horizontal" )
local capi,module = { mouse = mouse , screen = screen, keygrabber = keygrabber },{} local capi,module = { mouse = mouse , screen = screen, keygrabber = keygrabber },{}
@ -58,95 +59,6 @@ local function setup_drawable(data)
data.draw = internal.margin.draw data.draw = internal.margin.draw
end end
-- Use all the space, let "align_fit" compute the right size
local function textbox_fit(box,w,h)
return w,h
end
-- Force the width or compute the minimum space
local function align_fit(box,w,h)
if box._item.width then return box._item.width - box._data.item_style.margins.LEFT - box._data.item_style.margins.RIGHT,h end
return box.first:fit(w,h)+wibox.widget.textbox.fit(box.second,w,h)+box.third:fit(w,h),h
end
-- Create the actual widget
local function create_item(item,data,args)
-- Background
local bg = wibox.widget.background()
-- Margins
local m = wibox.layout.margin(la)
m:set_margins (0)
m:set_left ( data.item_style.margins.LEFT )
m:set_right ( data.item_style.margins.RIGHT )
m:set_top ( data.item_style.margins.TOP )
m:set_bottom( data.item_style.margins.BOTTOM )
-- Layout (left)
local layout = wibox.layout.fixed.horizontal()
bg:set_widget(m)
-- Layout (right)
local right = wibox.layout.fixed.horizontal()
-- F keys
vertical:setup_fkey(item,data)
if data.fkeys_prefix == true then
layout:add(fkey(data,item))
end
-- Icon
layout:add(vertical:setup_icon(item,data))
-- Prefix
if args.prefix_widget then
layout:add(args.prefix_widget)
end
-- Text
local tb = wibox.widget.textbox()
tb.fit = textbox_fit
tb.draw = function(self,w, cr, width, height)
if item.underlay then
vertical.paint_underlay(data,item,cr,width,height)
end
wibox.widget.textbox.draw(self,w, cr, width, height)
end
item.widget = bg
tb:set_text(item.text)
-- Checkbox
local ck = vertical:setup_checked(item,data)
if ck then
right:add(ck)
end
-- Suffix
if args.suffix_widget then
right:add(args.suffix_widget)
end
-- Layout (align)
local align = wibox.layout.align.horizontal()
align:set_middle( tb )
align:set_left ( layout )
align:set_right ( right )
m:set_widget ( align )
align._item = item
align._data = data
align.fit = align_fit
item._internal.align = align
-- Tooltip
item.widget:set_tooltip(item.tooltip)
-- Draw
data.item_style(data,item,{})
item.widget:set_fg(item._private_data.fg)
return bg
end
local function setup_buttons(data,item,args) local function setup_buttons(data,item,args)
local buttons = {} local buttons = {}
for i=1,10 do for i=1,10 do
@ -178,7 +90,7 @@ end
local function setup_item(data,item,args) local function setup_item(data,item,args)
-- Add widgets -- Add widgets
data._internal.layout:add(create_item(item,data,args)) data._internal.layout:add(item_layout(item,data,args))
item.widget:connect_signal("mouse::enter", function() item.selected = true end) item.widget:connect_signal("mouse::enter", function() item.selected = true end)
item.widget:connect_signal("mouse::leave", function() item.selected = false end) item.widget:connect_signal("mouse::leave", function() item.selected = false end)

163
item_layout/horizontal.lua Normal file
View File

@ -0,0 +1,163 @@
local setmetatable = setmetatable
local beautiful = require( "beautiful" )
local color = require( "gears.color" )
local cairo = require( "lgi" ).cairo
local wibox = require( "wibox" )
local checkbox = require( "radical.widgets.checkbox" )
local fkey = require( "radical.widgets.fkey" )
local module = {}
-- Add [F1], [F2] ... to items
function module:setup_fkey(item,data)
item._internal.set_map.f_key = function(value)
item._internal.has_changed = true
item._internal.f_key = value
data:remove_key_hook("F"..value)
data:add_key_hook({}, "F"..value , "press", function()
item.button1()
data.visible = false
end)
end
item._internal.get_map.f_key = function() return item._internal.f_key end
end
-- Like an overlay, but under
function module.paint_underlay(data,item,cr,width,height)
cr:save()
local udl = underlay.draw(item.underlay)
cr:set_source_surface(udl,width-udl:get_width()-3)
cr:paint_with_alpha(data.underlay_alpha)
cr:restore()
end
-- Setup the item icon
function module:setup_icon(item,data)
local icon = wibox.widget.imagebox()
icon.fit = function(...)
local w,h = wibox.widget.imagebox.fit(...)
return w+3,h
end
if item.icon then
icon:set_image(item.icon)
end
item._internal.set_map.icon = function (value)
icon:set_image(value)
end
return icon
end
-- Show the checkbox
function module:setup_checked(item,data)
if item.checkable then
item._internal.get_map.checked = function()
if type(item._private_data.checked) == "function" then
return item._private_data.checked()
else
return item._private_data.checked
end
end
local ck = wibox.widget.imagebox()
ck:set_image(item.checked and checkbox.checked() or checkbox.unchecked())
item._internal.set_map.checked = function (value)
item._private_data.checked = value
ck:set_image(item.checked and checkbox.checked() or checkbox.unchecked())
item._internal.has_changed = true
end
return ck
end
end
-- Use all the space, let "align_fit" compute the right size
local function textbox_fit(box,w,h)
return w,h
end
-- Force the width or compute the minimum space
local function align_fit(box,w,h)
if box._item.width then return box._item.width - box._data.item_style.margins.LEFT - box._data.item_style.margins.RIGHT,h end
return box.first:fit(w,h)+wibox.widget.textbox.fit(box.second,w,h)+box.third:fit(w,h),h
end
-- Create the actual widget
local function create_item(item,data,args)
-- Background
local bg = wibox.widget.background()
-- Margins
local m = wibox.layout.margin(la)
m:set_margins (0)
m:set_left ( data.item_style.margins.LEFT )
m:set_right ( data.item_style.margins.RIGHT )
m:set_top ( data.item_style.margins.TOP )
m:set_bottom( data.item_style.margins.BOTTOM )
-- Layout (left)
local layout = wibox.layout.fixed.horizontal()
bg:set_widget(m)
-- Layout (right)
local right = wibox.layout.fixed.horizontal()
-- F keys
module:setup_fkey(item,data)
if data.fkeys_prefix == true then
layout:add(fkey(data,item))
end
-- Icon
layout:add(module:setup_icon(item,data))
-- Prefix
if args.prefix_widget then
layout:add(args.prefix_widget)
end
-- Text
local tb = wibox.widget.textbox()
tb.fit = textbox_fit
tb.draw = function(self,w, cr, width, height)
if item.underlay then
module.paint_underlay(data,item,cr,width,height)
end
wibox.widget.textbox.draw(self,w, cr, width, height)
end
item.widget = bg
tb:set_text(item.text)
-- Checkbox
local ck = module:setup_checked(item,data)
if ck then
right:add(ck)
end
-- Suffix
if args.suffix_widget then
right:add(args.suffix_widget)
end
-- Layout (align)
local align = wibox.layout.align.horizontal()
align:set_middle( tb )
align:set_left ( layout )
align:set_right ( right )
m:set_widget ( align )
align._item = item
align._data = data
align.fit = align_fit
item._internal.align = align
-- Tooltip
item.widget:set_tooltip(item.tooltip)
-- Draw
data.item_style(data,item,{})
item.widget:set_fg(item._private_data.fg)
return bg
end
return setmetatable(module, { __call = function(_, ...) return create_item(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;

View File

@ -11,6 +11,7 @@ local beautiful = require("beautiful" )
local wibox = require( "wibox" ) local wibox = require( "wibox" )
local color = require( "gears.color" ) local color = require( "gears.color" )
local cairo = require( "lgi" ).cairo local cairo = require( "lgi" ).cairo
local item_layout= require( "radical.item_layout.horizontal" )
local module = {} local module = {}
@ -65,15 +66,6 @@ local function item_fit(data,item,...)
return w, item._private_data.height or h return w, item._private_data.height or h
end end
-- Like an overlay, but under
function module.paint_underlay(data,item,cr,width,height)
cr:save()
local udl = underlay.draw(item.underlay)
cr:set_source_surface(udl,width-udl:get_width()-3)
cr:paint_with_alpha(data.underlay_alpha)
cr:restore()
end
-- As of July 2013, LGI is too slow to redraw big menus at ok speed -- As of July 2013, LGI is too slow to redraw big menus at ok speed
-- This do a pixmap cache to allow pre-rendering -- This do a pixmap cache to allow pre-rendering
local function cache_pixmap(item) local function cache_pixmap(item)
@ -96,61 +88,12 @@ local function cache_pixmap(item)
end end
end end
function module:setup_fkey(item,data)
item._internal.set_map.f_key = function(value)
item._internal.has_changed = true
item._internal.f_key = value
data:remove_key_hook("F"..value)
data:add_key_hook({}, "F"..value , "press", function()
item.button1()
data.visible = false
end)
end
item._internal.get_map.f_key = function() return item._internal.f_key end
end
function module:setup_checked(item,data)
if item.checkable then
item._internal.get_map.checked = function()
if type(item._private_data.checked) == "function" then
return item._private_data.checked()
else
return item._private_data.checked
end
end
local ck = wibox.widget.imagebox()
ck:set_image(item.checked and checkbox.checked() or checkbox.unchecked())
item._internal.set_map.checked = function (value)
item._private_data.checked = value
ck:set_image(item.checked and checkbox.checked() or checkbox.unchecked())
item._internal.has_changed = true
end
return ck
end
end
function module:setup_icon(item,data)
local icon = wibox.widget.imagebox()
icon.fit = function(...)
local w,h = wibox.widget.imagebox.fit(...)
return w+3,h
end
if item.icon then
icon:set_image(item.icon)
end
item._internal.set_map.icon = function (value)
icon:set_image(value)
end
return icon
end
function module:setup_text(item,data) function module:setup_text(item,data)
local text_w = wibox.widget.textbox() local text_w = wibox.widget.textbox()
text_w.draw = function(self,w, cr, width, height) text_w.draw = function(self,w, cr, width, height)
if item.underlay then if item.underlay then
module.paint_underlay(data,item,cr,width,height) item_layout.paint_underlay(data,item,cr,width,height)
end end
wibox.widget.textbox.draw(self,w, cr, width, height) wibox.widget.textbox.draw(self,w, cr, width, height)
end end
@ -241,11 +184,11 @@ function module:setup_item(data,item,args)
end end
-- Icon -- Icon
local icon = module:setup_icon(item,data) local icon = item_layout:setup_icon(item,data)
l:add(icon) l:add(icon)
-- Checkbox -- Checkbox
local ck = module:setup_checked(item,data) local ck = item_layout:setup_checked(item,data)
if ck then if ck then
lr:add(ck) lr:add(ck)
end end
@ -279,7 +222,7 @@ function module:setup_item(data,item,args)
end end
-- F keys -- F keys
module:setup_fkey(item,data) item_layout:setup_fkey(item,data)
-- Enable scrollbar if necessary -- Enable scrollbar if necessary
if data._internal.scroll_w and data.rowcount > data.max_items then if data._internal.scroll_w and data.rowcount > data.max_items then

View File

@ -94,7 +94,7 @@ local function draw(data,args)
--END set_arrow --END set_arrow
set_direction(data,direction) set_direction(data,direction)
data._internal.set_position(data) -- data._internal.set_position(data) --TODO DEAD CODE?
local margins = data.margins local margins = data.margins
local margin = data._internal.margin local margin = data._internal.margin