summaryrefslogtreecommitdiff
path: root/todo-widget/todo.lua
blob: e612545d1a04e4756b56f019063f0178bbee7c4b (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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
-------------------------------------------------
-- ToDo Widget for Awesome Window Manager
-- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/todo-widget

-- @author Pavel Makhov
-- @copyright 2020 Pavel Makhov
-------------------------------------------------

local awful = require("awful")
local wibox = require("wibox")
local json = require("json")
local spawn = require("awful.spawn")
local gears = require("gears")
local beautiful = require("beautiful")
local gfs = require("gears.filesystem")

local HOME_DIR = os.getenv("HOME")
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/todo-widget'
local STORAGE = HOME_DIR .. '/.cache/awmw/todo-widget/todos.json'

local GET_TODO_ITEMS = 'bash -c "cat ' .. STORAGE .. '"'

local rows  = { layout = wibox.layout.fixed.vertical }
local todo_widget = {}
local update_widget
todo_widget.widget = wibox.widget {
    {
        {
            id = "icon",
            widget = wibox.widget.imagebox
        },
        id = "margin",
        margins = 4,
        layout = wibox.container.margin
    },
    {
        id = "txt",
        widget = wibox.widget.textbox
    },
    layout = wibox.layout.fixed.horizontal,
    set_text = function(self, new_value)
        self.txt.text = new_value
    end,
    set_icon = function(self, new_value)
        self.margin.icon.image = new_value
    end
}

function todo_widget:update_counter(todos)
    local todo_count = 0
    for _,p in ipairs(todos) do
        if not p.status then
            todo_count = todo_count + 1
        end
    end

    todo_widget.widget:set_text(todo_count)
end

local popup = awful.popup{
    ontop = true,
    hide_on_right_click = true,
    bg = beautiful.bg_normal,
    visible = false,
    shape = gears.shape.rounded_rect,
    border_width = 1,
    border_color = beautiful.bg_focus,
    maximum_width = 400,
    offset = { y = 5 },
    widget = {}
}

local add_button = wibox.widget {
    {
        {
            image = WIDGET_DIR .. '/list-add-symbolic.svg',
            resize = false,
            widget = wibox.widget.imagebox
        },
        top = 11,
        left = 8,
        right = 8,
        layout = wibox.container.margin
    },
    shape = function(cr, width, height)
        gears.shape.circle(cr, width, height, 12)
    end,
    widget = wibox.container.background
}

add_button:connect_signal("button::press", function()
    local pr = awful.widget.prompt()

    table.insert(rows, wibox.widget {
        {
            {
                pr.widget,
                spacing = 8,
                layout = wibox.layout.align.horizontal
            },
            margins = 8,
            layout = wibox.container.margin
        },
        bg = beautiful.bg_normal,
        widget = wibox.container.background
    })
    awful.prompt.run{
        prompt = "<b>New item</b>: ",
        bg = beautiful.bg_normal,
        bg_cursor = beautiful.fg_urgent,
        textbox = pr.widget,
        exe_callback = function(input_text)
            if not input_text or #input_text == 0 then return end
            spawn.easy_async(GET_TODO_ITEMS, function(stdout)
                local res = json.decode(stdout)
                table.insert(res.todo_items, {todo_item = input_text, status = false})
                spawn.easy_async_with_shell("echo '" .. json.encode(res) .. "' > " .. STORAGE, function()
                    spawn.easy_async(GET_TODO_ITEMS, function(items) update_widget(items) end)
                end)
            end)
        end
    }
    popup:setup(rows)
end)
add_button:connect_signal("mouse::enter", function(c) c:set_bg(beautiful.bg_focus) end)
add_button:connect_signal("mouse::leave", function(c) c:set_bg(beautiful.bg_normal) end)

local function worker(user_args)

    local args = user_args or {}

    local icon = args.icon or WIDGET_DIR .. '/checkbox-checked-symbolic.svg'

    todo_widget.widget:set_icon(icon)

    function update_widget(stdout)
        local result = json.decode(stdout)
        if result == nil or result == '' then result = {} end
        todo_widget:update_counter(result.todo_items)

        for i = 0, #rows do rows[i]=nil end

        local first_row = wibox.widget {
            {
                {widget = wibox.widget.textbox},
                {
                    markup = '<span size="large" font_weight="bold" color="#ffffff">ToDo</span>',
                    align = 'center',
                    forced_width = 350, -- for horizontal alignment
                    forced_height = 40,
                    widget = wibox.widget.textbox
                },
                add_button,
                spacing = 8,
                layout = wibox.layout.fixed.horizontal
            },
            bg = beautiful.bg_normal,
            widget = wibox.container.background
        }

        table.insert(rows, first_row)

        for i, todo_item in ipairs(result.todo_items) do

            local checkbox = wibox.widget {
                checked       = todo_item.status,
                color         = beautiful.bg_normal,
                paddings      = 2,
                shape         = gears.shape.circle,
                forced_width = 20,
                forced_height = 20,
                check_color = beautiful.fg_urgent,
                widget        = wibox.widget.checkbox
            }

            checkbox:connect_signal("button::press", function(c)
                c:set_checked(not c.checked)
                todo_item.status = not todo_item.status
                result.todo_items[i] = todo_item
                spawn.easy_async_with_shell("echo '" .. json.encode(result) .. "' > " .. STORAGE, function ()
                    todo_widget:update_counter(result.todo_items)
                end)
            end)


            local trash_button = wibox.widget {
                {
                    {    image = WIDGET_DIR .. '/window-close-symbolic.svg',
                        resize = false,
                        widget = wibox.widget.imagebox
                    },
                    margins = 5,
                    layout = wibox.container.margin
                },
                border_width = 1,
                shape = function(cr, width, height)
                    gears.shape.circle(cr, width, height, 10)
                end,
                widget = wibox.container.background
            }

            trash_button:connect_signal("button::press", function()
                table.remove(result.todo_items, i)
                spawn.easy_async_with_shell("printf '" .. json.encode(result) .. "' > " .. STORAGE, function ()
                    spawn.easy_async(GET_TODO_ITEMS, function(items) update_widget(items) end)
                end)
            end)


            local move_up = wibox.widget {
                image = WIDGET_DIR .. '/chevron-up.svg',
                resize = false,
                widget = wibox.widget.imagebox
            }

            move_up:connect_signal("button::press", function()
                local temp = result.todo_items[i]
                result.todo_items[i] = result.todo_items[i-1]
                result.todo_items[i-1] = temp
                spawn.easy_async_with_shell("printf '" .. json.encode(result) .. "' > " .. STORAGE, function ()
                    spawn.easy_async(GET_TODO_ITEMS, function(items) update_widget(items) end)
                end)
            end)

            local move_down = wibox.widget {
                image = WIDGET_DIR .. '/chevron-down.svg',
                resize = false,
                widget = wibox.widget.imagebox
            }

            move_down:connect_signal("button::press", function()
                local temp = result.todo_items[i]
                result.todo_items[i] = result.todo_items[i+1]
                result.todo_items[i+1] = temp
                spawn.easy_async_with_shell("printf '" .. json.encode(result) .. "' > " .. STORAGE, function ()
                    spawn.easy_async(GET_TODO_ITEMS, function(items) update_widget(items) end)
                end)
            end)


            local move_buttons = {
                layout = wibox.layout.fixed.vertical
            }

            if i == 1 and #result.todo_items > 1 then
                table.insert(move_buttons, move_down)
            elseif i == #result.todo_items and #result.todo_items > 1 then
                table.insert(move_buttons, move_up)
            elseif #result.todo_items > 1 then
                table.insert(move_buttons, move_up)
                table.insert(move_buttons, move_down)
            end

            local row = wibox.widget {
                {
                    {
                        {
                            checkbox,
                            valign = 'center',
                            layout = wibox.container.place,
                        },
                        {
                            {
                                text = todo_item.todo_item,
                                align = 'left',
                                widget = wibox.widget.textbox
                            },
                            left = 10,
                            layout = wibox.container.margin
                        },
                        {
                            {
                                move_buttons,
                                valign = 'center',
                                layout = wibox.container.place,
                            },
                            {
                                trash_button,
                                valign = 'center',
                                layout = wibox.container.place,
                            },
                            spacing = 8,
                            layout = wibox.layout.align.horizontal
                        },
                        spacing = 8,
                        layout = wibox.layout.align.horizontal
                    },
                    margins = 8,
                    layout = wibox.container.margin
                },
                bg = beautiful.bg_normal,
                widget = wibox.container.background
            }

            row:connect_signal("mouse::enter", function(c) c:set_bg(beautiful.bg_focus) end)
            row:connect_signal("mouse::leave", function(c) c:set_bg(beautiful.bg_normal) end)

            table.insert(rows, row)
        end

        popup:setup(rows)
    end

    todo_widget.widget:buttons(
            awful.util.table.join(
                    awful.button({}, 1, function()
                        if popup.visible then
                            popup.visible = not popup.visible
                        else
                            popup:move_next_to(mouse.current_widget_geometry)
                        end
                    end)
            )
    )

    spawn.easy_async(GET_TODO_ITEMS, function(stdout) update_widget(stdout) end)

    return todo_widget.widget
end

if not gfs.file_readable(STORAGE) then
    spawn.easy_async(string.format([[bash -c "dirname %s | xargs mkdir -p && echo '{\"todo_items\":{}}' > %s"]],
    STORAGE, STORAGE))
end

return setmetatable(todo_widget, { __call = function(_, ...) return worker(...) end })