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 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions lua/lazygit/window.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local api = vim.api

--- open floating window with nice borders
local function open_floating_window()
local function get_window_pos()
local floating_window_scaling_factor = vim.g.lazygit_floating_window_scaling_factor

-- Why is this required?
Expand All @@ -12,15 +11,21 @@ local function open_floating_window()

local status, plenary = pcall(require, 'plenary.window.float')
if status and vim.g.lazygit_floating_window_use_plenary and vim.g.lazygit_floating_window_use_plenary ~= 0 then
local ret = plenary.percentage_range_window(floating_window_scaling_factor, floating_window_scaling_factor, {winblend=vim.g.lazygit_floating_window_winblend})
local ret = plenary.percentage_range_window(floating_window_scaling_factor, floating_window_scaling_factor,
{ winblend = vim.g.lazygit_floating_window_winblend })
return ret.win_id, ret.bufnr
end

local height = math.ceil(vim.o.lines * floating_window_scaling_factor) - 1
local width = math.ceil(vim.o.columns * floating_window_scaling_factor)

local row = math.ceil(vim.o.lines - height) / 2
local col = math.ceil(vim.o.columns - width) / 2
return width, height, row, col
end

--- open floating window with nice borders
local function open_floating_window()
local width, height, row, col = get_window_pos()

local border_opts = {
style = 'minimal',
Expand All @@ -38,7 +43,7 @@ local function open_floating_window()
if type(window_chars) == 'table' and #window_chars == 8 then
topleft, top, topright, right, botright, bot, botleft, left = unpack(window_chars)
else
topleft, top, topright, right, botright, bot, botleft, left = '╭','─', '╮', '│', '╯','─', '╰', '│'
topleft, top, topright, right, botright, bot, botleft, left = '╭', '─', '╮', '│', '╯', '─', '╰', '│'
end

local border_lines = { topleft .. string.rep(top, width) .. topright }
Expand Down Expand Up @@ -80,6 +85,20 @@ local function open_floating_window()
vim.cmd(cmd)
cmd = [[autocmd WinLeave <buffer> silent! execute 'silent bdelete! %s']]
vim.cmd(cmd:format(border_buffer))
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
})
Comment on lines +88 to +101
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 :(


return win, LAZYGIT_BUFFER
end
Expand Down