summaryrefslogtreecommitdiffstats
path: root/modules/desktop/awesome/widgets/calendar.lua
blob: 6e791b33a6dca929cc596f2e4398bd1a8d75e5c3 (plain) (blame)
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
local awful = require("awful")
local beautiful = require("beautiful")
local wibox = require("wibox")
local gears = require("gears")

local calendar_widget = {}

local function worker(user_args)
    local args = user_args or {}

    local style = {}

    style.month = {
        padding = 4,
        bg_color = beautiful.bg_normal,
        border_width = 0
    }

    style.normal = {
        markup = function(t) return t end,
        shape = function(cr, width, height) gears.shape.rounded_rect(cr, width, height, 4) end,
    }
    
    style.focused = {
        bg_color = beautiful.bg_focus,
        fg_color = beautiful.fg_focus,
        markup = function(t) return '<b>' .. t .. '</b>' end,
        shape = function(cr, width, height) gears.shape.rounded_rect(cr, width, height, 4) end,
    }

    style.header = {
        bg_color = beautiful.bg_focus,
        fg_color = beautiful.fg_focus,
        markup = function(t) return '<b>'..t..'</b>' end,
    }

    style.weekday = {
        bg_color = beautiful.bg_focus,
        fg_color = beautiful.fg_focus,
        markup = function(t) return '<b>'..t..'</b>' end,
    }

    local function decorate(widget, flag, date)
        if flag == 'monthheader' then flag = 'header' end
        if flag == 'focus' then
            local today = os.date('*t')
            if not today.month == date.month and today.year == date.year then
                flag = 'normal'
            end
        end
        local properties = style[flag] or {}
        if properties.markup and widget.get_text and widget.set_markup then
            widget:set_markup(properties.markup(widget:get_text()))
        end
        local thisdate = { year = date.year, month = (date.month or 1), day = (date.day or 1) }
        local weekday = tonumber(os.date('%w', os.time(d)))
        local default_bg = (weekday == 0 or weekday == 6)
                           and beautiful.bg_focused
                           or beautiful.bg_normal
        return wibox.widget {
            {
                {
                    widget,
                    halign = 'center',
                    widget = wibox.container.place
                },
                margins = (properties.padding or 2) + (properties.border_width or 0),
                widget = wibox.container.margin
            },
            shape = properties.shape,
            shape_border_color = properties.border_color or '#00000000',
            shape_border_width = properties.border_width or 0,
            fg = properties.fg_color or beautiful.fg_normal,
            bg = properties.bg_color or beautiful.bg_normal,
            widget = wibox.container.background
        }
    end

    local cal = wibox.widget {
        date = os.date('*t'),
        font = beautiful.get_font(),
        fn_embed = decorate,
        long_weekdays = true,
        start_sunday = true,
        widget = wibox.widget.calendar.month
    }

    local popup = awful.popup {
        ontop = true,
        visible = false,
        shape = gears.shape.rounded_rect,
        offset = { y = 5 },
        border_width = 2,
        border_color = beautiful.fg_minimize,
        widget = cal
    }

    function calendar_widget.toggle()
        if popup.visible then
            cal:set_date(nil)
            cal:set_date(os.date('*t'))
            popup:set_widget(nil)
            popup:set_widget(cal)
            popup.visible = not popup.visible
        else
            awful.placement.top_right(popup, { margins = { top = 30, right = 10 }, parent = awful.screen.focused() })
            popup.visible = true
        end
    end
    return calendar_widget
end

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