Fixed window shade not working. Added ability to move window while shaded

This commit is contained in:
mut-ex 2020-09-09 11:35:00 -04:00
parent ac588b5ef5
commit dbcc6ab9c8
2 changed files with 93 additions and 28 deletions

View File

@ -1,9 +1,18 @@
--[[
Author: mu-tex
License: MIT
Repository: https://github.com/mut-ex/awesome-wm-nice
]] -- ============================================================
-- local ret, helpers = pcall(require, "helpers")
-- local debug = ret and helpers.debug or function() end
-- => Awesome WM
-- ============================================================ -- ============================================================
local ret, helpers = pcall(require, "helpers")
local debug = ret and helpers.debug or function() end
local pairs = pairs
local ipairs = ipairs
-- > Awesome WM LIBS
local awful = require("awful") local awful = require("awful")
local atooltip = awful.tooltip local atooltip = awful.tooltip
local abutton = awful.button local abutton = awful.button
@ -16,6 +25,7 @@ local textbox = wibox.widget.textbox
local wlayout = wibox.layout local wlayout = wibox.layout
local wlayout_align_horizontal = wlayout.align.horizontal local wlayout_align_horizontal = wlayout.align.horizontal
local wlayout_fixed_horizontal = wlayout.fixed.horizontal local wlayout_fixed_horizontal = wlayout.fixed.horizontal
local wlayout_flex_horizontal = wlayout.flex.horizontal
-- Containers -- Containers
local wcontainer = wibox.container local wcontainer = wibox.container
local wcontainer_background = wcontainer.background local wcontainer_background = wcontainer.background
@ -26,20 +36,32 @@ local wcontainer_place = wcontainer.place
local gsurface = require("gears.surface") local gsurface = require("gears.surface")
local gtimer = require("gears.timer") local gtimer = require("gears.timer")
local gtimer_weak_start_new = gtimer.weak_start_new local gtimer_weak_start_new = gtimer.weak_start_new
-- > Math -- ------------------------------------------------------------
-- => Math + standard Lua methods
-- ============================================================
local math = math local math = math
local max = math.max local max = math.max
local abs = math.abs local abs = math.abs
local rad = math.rad local rad = math.rad
local floor = math.floor local floor = math.floor
-- > LGI local pairs = pairs
local ipairs = ipairs
-- ------------------------------------------------------------
-- => LGI
-- ============================================================
local lgi = require("lgi") local lgi = require("lgi")
local cairo = lgi.cairo local cairo = lgi.cairo
local gdk = lgi.Gdk local gdk = lgi.Gdk
local get_default_root_window = gdk.get_default_root_window local get_default_root_window = gdk.get_default_root_window
local pixbuf_get_from_surface = gdk.pixbuf_get_from_surface local pixbuf_get_from_surface = gdk.pixbuf_get_from_surface
local pixbuf_get_from_window = gdk.pixbuf_get_from_window local pixbuf_get_from_window = gdk.pixbuf_get_from_window
-- > NICE LIBS -- ------------------------------------------------------------
-- => nice
-- ============================================================
-- Colors -- Colors
local colors = require("nice.colors") local colors = require("nice.colors")
local color_darken = colors.darken local color_darken = colors.darken
@ -52,6 +74,7 @@ local create_corner_top_left = shapes.create_corner_top_left
local create_edge_left = shapes.create_edge_left local create_edge_left = shapes.create_edge_left
local create_edge_top_middle = shapes.create_edge_top_middle local create_edge_top_middle = shapes.create_edge_top_middle
local gradient = shapes.duotone_gradient_vertical local gradient = shapes.duotone_gradient_vertical
-- ------------------------------------------------------------
gdk.init({}) gdk.init({})
@ -373,7 +396,7 @@ end
local function get_titlebar_mouse_bindings(c) local function get_titlebar_mouse_bindings(c)
local client_color = c._nice_base_color local client_color = c._nice_base_color
local shade_enabled = _private.window_shade_enabled local shade_enabled = _private.win_shade_enabled
-- Add functionality for double click to (un)maximize, and single click and hold to move -- Add functionality for double click to (un)maximize, and single click and hold to move
local clicks = 0 local clicks = 0
local tolerance = double_click_jitter_tolerance local tolerance = double_click_jitter_tolerance
@ -393,10 +416,12 @@ local function get_titlebar_mouse_bindings(c)
c.maximized = not c.maximized c.maximized = not c.maximized
end end
else else
if shade_enabled then if shade_enabled and c._nice_window_shade_up then
_private.shade_roll_down(c) -- _private.shade_roll_down(c)
awful.mouse.wibox.move(c._nice_window_shade)
else
c:activate{context = "titlebar", action = "mouse_move"}
end end
c:activate{context = "titlebar", action = "mouse_move"}
end end
-- Start a timer to clear the click count -- Start a timer to clear the click count
gtimer_weak_start_new( gtimer_weak_start_new(
@ -447,7 +472,7 @@ local function get_titlebar_mouse_bindings(c)
end), end),
} }
if _private.window_shade_enabled then if _private.win_shade_enabled then
buttons[#buttons + 1] = abutton( buttons[#buttons + 1] = abutton(
{}, _private.mb_win_shade_rollup, {}, _private.mb_win_shade_rollup,
function() function()
@ -494,7 +519,7 @@ local function create_titlebar_title(c)
local margin_vertical = leftover_space > 1 and leftover_space / 2 or 0 local margin_vertical = leftover_space > 1 and leftover_space / 2 or 0
return { return {
title_widget, title_widget,
widget = wibox.container.margin, widget = wcontainer_margin,
top = margin_vertical, top = margin_vertical,
bottom = margin_vertical, bottom = margin_vertical,
} }
@ -555,35 +580,41 @@ local function add_window_shade(c, src_top, src_bottom)
w.background = "transparent" w.background = "transparent"
w.x = geo.x w.x = geo.x
w.y = geo.y w.y = geo.y
w.height = _private.titlebar_height + 3 w.height = _private.titlebar_height + bottom_edge_height
w.ontop = true w.ontop = true
w.visible = false w.visible = false
w.shape = shapes.rounded_rect { w.shape = shapes.rounded_rect {
tl = _private.titlebar_radius + 1, tl = _private.titlebar_radius,
tr = _private.titlebar_radius + 1, tr = _private.titlebar_radius,
bl = 4, bl = 4,
br = 4, br = 4,
} }
w.widget = wibox.widget { -- Need to use a manual layout because layout fixed seems to introduce a thin gap
{src_top, src_bottom, layout = wlayout.fixed.vertical}, src_top.point = {x = 0, y = 0}
widget = wcontainer_background, src_top.forced_width = geo.width
bg = "transparent", src_bottom.point = {x = 0, y = _private.titlebar_height}
} w.widget = {src_top, src_bottom, layout = wlayout.manual}
-- Clean up resources when a client is killed
c:connect_signal( c:connect_signal(
"request::unmanage", function() "request::unmanage", function()
if c._nice_window_shade then if c._nice_window_shade then
c._nice_window_shade.visible = false c._nice_window_shade.visible = false
c._nice_window_shade = nil c._nice_window_shade = nil
end end
-- Clean up
collectgarbage("collect")
end) end)
c._nice_window_shade_up = false
c._nice_window_shade = w c._nice_window_shade = w
end end
-- Shows the window contents -- Shows the window contents
function _private.shade_roll_down(c) function _private.shade_roll_down(c)
c:geometry{x = c._nice_window_shade.x, y = c._nice_window_shade.y}
c:activate() c:activate()
c._nice_window_shade.visible = false c._nice_window_shade.visible = false
c._nice_window_shade_up = false
end end
-- Hides the window contents -- Hides the window contents
@ -596,6 +627,7 @@ function _private.shade_roll_up(c)
c.minimized = true c.minimized = true
w.visible = true w.visible = true
w.ontop = true w.ontop = true
c._nice_window_shade_up = true
end end
-- Toggles the window shade state -- Toggles the window shade state
@ -675,7 +707,7 @@ function _private.add_window_decorations(c)
local titlebar = awful.titlebar( local titlebar = awful.titlebar(
c, {size = titlebar_height, bg = "transparent"}) c, {size = titlebar_height, bg = "transparent"})
-- Arrange the graphics -- Arrange the graphics
titlebar:setup{ titlebar.widget = {
imagebox(corner_top_left_img, false), imagebox(corner_top_left_img, false),
{ {
{ {
@ -687,7 +719,7 @@ function _private.add_window_decorations(c)
{ {
create_titlebar_items(c, _private.titlebar_items.middle), create_titlebar_items(c, _private.titlebar_items.middle),
buttons = get_titlebar_mouse_bindings(c), buttons = get_titlebar_mouse_bindings(c),
layout = wibox.layout.flex.horizontal, layout = wlayout_flex_horizontal,
}, },
{ {
create_titlebar_items(c, _private.titlebar_items.right), create_titlebar_items(c, _private.titlebar_items.right),
@ -785,14 +817,16 @@ function _private.add_window_decorations(c)
bg = "transparent", bg = "transparent",
position = "bottom", position = "bottom",
}) })
bottom:setup{ bottom.widget = wibox.widget {
imagebox(corner_bottom_left_img, false), imagebox(corner_bottom_left_img, false),
{widget = wcontainer_background, bgimage = bottom_edge}, -- {widget = wcontainer_background, bgimage = bottom_edge},
imagebox(bottom_edge, false),
imagebox(corner_bottom_right_img, false), imagebox(corner_bottom_right_img, false),
layout = wlayout_align_horizontal, layout = wlayout_align_horizontal,
buttons = resize_button, buttons = resize_button,
} }
if _private.window_shade_enabled then if _private.win_shade_enabled then
add_window_shade(c, titlebar.widget, bottom.widget) add_window_shade(c, titlebar.widget, bottom.widget)
end end
@ -821,6 +855,7 @@ function _private.add_window_decorations(c)
end end
end) end)
end end
-- Clean up -- Clean up
collectgarbage("collect") collectgarbage("collect")
end end

View File

@ -214,6 +214,35 @@ local function create_edge_left(args)
return surface return surface
end end
local function set_font(cr, font)
cr:set_font_size(font.size)
cr:select_font_face(
font.font or "Inter", font.italic and 1 or 0, font.bold and 1 or 0)
end
local function text_label(args)
local surface = cairo.ImageSurface.create("ARGB32", 1, 1)
local cr = cairo.Context.create(surface)
set_font(cr, args.font)
local text = args.text
local kern = args.font.kerning or 0
local ext = cr:text_extents(text)
surface = cairo.ImageSurface.create(
"ARGB32", ext.width + string.len(text) * kern, ext.height)
cr = cairo.Context.create(surface)
set_font(cr, args.font)
cr:move_to(0, ext.height)
cr:set_source_rgb(hex2rgb(args.color))
-- cr:show_text(text)
text:gsub(
".", function(c)
-- do something with c
cr:show_text(c)
cr:rel_move_to(kern, 0)
end)
return surface
end
return { return {
rounded_rect = rounded_rect, rounded_rect = rounded_rect,
circle_filled = circle_filled, circle_filled = circle_filled,
@ -222,4 +251,5 @@ return {
create_corner_top_left = create_corner_top_left, create_corner_top_left = create_corner_top_left,
create_edge_top_middle = create_edge_top_middle, create_edge_top_middle = create_edge_top_middle,
create_edge_left = create_edge_left, create_edge_left = create_edge_left,
text_label = text_label,
} }