8000 mini.nvim/lua/mini/animate.lua at main · W4RH4WK/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2146 lines (1880 loc) · 81.8 KB

animate.lua

File metadata and controls

2146 lines (1880 loc) · 81.8 KB
--- Notes:
--- - Animation is also stopped on action error or if maximum number of steps
--- is reached.
--- - Asynchronous execution is done with |uv.new_timer()|. It only allows
--- integer parts as repeat value. This has several implications:
--- - Outputs of `step_timing()` are accumulated in order to preserve total
--- execution time.
--- - Any wait time less than 1 ms means that action will be executed
--- immediately.
---
---@param step_action function|table Callable which takes `step` (integer 0, 1, 2,
--- etc. indicating current step) and executes some action. Its return value
--- defines when animation should stop: values `false` and `nil` (equivalent
--- to no explicit return) stop animation timer; any other continues it.
---@param step_timing function|table Callable which takes `step` (integer 1, 2, etc.
--- indicating next step) and returns how many milliseconds to wait before
--- executing this step action.
---@param opts table|nil Options. Possible fields:
--- - <max_steps> - Maximum value of allowed step to execute. Default: 10000000.
MiniAnimate.animate = function(step_action, step_timing, opts)
opts = vim.tbl_deep_extend('force', { max_steps = 10000000 }, opts or {})
local step, max_steps = 0, opts.max_steps
local timer, wait_time = vim.loop.new_timer(), 0
local draw_step
draw_step = vim.schedule_wrap(function()
local ok, should_continue = pcall(step_action, step)
if not (ok and should_continue and step < max_steps) then
timer:stop()
return
end
step = step + 1
wait_time = wait_time + step_timing(step)
-- Repeat value of `timer` seems to be rounded down to milliseconds. This
-- means that values less than 1 will lead to timer stop repeating. Instead
-- call next step function directly.
if wait_time < 1 then
timer:set_repeat(0)
-- Use `return` to make this proper "tail call"
return draw_step()
else
timer:set_repeat(wait_time)
wait_time = wait_time - timer:get_repeat()
timer:again()
end
end)
-- Start non-repeating timer without callback execution
timer:start(10000000, 0, draw_step)
-- Draw step zero (at origin) immediately
draw_step()
end
--- Generate animation timing
---
--- Each field corresponds to one family of progression which can be customized
--- further by supplying appropriate arguments.
---
--- This is a table with function elements. Call to actually get timing function.
---
--- Example: >lua
---
--- local animate = require('mini.animate')
--- animate.setup({
--- cursor = {
--- timing = animate.gen_timing.linear({ duration = 100, unit = 'total' })
--- },
--- })
--- <
---@seealso |MiniIndentscope.gen_animation| for similar concept in 'mini.indentscope'.
MiniAnimate.gen_timing = {}
---@alias __animate_timing_opts table|nil Options that control progression. Possible keys:
--- - <easing> `(string)` - a subtype of progression. One of "in"
--- (accelerating from zero speed), "out" (decelerating to zero speed),
--- "in-out" (default; accelerating halfway, decelerating after).
--- - <duration> `(number)` - duration (in ms) of a unit. Default: 20.
--- - <unit> `(string)` - which unit's duration `opts.duration` controls. One
--- of "step" (default; ensures average duration of step to be `opts.duration`)
--- or "total" (ensures fixed total duration regardless of scope's range).
---@alias __animate_timing_return function Timing function (see |MiniAnimate-timing|).
--- Generate timing with no animation
---
--- Show final result immediately. Usually better to use `enable` field in `config`
--- if you want to disable animation.
MiniAnimate.gen_timing.none = function()
return function() return 0 end
end
--- Generate timing with linear progression
---
---@param opts __animate_timing_opts
---
---@return __animate_timing_return
MiniAnimate.gen_timing.linear = function(opts) return H.timing_arithmetic(0, H.normalize_timing_opts(opts)) end
--- Generate timing with quadratic progression
---
---@param opts __animate_timing_opts
---
---@return __animate_timing_return
MiniAnimate.gen_timing.quadratic = function(opts) return H.timing_arithmetic(1, H.normalize_timing_opts(opts)) end
--- Generate timing with cubic progression
---
---@param opts __animate_timing_opts
---
---@return __animate_timing_return
MiniAnimate.gen_timing.cubic = function(opts) return H.timing_arithmetic(2, H.normalize_timing_opts(opts)) end
--- Generate timing with quartic progression
---
---@param opts __animate_timing_opts
---
---@return __animate_timing_return
MiniAnimate.gen_timing.quartic = function(opts) return H.timing_arithmetic(3, H.normalize_timing_opts(opts)) end
--- Generate timing with exponential progression
---
---@param opts __animate_timing_opts
---
---@return __animate_timing_return
MiniAnimate.gen_timing.exponential = function(opts) return H.timing_geometrical(H.normalize_timing_opts(opts)) end
--- Generate cursor animation path
---
--- For more information see |MiniAnimate.config.cursor|.
---
--- This is a table with function elements. Call to actually get generator.
---
--- Example: >lua
---
--- local animate = require('mini.animate')
--- animate.setup({
--- cursor = {
--- -- Animate with line-column angle instead of shortest line
--- path = animate.gen_path.angle(),
--- }
--- })
--- <
MiniAnimate.gen_path = {}
---@alias __animate_path_opts_common table|nil Options that control generator. Possible keys:
--- - <predicate> `(function)` - a callable which takes `destination` as input and
--- returns boolean value indicating whether animation should be done.
--- Default: `false` if `destination` is within one line of origin (reduces
--- flickering), `true` otherwise.
---@alias __animate_path_return function Path function (see |MiniAnimate.config.cursor|).
--- Generate path as shortest line
---
---@param opts __animate_path_opts_common
--- - <max_output_steps> `(number)` - maximum number of steps in output.
--- Default: 1000.
---
---@return __animate_path_return
MiniAnimate.gen_path.line = function(opts)
opts = vim.tbl_deep_extend('force', { predicate = H.default_path_predicate, max_output_steps = 1000 }, opts or {})
return function(destination) return H.path_line(destination, opts) end
end
--- Generate path as line/column angle
---
---@param opts __animate_path_opts_common
--- - <max_output_steps> `(number)` - maximum number of steps per side in output.
--- Default: 1000.
--- - <first_direction> `(string)` - one of `"horizontal"` (default; animates
--- across initial line first) or `"vertical"` (animates across initial
--- column first).
---
---@return __animate_path_return
MiniAnimate.gen_path.angle = function(opts)
local default_opts = { predicate = H.default_path_predicate, max_output_steps = 1000, first_direction = 'horizontal' }
opts = vim.tbl_deep_extend('force', default_opts, opts or {})
local append_horizontal = function(res, dest_col, const_line)
if dest_col == 0 then return end
local n_steps = math.min(math.abs(dest_col), opts.max_output_steps)
local coef = dest_col / n_steps
for i = 0, n_steps - 1 do
table.insert(res, { const_line, H.round(coef * i) })
end
end
local append_vertical = function(res, dest_line, const_col)
if dest_line == 0 then return end
local n_steps = math.min(math.abs(dest_line), opts.max_output_steps)
local coef = dest_line / n_steps
for i = 0, n_steps - 1 do
table.insert(res, { H.round(coef * i), const_col })
end
end
return function(destination)
-- Don't animate in case of false predicate
if not opts.predicate(destination) then return {} end
-- Travel along horizontal/vertical lines
local res = {}
if opts.first_direction == 'horizontal' then
append_horizontal(res, destination[2], 0)
append_vertical(res, destination[1], destination[2])
else
append_vertical(res, destination[1], 0)
append_horizontal(res, destination[2], destination[1])
end
return res
end
end
--- Generate path as closing walls at final position
---
---@param opts __animate_path_opts_common
--- - <width> `(number)` - initial width of left and right walls. Default: 10.
---
---@return __animate_path_return
MiniAnimate.gen_path.walls = function(opts)
opts = opts or {}
local predicate = opts.predicate or H.default_path_predicate
local width = opts.width or 10
return function(destination)
-- Don't animate in case of false predicate
if not predicate(destination) then return {} end
-- Don't animate in case of no movement
if destination[1] == 0 and destination[2] == 0 then return {} end
local dest_line, dest_col = destination[1], destination[2]
local res = {}
for i = width, 1, -1 do
table.insert(res, { dest_line, dest_col + i })
table.insert(res, { dest_line, dest_col - i })
end
return res
end
end
--- Generate path as diminishing spiral at final position
---
---@param opts __animate_path_opts_common
--- - <width> `(number)` - initial width of spiral. Default: 2.
---
---@return __animate_path_return
MiniAnimate.gen_path.spiral = function(opts)
opts = opts or {}
local predicate = opts.predicate or H.default_path_predicate
local width = opts.width or 2
local add_layer = function(res, w, destination)
local dest_line, dest_col = destination[1], destination[2]
--stylua: ignore start
for j = -w, w-1 do table.insert(res, { dest_line - w, dest_col + j }) end
for i = -w, w-1 do table.insert(res, { dest_line + i, dest_col + w }) end
for j = -w, w-1 do table.insert(res, { dest_line + w, dest_col - j }) end
for i = -w, w-1 do table.insert(res, { dest_line - i, dest_col - w }) end
--stylua: ignore end
end
return function(destination)
-- Don't animate in case of false predicate
if not predicate(destination) then return {} end
-- Don't animate in case of no movement
if destination[1] == 0 and destination[2] == 0 then return {} end
local res = {}
for w = width, 1, -1 do
add_layer(res, w, destination)
end
return res
end
end
--- Generate scroll animation subscroll
---
--- For more information see |MiniAnimate.config.scroll|.
---
--- This is a table with function elements. Call to actually get generator.
---
--- Example: >lua
---
--- local animate = require('mini.animate')
--- animate.setup({
--- scroll = {
--- -- Animate equally but with 120 maximum steps instead of default 60
--- subscroll = animate.gen_subscroll.equal({ max_output_steps = 120 }),
--- }
--- })
--- <
MiniAnimate.gen_subscroll = {}
--- Generate subscroll with equal steps
---
---@param opts table|nil Options that control generator. Possible keys:
--- - <predicate> `(function)` - a callable which takes `total_scroll` as
--- input and returns boolean value indicating whether animation should be
--- done. Default: `false` if `total_scroll` is 1 or less (reduces
--- unnecessary waiting), `true` otherwise.
--- - <max_output_steps> `(number)` - maximum number of subscroll steps in output.
--- Adjust this to reduce computations in expense of reduced smoothness.
--- Default: 60.
---
---@return function Subscroll function (see |MiniAnimate.config.scroll|).
MiniAnimate.gen_subscroll.equal = function(opts)
opts = vim.tbl_deep_extend('force', { predicate = H.default_subscroll_predicate, max_output_steps = 60 }, opts or {})
return function(total_scroll) return H.subscroll_equal(total_scroll, opts) end
end
--- Generate resize animation subresize
---
--- For more information see |MiniAnimate.config.resize|.
---
--- This is a table with function elements. Call to actually get generator.
---
--- Example: >lua
---
--- local is_many_wins = function(sizes_from, sizes_to)
--- return vim.tbl_count(sizes_from) >= 3
--- end
--- local animate = require('mini.animate')
--- animate.setup({
--- resize = {
--- -- Animate only if there are at least 3 windows
--- subresize = animate.gen_subresize.equal({ predicate = is_many_wins }),
--- }
--- })
--- <
MiniAnimate.gen_subresize = {}
--- Generate subresize with equal steps
---
---@param opts table|nil Options that control generator. Possible keys:
--- - <predicate> `(function)` - a callable which takes `sizes_from` and
--- `sizes_to` as input and returns boolean value indicating whether
--- animation should be done. Default: always `true`.
---
---@return function Subresize function (see |MiniAnimate.config.resize|).
MiniAnimate.gen_subresize.equal = function(opts)
opts = vim.tbl_deep_extend('force', { predicate = H.default_subresize_predicate }, opts or {})
return function(sizes_from, sizes_to) return H.subresize_equal(sizes_from, sizes_to, opts) end
end
--- Generate open/close animation winconfig
---
--- For more information see |MiniAnimate.config.open| or |MiniAnimate.config.close|.
---
--- This is a table with function elements. Call to actually get generator.
---
--- Example: >lua
---
--- local is_not_single_window = function(win_id)
--- local tabpage_id = vim.api.nvim_win_get_tabpage(win_id)
--- return #vim.api.nvim_tabpage_list_wins(tabpage_id) > 1
--- end
--- local animate = require('mini.animate')
--- animate.setup({
--- open = {
--- -- Animate with wiping from nearest edge instead of default static one
--- -- and only if it is not a single window in tabpage
--- winconfig = animate.gen_winconfig.wipe({
--- predicate = is_not_single_window,
--- direction = 'from_edge',
--- }),
--- },
--- close = {
--- -- Animate with wiping to nearest edge instead of default static one
--- -- and only if it is not a single window in tabpage
--- winconfig = animate.gen_winconfig.wipe({
--- predicate = is_not_single_window,
--- direction = 'to_edge',
--- }),
--- },
--- })
--- <
MiniAnimate.gen_winconfig = {}
---@alias __animate_winconfig_opts_common table|nil Options that control generator. Possible keys:
--- - <predicate> `(function)` - a callable which takes `win_id` as input and
--- returns boolean value indicating whether animation should be done.
--- Default: always `true`.
---@alias __animate_winconfig_return function Winconfig function (see |MiniAnimate.config.open|
--- or |MiniAnimate.config.close|).
--- Generate winconfig for static floating window
---
--- This will result into floating window statically covering whole target
--- window.
---
---@param opts __animate_winconfig_opts_common
--- - <n_steps> `(number)` - number of output steps, all with same config.
--- Useful to tweak smoothness of transparency animation (done inside
--- `winblend` config option). Default: 25.
---
---@return __animate_winconfig_return
MiniAnimate.gen_winconfig.static = function(opts)
opts = vim.tbl_deep_extend('force', { predicate = H.default_winconfig_predicate, n_steps = 25 }, opts or {})
return function(win_id) return H.winconfig_static(win_id, opts) end
end
--- Generate winconfig for center-focused animated floating window
---
--- This will result into floating window growing from or shrinking to the
--- target window center.
---
---@param opts __animate_winconfig_opts_common
--- - <direction> `(string)` - one of `"to_center"` (default; window will
--- shrink from full coverage to center) or `"from_center"` (window will
--- grow from center to full coverage).
---
---@return __animate_winconfig_return
MiniAnimate.gen_winconfig.center = function(opts)
opts = opts or {}
local predicate = opts.predicate or H.default_winconfig_predicate
local direction = opts.direction or 'to_center'
return function(win_id)
-- Don't animate in case of false predicate
if not predicate(win_id) then return {} end
local pos = vim.fn.win_screenpos(win_id)
local row, col = pos[1] - 1, pos[2] - 1
local height, width = vim.api.nvim_win_get_height(win_id), vim.api.nvim_win_get_width(win_id)
0