From b28e76ac4ed2f29d1f466ac084cd31ce9c283c42 Mon Sep 17 00:00:00 2001 From: Andrea Dragotta Date: Tue, 26 Apr 2022 09:14:24 +0200 Subject: [PATCH 1/9] Added option to exclude filetypes --- lua/shade.lua | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lua/shade.lua b/lua/shade.lua index 991f168..b630595 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -148,6 +148,33 @@ local function map_key(mode, key, action) end +-- + +local function is_filetype_excluded(filetype) + for _, value in ipairs(state.exclude_filetypes) do + if value == filetype then + return true + end + + end + return false +end + +local function can_shade(winid) + if #state.exclude_filetypes == 0 then + return true + end + local buf_id = vim.api.nvim_win_get_buf(winid) + local filetype = vim.api.nvim_buf_get_option(buf_id, "filetype") + + if is_filetype_excluded(filetype) then + return false + end + + return true +end + +-- local function shade_window(winid) local overlay = state.active_overlays[winid] @@ -181,8 +208,10 @@ local function shade_tabpage(winid) for overlay_winid, _ in pairs(state.active_overlays) do local diff_enabled = api.nvim_win_get_option(overlay_winid, 'diff') if overlay_winid ~= winid and diff_enabled == false then - log("deactivating window", overlay_winid) - shade_window(overlay_winid) + if can_shade(overlay_winid) then + log("deactivating window", overlay_winid) + shade_window(overlay_winid) + end end end end @@ -232,6 +261,7 @@ shade.init = function(opts) E.DEFAULT_OVERLAY_OPACITY) state.opacity_step = opts.opacity_step or E.DEFAULT_OPACITY_STEP state.shade_under_float = opts.shade_under_float or true + state.exclude_filetypes = opts.exclude_filetypes or {} state.shade_nsid = api.nvim_create_namespace("shade") From 5afa45d3555c184db35a0ab05d78779b07f3a0f0 Mon Sep 17 00:00:00 2001 From: andreadev-it <68549121+andreadev-it@users.noreply.github.com> Date: Tue, 26 Apr 2022 09:16:44 +0200 Subject: [PATCH 2/9] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f227265..8ab9adb 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ require'shade'.setup({ brightness_up = '', brightness_down = '', toggle = 's', - } + }, + exclude_filetypes = { "neo-tree", "markdown" } }) ``` @@ -35,6 +36,8 @@ require'shade'.setup({ * The color of the numbers in the brightness control popup can be customized by creating a highlight group named: `ShadeBrightnessPopup` and setting the attributes to your liking. +* The `exclude_filetypes` table above is just an example. No filetypes are excluded by default. + ## License Copyright (c) Senghan Bright. Distributed under the MIT license From 43372ca2ab5d9bc8ece7799fb9d84a05df21e25c Mon Sep 17 00:00:00 2001 From: Andrea Dragotta Date: Tue, 26 Apr 2022 09:33:58 +0200 Subject: [PATCH 3/9] Prevent shading of filetypes when navigating tabs --- lua/shade.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/shade.lua b/lua/shade.lua index b630595..f7877d0 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -242,7 +242,9 @@ local function create_tabpage_overlays(tabid) local wincfg for _, winid in pairs(api.nvim_tabpage_list_wins(tabid)) do wincfg = api.nvim_call_function("getwininfo", {winid})[1] - create_overlay_window(winid, filter_wininfo(wincfg)) + if can_shade(winid) then + create_overlay_window(winid, filter_wininfo(wincfg)) + end end unshade_window(api.nvim_get_current_win()) end From 423cdb9be77ae64af29e770ef985e30c1c96d690 Mon Sep 17 00:00:00 2001 From: Andrea Dragotta Date: Tue, 26 Apr 2022 09:43:52 +0200 Subject: [PATCH 4/9] Fixed indentation to make it coherent --- lua/shade.lua | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lua/shade.lua b/lua/shade.lua index f7877d0..4fe803e 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -151,27 +151,27 @@ end -- local function is_filetype_excluded(filetype) - for _, value in ipairs(state.exclude_filetypes) do - if value == filetype then - return true - end - + for _, value in ipairs(state.exclude_filetypes) do + if value == filetype then + return true end - return false + + end + return false end local function can_shade(winid) - if #state.exclude_filetypes == 0 then - return true - end - local buf_id = vim.api.nvim_win_get_buf(winid) - local filetype = vim.api.nvim_buf_get_option(buf_id, "filetype") + if #state.exclude_filetypes == 0 then + return true + end + local buf_id = vim.api.nvim_win_get_buf(winid) + local filetype = vim.api.nvim_buf_get_option(buf_id, "filetype") - if is_filetype_excluded(filetype) then - return false - end + if is_filetype_excluded(filetype) then + return false + end - return true + return true end -- From 0adb4dddb7ac0c509cbf2b8ac9a68e6d5aa1879a Mon Sep 17 00:00:00 2001 From: valentino-sm <5059499+valentino-sm@users.noreply.github.com> Date: Thu, 4 Jan 2024 04:54:36 +0500 Subject: [PATCH 5/9] fix: shade overlay under floating windows --- lua/shade.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/shade.lua b/lua/shade.lua index 4fe803e..d6228c4 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -105,6 +105,7 @@ local function filter_wininfo(wininfo) col = wininfo.wincol - 1, width = wininfo.width, height = wininfo.height, + zindex = 1, } end From 2d3807eff7da22c06df87ce3db42b10d55b0d9c5 Mon Sep 17 00:00:00 2001 From: valentino-sm <5059499+valentino-sm@users.noreply.github.com> Date: Sat, 6 Jan 2024 05:27:51 +0500 Subject: [PATCH 6/9] fix: check all nvim_win_is_valid --- lua/shade.lua | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lua/shade.lua b/lua/shade.lua index d6228c4..a82fcda 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -207,11 +207,13 @@ end local function shade_tabpage(winid) winid = winid or api.nvim_get_current_win() for overlay_winid, _ in pairs(state.active_overlays) do - local diff_enabled = api.nvim_win_get_option(overlay_winid, 'diff') - if overlay_winid ~= winid and diff_enabled == false then - if can_shade(overlay_winid) then - log("deactivating window", overlay_winid) - shade_window(overlay_winid) + if api.nvim_win_is_valid(overlay_winid) then + local diff_enabled = api.nvim_win_get_option(overlay_winid, 'diff') + if overlay_winid ~= winid and diff_enabled == false then + if can_shade(overlay_winid) then + log("deactivating window", overlay_winid) + shade_window(overlay_winid) + end end end end @@ -221,7 +223,9 @@ end local function remove_all_overlays() for _, overlay in pairs(state.active_overlays) do - api.nvim_win_close(overlay.winid, true) + if api.nvim_win_is_valid(overlay.winid) then + api.nvim_win_close(overlay.winid, true) + end end state.active_overlays = {} end @@ -356,9 +360,14 @@ shade.on_win_closed = function(event, winid) if overlay == nil then log(event, "no overlay to close") else - api.nvim_win_close(overlay.winid, false) - log(event, ("[%d] : overlay %d destroyed"):format(winid, overlay.winid)) - state.active_overlays[winid] = nil + log(event, ("trying to close overlay %d"):format(winid)) + if api.nvim_win_is_valid(overlay.winid) then + api.nvim_win_close(overlay.winid, false) + log(event, ("[%d] : overlay %d destroyed"):format(winid, overlay.winid)) + if (winid) then + state.active_overlays[winid] = nil + end + end end end From 62cf25479101118eaa498ada4dfbedd19a69d20b Mon Sep 17 00:00:00 2001 From: 0x100101 <> Date: Fri, 3 Jun 2022 22:34:27 -0500 Subject: [PATCH 7/9] fix: session manager support The `only` command executed by a session restore seems to leave shade in a broken state. I'm not certain, but it seems the root issue is that shade doesn't receive `WinClosed` events for the windows being closed by `only`. 1) Respond to the 'SessionLoadPost' autocmd by toggling shade off, and then back on in the next event loop. ie. after the session has loaded. 2) Only attempt to close valid windows when performing window cleanup. --- lua/shade.lua | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/lua/shade.lua b/lua/shade.lua index a82fcda..7bc0d84 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -300,6 +300,7 @@ shade.init = function(opts) au WinClosed * call v:lua.require'shade'.autocmd('WinClosed', expand('')) au TabEnter * call v:lua.require'shade'.autocmd('TabEnter', win_getid()) au OptionSet diff call v:lua.require'shade'.autocmd('OptionSet', win_getid()) + au SessionLoadPost * call v:lua.require'shade'.autocmd('SessionLoadPost') augroup END ]], false) @@ -371,6 +372,27 @@ shade.on_win_closed = function(event, winid) end end +shade.on_session_load_post = function() + shade.toggle_off() + vim.schedule(function() + shade.toggle_on() + end) +end + +shade.toggle_on = function() + create_tabpage_overlays(0) + state.active = true +end + +shade.toggle_off = function() + remove_all_overlays() + state.active = false +end + +shade.toggle = function() + if state.active then shade.toggle_off() else shade.toggle_on() end +end + shade.change_brightness = function(level) local curr_winid = api.nvim_get_current_win() state.overlay_opacity = level @@ -455,23 +477,12 @@ M.brightness_down = function() shade.change_brightness(adjusted) end -M.toggle = function() - if state.active then - remove_all_overlays() - print("off") - state.active = false - else - create_tabpage_overlays(0) - print("on") - state.active = true - end -end +M.toggle = shade.toggle M.autocmd = function(event, winid) if not state.active then return end - log("AutoCmd: " .. event .. " : " .. winid) local event_fn = { ["WinEnter"] = function() @@ -490,6 +501,9 @@ M.autocmd = function(event, winid) unshade_window(winid) shade_tabpage(winid) end + end, + ["SessionLoadPost"] = function() + shade.on_session_load_post() end } From 78adba342082b5b05b55ad4c6d744748a99ce984 Mon Sep 17 00:00:00 2001 From: valentino-sm <5059499+valentino-sm@users.noreply.github.com> Date: Sat, 6 Jan 2024 05:54:13 +0500 Subject: [PATCH 8/9] fix: revert logging back --- lua/shade.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lua/shade.lua b/lua/shade.lua index 7bc0d84..fe4cce8 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -483,6 +483,11 @@ M.autocmd = function(event, winid) if not state.active then return end + if (winid) then + log("AutoCmd: " .. event .. " : " .. winid) + else + log("AutoCmd: " .. event) + end local event_fn = { ["WinEnter"] = function() From 9f34ac97a3bc31151451a10e847e8ed0f1e5f2b4 Mon Sep 17 00:00:00 2001 From: Double Dimos Date: Wed, 31 Aug 2022 12:25:50 +0800 Subject: [PATCH 9/9] fix: nvim_win_set_option error --- lua/shade.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/shade.lua b/lua/shade.lua index fe4cce8..e45b093 100644 --- a/lua/shade.lua +++ b/lua/shade.lua @@ -346,7 +346,7 @@ shade.event_listener = function(_, winid, _, _, _) if current_wincfg[m] ~= cached.wincfg[m] then log("event_listener: resized", winid) state.active_overlays[winid].wincfg = current_wincfg - api.nvim_win_set_config(cached.winid, filter_wininfo(current_wincfg)) + pcall(api.nvim_win_set_config, cached.winid, filter_wininfo(current_wincfg)) goto continue end end