Skip to content

Commit

Permalink
perf: mitigate the cursor flickering issue (#298)
Browse files Browse the repository at this point in the history
Instead of letting the `:hl` to redraw all things globally, use the
`nvim__redraw` api to limit the redraw on the notification windows only.
The api also handles the case of redrawing during blocking events by
flushing the screen changes.
  • Loading branch information
wnineg authored Dec 28, 2024
1 parent 29b33ef commit 3a4e83e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 47 deletions.
8 changes: 4 additions & 4 deletions lua/notify/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ local function validate_highlight(colour_or_group, needs_opacity)
end
end
return function()
local group = vim.api.nvim_get_hl_by_name(colour_or_group, true)
if not group or not group.background then
local group = vim.api.nvim_get_hl(0, { name = colour_or_group, create = false, link = false })
if not group or not group.bg then
if needs_opacity and not opacity_warned then
opacity_warned = true
vim.schedule(function()
Expand All @@ -88,14 +88,14 @@ Defaulting to #000000]], "warn", {
title = "nvim-notify",
on_open = function(win)
local buf = vim.api.nvim_win_get_buf(win)
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
vim.api.nvim_set_option_value("filetype", "markdown", { buf = buf })
end,
})
end)
end
return "#000000"
end
return string.format("#%x", group.background)
return string.format("#%x", group.bg)
end
end

Expand Down
52 changes: 18 additions & 34 deletions lua/notify/service/buffer/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ local util = require("notify.util")
---@field _config table
local NotifyBufHighlights = {}

local function manual_get_hl(name)
local synID = vim.fn.synIDtrans(vim.fn.hlID(name))
local result = {
foreground = tonumber(vim.fn.synIDattr(synID, "fg"):gsub("#", ""), 16),
background = tonumber(vim.fn.synIDattr(synID, "bg"):gsub("#", ""), 16),
}
return result
end

local function get_hl(name)
local definition = vim.api.nvim_get_hl_by_name(name, true)
if definition[true] then
-- https://github.com/neovim/neovim/issues/18024
return manual_get_hl(name)
end
return definition
end

function NotifyBufHighlights:new(level, buffer, config)
local function linked_group(section)
local orig = "Notify" .. level .. section
Expand All @@ -38,8 +20,11 @@ function NotifyBufHighlights:new(level, buffer, config)
local new = orig .. buffer

vim.api.nvim_set_hl(0, new, { link = orig })
local hl = vim.api.nvim_get_hl(0, { name = orig, create = false, link = false })
-- Removes the unwanted 'default' key, as we will copy the table for updating the highlight later.
hl.default = nil

return new, get_hl(new)
return new, hl
end

local title, title_def = linked_group("Title")
Expand Down Expand Up @@ -83,7 +68,7 @@ function NotifyBufHighlights:_redefine_treesitter()
return new
end
vim.api.nvim_set_hl(0, new, { link = orig })
self.groups[new] = get_hl(new)
self.groups[new] = vim.api.nvim_get_hl(0, { name = new, link = false })
return new
end

Expand Down Expand Up @@ -150,31 +135,30 @@ end
function NotifyBufHighlights:set_opacity(alpha)
if
not self._treesitter_redefined
and vim.api.nvim_buf_get_option(self.buffer, "filetype") ~= "notify"
and vim.api.nvim_get_option_value("filetype", { buf = self.buffer }) ~= "notify"
then
self:_redefine_treesitter()
end
self.opacity = alpha
local background = self._config.background_colour()
local updated = false
for group, fields in pairs(self.groups) do
local updated_fields = {}
vim.api.nvim_set_hl(0, group, updated_fields)
local hl_string = ""
if fields.foreground then
hl_string = "guifg=#"
.. string.format("%06x", util.blend(fields.foreground, background, alpha / 100))
local fg = fields.fg
if fg then
fg = util.blend(fg, background, alpha / 100)
end
if fields.background then
hl_string = hl_string
.. " guibg=#"
.. string.format("%06x", util.blend(fields.background, background, alpha / 100))
local bg = fields.bg
if bg then
bg = util.blend(bg, background, alpha / 100)
end

if hl_string ~= "" then
-- Can't use nvim_set_hl https://github.com/neovim/neovim/issues/18160
vim.cmd("hi " .. group .. " " .. hl_string)
if fg ~= fields.fg or bg ~= fields.bg then
local hl = vim.tbl_extend('force', fields, { fg = fg, bg = bg })
vim.api.nvim_set_hl(0, group, hl)
updated = true
end
end
return updated
end

function NotifyBufHighlights:get_opacity()
Expand Down
5 changes: 0 additions & 5 deletions lua/notify/service/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ function NotificationService:push(notif)
self._pending:push(notif_buf)
if not self._running then
self:_run()
else
-- Forces a render during blocking events
-- https://github.com/rcarriga/nvim-notify/issues/5
pcall(self._animator.render, self._animator, self._pending, 1 / self._fps)
end
vim.cmd("redraw")
return buf
end

Expand Down
10 changes: 6 additions & 4 deletions lua/notify/windows/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,11 @@ function WindowAnimator:_get_dimensions(notif_buf)
end

function WindowAnimator:_apply_win_state(win, win_state)
local win_updated = false
local hl_updated = false
if win_state.opacity then
win_updated = true
local notif_buf = self.notif_bufs[win]
if notif_buf:is_valid() then
notif_buf.highlights:set_opacity(win_state.opacity.position)
hl_updated = notif_buf.highlights:set_opacity(win_state.opacity.position)
vim.fn.setwinvar(
win,
"&winhl",
Expand All @@ -288,6 +287,7 @@ function WindowAnimator:_apply_win_state(win, win_state)
end
local exists, conf = util.get_win_config(win)
local new_conf = {}
local win_updated = false
if not exists then
self:_remove_win(win)
else
Expand Down Expand Up @@ -317,7 +317,9 @@ function WindowAnimator:_apply_win_state(win, win_state)
api.nvim_win_set_config(win, new_conf)
end
end
return win_updated
-- The 'flush' key is set to enforce redrawing during blocking event.
vim.api.nvim__redraw({ win = win, valid = false, flush = true })
return hl_updated or win_updated
end

---@return WindowAnimator
Expand Down

0 comments on commit 3a4e83e

Please sign in to comment.