-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.lua
96 lines (78 loc) · 1.91 KB
/
utils.lua
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
85
86
87
88
89
90
91
92
93
94
95
96
local module = {}
function noop() end
function module.debounce(func, delay)
local timer = nil
return function(...)
local args = { ... }
if timer then
timer:stop()
timer = nil
end
timer = hs.timer.doAfter(delay, function()
func(table.unpack(args))
end)
end
end
function module.throttle(func, delay)
local wait = false
local storedArgs = nil
local timer = nil
local function checkStoredArgs()
if storedArgs == nil then
wait = false
else
func(table.unpack(storedArgs))
storedArgs = nil
timer = hs.timer.doAfter(delay, checkStoredArgs)
end
end
return function(...)
local args = { ... }
if wait then
storedArgs = args
return
end
func(table.unpack(args))
wait = true
timer = hs.timer.doAfter(delay, checkStoredArgs)
end
end
function module.clamp(value, min, max)
return math.max(math.min(value, max), min)
end
--- 过渡效果工具函数
-- @param options 参数配置
-- @field duration 过渡时长
-- @field easing 缓动函数,函数接受一个真实进度并返回缓动后的进度
-- @field onProgress 过渡时触发
-- @field onEnd 过渡结束后触发
-- @return 用于取消过渡的函数
function module.animate(options)
local duration = options.duration
local easing = options.easing
local onProgress = options.onProgress
local onEnd = options.onEnd or noop
local st = hs.timer.absoluteTime()
local timer = nil
local function progress()
local now = hs.timer.absoluteTime()
local diffSec = (now - st) / 1000000000
if diffSec <= duration then
onProgress(easing(diffSec / duration))
timer = hs.timer.doAfter(1 / 60, function() progress() end)
else
timer = nil
onProgress(1)
onEnd()
end
end
-- 初始执行
progress()
return function()
if timer then
timer:stop()
onEnd()
end
end
end
return module