mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-06 11:48:23 +00:00
feat(animate): allow disabling all animations globally or per buffer
This commit is contained in:
parent
4f22016b4b
commit
25c290d7c0
5 changed files with 27 additions and 5 deletions
|
@ -38,6 +38,7 @@ local defaults = {
|
||||||
}
|
}
|
||||||
|
|
||||||
---@class snacks.animate.Opts: snacks.animate.Config
|
---@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 int? boolean interpolate the value to an integer
|
||||||
---@field id? number|string unique identifier for the animation
|
---@field id? number|string unique identifier for the animation
|
||||||
|
|
||||||
|
@ -70,6 +71,9 @@ Animation.__index = Animation
|
||||||
---@return number value, boolean done
|
---@return number value, boolean done
|
||||||
function Animation:next()
|
function Animation:next()
|
||||||
self.start = self.start == 0 and uv.hrtime() or self.start
|
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 elapsed = (uv.hrtime() - self.start) / 1e6 -- ms
|
||||||
local b, c, d = self.from, self.to - self.from, self.duration
|
local b, c, d = self.from, self.to - self.from, self.duration
|
||||||
local t, done = math.min(elapsed, d), elapsed >= d
|
local t, done = math.min(elapsed, d), elapsed >= d
|
||||||
|
@ -78,6 +82,10 @@ function Animation:next()
|
||||||
return value, done
|
return value, done
|
||||||
end
|
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
|
---@return boolean done
|
||||||
function Animation:update()
|
function Animation:update()
|
||||||
local value, done = self:next()
|
local value, done = self:next()
|
||||||
|
|
|
@ -116,7 +116,7 @@ function M.enable(opts)
|
||||||
end
|
end
|
||||||
scopes_anim[win].from = v
|
scopes_anim[win].from = v
|
||||||
Snacks.util.redraw(win)
|
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)
|
Snacks.animate(scopes_anim[win].to, scope.to, function(v)
|
||||||
if not scopes_anim[win] then
|
if not scopes_anim[win] then
|
||||||
|
@ -124,7 +124,7 @@ function M.enable(opts)
|
||||||
end
|
end
|
||||||
scopes_anim[win].to = v
|
scopes_anim[win].to = v
|
||||||
Snacks.util.redraw(win)
|
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
|
||||||
end, opts.scope)
|
end, opts.scope)
|
||||||
if not scopes.enabled then
|
if not scopes.enabled then
|
||||||
|
|
|
@ -356,11 +356,11 @@ end
|
||||||
|
|
||||||
-- Called when the scope changes
|
-- Called when the scope changes
|
||||||
---@param win number
|
---@param win number
|
||||||
---@param _buf number
|
---@param buf number
|
||||||
---@param scope snacks.indent.Scope?
|
---@param scope snacks.indent.Scope?
|
||||||
---@param prev snacks.indent.Scope?
|
---@param prev snacks.indent.Scope?
|
||||||
---@private
|
---@private
|
||||||
function M.on_scope(win, _buf, scope, prev)
|
function M.on_scope(win, buf, scope, prev)
|
||||||
stats.scope = stats.scope + 1
|
stats.scope = stats.scope + 1
|
||||||
if prev then -- clear previous scope
|
if prev then -- clear previous scope
|
||||||
Snacks.util.redraw_range(win, prev.from, prev.to)
|
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", {
|
vim.tbl_extend("keep", {
|
||||||
int = true,
|
int = true,
|
||||||
id = "indent_scope_" .. win,
|
id = "indent_scope_" .. win,
|
||||||
|
buf = buf,
|
||||||
}, config.scope.animate)
|
}, config.scope.animate)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -260,7 +260,7 @@ function M.check(win)
|
||||||
vim.fn.winrestview(state.current)
|
vim.fn.winrestview(state.current)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
|
|
|
@ -370,4 +370,17 @@ function M.zoom()
|
||||||
})
|
})
|
||||||
end
|
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
|
return M
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue