-
Notifications
You must be signed in to change notification settings - Fork 1
/
remorseful.lua
113 lines (94 loc) · 2.29 KB
/
remorseful.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-- usage:
-- - embed the widget somewhere: `wibox:add(remorseful.widget)`
-- - set time limit: `remorseful.time_limit = 10`
-- - set text to be displayed: `remorseful.text = 'Press Alt-Shift-u to cancel'`
-- - invoke an action that you can cancel:
-- ```
-- remorseful {
-- cancel = function()
-- c.hidden = false
-- end,
-- commit = function()
-- c:kill()
-- end,
-- start = function()
-- c.hidden = true
-- end,
-- }
-- ```
-- - all other buttons/keys to cancel: `key(..., remorseful.cancel)`
local awful = require 'awful'
local gears = require 'gears'
local naughty = require 'naughty'
local wibox = require 'wibox'
local WIDTH = 200
local TIME_STEP = 0.1
local remorseful = {}
local remorseful_mt = {}
local text_widget = wibox.widget {
text = '',
widget = wibox.widget.textbox,
}
local pbar_widget = wibox.widget {
max_value = 1,
value = 0,
forced_width = 0, -- XXX configurable
widget = wibox.widget.progressbar,
}
local top_widget = wibox.widget {
pbar_widget,
text_widget,
layout = wibox.layout.stack,
}
local timer
local value
local cancel_fn = function() end
remorseful.widget = top_widget
remorseful.time_limit = 5
function remorseful_mt:__call(args)
if timer then
naughty.notify {
text = 'Remorseful action already active!',
preset = naughty.config.presets.critical,
}
return
end
if args.start then
args.start()
end
if remorseful.text then
text_widget.text = remorseful.text
else
text_widget.text = ''
end
top_widget.forced_width = WIDTH -- XXX configurable
pbar_widget:set_value(0)
value = 0
timer = gears.timer.start_new(TIME_STEP, function()
value = value + TIME_STEP
pbar_widget:set_value(value / remorseful.time_limit)
if value >= remorseful.time_limit then
top_widget.forced_width = 0
timer = nil
cancel_fn = function() end
pcall(args.commit)
return false
else
return true
end
end)
cancel_fn = args.cancel
end
function remorseful.cancel()
top_widget.forced_width = 0
if timer then
timer:stop()
timer = nil
end
pcall(cancel_fn)
cancel_fn = function() end
end
top_widget:buttons(awful.util.table.join(
awful.button({}, 1, remorseful.cancel)
))
return setmetatable(remorseful, remorseful_mt)