feat(animate): allow disabling all animations globally or per buffer

This commit is contained in:
Folke Lemaitre 2024-12-12 08:44:19 +01:00
parent 4f22016b4b
commit 25c290d7c0
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
5 changed files with 27 additions and 5 deletions

View file

@ -38,6 +38,7 @@ local defaults = {
}
---@class snacks.animate.Opts: snacks.animate.Config
---@field buf? number optional buffer to check if animations should be enabled
---@field int? boolean interpolate the value to an integer
---@field id? number|string unique identifier for the animation
@ -70,6 +71,9 @@ Animation.__index = Animation
---@return number value, boolean done
function Animation:next()
self.start = self.start == 0 and uv.hrtime() or self.start
if not self:enabled() then
return self.to, true
end
local elapsed = (uv.hrtime() - self.start) / 1e6 -- ms
local b, c, d = self.from, self.to - self.from, self.duration
local t, done = math.min(elapsed, d), elapsed >= d
@ -78,6 +82,10 @@ function Animation:next()
return value, done
end
function Animation:enabled()
return vim.g.snacks_animate ~= false and (vim.b[self.opts.buf or 0].snacks_animate ~= false)
end
---@return boolean done
function Animation:update()
local value, done = self:next()

View file

@ -116,7 +116,7 @@ function M.enable(opts)
end
scopes_anim[win].from = v
Snacks.util.redraw(win)
end, vim.tbl_extend("keep", { int = true, id = "snacks_dim_from_" .. win }, opts.animate))
end, vim.tbl_extend("keep", { int = true, id = "snacks_dim_from_" .. win, buf = buf }, opts.animate))
Snacks.animate(scopes_anim[win].to, scope.to, function(v)
if not scopes_anim[win] then
@ -124,7 +124,7 @@ function M.enable(opts)
end
scopes_anim[win].to = v
Snacks.util.redraw(win)
end, vim.tbl_extend("keep", { int = true, id = "snacks_dim_to_" .. win }, opts.animate))
end, vim.tbl_extend("keep", { int = true, id = "snacks_dim_to_" .. win, buf = buf }, opts.animate))
end
end, opts.scope)
if not scopes.enabled then

View file

@ -356,11 +356,11 @@ end
-- Called when the scope changes
---@param win number
---@param _buf number
---@param buf number
---@param scope snacks.indent.Scope?
---@param prev snacks.indent.Scope?
---@private
function M.on_scope(win, _buf, scope, prev)
function M.on_scope(win, buf, scope, prev)
stats.scope = stats.scope + 1
if prev then -- clear previous scope
Snacks.util.redraw_range(win, prev.from, prev.to)
@ -382,6 +382,7 @@ function M.on_scope(win, _buf, scope, prev)
vim.tbl_extend("keep", {
int = true,
id = "indent_scope_" .. win,
buf = buf,
}, config.scope.animate)
)
end

View file

@ -260,7 +260,7 @@ function M.check(win)
vim.fn.winrestview(state.current)
end
end)
end, vim.tbl_extend("keep", { int = true, id = ("scroll_%d"):format(win) }, config.animate))
end, vim.tbl_extend("keep", { int = true, id = ("scroll_%d"):format(win), buf = state.buf }, config.animate))
end
---@private

View file

@ -370,4 +370,17 @@ function M.zoom()
})
end
function M.animate()
return M.new({
id = "animate",
name = "Animations",
get = function()
return vim.g.snacks_animate ~= false
end,
set = function(state)
vim.g.snacks_animate = state
end,
})
end
return M