feat: add on_tree_resize event

This commit is contained in:
kiyan 2022-07-06 13:35:29 +02:00
parent eb6dde4733
commit c84735483f
3 changed files with 25 additions and 2 deletions

View File

@ -1233,4 +1233,12 @@ on_tree_close({handler})
{handler} `{function}` Handler function, with the
signature `function()`.
*nvim-tree.events.on_tree_resize()*
on_tree_resize({handler})
Registers a handler for when NvimTree is resized.
Parameters: ~
{handler} `{function}` Handler function, with the
signature `function(size)`.
vim:tw=78:ts=4:sw=4:et:ft=help:norl:

View File

@ -11,6 +11,7 @@ local Event = {
FileRemoved = "FileRemoved",
FolderCreated = "FolderCreated",
FolderRemoved = "FolderRemoved",
Resize = "Resize",
}
local function get_handlers(event_name)
@ -72,6 +73,11 @@ function M._dispatch_on_tree_close()
dispatch(Event.TreeClose, nil)
end
--@private
function M._dispatch_on_tree_resize(size)
dispatch(Event.Resize, size)
end
--Registers a handler for the Ready event.
--@param handler (function) Handler with the signature `function()`
function M.on_nvim_tree_ready(handler)
@ -126,4 +132,10 @@ function M.on_tree_close(handler)
register_handler(Event.TreeClose, handler)
end
--Registers a handler for the Resize event.
--@param handler (function) Handler with the signature function(size)
function M.on_tree_resize(handler)
register_handler(Event.Resize, handler)
end
return M

View File

@ -244,12 +244,15 @@ function M.resize(size)
return
end
local new_size = get_size()
if M.is_vertical() then
a.nvim_win_set_width(M.get_winnr(), get_size())
a.nvim_win_set_width(M.get_winnr(), new_size)
else
a.nvim_win_set_height(M.get_winnr(), get_size())
a.nvim_win_set_height(M.get_winnr(), new_size)
end
events._dispatch_on_tree_resize(new_size)
if not M.View.preserve_window_proportions then
vim.cmd ":wincmd ="
end