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

local taglist_buttons =
    gears.table.join(
    awful.button(
        {},
        1,
        function(t)
            t:view_only()
        end
    ),
    awful.button(
        {modkey},
        1,
        function(t)
            if client.focus then
                client.focus:move_to_tag(t)
            end
        end
    ),
    awful.button({}, 3, awful.tag.viewtoggle),
    awful.button(
        {modkey},
        3,
        function(t)
            if client.focus then
                client.focus:toggle_tag(t)
            end
        end
    )
)

function taglist(s)
    return awful.widget.taglist {
        screen = s,
        filter = awful.widget.taglist.filter.all, --.noempty,
        buttons = taglist_buttons,
        widget_template = {
            {
                {
                    id     = 'index_role',
                    widget = wibox.widget.textbox,
                },
                margins = 2,
                widget  = wibox.container.margin,
            },
            id     = 'background_role',
            widget = wibox.container.background,
            create_callback = function(self, c3, index, objects)
                self:get_children_by_id('index_role')[1].markup = '<span> '..index..' </span>'

                self:connect_signal('mouse::enter', function()
                    if self.bg ~= beautiful.taglist_bg_focus then
                        self.backup     = self.bg
                        self.has_backup = true
                    end
                    self.bg = beautiful.taglist_bg_focus
                end)

                self:connect_signal('mouse::leave', function()
                    if self.has_backup then self.bg = self.backup end
                end)
            end,
        },
    }
end