support for custom buttons definitions

This commit is contained in:
BZ 2020-08-16 19:55:14 +02:00
parent 21340d3446
commit 3fa0c82fd8
2 changed files with 548 additions and 499 deletions

View File

@ -94,6 +94,24 @@ Customization:
| `resize_factor` | 0.01 | default client resize factor |
| `stealth` | false | show only button colors on hover (set button colors to border colors) |
Custom Buttons:
------------
Add a custom button to `buttons` (`marked` in this example):
```
buttons = { "minimize", "maximize", "close", "marked" }
```
Define the button:
```
button_marked_name = "my fancy custom button",
button_marked_size = "25",
button_marked_function = function(c) c.marked = not c.marked end,
color_marked_focus = "#ff00ff",
color_marked_normal = "#ffff00",
color_marked_hover = "#ff0000",
```
Example Configuration (as shown on gif):
------------

163
init.lua
View File

@ -51,9 +51,12 @@ local menu_move2tag = function(c, scr)
for _, t in pairs(s.tags) do
if t ~= awful.screen.focused().selected_tag then
count = count + 1
local entry = { t.index .. ": " .. t.name .. menu_marker(t.selected) .. " ", function()
local entry = {
t.index .. ": " .. t.name .. menu_marker(t.selected) .. " ",
function()
c:move_to_tag(t)
end }
end
}
table.insert(list, entry)
end
end
@ -70,7 +73,7 @@ local menu_move2screen = function(c)
local desc = next(s.outputs) or ""
if s.index ~= awful.screen.focused().index then
count = count + 1
local entry = { s.index .. ": " .. desc .. " ", menu_move2tag(c, s) }
local entry = {s.index .. ": " .. desc .. " ", menu_move2tag(c, s)}
table.insert(list, entry)
end
end
@ -85,57 +88,80 @@ function module.menu_client(c)
local list_tags = menu_move2tag(c)
if list_tags then
table.insert(list, { "move to tag", list_tags })
table.insert(list, {"move to tag", list_tags})
end
local list_screens = menu_move2screen(c)
if list_screens then
table.insert(list, { "move to screen", list_screens })
table.insert(list, {"move to screen", list_screens})
end
table.insert(list, { "fullscreen" .. menu_marker(c.fullscreen), function()
table.insert(list, {
"fullscreen" .. menu_marker(c.fullscreen),
function()
c.fullscreen = not c.fullscreen
c:raise()
end })
end
})
table.insert(list, { "maximize" .. menu_marker(c.maximized), function()
table.insert(list, {
"maximize" .. menu_marker(c.maximized),
function()
c.maximized = not c.maximized
c:raise()
end })
end
})
table.insert(list, { "master" .. menu_marker(c == awful.client.getmaster()), function()
table.insert(list, {
"master" .. menu_marker(c == awful.client.getmaster()),
function()
c:swap(awful.client.getmaster())
end })
end
})
table.insert(list, { "sticky" .. menu_marker(c.sticky), function()
table.insert(list, {
"sticky" .. menu_marker(c.sticky),
function()
c.sticky = not c.sticky
end })
end
})
table.insert(list, { "top" .. menu_marker(c.ontop), function()
table.insert(list, {
"top" .. menu_marker(c.ontop),
function()
c.ontop = not c.ontop
end })
end
})
table.insert(list, { "minimize" .. menu_marker(c.minimized), function()
table.insert(list, {
"minimize" .. menu_marker(c.minimized),
function()
if c.minimized then
c.minimized = false
c:raise()
else
c.minimized = true
end
end })
end
})
table.insert(list, { "floating" .. menu_marker(c.floating), function()
table.insert(list, {
"floating" .. menu_marker(c.floating),
function()
c.floating = not c.floating
end })
end
})
table.insert(list, { menu_marker(nil) .. "close", function()
table.insert(list, {
menu_marker(nil) .. "close",
function()
c:kill()
end })
end
})
return list
end
local rounded_corner_shape = function(radius, position)
if position == "bottom" then
return function(cr, width, height)
@ -151,8 +177,8 @@ end
local function new(config)
local cfg = config or {}
local positions = cfg.positions or { "left", "right", "top", "bottom" }
local button_positions = cfg.button_positions or { "top" }
local positions = cfg.positions or {"left", "right", "top", "bottom"}
local button_positions = cfg.button_positions or {"top"}
local border_width = cfg.border_width or dpi(6)
local rounded_corner = cfg.rounded_corner or nil
@ -205,7 +231,7 @@ local function new(config)
local align_horizontal = cfg.align_horizontal or "right" -- "left" | "center" | "right"
local align_vertical = cfg.align_vertical or "center" -- "top" | "center" | "bottom"
local buttons = cfg.buttons or { "floating", "minimize", "maximize", "close" }
local buttons = cfg.buttons or {"floating", "minimize", "maximize", "close"}
local button_left_click = cfg.button_left_click or function(c)
if c.maximized then
@ -351,23 +377,19 @@ local function new(config)
end
if type(button_positions) == "string" then
button_positions = { button_positions }
button_positions = {button_positions}
end
local smart_border_titlebars = function(c)
local button_widgets = {}
local border_bg = wibox.widget.base.make_widget_declarative({
id = "border_bg",
bg = color_normal,
widget = wibox.container.background
})
local border_bg = wibox.widget.base.make_widget_declarative(
{id = "border_bg", bg = color_normal, widget = wibox.container.background})
border_bg:connect_signal("button::press", function(_, _, _, button)
handle_button_press(c, button)
end)
if color_hover then
border_bg:connect_signal("mouse::enter", function()
border_bg.bg = color_hover
@ -384,7 +406,8 @@ local function new(config)
local border_expander, border_expander_center
if layout == "fixed" then
border_expander_center = wibox.widget.base.make_widget_declarative({
border_expander_center = wibox.widget.base.make_widget_declarative(
{
fill_vertical = true,
fill_horizontal = true,
content_fill_vertical = true,
@ -392,10 +415,11 @@ local function new(config)
border_bg,
widget = wibox.container.place
})
border_expander = wibox.widget.base.make_widget_declarative({
{ layout = wibox.layout.fixed.horizontal },
border_expander = wibox.widget.base.make_widget_declarative(
{
{layout = wibox.layout.fixed.horizontal},
border_bg,
{ layout = wibox.layout.fixed.horizontal },
{layout = wibox.layout.fixed.horizontal},
widget = wibox.layout.align.horizontal
})
end
@ -403,11 +427,7 @@ local function new(config)
local _button_positions = list2map(button_positions)
for _, pos in pairs(positions) do
local tb = awful.titlebar(c, {
size = border_width,
position = pos,
bg = "#00000000"
})
local tb = awful.titlebar(c, {size = border_width, position = pos, bg = "#00000000"})
local btn_layout
if layout == "fixed" then
@ -419,18 +439,16 @@ local function new(config)
if _button_positions[pos] then
-- border with buttons
local button_layout = wibox.widget.base.make_widget_declarative({
id = "button_layout",
spacing_widget = spacing_widget,
layout = btn_layout
})
local button_layout = wibox.widget.base.make_widget_declarative(
{id = "button_layout", spacing_widget = spacing_widget, layout = btn_layout})
local titlebar_widget
if layout == "fixed" then
if ori(pos) == "v" then
local expander = align_vertical == "center" and border_expander_center or border_expander
titlebar_widget = wibox.widget.base.make_widget_declarative({
titlebar_widget = wibox.widget.base.make_widget_declarative(
{
align_vertical == "top" and button_layout or expander,
align_vertical == "center" and button_layout or expander,
align_vertical == "bottom" and button_layout or expander,
@ -439,7 +457,8 @@ local function new(config)
})
else
local expander = align_horizontal == "center" and border_expander_center or border_expander
titlebar_widget = wibox.widget.base.make_widget_declarative({
titlebar_widget = wibox.widget.base.make_widget_declarative(
{
align_horizontal == "left" and button_layout or expander,
align_horizontal == "center" and button_layout or expander,
align_horizontal == "right" and button_layout or expander,
@ -450,12 +469,8 @@ local function new(config)
end
if layout == "ratio" then
titlebar_widget = wibox.widget.base.make_widget_declarative({
button_layout,
id = "titlebar_widget",
bg = color_normal,
widget = wibox.container.background
})
titlebar_widget = wibox.widget.base.make_widget_declarative(
{button_layout, id = "titlebar_widget", bg = color_normal, widget = wibox.container.background})
end
tb:setup{
@ -465,7 +480,8 @@ local function new(config)
widget = wibox.container.background()
}
local ratio_button_layout = wibox.widget.base.make_widget_declarative({
local ratio_button_layout = wibox.widget.base.make_widget_declarative(
{
homogeneous = layout == "ratio" and true or false,
expand = true,
layout = ori(pos) == "h" and wibox.layout.grid.horizontal or wibox.layout.grid.vertical
@ -474,8 +490,20 @@ local function new(config)
local list_of_buttons = {}
for _, btn in pairs(buttons) do
local b = button_definitions[btn]
if b then
local button_widget = wibox.widget.base.make_widget_declarative({
if not b then
-- custom button
b = {}
b.name = cfg["button_" .. btn .. "_name"] or btn
b.button_size = cfg["button_" .. btn .. "_size"] or button_size
b.color_focus = cfg["color_" .. btn .. "_focus"] or "#ff00ff"
b.color_normal = cfg["color_" .. btn .. "_normal"] or "#ff00ff"
b.color_hover = cfg["color_" .. btn .. "_normal"] or "#ff1aff"
b.action = cfg["button_" .. btn .. "_function"] or nil
end
local button_widget = wibox.widget.base.make_widget_declarative(
{
id = b.name,
forced_width = ori(pos) == "h" and b.button_size or nil,
forced_height = ori(pos) == "v" and b.button_size or nil,
@ -484,8 +512,8 @@ local function new(config)
})
if show_button_tooltips then
awful.tooltip{
objects = { button_widget },
awful.tooltip {
objects = {button_widget},
timer_function = function()
return b.name
end
@ -526,7 +554,6 @@ local function new(config)
button_widgets[b.name] = button_widget
end
end
if layout == "ratio" then
ratio_button_layout:set_children(list_of_buttons)
@ -539,10 +566,12 @@ local function new(config)
if (ori(pos) == "h" and align_horizontal == "left") or (ori(pos) == "v" and align_vertical == "top") then
button_layout:ajust_ratio(2, 0, button_ratio, 1.0 - button_ratio)
end
if (ori(pos) == "h" and align_horizontal == "right") or (ori( pos) == "v" and align_vertical == "bottom") then
if (ori(pos) == "h" and align_horizontal == "right") or
(ori(pos) == "v" and align_vertical == "bottom") then
button_layout:ajust_ratio(2, 1.0 - button_ratio, button_ratio, 0)
end
if (ori(pos) == "h" and align_horizontal == "center") or (ori( pos) == "v" and align_vertical == "center") then
if (ori(pos) == "h" and align_horizontal == "center") or
(ori(pos) == "v" and align_vertical == "center") then
local side_ratio = (1.0 - button_ratio) / 2
button_layout:ajust_ratio(2, side_ratio, button_ratio, side_ratio)
end
@ -563,8 +592,8 @@ local function new(config)
-- show client title tooltip on border hover
if show_title_tooltip then
awful.tooltip{
objects = { border_bg },
awful.tooltip {
objects = {border_bg},
timer_function = function()
return c.name
end
@ -597,7 +626,9 @@ local function new(config)
client.connect_signal("request::titlebars", smart_border_titlebars)
end
return setmetatable(module, { __call = function(_, ...)
return setmetatable(module, {
__call = function(_, ...)
new(...)
return module
end })
end
})