summaryrefslogtreecommitdiff
path: root/config/awesome/countdown.lua
blob: 52675e689d3014bc21b32e7f37638d36f4774f99 (plain)
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
-- from https://awesomewm.org/recipes/countdown/
-- on 2018-04-10
-- slightly modified

local gears = require("gears")
local awful = require("awful")
local wibox = require("wibox")
local naughty = require("naughty")
local beautiful = require("beautiful")

countdown = {
    widget   = wibox.widget.textbox(),
    checkbox = wibox.widget {
        checked      = false,
        check_color  = "#ff0000",  -- customize
        border_color = beautiful.fg_normal, -- customize
        border_width = 2,                   -- customize
        shape        = gears.shape.circle,
        widget       = wibox.widget.checkbox
    }
}

function countdown.set()
    awful.prompt.run {
        prompt       = "Countdown minutes: ", -- floats accepted
        textbox      = awful.screen.focused().mypromptbox.widget,
        exe_callback = function(timeout)
            if countdown.timer and countdown.timer.started then
                countdown.seconds = tonumber(timeout) * 60
                return
            end

            countdown.seconds = tonumber(timeout)
            if not countdown.seconds then return end
            countdown.checkbox.checked = false
            countdown.minute_t = countdown.seconds > 1 and "minutes" or "minute"
            countdown.seconds = countdown.seconds * 60
            countdown.timer = gears.timer({ timeout = 1 })
            countdown.timer:connect_signal("timeout", function()
                if countdown.seconds > 0 then
                    local minutes = math.floor(countdown.seconds / 60)
                    local seconds = math.fmod(countdown.seconds, 60)
                    countdown.widget:set_markup(string.format("%d:%02d", minutes, seconds))
                    countdown.seconds = countdown.seconds - 1
                else
                    naughty.notify({
                        preset = naughty.config.presets.critical,
                        title = "Countdown",
                        text  = string.format("%s %s timeout", timeout, countdown.minute_t),
                        timeout = 30,
                        position = "top_middle",
                        run = function(notification)
                            countdown.widget:set_markup("")
                            countdown.checkbox.checked = false
                            notification.die(naughty.notificationClosedReason.dismissedByUser)
                        end
                    })
                    countdown.widget:set_markup("")
                    countdown.checkbox.checked = true
                    countdown.timer:stop()
                end
            end)
            countdown.timer:start()
        end
    }
end

countdown.checkbox:buttons(awful.util.table.join(
    awful.button({}, 1, function() countdown.set() end), -- left click
    awful.button({}, 3, function() -- right click
        if countdown.timer and countdown.timer.started then
            countdown.timer:stop()
            naughty.notify({ title = "Countdown", text  = "Timer stopped" })
        end
        countdown.widget:set_markup("")
        countdown.checkbox.checked = false
    end)
))


local countdown_tooltip = awful.tooltip { }
countdown_tooltip:add_to_object(countdown.checkbox)

countdown.checkbox:connect_signal('mouse::enter', function()
    countdown_tooltip.text = 'Click to start countdown'
end)