From 8fa38c2a2c267ce9bd22b21d65866664a6824bf2 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Wed, 5 Feb 2014 21:43:06 -0500 Subject: [PATCH] Add :move() and :remove() methods --- README.md | 6 ++++++ bar.lua | 12 ++++++++++-- base.lua | 37 +++++++++++++++++++++++++++++++++++++ layout/vertical.lua | 18 +++++++++++++----- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f00d025..b584555 100644 --- a/README.md +++ b/README.md @@ -224,8 +224,14 @@ here is the list: | clear | Remove all items | --- | --- | | scroll_down | If the menu is cropped, scroll down | --- | --- | | 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 Radical also use the some of the same theme options as awful.menu, plus some: diff --git a/bar.lua b/bar.lua index e644814..6453db5 100644 --- a/bar.lua +++ b/bar.lua @@ -1,4 +1,4 @@ -local setmetatable,unpack = setmetatable,unpack +local setmetatable,unpack,table = setmetatable,unpack,table local base = require( "radical.base" ) local color = require( "gears.color" ) local wibox = require( "wibox" ) @@ -58,11 +58,19 @@ local function setup_drawable(data) data.fit = internal.margin.fit data.draw = internal.margin.draw - --Swap + -- Swap / Move / Remove 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:emit_signal("widget::updated") 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 local function setup_buttons(data,item,args) diff --git a/base.lua b/base.lua index fb78345..6d11506 100644 --- a/base.lua +++ b/base.lua @@ -2,6 +2,7 @@ local setmetatable = setmetatable local pairs,ipairs = pairs, ipairs local type,string = type,string local print,unpack = print, unpack +local table = table local beautiful = require( "beautiful" ) local util = require( "awful.util" ) local aw_key = require( "awful.key" ) @@ -472,6 +473,42 @@ local function new(args) 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() 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 diff --git a/layout/vertical.lua b/layout/vertical.lua index 1372c8b..899e634 100644 --- a/layout/vertical.lua +++ b/layout/vertical.lua @@ -150,10 +150,10 @@ function module:setup_item(data,item,args) -- Text need to take as much space as possible, override default module:setup_text(item,data) - --TODO DEAD CODE? --- local fit_w,fit_h = data._internal.layout:fit() --- data.width = fit_w --- data.height = fit_h + -- Necessary for :set_position() + local fit_w,fit_h = data._internal.layout:fit() + data.width = fit_w + data.height = fit_h -- Enable scrollbar if necessary 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 data._internal.content_layout = l - --SWAP + --SWAP / MOVE / REMOVE 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:emit_signal("widget::updated") 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 end