Repository navigation
Expand file tree
/
Copy pathanimations.lua
More file actions
84 lines (77 loc) · 3.34 KB
/
Copy pathanimations.lua
File metadata and controls
84 lines (77 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- plugins/animations.lua
-- Windows fade in when they open and fade out (shrinking a little) when
-- they close.
--
-- plugin("@boring/animations"):mount({
-- open = { duration = 180, easing = "out-cubic" }, -- false: no animation
-- close = { duration = 160, easing = "out-cubic", scale = 0.92 },
-- })
--
-- Closing windows are gone by the time they'd animate, so what fades out is
-- a copy of their last frame: the same buffers, in a tree of their own.
local animate = require("@boring/library/animate")
local scene = require("@boring/library/scene")
local plugin = { name = "animations" }
function plugin:mount(config, ctx)
local scope = ctx.scope
local open = config.open ~= false and (config.open or {}) or nil
local close = config.close ~= false and (config.close or {}) or nil
if open then
scope:subscribe(boring.events, "window:map", function(w)
local tree = scene.window(w)
local function fade(v)
if w.mapped then tree:set_opacity(v) end
end
animate.run(scope, animate.ease({
from = 0, to = 1, duration = open.duration or 180, easing = open.easing or "out-cubic",
}), fade, function() fade(1) end)
end)
end
if close then
local final_scale = close.scale or 0.92
local ghosts = {} -- finish functions of copies still fading
scope:defer(function()
for finish in pairs(ghosts) do finish() end
end)
scope:subscribe(boring.events, "window:unmap", function(w)
if not w:visible() then return end
-- The copy, where the window was, just above it.
local ghost = scene.window(w):snapshot()
local copies = {}
local min_x, min_y, max_x, max_y = math.huge, math.huge, -math.huge, -math.huge
for node in ghost:children() do
local x, y = node:position()
local width, height = node:size()
copies[#copies + 1] = { copy = node, x = x, y = y, width = width, height = height }
min_x, min_y = math.min(min_x, x), math.min(min_y, y)
max_x, max_y = math.max(max_x, x + width), math.max(max_y, y + height)
end
if #copies == 0 then
ghost:destroy()
return
end
local cx, cy = (min_x + max_x) / 2, (min_y + max_y) / 2
local function frame(p)
local s = 1 + (final_scale - 1) * p
for _, c in ipairs(copies) do
c.copy:set_position(math.floor(cx + (c.x - cx) * s + 0.5), math.floor(cy + (c.y - cy) * s + 0.5))
c.copy:set_size(math.max(1, math.floor(c.width * s + 0.5)), math.max(1, math.floor(c.height * s + 0.5)))
end
ghost:set_opacity(1 - p)
end
frame(0)
local binding
local function finish()
if not ghosts[finish] then return end
ghosts[finish] = nil
animate.stop(binding)
ghost:destroy()
end
ghosts[finish] = true
binding = animate.run(nil, animate.ease({
from = 0, to = 1, duration = close.duration or 160, easing = close.easing or "out-cubic",
}), frame, finish)
end)
end
end
return plugin