use awful.keygrabber in awful.menu

this makes the use of cur_menu obsolete because now every menu and submenu gets its own keygrabber. there is no need anymore to track the latest opened menu globaly.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
dodo 2012-04-30 17:45:48 +02:00 committed by Uli Schlachter
parent 56c15da307
commit ac5af147f0
1 changed files with 18 additions and 25 deletions

View File

@ -10,6 +10,7 @@ local wibox = require("wibox")
local button = require("awful.button") local button = require("awful.button")
local util = require("awful.util") local util = require("awful.util")
local tags = require("awful.tag") local tags = require("awful.tag")
local keygrabber = require("awful.keygrabber")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local object = require("gears.object") local object = require("gears.object")
local setmetatable = setmetatable local setmetatable = setmetatable
@ -27,7 +28,6 @@ local capi = {
screen = screen, screen = screen,
mouse = mouse, mouse = mouse,
client = client, client = client,
keygrabber = keygrabber,
awesome = awesome, awesome = awesome,
oocairo = oocairo } oocairo = oocairo }
@ -49,8 +49,6 @@ local table_merge = function (t, set)
end end
local cur_menu
--- Key bindings for menu navigation. --- Key bindings for menu navigation.
-- Keys are: up, down, exec, enter, back, close. Value are table with a list of valid -- Keys are: up, down, exec, enter, back, close. Value are table with a list of valid
-- keys for the action, i.e. menu_keys.up = { "j", "k" } will bind 'j' and 'k' -- keys for the action, i.e. menu_keys.up = { "j", "k" } will bind 'j' and 'k'
@ -193,26 +191,26 @@ local function check_access_key(menu, key)
end end
local function grabber(mod, key, event) local function grabber(menu, mod, key, event)
if event ~= "press" then return end if event ~= "press" then return end
local sel = cur_menu.sel or 0 local sel = menu.sel or 0
if util.table.hasitem(menu_keys.up, key) then if util.table.hasitem(menu_keys.up, key) then
local sel_new = sel-1 < 1 and #cur_menu.items or sel-1 local sel_new = sel-1 < 1 and #menu.items or sel-1
cur_menu:item_enter(sel_new) menu:item_enter(sel_new)
elseif util.table.hasitem(menu_keys.down, key) then elseif util.table.hasitem(menu_keys.down, key) then
local sel_new = sel+1 > #cur_menu.items and 1 or sel+1 local sel_new = sel+1 > #menu.items and 1 or sel+1
cur_menu:item_enter(sel_new) menu:item_enter(sel_new)
elseif sel > 0 and util.table.hasitem(menu_keys.enter, key) then elseif sel > 0 and util.table.hasitem(menu_keys.enter, key) then
cur_menu:exec(sel) menu:exec(sel)
elseif sel > 0 and util.table.hasitem(menu_keys.exec, key) then elseif sel > 0 and util.table.hasitem(menu_keys.exec, key) then
cur_menu:exec(sel, { exec = true }) menu:exec(sel, { exec = true })
elseif util.table.hasitem(menu_keys.back, key) then elseif util.table.hasitem(menu_keys.back, key) then
cur_menu:hide() menu:hide()
elseif util.table.hasitem(menu_keys.close, key) then elseif util.table.hasitem(menu_keys.close, key) then
get_root(cur_menu):hide() get_root(menu):hide()
else else
check_access_key(cur_menu, key) check_access_key(menu, key)
end end
end end
@ -284,7 +282,6 @@ function item_enter(menu, num, opts)
--print("sel", num, menu.sel, item.theme.bg_focus) --print("sel", num, menu.sel, item.theme.bg_focus)
item._background:set_fg(item.theme.fg_focus) item._background:set_fg(item.theme.fg_focus)
item._background:set_bg(item.theme.bg_focus) item._background:set_bg(item.theme.bg_focus)
cur_menu = menu
menu.sel = num menu.sel = num
if menu.auto_expand and opts.hover then if menu.auto_expand and opts.hover then
@ -321,10 +318,7 @@ function show(menu, args)
if not set_size(menu) then return end if not set_size(menu) then return end
set_coords(menu, screen_index, coords) set_coords(menu, screen_index, coords)
if not cur_menu then keygrabber.run(menu._keygrabber)
capi.keygrabber.run(grabber)
end
cur_menu = menu
menu.wibox.visible = true menu.wibox.visible = true
end end
@ -341,12 +335,7 @@ function hide(menu)
end end
menu.sel = nil menu.sel = nil
if cur_menu == menu then keygrabber.stop(menu._keygrabber)
cur_menu = cur_menu.parent
end
if not cur_menu then
capi.keygrabber.stop()
end
menu.wibox.visible = false menu.wibox.visible = false
end end
@ -663,6 +652,10 @@ function new(args, parent)
for i, v in pairs(args.items) do menu:add(v) end for i, v in pairs(args.items) do menu:add(v) end
end end
menu._keygrabber = function (...)
grabber(menu, ...)
end
menu.wibox = wibox({ menu.wibox = wibox({
ontop = true, ontop = true,
fg = menu.theme.fg_normal, fg = menu.theme.fg_normal,