feat(win): width/height can now be a function

This commit is contained in:
Folke Lemaitre 2024-12-02 20:31:06 +01:00
parent 1f74d1ce77
commit 964d7ae99a
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -21,8 +21,8 @@ local M = setmetatable({}, {
---@class snacks.win.Config: vim.api.keyset.win_config
---@field style? string merges with config from `Snacks.config.styles[style]`
---@field show? boolean Show the window immediately (default: true)
---@field height? number Height of the window. Use <1 for relative height. 0 means full height. (default: 0.9)
---@field width? number Width of the window. Use <1 for relative width. 0 means full width. (default: 0.9)
---@field height? number|fun():number Height of the window. Use <1 for relative height. 0 means full height. (default: 0.9)
---@field width? number|fun():number Width of the window. Use <1 for relative width. 0 means full width. (default: 0.9)
---@field minimal? boolean Disable a bunch of options to make the window minimal (default: true)
---@field position? "float"|"bottom"|"top"|"left"|"right"
---@field buf? number If set, use this buffer instead of creating a new one
@ -573,6 +573,8 @@ function M:win_opts()
opts[k] = self.opts[k]
end
local parent = self:parent_size()
opts.height = type(opts.height) == "function" and opts.height() or opts.height
opts.width = type(opts.width) == "function" and opts.width() or opts.width
-- Special case for 0, which means 100%
opts.height = opts.height == 0 and parent.height or opts.height
opts.width = opts.width == 0 and parent.width or opts.width