Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resize window on nvim resize #117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chenasraf
Copy link

@chenasraf chenasraf commented Feb 13, 2024

Fixes #116

Screen.Recording.2024-02-13.at.3.51.17.mov

The defer is a bit of a hack, but otherwise the new size doesn't get enough time to be properly registered. It still sometimes messes up and positions itself slightly off sometimes if I try to do it several times fast, so maybe the delay needs to be adjusted, some debounce added, or we might want to find a different method for ensuring the window has the correct size.

fix: resize event name

fix: try fix resize

fix: resize

fix: resize

fix: resie
@kdheepak
Copy link
Owner

Thank you for the PR! This is something I've been meaning to look into for a while but haven't had the time to get to it.

Comment on lines +88 to +101
vim.api.nvim_create_autocmd('VimResized', {
callback = function()
vim.defer_fn(function()
if not vim.api.nvim_win_is_valid(border_window) then
return
end
local new_width, new_height, new_row, new_col = get_window_pos()
api.nvim_win_set_config(border_window,
{ width = new_width + 2, height = new_height + 2, relative = 'editor', row = new_row - 1, col = new_col - 1, })
api.nvim_win_set_config(win,
{ width = new_width, height = new_height, relative = 'editor', row = new_row, col = new_col, })
end, 20)
end
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on why defer_fn is needed?

Would this not work?

Suggested change
vim.api.nvim_create_autocmd('VimResized', {
callback = function()
vim.defer_fn(function()
if not vim.api.nvim_win_is_valid(border_window) then
return
end
local new_width, new_height, new_row, new_col = get_window_pos()
api.nvim_win_set_config(border_window,
{ width = new_width + 2, height = new_height + 2, relative = 'editor', row = new_row - 1, col = new_col - 1, })
api.nvim_win_set_config(win,
{ width = new_width, height = new_height, relative = 'editor', row = new_row, col = new_col, })
end, 20)
end
})
vim.api.nvim_create_autocmd('VimResized', {
callback = function()
if not vim.api.nvim_win_is_valid(border_window) then
return
end
local new_width, new_height, new_row, new_col = get_window_pos()
api.nvim_win_set_config(border_window,
{ width = new_width + 2, height = new_height + 2, relative = 'editor', row = new_row - 1, col = new_col - 1, })
api.nvim_win_set_config(win,
{ width = new_width, height = new_height, relative = 'editor', row = new_row, col = new_col, })
end
})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, if you try it out, the position/size after resizing would be messed up, I think it is some race between the event and when the new size detail gets updated

It causes the contents of the window to shift too much left

There is probably a better solution

Copy link
Author

@chenasraf chenasraf Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without defering, this is how it looks - I'm pretty sure it looked worse for me yesterday, but maybe it was during playing around with parameters. Without the delay it's still a bit wonky - notice how LG expands outside the border window

Screen.Recording.2024-02-13.at.10.53.25.mov

Also honestly this is the first time I've played around with nvim floating windows, perhaps there is a more reliable way to do this, which involves checking a condition and not using an arbitrary amount of time, but I wouldn't know what to check against (e.g. some boolean that says the ui updates are ready), so any ideas would be appreciated and I would love to play around with it and update the PR :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually on another computer, the defer doesn't fix the problem, and it really jumps around much. See:

Screen.Recording.2024-02-13.at.15.06.21.mov

Don't know why this happens or how to solve it :(

@savrielynck
Copy link

Hey, I was just facing this issue and just saw they are already a fix for it, thanks @chenasraf.
Is there anything blocking that I can help with to get this into the mainline? Happy to learn some of the nvim API

@chenasraf
Copy link
Author

Hey, I was just facing this issue and just saw they are already a fix for it, thanks @chenasraf.
Is there anything blocking that I can help with to get this into the mainline? Happy to learn some of the nvim API

It's pretty much working, the problem is it's kind of wonky and isn't very consistent. You can see the discussion here - something about the resizing logic between the 2 systems is not aligning right.

Honestly I have no idea why this happens, if you want to give it a go, please be my guest :)

I am currently using my own fork which is exactly only this PR added, so I am using it daily myself, but I'm not sure it's good enough quality to have for everyone yet.

@tamis-laan
Copy link

https://github.com/nvim-telescope/telescope.nvim

Neovim telescope does window resizing very well, maybe worth trying to take a look at their implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request] LazyGit window should resize with the viewport
4 participants