2017-12-30 13:05:41 +01:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
|
|
|
* (c) 2017, Simon Désaulniers <sim.desaulniers@gmail.com>
|
|
|
|
* (c) 2017, Uli Schlachter
|
|
|
|
* (c) 2017, Jeferson Siqueira <jefersonlsiq@gmail.com>
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
2018-02-17 14:06:20 +01:00
|
|
|
-- Menu iterator with Naughty notifications
|
|
|
|
-- lain.util.menu_iterator
|
2017-12-30 13:05:41 +01:00
|
|
|
|
|
|
|
local naughty = require("naughty")
|
2018-02-17 14:06:20 +01:00
|
|
|
local util = require("lain.util")
|
|
|
|
local atable = require("awful.util").table
|
|
|
|
local assert = assert
|
|
|
|
local pairs = pairs
|
|
|
|
local tconcat = table.concat
|
|
|
|
local unpack = unpack
|
2017-12-30 13:05:41 +01:00
|
|
|
|
|
|
|
local state = { cid = nil }
|
|
|
|
|
|
|
|
local function naughty_destroy_callback(reason)
|
2018-02-17 14:06:20 +01:00
|
|
|
local closed = naughty.notificationClosedReason
|
|
|
|
if reason == closed.expired or reason == closed.dismissedByUser then
|
2017-12-30 13:05:41 +01:00
|
|
|
local actions = state.index and state.menu[state.index - 1][2]
|
|
|
|
if actions then
|
|
|
|
for _,action in pairs(actions) do
|
|
|
|
-- don't try to call nil callbacks
|
|
|
|
if action then action() end
|
|
|
|
end
|
|
|
|
state.index = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-17 14:06:20 +01:00
|
|
|
-- Iterates over a menu.
|
|
|
|
-- After the timeout, callbacks associated to the last visited choice are
|
|
|
|
-- executed. Inputs:
|
|
|
|
-- * menu: a list of {label, {callbacks}} pairs
|
|
|
|
-- * timeout: time to wait before confirming the menu selection
|
|
|
|
-- * icon: icon to display in the notification of the chosen label
|
2017-12-30 13:05:41 +01:00
|
|
|
local function iterate(menu, timeout, icon)
|
2018-02-17 14:06:20 +01:00
|
|
|
local timeout = timeout or 4 -- default timeout for each menu entry
|
|
|
|
local icon = icon or nil -- icon to display on the menu
|
2017-12-30 13:05:41 +01:00
|
|
|
|
|
|
|
-- Build the list of choices
|
|
|
|
if not state.index then
|
|
|
|
state.menu = menu
|
|
|
|
state.index = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Select one and display the appropriate notification
|
2018-02-17 14:06:20 +01:00
|
|
|
local label
|
|
|
|
local next = state.menu[state.index]
|
2017-12-30 13:05:41 +01:00
|
|
|
state.index = state.index + 1
|
|
|
|
|
|
|
|
if not next then
|
|
|
|
label = "Cancel"
|
|
|
|
state.index = nil
|
|
|
|
else
|
|
|
|
label, _ = unpack(next)
|
|
|
|
end
|
2018-02-17 14:06:20 +01:00
|
|
|
|
2017-12-30 13:05:41 +01:00
|
|
|
state.cid = naughty.notify({
|
2018-02-17 14:06:20 +01:00
|
|
|
text = label,
|
|
|
|
icon = icon,
|
|
|
|
timeout = timeout,
|
|
|
|
screen = mouse.screen,
|
2017-12-30 13:05:41 +01:00
|
|
|
replaces_id = state.cid,
|
2018-02-17 14:06:20 +01:00
|
|
|
destroy = naughty_destroy_callback
|
2017-12-30 13:05:41 +01:00
|
|
|
}).id
|
|
|
|
end
|
|
|
|
|
2018-02-17 14:06:20 +01:00
|
|
|
-- Generates a menu compatible with the first argument of `iterate` function and
|
|
|
|
-- suitable for the following cases:
|
|
|
|
-- * all possible choices individually (partition of singletons);
|
|
|
|
-- * all possible subsets of the set of choices (powerset).
|
2018-02-14 23:04:06 +01:00
|
|
|
--
|
2018-02-17 14:06:20 +01:00
|
|
|
-- Inputs:
|
2018-02-14 23:04:06 +01:00
|
|
|
-- * args: an array containing the following members:
|
2018-02-17 14:06:20 +01:00
|
|
|
-- * choices: Array of choices (string) on which the menu will be
|
|
|
|
-- generated.
|
|
|
|
-- * name: Displayed name of the menu (in the form "name: choices").
|
|
|
|
-- * selected_cb: Callback to execute for each selected choice. Takes
|
|
|
|
-- the choice as a string argument. Can be `nil` (no action
|
|
|
|
-- to execute).
|
|
|
|
-- * rejected_cb: Callback to execute for each rejected choice (possible
|
|
|
|
-- choices which are not selected). Takes the choice as a
|
|
|
|
-- string argument. Can be `nil` (no action to execute).
|
|
|
|
-- * extra_choices: An array of extra { choice_str, callback_fun } pairs to be
|
|
|
|
-- added to the menu. Each callback_fun can be `nil`.
|
|
|
|
-- * combination: The combination of choices to generate. Possible values:
|
|
|
|
-- "powerset" and "single" (default).
|
|
|
|
-- Output:
|
|
|
|
-- * m: menu to be iterated over.
|
2018-02-14 23:04:06 +01:00
|
|
|
local function menu(args)
|
2018-02-17 14:06:20 +01:00
|
|
|
local choices = assert(args.choices or args[1])
|
|
|
|
local name = assert(args.name or args[2])
|
|
|
|
local selected_cb = args.selected_cb
|
|
|
|
local rejected_cb = args.rejected_cb
|
2018-02-14 23:04:06 +01:00
|
|
|
local extra_choices = args.extra_choices or {}
|
|
|
|
|
|
|
|
local ch_combinations = args.combination == "powerset" and helpers.powerset(choices) or helpers.trivial_partition_set(choices)
|
2018-02-17 14:06:20 +01:00
|
|
|
|
2018-02-14 23:04:06 +01:00
|
|
|
for _,c in pairs(extra_choices) do
|
2018-02-17 14:06:20 +01:00
|
|
|
ch_combinations = atable.join(ch_combinations, {{c[1]}})
|
2018-02-14 23:04:06 +01:00
|
|
|
end
|
|
|
|
|
2018-02-17 14:06:20 +01:00
|
|
|
local m = {} -- the menu
|
|
|
|
|
2018-02-14 23:04:06 +01:00
|
|
|
for _,c in pairs(ch_combinations) do
|
|
|
|
if #c > 0 then
|
|
|
|
local cbs = {}
|
2018-02-17 14:06:20 +01:00
|
|
|
|
2018-02-14 23:04:06 +01:00
|
|
|
-- selected choices
|
|
|
|
for _,ch in pairs(c) do
|
2018-02-17 14:06:20 +01:00
|
|
|
if atable.hasitem(choices, ch) then
|
2018-02-14 23:04:06 +01:00
|
|
|
cbs[#cbs + 1] = selected_cb and function() selected_cb(ch) end or nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- rejected choices
|
|
|
|
for _,ch in pairs(choices) do
|
2018-02-17 14:06:20 +01:00
|
|
|
if not atable.hasitem(c, ch) and atable.hasitem(choices, ch) then
|
2018-02-14 23:04:06 +01:00
|
|
|
cbs[#cbs + 1] = rejected_cb and function() rejected_cb(ch) end or nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-17 14:06:20 +01:00
|
|
|
-- add user extra choices (like the choice "None" for example)
|
2018-02-14 23:04:06 +01:00
|
|
|
for _,x in pairs(extra_choices) do
|
|
|
|
if x[1] == c[1] then
|
|
|
|
cbs[#cbs + 1] = x[2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-17 14:06:20 +01:00
|
|
|
m[#m + 1] = { name .. ": " .. tconcat(c, " + "), cbs }
|
2018-02-14 23:04:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return m
|
|
|
|
end
|
|
|
|
|
2018-02-17 14:06:20 +01:00
|
|
|
return { iterate = iterate, menu = menu }
|