mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-02 09:52:23 +00:00
feat(animate): allow toggling buffer-local / global animations with or without id
This commit is contained in:
parent
42439123c4
commit
50912dc2fd
4 changed files with 32 additions and 26 deletions
|
@ -83,7 +83,7 @@ function Animation:next()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Animation:enabled()
|
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
|
end
|
||||||
|
|
||||||
---@return boolean done
|
---@return boolean done
|
||||||
|
@ -106,6 +106,19 @@ function Animation:stop()
|
||||||
active[self.id] = nil
|
active[self.id] = nil
|
||||||
end
|
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
|
--- Add an animation
|
||||||
---@param from number
|
---@param from number
|
||||||
---@param to number
|
---@param to number
|
||||||
|
|
|
@ -64,8 +64,9 @@ function M.on_win(win, buf, top, bottom)
|
||||||
ephemeral = true,
|
ephemeral = true,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
local from = M.animating and scopes_anim[win] and scopes_anim[win].from or scope.from
|
local animating = Snacks.animate.enabled({ buf = buf, name = "dim" })
|
||||||
local to = M.animating and scopes_anim[win] and scopes_anim[win].to or scope.to
|
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
|
for l = top, math.min(from - 1, bottom) do
|
||||||
add(l)
|
add(l)
|
||||||
end
|
end
|
||||||
|
@ -83,9 +84,7 @@ function M.enable(opts)
|
||||||
|
|
||||||
M.enabled = true
|
M.enabled = true
|
||||||
|
|
||||||
if opts.animate then
|
vim.g.snacks_animate_dim = opts.animate.enabled
|
||||||
M.animating = true
|
|
||||||
end
|
|
||||||
|
|
||||||
-- setup decoration provider
|
-- setup decoration provider
|
||||||
vim.api.nvim_set_decoration_provider(ns, {
|
vim.api.nvim_set_decoration_provider(ns, {
|
||||||
|
@ -98,7 +97,7 @@ function M.enable(opts)
|
||||||
|
|
||||||
scopes = scopes
|
scopes = scopes
|
||||||
or Snacks.scope.attach(function(win, buf, scope)
|
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)
|
Snacks.util.redraw(win)
|
||||||
else
|
else
|
||||||
if not (scopes_anim[win] and scopes_anim[win].buf == buf) then
|
if not (scopes_anim[win] and scopes_anim[win].buf == buf) then
|
||||||
|
@ -145,9 +144,4 @@ function M.disable()
|
||||||
vim.cmd([[redraw!]])
|
vim.cmd([[redraw!]])
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Toggle scope animations
|
|
||||||
function M.animate()
|
|
||||||
M.animating = not M.animating
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -6,7 +6,6 @@ M.meta = {
|
||||||
}
|
}
|
||||||
|
|
||||||
M.enabled = false
|
M.enabled = false
|
||||||
M.animating = false
|
|
||||||
|
|
||||||
---@class snacks.indent.Config
|
---@class snacks.indent.Config
|
||||||
---@field enabled? boolean
|
---@field enabled? boolean
|
||||||
|
@ -173,6 +172,10 @@ local function get_extmark(indent, state)
|
||||||
return cache_extmarks[key]
|
return cache_extmarks[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function animating(buf)
|
||||||
|
return Snacks.animate.enabled({ buf = buf, name = "indent" })
|
||||||
|
end
|
||||||
|
|
||||||
---@param win number
|
---@param win number
|
||||||
---@param buf number
|
---@param buf number
|
||||||
---@param top number
|
---@param top number
|
||||||
|
@ -275,7 +278,7 @@ end
|
||||||
function M.render_scope(scope, state)
|
function M.render_scope(scope, state)
|
||||||
local indent = (scope.indent or 2)
|
local indent = (scope.indent or 2)
|
||||||
local hl = get_hl(scope.indent + 1, config.scope.hl)
|
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
|
local col = indent - state.leftcol
|
||||||
|
|
||||||
if config.scope.underline and scope.from >= state.top and scope.from <= state.bottom then
|
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
|
if col < 0 then -- scope is hidden
|
||||||
return
|
return
|
||||||
end
|
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 hl = get_hl(scope.indent + 1, config.chunk.hl)
|
||||||
local char = config.chunk.char
|
local char = config.chunk.char
|
||||||
|
|
||||||
|
@ -349,11 +352,6 @@ function M.render_chunk(scope, state)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Toggle scope animations
|
|
||||||
function M.animate()
|
|
||||||
M.animating = not M.animating
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Called when the scope changes
|
-- Called when the scope changes
|
||||||
---@param win number
|
---@param win number
|
||||||
---@param buf number
|
---@param buf number
|
||||||
|
@ -368,7 +366,7 @@ function M.on_scope(win, buf, scope, prev)
|
||||||
if scope then
|
if scope then
|
||||||
scope.win = win
|
scope.win = win
|
||||||
scope.step = scope.from
|
scope.step = scope.from
|
||||||
if M.animating then
|
if animating(scope.buf) then
|
||||||
Snacks.animate(
|
Snacks.animate(
|
||||||
scope.from,
|
scope.from,
|
||||||
scope.to,
|
scope.to,
|
||||||
|
@ -383,10 +381,10 @@ function M.on_scope(win, buf, scope, prev)
|
||||||
int = true,
|
int = true,
|
||||||
id = "indent_scope_" .. win,
|
id = "indent_scope_" .. win,
|
||||||
buf = buf,
|
buf = buf,
|
||||||
}, config.scope.animate)
|
}, config.animate)
|
||||||
)
|
)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -416,9 +414,7 @@ function M.enable()
|
||||||
M.debug()
|
M.debug()
|
||||||
end
|
end
|
||||||
|
|
||||||
if config.scope.animate.enabled then
|
vim.g.snacks_animate_indent = config.animate.enabled
|
||||||
M.animate()
|
|
||||||
end
|
|
||||||
|
|
||||||
M.enabled = true
|
M.enabled = true
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,9 @@ local function get_state(win)
|
||||||
if not config.filter(buf) then
|
if not config.filter(buf) then
|
||||||
return
|
return
|
||||||
end
|
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
|
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
|
if not (states[win] and states[win].buf == buf) then
|
||||||
---@diagnostic disable-next-line: missing-fields
|
---@diagnostic disable-next-line: missing-fields
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue