More style/colors oriented refactoring

This commit is contained in:
Emmanuel Lepage Vallee 2014-03-02 16:28:30 -05:00
parent fabd52d1a6
commit 95ed8b26c4
14 changed files with 125 additions and 61 deletions

View File

@ -35,11 +35,12 @@ local module = {
SELECTED = 3 , -- Single item selected [[FOCUS]]
PRESSED = 4 , -- Mouse pressed
HOVERED = 5 , -- Mouse hover
USED = 6 , -- Common flag
CHECKED = 7 , -- When checkbox isn't enough
ALTERNATE = 8 ,
HIGHLIGHT = 9 ,
HEADER = 10,
CHANGED = 6 , -- The item changed, need attention
USED = 7 , -- Common flag
CHECKED = 8 , -- When checkbox isn't enough
ALTERNATE = 9 ,
HIGHLIGHT = 10 ,
HEADER = 11,
-- Implementation defined flags
USR1 = 101,
@ -61,6 +62,7 @@ theme.register_color(module.item_flags.URGENT , "urgent" , "urgent" , t
theme.register_color(module.item_flags.SELECTED , "focus" , "focus" , true )
theme.register_color(module.item_flags.PRESSED , "pressed" , "pressed" , true )
theme.register_color(module.item_flags.HOVERED , "hover" , "hover" , true )
theme.register_color(module.item_flags.CHANGED , "changed" , "changed" , true )
theme.register_color(module.item_flags.USED , "used" , "used" , true )
theme.register_color(module.item_flags.CHECKED , "checked" , "checked" , true )
theme.register_color(module.item_flags.ALTERNATE , "alternate" , "alternate" , true )
@ -163,7 +165,7 @@ local function add_item(data,args)
if args.selected == true then
item.selected = true
end
item.index = data.rowcount
return item
end
@ -323,7 +325,7 @@ local function new(args)
-- data._tmp_menu = nil
data._current_item._tmp_menu = nil
-- data._current_item.selected = false
data.item_style(data,data._current_item,{})
data.item_style(data._current_item,{})
end
if internal.has_changed and data.style then
data.style(data,{arrow_x=20,margin=internal.margin})
@ -432,6 +434,7 @@ local function new(args)
end
if idx1 and idx2 then
internal.items[idx1],internal.items[idx2] = internal.items[idx2],internal.items[idx1]
item1.index,item2.index = idx2,idx1
data:emit_signal("item::swapped",item1,item2,idx1,idx2)
end
end
@ -454,7 +457,11 @@ local function new(args)
end
if idx ~= idx1 then
table.insert(internal.items,idx1,table.remove(internal.items,idx))
item.index = idx
data:emit_signal("item::moved",item,idx,idx1)
for i=idx,idx1 do
internal.items[i][1].index = i
end
end
end
end
@ -471,6 +478,9 @@ local function new(args)
if idx1 then
table.remove(internal.items,idx1)
data:emit_signal("item::removed",item,idx1)
for i=idx1,#internal.items do
internal.items[i][1].index = i
end
end
end

View File

@ -5,6 +5,7 @@ local capi = { mouse = mouse, screen = screen }
local function set_position(data)
local s = data.screen or capi.mouse.screen
s = s > capi.screen.count() and 1 or s
local geom = capi.screen[s].geometry
data.wibox.x = geom.x + (geom.width/2) - data.width/2
data.wibox.y = geom.y + (geom.height/2) - data.height/2
@ -15,8 +16,7 @@ local function new(args)
args.internal = args.internal or {}
args.arrow_type = base.arrow_type.NONE
args.internal.set_position = args.internal.set_position or set_position
local ret = context(args)
return ret
return context(args)
end
return setmetatable({}, { __call = function(_, ...) return new(...) end })

View File

@ -78,15 +78,14 @@ local function new_item(data,args)
local item,set_map,get_map,private_data = object({
private_data = {
text = args.text or "" ,
height = args.height or beautiful.menu_height or 30 ,
height = args.height or data.item_height or beautiful.menu_height or 30 ,
width = args.width or nil ,
icon = args.icon or nil ,
prefix = args.prefix or "" ,
suffix = args.suffix or "" ,
bg = args.bg or nil ,
fg = args.fg or data.fg or beautiful.menu_fg_normal or beautiful.fg_normal ,
fg_focus = args.fg_focus or data.fg_focus or beautiful.menu_fg_focus or beautiful.fg_focus ,
bg_focus = args.bg_focus or data.bg_focus or beautiful.menu_bg_focus or beautiful.bg_focus ,
fg = args.fg or data.fg , --TODO don't do this
border_color= args.border_color or data.border_color ,
bg_prefix = args.bg_prefix or data.bg_prefix ,
sub_menu_m = (args.sub_menu and type(args.sub_menu) == "table" and args.sub_menu.is_menu) and args.sub_menu or nil,
sub_menu_f = (args.sub_menu and type(args.sub_menu) == "function") and args.sub_menu or nil ,
@ -94,15 +93,15 @@ local function new_item(data,args)
checked = args.checked or false ,
underlay = args.underlay or nil ,
tooltip = args.tooltip or nil ,
item_style = args.item_style or nil ,
style = args.style or data.item_style ,
item_layout = args.item_layout or nil ,
selected = false ,
overlay = args.overlay or data.overlay or nil ,
state = theme.init_state() ,
},
force_private = {
visible = true,
selected = true,
index = true,
},
get_map = {
y = function() return (args.y and args.y >= 0) and args.y or data.height - (data.margins.top or data.border_width) - data.item_height end, --Hack around missing :fit call for last item
@ -113,6 +112,14 @@ local function new_item(data,args)
})
item._private_data = private_data
item._internal = {get_map=get_map,set_map=set_map}
theme.setup_item_colors(data,item,args)
item.get_bg = function()
return data.bg
end
item.get_fg = function()
return data.fg
end
item.state = theme.init_state(item)
for i=1,10 do
item["button"..i] = args["button"..i]
@ -134,7 +141,7 @@ local function new_item(data,args)
set_map.selected = function(value)
private_data.selected = value
if value == false then
data.item_style(data,item,{})
data.item_style(item,{})
return
end
local current_item = data._current_item
@ -145,14 +152,14 @@ local function new_item(data,args)
current_item._tmp_menu = nil
data._tmp_menu = nil
end
data.item_style(data,current_item,{})
data.item_style(current_item,{})
current_item.selected = false
end
if data.sub_menu_on == module.event.SELECTED and current_item ~= item then
module.execute_sub_menu(data,item)
end
item.state[module.item_flags.SELECTED] = true
data.item_style(data,item,{})
data.item_style(item,{})
data._current_item = item
end
return item

View File

@ -87,7 +87,7 @@ function module:setup_hover(item,data)
item._internal.set_map.hover = function(value)
local item_style = item.item_style or data.item_style
item.state[5] = value and true or nil
item_style(data,item,{})
item_style(item,{})
end
end
@ -250,18 +250,22 @@ local function create_item(item,data,args)
item.widget:emit_signal("widget::updated")
end
-- Draw
local item_style = item.item_style or data.item_style
item_style(data,item,{})
item.widget:set_fg(item._private_data.fg)
item._internal.text_w = tb
item._internal.icon_w = icon
item._internal.margin_w = m
-- Draw
local item_style = item.item_style or data.item_style
item_style(item,{})
item.widget:set_fg(item._private_data.fg)
-- Setup events
module.setup_event(data,item)
if item.buttons then
bg:buttons(item.buttons)
end
return bg
end

View File

@ -29,6 +29,7 @@ module.get_end_arrow = function(args)
local img = cairo.ImageSurface(cairo.Format.ARGB32, width+(args.padding or 0), height)
local cr = cairo.Context(img)
cr:set_source(color(args.bg_color or beautiful.bg_normal))
cr:set_antialias(cairo.ANTIALIAS_NONE)
cr:new_path()
if (args.direction == "left") then
cr:move_to(0,width+(args.padding or 0))
@ -58,6 +59,7 @@ module.get_beg_arrow = function(args)
local img = cairo.ImageSurface(cairo.Format.ARGB32, width, height)
local cr = cairo.Context(img)
cr:set_source(color(args.bg_color or beautiful.fg_normal))
cr:set_antialias(cairo.ANTIALIAS_NONE)
cr:new_path()
if (args.direction == "left") then
cr:move_to(0,width)
@ -101,7 +103,7 @@ local function get_prev(data,item)
end
end
local function draw(data,item,args)
local function draw(item,args)
local args = args or {}
if item.widget.draw ~= draw_real then
item.widget.draw = draw_real
@ -120,19 +122,22 @@ local function draw(data,item,args)
local prev_item = get_prev(data,item)
if current_state == base.item_flags.SELECTED or (item._tmp_menu) then
if prev_item and prev_item.widget.next_color ~= (args.color or data.bg_focus) then
prev_item.widget.next_color = args.color or data.bg_focus
if prev_item and prev_item.widget.next_color ~= (args.color) then
prev_item.widget.next_color = args.color
prev_item.widget:emit_signal("widget::updated")
end
item.widget:set_bg(args.color or data.bg_focus)
item.widget:set_fg(item["fg_focus"])
item.widget:set_bg(args.color)
elseif state_name then --TODO untested, most likely broken
item.widget:set_bg(args.color or item["bg_"..state_name] or data["bg_"..state_name])
item.widget:set_bg(args.color or item["bg_"..state_name])
item.widget:set_fg( item["fg_"..state_name])
else
if prev_item and prev_item.widget.next_color ~= hcode[color_idx] then
prev_item.widget.next_color = hcode[color_idx]
prev_item.widget:emit_signal("widget::updated")
end
item.widget:set_bg(args.color or hcode[color_idx])
item.widget:set_fg(item["fg_normal"])
end
end

View File

@ -53,7 +53,7 @@ local function widget_draw(self, w, cr, width, height)
end
end
local function draw(data,item,args)
local function draw(item,args)
local args = args or {}
if not item.widget._overlay_init then
@ -85,11 +85,14 @@ local function draw(data,item,args)
local state_name = base.colors_by_id[current_state]
if current_state == base.item_flags.SELECTED or (item._tmp_menu) then
item.widget:set_bg(args.color or data.bg_focus)
item.widget:set_bg(args.color or item.bg_focus)
item.widget:set_fg(item["fg_focus"])
elseif state_name then
item.widget:set_bg(args.color or item["bg_"..state_name] or data["bg_"..state_name])
item.widget:set_bg(args.color or item["bg_"..state_name])
item.widget:set_fg( item["fg_"..state_name])
else
item.widget:set_bg(args.color or nil)
item.widget:set_fg(item["fg"])
end
end

View File

@ -36,7 +36,7 @@ local function suffix_draw(self, w, cr, width, height)
end
local function draw(data,item,args)
local function draw(item,args)
local args = args or {}
item.widget.draw = suffix_draw
@ -45,11 +45,14 @@ local function draw(data,item,args)
local current_state = state._current_key or nil
local state_name = base.colors_by_id[current_state]
if current_state == base.item_flags.SELECTED or (item._tmp_menu) then
item.widget:set_bg(args.color or item.bg_focus or data.bg_focus)
item.widget:set_bg(args.color or item.bg_focus)
item.widget:set_fg(item.fg_focus)
elseif state_name then
item.widget:set_bg(args.color or item["bg_"..state_name] or data["bg_"..state_name])
item.widget:set_bg(args.color or item["bg_"..state_name])
item.widget:set_fg( item["fg_"..state_name])
else
item.widget:set_bg(args.color or nil)
-- item.widget:set_fg(item["fg"])
end
end

View File

@ -20,7 +20,7 @@ local function widget_draw23(self, w, cr, width, height)
end
end
local function draw(data,item,args)
local function draw(item,args)
local args = args or {}
if not item.widget._overlay_init and not item.widget._draw then
@ -34,11 +34,14 @@ local function draw(data,item,args)
local state_name = base.colors_by_id[current_state]
if current_state == base.item_flags.SELECTED or (item._tmp_menu) then
item.widget:set_bg(args.color or data.bg_focus)
item.widget:set_bg(item.bg_focus)
item.widget:set_fg(item.fg_focus)
elseif state_name then
item.widget:set_bg(args.color or item["bg_"..state_name] or data["bg_"..state_name])
item.widget:set_bg(args.color or item["bg_"..state_name])
item.widget:set_fg( item["fg_"..state_name])
else
item.widget:set_bg(args.color or nil)
-- item.widget:set_fg(item["fg_normal"] or data["fg_normal"])
end
end

View File

@ -35,7 +35,7 @@ local function widget_draw(self, w, cr, width, height)
end
end
local function draw(data,item,args)
local function draw(item,args)
local args = args or {}
local col = args.color
@ -45,14 +45,14 @@ local function draw(data,item,args)
item.widget._overlay_init = true
end
local ih = data.item_height
local ih = item.height
if not focussed or not focussed[ih] then
if not focussed then
focussed,default,alt={},{},{}
end
local bc = data.border_color
focussed[ih] = gen(ih,data.bg_focus,bc)
default [ih] = gen(ih,data.bg,bc)
local bc = item.border_color
focussed[ih] = gen(ih,item.bg_focus,bc)
default [ih] = gen(ih,item.bg,bc)
end
if col and (not alt[col] or not alt[col][ih]) then
alt[col] = alt[col] or {}
@ -60,15 +60,18 @@ local function draw(data,item,args)
end
local state = item.state or {}
-- local current_state = state._current_key or nil --TODO
-- local state_name = base.colors_by_id[current_state]
local current_state = state._current_key or nil
local state_name = base.colors_by_id[current_state]
if current_state == base.item_flags.SELECTED or (item._tmp_menu) then
item.widget:set_bg(focussed[ih])
item.widget:set_fg(item["fg_focus"])
elseif col then
item.widget:set_bg(alt[col][ih])
item.widget:set_fg(item["fg_"..state_name])
else
item.widget:set_bg(default[ih])
-- item.widget:set_fg(item["fg_normal"])
end
end

View File

@ -40,7 +40,7 @@ local function widget_draw(self, w, cr, width, height)
end
end
local function draw(data,item,args)
local function draw(item,args)
local args = args or {}
if not item.widget._overlay_init then
@ -49,14 +49,14 @@ local function draw(data,item,args)
item.widget._overlay_init = true
end
local ih = data.item_height
local ih = item.height
if not focussed or not focussed[ih] then
if not focussed then
focussed,default={},{}
end
local bc = data.border_color
focussed[ih] = gen(ih,data.bg_focus,bc)
default [ih] = gen(ih,data.bg,bc)
local bc = item.border_color
focussed[ih] = gen(ih,item.bg_focus,bc)
default [ih] = gen(ih,item.bg,bc)
end
local state = item.state or {}
@ -65,10 +65,13 @@ local function draw(data,item,args)
if current_state == base.item_flags.SELECTED or (item._tmp_menu) then
item.widget:set_bg(focussed[ih])
item.widget:set_fg(item["fg_focus"])
elseif state_name then --TODO incomplete
item.widget:set_bg(args.color or item["bg_"..state_name] or data["bg_"..state_name])
item.widget:set_bg(args.color or item["bg_"..state_name])
item.widget:set_fg( item["fg_"..state_name])
else
item.widget:set_bg(default[ih])
item.widget:set_fg(item["fg_normal"])
end
end

View File

@ -124,7 +124,7 @@ function module:setup_item(data,item,args)
-- Set widget
item.widget = bg
data.item_style(data,item,{})
data.item_style(item,{})
setup_event(data,item,args)
end

View File

@ -177,7 +177,7 @@ function module:setup_item(data,item,args)
-- Apply item style
local item_style = item.item_style or data.item_style
item_style(data,item,{})
item_style(item,{})
item.widget:emit_signal("widget::updated")
end

View File

@ -1,4 +1,5 @@
local math = math
local rawget,rawset=rawget,rawset
local beautiful = require( "beautiful" )
local module = {
@ -19,14 +20,16 @@ local function change_data(tab, key,value)
end
end
rawset(tab,"_current_key",win ~= math.huge and win or nil)
tab._item:style()
elseif value and (rawget(tab,"_current_key") or math.huge) > key then
rawset(tab,"_current_key",key)
tab._item:style()
end
tab._real_table[key] = value
end
function module.init_state()
function module.init_state(item)
local mt = {__newindex = change_data,__index=return_data}
return setmetatable({_real_table={}},mt)
return setmetatable({_real_table={},_item=item},mt)
end
-- Util to help match colors to states
@ -38,11 +41,31 @@ end
function module.setup_colors(data,args)
local priv = data._internal.private_data
for k,v in pairs(theme_colors) do
priv["fg_"..k] = args["fg_"..k] or beautiful["menu_fg_"..v.beautiful_name] or beautiful["fg_"..v.beautiful_name] or (v.fallback and "#ff0000")
priv["bg_"..k] = args["bg_"..k] or beautiful["menu_bg_"..v.beautiful_name] or beautiful["bg_"..v.beautiful_name] or (v.fallback and "#00ff00")
priv["fg_"..k] = args["fg_"..k] or beautiful["menu_fg_"..v.beautiful_name] or beautiful["fg_"..v.beautiful_name] or (v.fallback and beautiful.fg_normal)
priv["bg_"..k] = args["bg_"..k] or beautiful["menu_bg_"..v.beautiful_name] or beautiful["bg_"..v.beautiful_name] or (v.fallback and beautiful.bg_normal)
end
end
function module.setup_item_colors(data,item,args)
local priv = item._private_data
for k,v in pairs(theme_colors) do
if args["fg_"..k] then
priv["fg_"..k] = args["fg_"..k]
else
rawset(item,"get_fg_"..k,function()
return data["fg_"..k]
end)
end
if args["bg_"..k] then
priv["bg_"..k] = args["bg_"..k]
else
rawset(item,"get_bg_"..k, function()
return data["bg_"..k]
end)
end
end
end
return setmetatable(module, { __call = function(_, ...) return new(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;

View File

@ -65,12 +65,12 @@ local function new(data)
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]},{color=data.bg_highlight})
data.item_style({widget=scroll_w[v]},{color=data.bg_highlight})
scroll_w[v]:connect_signal("mouse::enter",function()
data.item_style(data,{widget=scroll_w[v]},{color=data.bg_alternate or beautiful.bg_focus})
data.item_style({widget=scroll_w[v]},{color=data.bg_alternate or beautiful.bg_focus})
end)
scroll_w[v]:connect_signal("mouse::leave",function()
data.item_style(data,{widget=scroll_w[v]},{color=data.bg_highlight})
data.item_style({widget=scroll_w[v]},{color=data.bg_highlight})
end)
scroll_w[v]:buttons( util.table.join( button({ }, 1, function()
data["scroll_"..v](data)