Add :move() and :remove() methods

This commit is contained in:
Emmanuel Lepage Vallee 2014-02-05 21:43:06 -05:00
parent ddd67ca569
commit 8fa38c2a2c
4 changed files with 66 additions and 7 deletions

View File

@ -224,8 +224,14 @@ here is the list:
| clear | Remove all items | --- | --- | | clear | Remove all items | --- | --- |
| scroll_down | If the menu is cropped, scroll down | --- | --- | | scroll_down | If the menu is cropped, scroll down | --- | --- |
| scroll_up | If the menu is cropped, scroll up | --- | --- | | scroll_up | If the menu is cropped, scroll up | --- | --- |
| swap | Swap 2 items | both items | --- |
| move | Move an item | the item, the new idx | --- |
| remove | Remove the item | the item | --- |
Menu also emit many signals, the syntax is usually `PROPERTY_NAME::changed`. The exeptions are
`item::moved`, `item::swapped`, `item::removed`
###Beautiful options ###Beautiful options
Radical also use the some of the same theme options as awful.menu, plus some: Radical also use the some of the same theme options as awful.menu, plus some:

12
bar.lua
View File

@ -1,4 +1,4 @@
local setmetatable,unpack = setmetatable,unpack local setmetatable,unpack,table = setmetatable,unpack,table
local base = require( "radical.base" ) local base = require( "radical.base" )
local color = require( "gears.color" ) local color = require( "gears.color" )
local wibox = require( "wibox" ) local wibox = require( "wibox" )
@ -58,11 +58,19 @@ local function setup_drawable(data)
data.fit = internal.margin.fit data.fit = internal.margin.fit
data.draw = internal.margin.draw data.draw = internal.margin.draw
--Swap -- Swap / Move / Remove
data:connect_signal("item::swapped",function(_,item1,item2,index1,index2) data:connect_signal("item::swapped",function(_,item1,item2,index1,index2)
internal.layout.widgets[index1],internal.layout.widgets[index2] = internal.layout.widgets[index2],internal.layout.widgets[index1] internal.layout.widgets[index1],internal.layout.widgets[index2] = internal.layout.widgets[index2],internal.layout.widgets[index1]
internal.layout:emit_signal("widget::updated") internal.layout:emit_signal("widget::updated")
end) end)
data:connect_signal("item::moved",function(_,item,new_idx,old_idx)
table.insert(internal.layout.widgets,new_idx,table.remove(internal.layout.widgets,old_idx))
internal.layout:emit_signal("widget::updated")
end)
data:connect_signal("item::removed",function(_,item,old_idx)
table.remove(internal.layout.widgets,old_idx)
internal.layout:emit_signal("widget::updated")
end)
end end
local function setup_buttons(data,item,args) local function setup_buttons(data,item,args)

View File

@ -2,6 +2,7 @@ local setmetatable = setmetatable
local pairs,ipairs = pairs, ipairs local pairs,ipairs = pairs, ipairs
local type,string = type,string local type,string = type,string
local print,unpack = print, unpack local print,unpack = print, unpack
local table = table
local beautiful = require( "beautiful" ) local beautiful = require( "beautiful" )
local util = require( "awful.util" ) local util = require( "awful.util" )
local aw_key = require( "awful.key" ) local aw_key = require( "awful.key" )
@ -472,6 +473,42 @@ local function new(args)
end end
end end
function data:move(item,idx)
local idx1 = nil
for k,v in ipairs(internal.items) do --rows
if item == v[1] then
idx1 = k
break
end
end
if idx1 then
-- if idx < idx1 then
-- idx = idx + 1
-- end
if idx1 > #internal.items + 1 then
idx1 = #internal.items + 1
end
if idx ~= idx1 then
table.insert(internal.items,idx1,table.remove(internal.items,idx))
data:emit_signal("item::moved",item,idx,idx1)
end
end
end
function data:remove(item)
local idx1 = nil
for k,v in ipairs(internal.items) do --rows
if item == v[1] then
idx1 = k
break
end
end
if idx1 then
table.remove(internal.items,idx1)
data:emit_signal("item::removed",item,idx1)
end
end
function data:scroll_up() function data:scroll_up()
if data.max_items ~= nil and data.rowcount >= data.max_items and (data._start_at or 1) > 1 then if data.max_items ~= nil and data.rowcount >= data.max_items and (data._start_at or 1) > 1 then
data._start_at = (data._start_at or 1) - 1 data._start_at = (data._start_at or 1) - 1

View File

@ -150,10 +150,10 @@ function module:setup_item(data,item,args)
-- Text need to take as much space as possible, override default -- Text need to take as much space as possible, override default
module:setup_text(item,data) module:setup_text(item,data)
--TODO DEAD CODE? -- Necessary for :set_position()
-- local fit_w,fit_h = data._internal.layout:fit() local fit_w,fit_h = data._internal.layout:fit()
-- data.width = fit_w data.width = fit_w
-- data.height = fit_h data.height = fit_h
-- 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
@ -235,11 +235,19 @@ local function new(data)
real_l.setup_item = module.setup_item real_l.setup_item = module.setup_item
data._internal.content_layout = l data._internal.content_layout = l
--SWAP --SWAP / MOVE / REMOVE
data:connect_signal("item::swapped",function(_,item1,item2,index1,index2) data:connect_signal("item::swapped",function(_,item1,item2,index1,index2)
real_l.widgets[index1],real_l.widgets[index2] = real_l.widgets[index2],real_l.widgets[index1] real_l.widgets[index1],real_l.widgets[index2] = real_l.widgets[index2],real_l.widgets[index1]
real_l:emit_signal("widget::updated") real_l:emit_signal("widget::updated")
end) end)
data:connect_signal("item::moved",function(_,item,new_idx,old_idx)
table.insert(real_l.widgets,new_idx,table.remove(real_l.widgets,old_idx))
real_l:emit_signal("widget::updated")
end)
data:connect_signal("item::removed",function(_,item,old_idx)
table.remove(real_l.widgets,old_idx)
real_l:emit_signal("widget::updated")
end)
return real_l return real_l
end end