From bb1fdfaa8db998e5c3fd23d6f71f3b763b9406ed Mon Sep 17 00:00:00 2001 From: xuanhung1509 Date: Sat, 20 Jul 2024 20:31:20 +0700 Subject: [PATCH] feat: add backdrop --- README.md | 3 +++ doc/mason.txt | 3 +++ lua/mason-core/ui/display.lua | 36 ++++++++++++++++++++++++++++++++++- lua/mason/settings.lua | 3 +++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e33b653c..1615eccb7 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,9 @@ local DEFAULT_SETTINGS = { -- The border to use for the UI window. Accepts same border values as |nvim_open_win()|. border = "none", + -- The backdrop opacity. 0 is fully opaque, 100 is fully transparent. + backdrop = 100, + ---@since 1.0.0 -- Width of the window. Accepts: -- - Integer greater than 1 for fixed width. diff --git a/doc/mason.txt b/doc/mason.txt index e7a2d3bf6..7c9a6e93f 100644 --- a/doc/mason.txt +++ b/doc/mason.txt @@ -331,6 +331,9 @@ Example: -- The border to use for the UI window. Accepts same border values as |nvim_open_win()|. border = "none", + -- The backdrop opacity. 0 is fully opaque, 100 is fully transparent. + backdrop = 100, + ---@since 1.0.0 -- Width of the window. Accepts: -- - Integer greater than 1 for fixed width. diff --git a/lua/mason-core/ui/display.lua b/lua/mason-core/ui/display.lua index c4aebd95d..07315f04f 100644 --- a/lua/mason-core/ui/display.lua +++ b/lua/mason-core/ui/display.lua @@ -209,7 +209,7 @@ end ---@param filetype string function M.new_view_only_win(name, filetype) local namespace = vim.api.nvim_create_namespace(("installer_%s"):format(name)) - local bufnr, renderer, mutate_state, get_state, unsubscribe, win_id, window_mgmt_augroup, autoclose_augroup, registered_keymaps, registered_keybinds, registered_effect_handlers, sticky_cursor + local bufnr, backdrop_bufnr, renderer, mutate_state, get_state, unsubscribe, win_id, backdrop_win_id, window_mgmt_augroup, autoclose_augroup, registered_keymaps, registered_keybinds, registered_effect_handlers, sticky_cursor local has_initiated = false ---@type WindowOpts local window_opts = {} @@ -239,6 +239,14 @@ function M.new_view_only_win(name, filetype) log.trace "Deleting buffer" vim.api.nvim_buf_delete(bufnr, { force = true }) end + if backdrop_win_id and vim.api.nvim_win_is_valid(backdrop_win_id) then + log.trace "Deleting backdrop window" + vim.api.nvim_win_close(backdrop_win_id, true) + end + if backdrop_bufnr and vim.api.nvim_buf_is_valid(backdrop_bufnr) then + log.trace "Deleting backdrop buffer" + vim.api.nvim_buf_delete(backdrop_bufnr, { force = true }) + end end) end @@ -371,6 +379,32 @@ function M.new_view_only_win(name, filetype) bufnr = vim.api.nvim_create_buf(false, true) win_id = vim.api.nvim_open_win(bufnr, true, create_popup_window_opts(window_opts, false)) + backdrop_bufnr = vim.api.nvim_create_buf(false, true) + backdrop_win_id = vim.api.nvim_open_win(backdrop_bufnr, false, { + relative = "editor", + width = vim.o.columns, + height = vim.o.lines, + row = 0, + col = 0, + style = "minimal", + focusable = false, + zindex = 44, + }) + + local wo = function(win, k, v) + if vim.api.nvim_set_option_value then + vim.api.nvim_set_option_value(k, v, { scope = "local", win = win }) + else + vim.wo[win][k] = v + end + end + + vim.api.nvim_set_hl(0, "MasonBackdrop", { bg = "#000000", default = true }) + wo(backdrop_win_id, "winhighlight", "Normal:MasonBackdrop") + wo(backdrop_win_id, "winblend", settings.current.ui.backdrop) + vim.bo[backdrop_bufnr].buftype = "nofile" + vim.bo[backdrop_bufnr].filetype = "mason_backdrop" + vim.api.nvim_create_autocmd("CmdLineEnter", { buffer = bufnr, callback = function() diff --git a/lua/mason/settings.lua b/lua/mason/settings.lua index 56fbcfb9f..200b1e75a 100644 --- a/lua/mason/settings.lua +++ b/lua/mason/settings.lua @@ -77,6 +77,9 @@ local DEFAULT_SETTINGS = { -- The border to use for the UI window. Accepts same border values as |nvim_open_win()|. border = "none", + -- The backdrop opacity. 0 is fully opaque, 100 is fully transparent. + backdrop = 100, + ---@since 1.0.0 -- Width of the window. Accepts: -- - Integer greater than 1 for fixed width.