feat(animate): allow toggling buffer-local / global animations with or without id

This commit is contained in:
Folke Lemaitre 2024-12-12 09:18:54 +01:00
parent 42439123c4
commit 50912dc2fd
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 32 additions and 26 deletions

View file

@ -83,7 +83,7 @@ function Animation:next()
end
function Animation:enabled()
return vim.g.snacks_animate ~= false and (vim.b[self.opts.buf or 0].snacks_animate ~= false)
return M.enabled({ buf = self.opts.buf, name = tostring(self.id) })
end
---@return boolean done
@ -106,6 +106,19 @@ function Animation:stop()
active[self.id] = nil
end
--- Check if animations are enabled.
--- Will return false if `snacks_animate` is set to false or if the buffer
--- local variable `snacks_animate` is set to false.
---@param opts? {buf?: number, name?: string}
function M.enabled(opts)
opts = opts or {}
if opts.name and not M.enabled({ buf = opts.buf }) then
return false
end
local key = "snacks_animate" .. (opts.name and ("_" .. opts.name) or "")
return Snacks.util.var(opts.buf, key, true)
end
--- Add an animation
---@param from number
---@param to number

View file

@ -64,8 +64,9 @@ function M.on_win(win, buf, top, bottom)
ephemeral = true,
})
end
local from = M.animating and scopes_anim[win] and scopes_anim[win].from or scope.from
local to = M.animating and scopes_anim[win] and scopes_anim[win].to or scope.to
local animating = Snacks.animate.enabled({ buf = buf, name = "dim" })
local from = animating and scopes_anim[win] and scopes_anim[win].from or scope.from
local to = animating and scopes_anim[win] and scopes_anim[win].to or scope.to
for l = top, math.min(from - 1, bottom) do
add(l)
end
@ -83,9 +84,7 @@ function M.enable(opts)
M.enabled = true
if opts.animate then
M.animating = true
end
vim.g.snacks_animate_dim = opts.animate.enabled
-- setup decoration provider
vim.api.nvim_set_decoration_provider(ns, {
@ -98,7 +97,7 @@ function M.enable(opts)
scopes = scopes
or Snacks.scope.attach(function(win, buf, scope)
if not M.animating then
if not Snacks.animate.enabled({ buf = buf, name = "dim" }) then
Snacks.util.redraw(win)
else
if not (scopes_anim[win] and scopes_anim[win].buf == buf) then
@ -145,9 +144,4 @@ function M.disable()
vim.cmd([[redraw!]])
end
-- Toggle scope animations
function M.animate()
M.animating = not M.animating
end
return M

View file

@ -6,7 +6,6 @@ M.meta = {
}
M.enabled = false
M.animating = false
---@class snacks.indent.Config
---@field enabled? boolean
@ -173,6 +172,10 @@ local function get_extmark(indent, state)
return cache_extmarks[key]
end
local function animating(buf)
return Snacks.animate.enabled({ buf = buf, name = "indent" })
end
---@param win number
---@param buf number
---@param top number
@ -275,7 +278,7 @@ end
function M.render_scope(scope, state)
local indent = (scope.indent or 2)
local hl = get_hl(scope.indent + 1, config.scope.hl)
local to = M.animating and scope.step or scope.to
local to = animating(scope.buf) and scope.step or scope.to
local col = indent - state.leftcol
if config.scope.underline and scope.from >= state.top and scope.from <= state.bottom then
@ -319,7 +322,7 @@ function M.render_chunk(scope, state)
if col < 0 then -- scope is hidden
return
end
local to = M.animating and scope.step or scope.to
local to = animating(scope.buf) and scope.step or scope.to
local hl = get_hl(scope.indent + 1, config.chunk.hl)
local char = config.chunk.char
@ -349,11 +352,6 @@ function M.render_chunk(scope, state)
end
end
-- Toggle scope animations
function M.animate()
M.animating = not M.animating
end
-- Called when the scope changes
---@param win number
---@param buf number
@ -368,7 +366,7 @@ function M.on_scope(win, buf, scope, prev)
if scope then
scope.win = win
scope.step = scope.from
if M.animating then
if animating(scope.buf) then
Snacks.animate(
scope.from,
scope.to,
@ -383,10 +381,10 @@ function M.on_scope(win, buf, scope, prev)
int = true,
id = "indent_scope_" .. win,
buf = buf,
}, config.scope.animate)
}, config.animate)
)
end
Snacks.util.redraw_range(win, scope.from, M.animating and scope.from + 1 or scope.to)
Snacks.util.redraw_range(win, scope.from, animating(scope.buf) and scope.from + 1 or scope.to)
end
end
@ -416,9 +414,7 @@ function M.enable()
M.debug()
end
if config.scope.animate.enabled then
M.animate()
end
vim.g.snacks_animate_indent = config.animate.enabled
M.enabled = true

View file

@ -51,6 +51,9 @@ local function get_state(win)
if not config.filter(buf) then
return
end
if not Snacks.animate.enabled({ buf = buf, name = "scroll" }) then
return
end
local view = vim.api.nvim_win_call(win, vim.fn.winsaveview) ---@type vim.fn.winsaveview.ret
if not (states[win] and states[win].buf == buf) then
---@diagnostic disable-next-line: missing-fields