--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
-- Additional client controls, hidden for cleaner UI in the submenu.
local awmtk2 = require("awmtk2")
local wibox = require("wibox")
local gears = require("gears")
local awful = require("awful")
local beautiful = require("beautiful")

return function(args)
    local style = awmtk2.create_style("client_buttons",
        awmtk2.generic.button_list,args.style)
    local templates = awmtk2.create_template_lib("client_buttons",awmtk2.templates,args.templates)
    local t = awmtk2.build_templates(templates,style)
    local floating_on = beautiful.titlebar_floating_button_normal_inactive
    local floating_off = beautiful.titlebar_floating_button_normal_active
    local ontop_on = beautiful.titlebar_ontop_button_normal_inactive
    local ontop_off = beautiful.titlebar_ontop_button_normal_active
    local sticky_on = beautiful.titlebar_sticky_button_normal_inactive
    local sticky_off = beautiful.titlebar_sticky_button_normal_active
    local floating = wibox.widget(t.button({
        image = (client.focus and client.focus.floating and floating_on) or floating_off,
        widget = wibox.widget.imagebox,
        id = "icon"
    },{
        forced_height = style.button.forced_height,
        forced_width = style.button.forced_width
    }))
    floating:connect_signal("button::press",style.button.onpress)
    floating:connect_signal("button::release",style.button.onrelease)
    floating:connect_signal("button::press",function(widget)
        client.focus.floating = (not client.focus.floating)
        widget:emit_signal("widget::update",widget)
    end)
    floating:connect_signal("widget::update",function(widget)
        local icon = widget:get_children_by_id("icon")[1]
        icon.image = (client.focus.floating and floating_on) or floating_off
    end)
    local ontop = wibox.widget(t.button({
        image = (client.focus and client.focus.ontop and ontop_on) or ontop_off,
        widget = wibox.widget.imagebox,
        id = "icon"
    },{
        forced_height = style.button.forced_height,
        forced_width = style.button.forced_width
    }))
    ontop:connect_signal("button::press",style.button.onpress)
    ontop:connect_signal("button::release",style.button.onrelease)
    ontop:connect_signal("button::press",function(widget)
        client.focus.ontop = (not client.focus.ontop)
        widget:emit_signal("widget::update",widget)
    end)
    ontop:connect_signal("widget::update",function(widget)
        local icon = widget:get_children_by_id("icon")[1]
        icon.image = (client.focus.ontop and ontop_on) or ontop_off
    end)
    local sticky = wibox.widget(t.button({ 
        image = (client.focus and client.focus.sticky and sticky_on) or sticky_off,
        widget = wibox.widget.imagebox,
        id = "icon"
    },{
        forced_height = style.button.forced_height,
        forced_width = style.button.forced_width
    }))
    sticky:connect_signal("button::press",style.button.onpress)
    sticky:connect_signal("button::release",style.button.onrelease)
    sticky:connect_signal("button::press",function(widget)
        client.focus.sticky = (not client.focus.sticky)
        widget:emit_signal("widget::update",widget)
    end)
    sticky:connect_signal("widget::update",function(widget)
        local icon = widget:get_children_by_id("icon")[1]
        icon.image = (client.focus.sticky and sticky_on) or sticky_off
    end)
    client.connect_signal("focus",function(c)
        sticky:emit_signal("widget::update")
        ontop:emit_signal("widget::update")
        floating:emit_signal("widget::update")
    end)
    local widget = wibox.widget({
        floating,
        ontop,
        sticky,
        layout = wibox.layout.fixed.horizontal,
        spacing = style.base.spacing
    })
    return widget
end
