Add support for custom item_style and item_layout for each items

This commit is contained in:
Emmanuel Lepage Vallee 2014-02-02 23:01:31 -05:00
parent b87282458f
commit daff0cb4dd
8 changed files with 107 additions and 43 deletions

View File

@ -99,6 +99,14 @@ On top of each styles, menu can also have different layouts to display items:
* **Horizontal:** Items are displayed alongside each other
* **Grid:** Items are displayed as a 2D array
### Item layout
Item layouts are how widgets (icons, label, prefix) are disposed in the item
* **horizontal:** Default layout used by the context style
* **icon:** Look like a desktop icon, used by horizontal menus
* **centerred:** Align widgets at the center instead of using all space
### Using styles and layouts
```lua
@ -143,6 +151,7 @@ Multiple items can have multiple sets of options.
| bg_alternate | Alternate background color | String/gradient/pattern |
| bg_highlight | Highlight background color | String/gradient/pattern |
| bg_header | Header (see widgets section) color | String/gradient/pattern |
| bg_prefix | Prefix background for item_styles that support it | String/gradient/pattern |
| border_color | Border color | String/gradient/pattern |
| border_width | Border width | number |
| item_height | Default height of items | number |
@ -191,6 +200,8 @@ Multiple items can have multiple sets of options.
| underlay | Text to render at the far-right of the item | [array of] string |
| prefix_widget | Widget to append at the begenning of the item| widget |
| suffix_widget | Widget to append at the end of the item | widget |
| item_style | Custom item_style for this item | item_style |
| item_layout | Custom item_layout for this item | item_layout |
| tooltip | A tooltip shown on the side or bottom | string |
| button1 | Left mouse button action | function |
| button2 | Mid mouse button action | function |

View File

@ -129,24 +129,27 @@ end
local function add_item(data,args)
local args = args or {}
local item,set_map,get_map,private_data = object({
private_data = {
text = args.text or "" ,
height = args.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 ,
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 ,
selected = false,
checkable = args.checkable or (args.checked ~= nil) or false,
checked = args.checked or false,
underlay = args.underlay or nil,
tooltip = args.tooltip or nil,
private_data = {
text = args.text or "" ,
height = args.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 ,
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 ,
checkable = args.checkable or (args.checked ~= nil) or false ,
checked = args.checked or false ,
underlay = args.underlay or nil ,
tooltip = args.tooltip or nil ,
item_style = args.item_style or nil ,
item_layout = args.item_layout or nil ,
selected = false,
},
force_private = {
visible = true,
@ -277,6 +280,7 @@ local function new(args)
bg_alternate = args.bg_alternate or beautiful.menu_bg_alternate or beautiful.bg_alternate or beautiful.bg_normal,
bg_highlight = args.bg_highlight or beautiful.menu_bg_highlight or beautiful.bg_highlight or beautiful.bg_normal,
bg_header = args.bg_header or beautiful.menu_bg_header or beautiful.fg_normal,
bg_prefix = args.bg_prefix or nil,
border_color = args.border_color or beautiful.menu_border_color or beautiful.border_color or "#333333",
border_width = args.border_width or beautiful.menu_border_width or beautiful.border_width or 3,
separator_color = args.separator_color or beautiful.menu_separator_color or args.border_color or beautiful.menu_border_color or beautiful.border_color or "#333333",

View File

@ -43,17 +43,18 @@ base.make_widget = function(...)
end
return {
layout = require( "radical.layout" ),
object = require( "radical.object" ),
base = require( "radical.base" ),
radial = require( "radical.radial" ),
context = require( "radical.context" ),
embed = require( "radical.embed" ),
box = require( "radical.box" ),
bar = require( "radical.bar" ),
style = require( "radical.style" ),
item_style = require( "radical.item_style" ),
widgets = require( "radical.widgets" ),
tooltip = tooltip
layout = require( "radical.layout" ),
object = require( "radical.object" ),
base = require( "radical.base" ),
radial = require( "radical.radial" ),
context = require( "radical.context" ),
embed = require( "radical.embed" ),
box = require( "radical.box" ),
bar = require( "radical.bar" ),
style = require( "radical.style" ),
item_style = require( "radical.item_style" ),
widgets = require( "radical.widgets" ),
item_layout = require( "radical.item_layout"),
tooltip = tooltip
}
-- kate: space-indent on; indent-width 2; replace-tabs on;

34
item_layout/centerred.lua Normal file
View File

@ -0,0 +1,34 @@
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 = {}
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 )
local text = wibox.widget.textbox()
-- Layout
local align = wibox.layout.align.horizontal()
align:set_middle( text )
m:set_widget(align)
bg:set_widget(m)
return bg
end
return setmetatable(module, { __call = function(_, ...) return create_item(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on;

View File

@ -89,10 +89,10 @@ local function create_item(item,data,args)
-- 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 )
m:set_left ( (item.item_style or data.item_style).margins.LEFT )
m:set_right ( (item.item_style or data.item_style).margins.RIGHT )
m:set_top ( (item.item_style or data.item_style).margins.TOP )
m:set_bottom( (item.item_style or data.item_style).margins.BOTTOM )
-- Layout (left)
local layout = wibox.layout.fixed.horizontal()
@ -153,7 +153,8 @@ local function create_item(item,data,args)
item.widget:set_tooltip(item.tooltip)
-- Draw
data.item_style(data,item,{})
local item_style = item.item_style or data.item_style
item_style(data,item,{})
item.widget:set_fg(item._private_data.fg)
return bg

5
item_layout/init.lua Normal file
View File

@ -0,0 +1,5 @@
return {
icon = require( "radical.item_layout.icon" ),
horizontal = require( "radical.item_layout.horizontal" ),
centerred = require( "radical.item_layout.centerred" ),
}

View File

@ -17,10 +17,11 @@ local module = {
local function prefix_draw(self, w, cr, width, height)
cr:save()
cr:set_source(color(beautiful.icon_grad or beautiful.fg_normal))
local col = self._item.bg_prefix or beautiful.icon_grad or beautiful.fg_normal
cr:set_source(color(col))
cr:rectangle(0,0,width-height/2-2-height/6,height)
cr:fill()
cr:set_source_surface(arrow_alt.get_beg_arrow({width=height/2+2,height=height,bg_color=beautiful.icon_grad or beautiful.fg_normal}),width-height/2-2 - height/6,0)
cr:set_source_surface(arrow_alt.get_beg_arrow({width=height/2+2,height=height,bg_color=col}),width-height/2-2 - height/6,0)
cr:paint()
cr:restore()
self._draw(self, w, cr, width, height)
@ -33,7 +34,7 @@ end
local function suffix_draw(self, w, cr, width, height)
cr:save()
cr:set_source_surface(arrow_alt.get_end_arrow({width=height/2+2,height=height,bg_color=beautiful.icon_grad or beautiful.fg_normal}),width-height/2-2,0)
cr:set_source_surface(arrow_alt.get_end_arrow({width=height/2+2,height=height,bg_color=self._item.bg_prefix or beautiful.icon_grad or beautiful.fg_normal}),width-height/2-2,0)
cr:paint()
cr:restore()
self._draw(self, w, cr, width, height)
@ -52,12 +53,14 @@ local function draw(data,item,args)
item._internal.align._setup = true
-- Replace prefix function
item._internal.align.first._item = item
item._internal.align.first._fit = item._internal.align.first.fit
item._internal.align.first._draw = item._internal.align.first.draw
item._internal.align.first.fit = prefix_fit
item._internal.align.first.draw = prefix_draw
-- Replace suffix function
item._internal.align.third._item = item
item._internal.align.third._fit = item._internal.align.third.fit
item._internal.align.third._draw = item._internal.align.third.draw
item._internal.align.third.fit = suffix_fit

View File

@ -129,7 +129,6 @@ function module:setup_item(data,item,args)
item.widget = wibox.widget.background()
cache_pixmap(item)
data.item_style(data,item,{})
item.widget:set_fg(item._private_data.fg)
item._internal.has_changed = true
@ -155,10 +154,10 @@ function module:setup_item(data,item,args)
local l,la,lr = wibox.layout.fixed.horizontal(),wibox.layout.align.horizontal(),wibox.layout.fixed.horizontal()
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 )
m:set_left ( (item.item_style or data.item_style).margins.LEFT )
m:set_right ( (item.item_style or data.item_style).margins.RIGHT )
m:set_top ( (item.item_style or data.item_style).margins.TOP )
m:set_bottom( (item.item_style or data.item_style).margins.BOTTOM )
-- Text
local text_w = module:setup_text(item,data)
@ -214,6 +213,8 @@ function module:setup_item(data,item,args)
la:set_middle(text_w)
la:set_right(lr)
item.widget:set_widget(m)
item._internal.align = la
local fit_w,fit_h = data._internal.layout:fit()
data.width = fit_w
data.height = fit_h
@ -233,6 +234,10 @@ function module:setup_item(data,item,args)
-- Setup tooltip
item.widget:set_tooltip(item.tooltip)
-- Apply item style
local item_style = item.item_style or data.item_style
item_style(data,item,{})
end
local function compute_geo(data)