]> git.madduck.net Git - etc/awesome.git/blob - util/menu_iterator.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

menu_iterator: naughty.notify based menu utility
[etc/awesome.git] / util / menu_iterator.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2017, Simon Désaulniers <sim.desaulniers@gmail.com>
5       * (c) 2017, Uli Schlachter
6       * (c) 2017, Jeferson Siqueira <jefersonlsiq@gmail.com>
7
8 --]]
9
10 -- Menu iterator using naughty.notify
11
12 local naughty = require("naughty")
13
14 local state = { cid = nil }
15
16 local function naughty_destroy_callback(reason)
17     if reason == naughty.notificationClosedReason.expired or
18         reason == naughty.notificationClosedReason.dismissedByUser then
19         local actions = state.index and state.menu[state.index - 1][2]
20         if actions then
21             for _,action in pairs(actions) do
22                 -- don't try to call nil callbacks
23                 if action then action() end
24             end
25             state.index = nil
26         end
27     end
28 end
29
30 -- Iterates over a list of pairs {label, {callbacks}}. After timeout, the last
31 -- visited choice associated callbacks are executed.
32 -- * menu:    a list of pairs {label, {callbacks}
33 -- * timeout: time to wait before confirming menu selection
34 -- * icon:    icon to display left to the choiced label
35 local function iterate(menu, timeout, icon)
36     timeout = timeout or 4 -- default timeout for each menu entry
37     icon    = icon or nil  -- icon to display on the menu
38
39     -- Build the list of choices
40     if not state.index then
41         state.menu = menu
42         state.index = 1
43     end
44
45     -- Select one and display the appropriate notification
46     local label, action
47     local next  = state.menu[state.index]
48     state.index = state.index + 1
49
50     if not next then
51         label = "Cancel"
52         state.index = nil
53     else
54         label, _ = unpack(next)
55     end
56     state.cid = naughty.notify({
57         text = label,
58         icon = icon,
59         timeout = timeout,
60         screen = mouse.screen,
61         replaces_id = state.cid,
62         destroy = naughty_destroy_callback
63     }).id
64 end
65
66 return { iterate = iterate }