Skip to content

Commit

Permalink
fix: save and restore fixed win dimensions on resize
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Oct 3, 2023
1 parent 40fee16 commit 4ea216b
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lua/focus/modules/resizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ local golden_ratio_minheight = function()
return math.floor(golden_ratio_height() / (3 * golden_ratio))
end

local function save_fixed_win_dims()
local fixed_dims = {}

for _, win in ipairs(vim.api.nvim_list_wins()) do
local buf = vim.api.nvim_win_get_buf(win)
if vim.w[win].focus_disable or vim.b[buf].focus_disable then
fixed_dims[win] = {
width = vim.api.nvim_win_get_width(win),
height = vim.api.nvim_win_get_height(win),
}
end
end

return fixed_dims
end

local function restore_fixed_win_dims(fixed_dims)
for win, dims in pairs(fixed_dims) do
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_set_width(win, dims.width)
vim.api.nvim_win_set_height(win, dims.height)
end
end
end

function M.autoresize(config)
local width
if config.autoresize.width > 0 then
Expand Down Expand Up @@ -49,9 +74,13 @@ function M.autoresize(config)
-- save cmdheight to ensure it is not changed by nvim_win_set_height
local cmdheight = vim.o.cmdheight

local fixed = save_fixed_win_dims()

vim.api.nvim_win_set_width(0, width)
vim.api.nvim_win_set_height(0, height)

restore_fixed_win_dims(fixed)

vim.o.cmdheight = cmdheight
end

Expand All @@ -62,9 +91,12 @@ end
function M.maximise()
local width, height = vim.o.columns, vim.o.lines

local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_width(win, width)
vim.api.nvim_win_set_height(win, height)
local fixed = save_fixed_win_dims()

vim.api.nvim_win_set_width(0, width)
vim.api.nvim_win_set_height(0, height)

restore_fixed_win_dims(fixed)
end

M.goal = 'autoresize'
Expand Down

0 comments on commit 4ea216b

Please sign in to comment.