awesome-wm-nice/utils.lua

140 lines
3.8 KiB
Lua
Raw Normal View History

2021-12-28 18:36:35 +01:00
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 floor = math.floor
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 = 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
return utils