Make vertical menu code more modular, easier to maintain

This commit is contained in:
Emmanuel Lepage Vallee 2013-07-07 00:39:17 -04:00
parent 6fc984e59d
commit 34f7e74a92
7 changed files with 180 additions and 42 deletions

View File

@ -264,6 +264,8 @@ local function new(args)
prefix_widget = args.prefix_widget or nil,
fkeys_prefix = args.fkeys_prefix or false,
underlay_alpha = args.underlay_alpha or 0.7,
filter_prefix = args.filter_prefix or "Filter:",
max_items = args.max_items or nil,
},
get_map = {
is_menu = function() return true end,

View File

@ -1,6 +1,7 @@
local setmetatable = setmetatable
local color = require( "gears.color" )
local cairo = require( "lgi" ).cairo
local beautiful = require( "beautiful" )
local print = print
local module = {
@ -12,7 +13,7 @@ local module = {
}
}
local focussed,default = nil, nil
local focussed,default,alt = nil, nil,nil
local function gen(item_height,bg_color,border_color)
local img = cairo.ImageSurface(cairo.Format.ARGB32, 800,item_height)
@ -25,21 +26,24 @@ local function gen(item_height,bg_color,border_color)
return cairo.Pattern.create_for_surface(img)
end
local function draw(data,item,is_focussed,is_pressed)
local function draw(data,item,is_focussed,is_pressed,is_alt)
local ih = data.item_height
if not focussed or not focussed[ih] then
if not focussed then
focussed,default={},{}
focussed,default,alt={},{},{}
end
local bc = data.border_color
focussed[ih] = gen(ih,data.bg_focus,bc)
default [ih] = gen(ih,data.bg,bc)
alt [ih] = gen(ih,beautiful.bg_highlight,bc)
end
if is_focussed then
item.widget:set_bg(focussed[ih])
elseif is_alt then
item.widget:set_bg(alt[ih])
else
item.widget:set_bg(default[ih])
item.widget:set_bg(default[ih])
end
end

View File

@ -5,6 +5,9 @@ local math = math
local util = require( "awful.util" )
local button = require( "awful.button" )
local checkbox = require( "radical.widgets.checkbox" )
local scroll = require( "radical.widgets.scroll" )
local filter = require( "radical.widgets.filter" )
local fkey = require( "radical.widgets.fkey" )
local beautiful = require("beautiful")
local wibox = require( "wibox" )
local color = require( "gears.color" )
@ -161,27 +164,8 @@ function module:setup_item(data,item,args)
return data._internal.layout.item_fit(data,item,...)
end
local pref
if data.fkeys_prefix == true then
pref = wibox.widget.textbox()
pref.draw = function(self,w, cr, width, height)
cr:set_source(color(beautiful.fg_normal))
cr:arc((height-4)/2 + 2, (height-4)/2 + 2, (height-4)/2,0,2*math.pi)
cr:arc(width - (height-4)/2 - 2, (height-4)/2 + 2, (height-4)/2,0,2*math.pi)
cr:rectangle((height-4)/2+2,2,width - (height),(height-4))
cr:fill()
cr:select_font_face("Verdana", cairo.FontSlant.NORMAL, cairo.FontWeight.BOLD)
cr:set_font_size(height-6)
cr:move_to(height/2,height-4)
cr:set_source(color(beautiful.bg_normal))
local text = (item._internal.f_key and item._internal.f_key <= 12) and ("F"..(item._internal.f_key)) or "---"
cr:show_text(text)
end
pref.fit = function(...)
return 35,data.item_height
end
pref:set_markup("<span fgcolor='".. beautiful.bg_normal .."'><tt><b>F11</b></tt></span>")
l:add(pref)
l:add(fkey(data,item))
m:set_left ( 0 )
end
@ -277,7 +261,7 @@ local function compute_geo(data)
w = data._internal.largest_item_w_v+100 > data.default_width and data._internal.largest_item_w_v+100 or data.default_width
end
if not data._internal.has_widget then
return w,(total and total > 0 and total or data.rowcount*data.item_height) + (data._internal.filter_tb and data.item_height or 0)
return w,(total and total > 0 and total or data.rowcount*data.item_height) + (data._internal.filter_tb and data.item_height or 0) + (data.max_items and data._internal.scroll_w["up"].visible and (2*data.item_height) or 0)
else
local h = (data.rowcount-#data._internal.widgets)*data.item_height
for k,v in ipairs(data._internal.widgets) do
@ -290,25 +274,23 @@ end
local function new(data)
local l,real_l = wibox.layout.fixed.vertical(),nil
local filter_tb = nil
real_l = wibox.layout.fixed.vertical()
if data.max_items then
data._internal.scroll_w = scroll(data)
real_l:add(data._internal.scroll_w["up"])
end
real_l:add(l)
if data.show_filter then
real_l = wibox.layout.fixed.vertical()
real_l:add(l)
filter_tb = wibox.widget.textbox()
local bg = wibox.widget.background()
bg:set_bg(beautiful.bg_highlight)
bg:set_widget(filter_tb)
filter_tb:set_markup("<b>Filter:</b>")
filter_tb.fit = function(tb,width,height)
return width,data.item_height
if data.max_items then
real_l:add(data._internal.scroll_w["down"])
end
data:connect_signal("filter_string::changed",function()
filter_tb:set_markup("<b>Filter:</b> "..data.filter_string)
end)
real_l:add(bg)
data._internal.filter_tb = filter_tb
local filter_tb = filter(data)
real_l:add(filter_tb)
data._internal.filter_tb = filter_tb.widget
else
real_l = l
if data.max_items then
real_l:add(data._internal.scroll_w["down"])
end
end
real_l.fit = function(a1,a2,a3)
if not data.visible then return 1,1 end

28
widgets/filter.lua Normal file
View File

@ -0,0 +1,28 @@
local setmetatable = setmetatable
local print = print
local color = require("gears.color")
local cairo = require( "lgi" ).cairo
local wibox = require("wibox")
local beautiful = require( "beautiful" )
local module = {}
local function new(data)
local filter_tb = wibox.widget.textbox()
local bg = wibox.widget.background()
bg:set_bg(beautiful.bg_highlight)
bg:set_widget(filter_tb)
filter_tb:set_markup(" <b>".. data.filter_prefix .."</b> ")
filter_tb.fit = function(tb,width,height)
return width,data.item_height
end
data:connect_signal("filter_string::changed",function()
filter_tb:set_markup(" <b>".. data.filter_prefix .."</b> "..data.filter_string)
end)
bg.widget = filter_tb
return bg
end
return setmetatable(module, { __call = function(_, ...) return new(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;

34
widgets/fkey.lua Normal file
View File

@ -0,0 +1,34 @@
local setmetatable = setmetatable
local print = print
local color = require("gears.color")
local cairo = require( "lgi" ).cairo
local wibox = require("wibox")
local beautiful = require( "beautiful" )
local module = {}
local function new(data,item)
local pref = wibox.widget.textbox()
pref.draw = function(self,w, cr, width, height)
cr:set_source(color(beautiful.fg_normal))
cr:arc((height-4)/2 + 2, (height-4)/2 + 2, (height-4)/2,0,2*math.pi)
cr:arc(width - (height-4)/2 - 2, (height-4)/2 + 2, (height-4)/2,0,2*math.pi)
cr:rectangle((height-4)/2+2,2,width - (height),(height-4))
cr:fill()
cr:select_font_face("Verdana", cairo.FontSlant.NORMAL, cairo.FontWeight.BOLD)
cr:set_font_size(height-6)
cr:move_to(height/2,height-4)
cr:set_source(color(beautiful.bg_normal))
local text = (item._internal.f_key and item._internal.f_key <= 12) and ("F"..(item._internal.f_key)) or "---"
cr:show_text(text)
end
pref.fit = function(...)
return 35,data.item_height
end
pref:set_markup("<span fgcolor='".. beautiful.bg_normal .."'><tt><b>F11</b></tt></span>")
return pref
end
return setmetatable(module, { __call = function(_, ...) return new(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;

View File

@ -1,3 +1,6 @@
return {
checkbox = require("radical.widgets.checkbox")
checkbox = require( "radical.widgets.checkbox" ),
scroll = require( "radical.widgets.scroll" ),
filter = require( "radical.widgets.filter" ),
fkey = require( "radical.widgets.fkey" ),
}

85
widgets/scroll.lua Normal file
View File

@ -0,0 +1,85 @@
local setmetatable = setmetatable
local print = print
local color = require("gears.color")
local cairo = require( "lgi" ).cairo
local wibox = require("wibox")
local beautiful = require( "beautiful" )
local module = {}
local arr_up
local arr_down
local isinit = false
local function init()
local size = beautiful.menu_height or 16
arr_down = cairo.ImageSurface(cairo.Format.ARGB32, size,size)
arr_up = cairo.ImageSurface(cairo.Format.ARGB32, size,size)
local cr2 = cairo.Context(arr_down)
local cr = cairo.Context(arr_up)
cr:set_operator(cairo.Operator.CLEAR)
cr2:set_operator(cairo.Operator.CLEAR)
cr:paint()
cr2:paint()
cr:set_operator(cairo.Operator.SOURCE)
cr2:set_operator(cairo.Operator.SOURCE)
local sp = 2.5
local rs = size - (2*sp)
cr:set_source(color(beautiful.fg_normal))
cr2:set_source(color(beautiful.fg_normal))
cr:set_line_width(2)
cr2:set_line_width(2)
cr:move_to( sp , sp );cr:line_to( rs , sp )
cr:move_to( sp , sp );cr:line_to( sp , rs )
cr:move_to( sp , rs );cr:line_to( rs , rs )
cr:move_to( rs , sp );cr:line_to( rs , rs )
cr:move_to( sp , sp );cr:line_to( rs , rs )
cr:move_to( sp , rs );cr:line_to( rs , sp )
cr:stroke()
cr2:move_to( sp , sp );cr2:line_to (rs , sp , beautiful.fg_normal )
cr2:move_to( sp , sp );cr2:line_to (sp , rs , beautiful.fg_normal )
cr2:move_to( sp , rs );cr2:line_to (rs , rs , beautiful.fg_normal )
cr2:move_to( rs , sp );cr2:line_to (rs , rs , beautiful.fg_normal )
cr2:stroke()
isinit = true
end
function module.up()
if not isinit then
init()
end
return arr_up
end
function module.down()
if not isinit then
init()
end
return arr_down
end
local function new(data)
local scroll_w = {}
for k,v in ipairs({"up","down"}) do
local ib = wibox.widget.imagebox()
ib:set_image(module[v]())
ib.fit = function(tb,width,height)
return width,data.item_height
end
ib.draw = function(self,wibox, cr, width, height)
cr:set_source_surface(self._image, width/2 - self._image:get_width()/2, 0)
cr:paint()
end
scroll_w[v] = wibox.widget.background()
scroll_w[v]:set_widget(ib)
scroll_w[v].visible = true
data.item_style(data,{widget=scroll_w[v]},false,false,true)
end
return scroll_w
end
return setmetatable(module, { __call = function(_, ...) return new(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;