awesome-wm-nice/utils.lua

169 lines
4.7 KiB
Lua

local gsurface = require "gears.surface"
local lgi = require "lgi"
local wibox = require "wibox"
local widgets = require "awesome-wm-nice.widgets"
local wlayout_fixed = require "wibox.layout.fixed"
local gdk = lgi.require("Gdk", "3.0")
local utils = {}
function utils.rel_lighten(lum)
return lum * 90 + 10
end
function utils.rel_darken(lum)
return -(lum * 70) + 100
end
-- Returns the hex color for the pixel at the given coordinates on the screen
function utils.get_pixel_at(x, y)
local pixbuf = gdk.pixbuf_get_from_window(
gdk.get_default_root_window(),
x,
y,
1,
1
)
local bytes = pixbuf:get_pixels()
return "#"
.. bytes:gsub(".", function(c)
return ("%02x"):format(c:byte())
end)
end
-- Determines the dominant color of the client's top region
function utils.get_dominant_color(client)
local color
-- gsurface(client.content):write_to_png(
-- "/home/mutex/nice/" .. client.class .. "_" .. client.instance .. ".png")
local pb
local bytes
local tally = {}
local content = gsurface(client.content)
local cgeo = client:geometry()
local x_offset = 2
local y_offset = 2
local x_lim = math.floor(cgeo.width / 2)
for x_pos = 0, x_lim, 2 do
for y_pos = 0, 8, 1 do
pb = gdk.pixbuf_get_from_surface(
content,
x_offset + x_pos,
y_offset + y_pos,
1,
1
)
bytes = pb:get_pixels()
color = "#"
.. bytes:gsub(".", function(c)
return ("%02x"):format(c:byte())
end)
if not tally[color] then
tally[color] = 1
else
tally[color] = tally[color] + 1
end
end
end
local mode
local mode_c = 0
for kolor, kount in pairs(tally) do
if kount > mode_c then
mode_c = kount
mode = kolor
end
end
color = mode
return color
end
-- Returns a titlebar item
function utils.get_titlebar_item(c, name)
if name == "close" then
return widgets.create_titlebar_button(c, name, function()
c:kill()
end)
elseif name == "maximize" then
return widgets.create_titlebar_button(c, name, function()
c.maximized = not c.maximized
end, "maximized")
elseif name == "minimize" then
return widgets.create_titlebar_button(c, name, function()
c.minimized = true
end)
elseif name == "ontop" then
return widgets.create_titlebar_button(c, name, function()
c.ontop = not c.ontop
end, "ontop")
elseif name == "floating" then
return widgets.create_titlebar_button(c, name, function()
c.floating = not c.floating
if c.floating then
c.maximized = false
end
end, "floating")
elseif name == "sticky" then
return widgets.create_titlebar_button(c, name, function()
c.sticky = not c.sticky
return c.sticky
end, "sticky")
elseif name == "title" then
return widgets.create_titlebar_title(c)
end
end
-- Creates titlebar items for a given group of item names
-- group can be a string (=item name) or a table (= array of "item-name"s)
function utils.create_titlebar_items(c, group)
if not group then
return nil
end
if type(group) == "string" then
return utils.get_titlebar_item(c, group)
end
local titlebar_group_items = wibox.widget {
layout = wlayout_fixed.horizontal,
}
local item
for _, name in ipairs(group) do
item = utils.get_titlebar_item(c, name)
if item then
titlebar_group_items:add(item)
end
end
return titlebar_group_items
end
function utils.validate_mb_bindings(private)
local action_mbs = {
"mb_move",
"mb_contextmenu",
"mb_resize",
"mb_win_shade_rollup",
"mb_win_shade_rolldown",
}
local mb_specified = { false, false, false, false, false }
local mb
local mb_conflict_test
for _, action_mb in ipairs(action_mbs) do
mb = private[action_mb]
if mb then
assert(mb >= 1 and mb <= 5, "Invalid mouse button specified!")
mb_conflict_test = mb_specified[mb]
if not mb_conflict_test then
mb_specified[mb] = action_mb
else
error(
("%s and %s can not be bound to the same mouse button"):format(
action_mb,
mb_conflict_test
)
)
end
end
end
end
return utils