feat(win): backdrop can now be made opaque

This commit is contained in:
Folke Lemaitre 2024-12-03 10:29:55 +01:00
parent 96ac87b6b1
commit 681b9c9d65
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -18,6 +18,11 @@ local M = setmetatable({}, {
---@field [2]? string|fun(self: snacks.win): any ---@field [2]? string|fun(self: snacks.win): any
---@field mode? string|string[] ---@field mode? string|string[]
---@class snacks.win.Backdrop
---@field bg? string
---@field blend? number
---@field transparent? boolean defaults to true
---@class snacks.win.Config: vim.api.keyset.win_config ---@class snacks.win.Config: vim.api.keyset.win_config
---@field style? string merges with config from `Snacks.config.styles[style]` ---@field style? string merges with config from `Snacks.config.styles[style]`
---@field show? boolean Show the window immediately (default: true) ---@field show? boolean Show the window immediately (default: true)
@ -28,7 +33,7 @@ local M = setmetatable({}, {
---@field buf? number If set, use this buffer instead of creating a new one ---@field buf? number If set, use this buffer instead of creating a new one
---@field file? string If set, use this file instead of creating a new buffer ---@field file? string If set, use this file instead of creating a new buffer
---@field enter? boolean Enter the window after opening (default: false) ---@field enter? boolean Enter the window after opening (default: false)
---@field backdrop? number|false Opacity of the backdrop (default: 60) ---@field backdrop? number|false|snacks.win.Backdrop Opacity of the backdrop (default: 60)
---@field wo? vim.wo window options ---@field wo? vim.wo window options
---@field bo? vim.bo buffer options ---@field bo? vim.bo buffer options
---@field ft? string filetype to use for treesitter/syntax highlighting. Won't override existing filetype ---@field ft? string filetype to use for treesitter/syntax highlighting. Won't override existing filetype
@ -132,7 +137,7 @@ Snacks.util.set_hl({
local id = 0 local id = 0
---@private --@private
---@param ...? snacks.win.Config|string ---@param ...? snacks.win.Config|string
---@return snacks.win.Config ---@return snacks.win.Config
function M.resolve(...) function M.resolve(...)
@ -388,7 +393,7 @@ function M:show()
end end
self:open_win() self:open_win()
if M.transparent then if Snacks.util.is_transparent() then
self.opts.wo.winblend = 0 self.opts.wo.winblend = 0
end end
Snacks.util.wo(self.win, self.opts.wo) Snacks.util.wo(self.win, self.opts.wo)
@ -503,14 +508,33 @@ end
---@private ---@private
function M:drop() function M:drop()
-- don't show a backdrop for non-floating windows local backdrop = self.opts.backdrop
if not backdrop then
return
end
backdrop = type(backdrop) == "number" and { blend = backdrop } or backdrop
backdrop = backdrop == true and {} or backdrop
backdrop = vim.tbl_extend("force", { bg = "#000000", blend = 60, transparent = true }, backdrop)
---@cast backdrop snacks.win.Backdrop
if if
M.transparent (Snacks.util.is_transparent() and backdrop.transparent)
or not (self:is_floating() and self.opts.backdrop and self.opts.backdrop < 100 and vim.o.termguicolors) or not vim.o.termguicolors
or backdrop.blend == 100
or not self:is_floating()
then then
return return
end end
local bg, winblend = backdrop.bg, backdrop.blend
if not backdrop.transparent then
bg = Snacks.util.blend(Snacks.util.color("Normal", "bg"), bg, winblend / 100)
winblend = 0
end
local group = ("SnacksBackdrop_%s"):format(bg:sub(2))
vim.api.nvim_set_hl(0, group, { bg = bg })
self.backdrop = M.new({ self.backdrop = M.new({
enter = false, enter = false,
backdrop = false, backdrop = false,
@ -522,8 +546,8 @@ function M:drop()
focusable = false, focusable = false,
zindex = self.opts.zindex - 1, zindex = self.opts.zindex - 1,
wo = { wo = {
winhighlight = "Normal:SnacksBackdrop", winhighlight = "Normal:" .. group,
winblend = self.opts.backdrop, winblend = winblend,
}, },
bo = { bo = {
buftype = "nofile", buftype = "nofile",