Skip to content

Commit

Permalink
fix: rendering glitches and handle buffer change clearing through dec…
Browse files Browse the repository at this point in the history
…oration provider
  • Loading branch information
3rd committed Jul 10, 2023
1 parent ddf4b58 commit 5979f47
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 68 deletions.
78 changes: 26 additions & 52 deletions lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ api.setup = function(options)
end

local height = vim.api.nvim_win_get_height(winid)
local needs_clear = prev.bufnr ~= bufnr
local needs_rerender = prev.topline ~= topline or prev.botline ~= botline or prev.height ~= height
local needs_clear = false
local needs_rerender = false

-- compute folded lines
-- clear if buffer changed
needs_clear = prev.bufnr ~= bufnr

-- rerender if height, topline, or botline changed
needs_rerender = prev.topline ~= topline or prev.botline ~= botline or prev.height ~= height

-- rerender if the amount of folded lines changed
local folded_lines = 0
local i = 1
while i < botline do
Expand All @@ -95,65 +101,33 @@ api.setup = function(options)
end
if prev.folded_lines ~= folded_lines then needs_rerender = true end

-- store new state
window_history[winid] =
{ topline = topline, botline = botline, bufnr = bufnr, height = height, folded_lines = folded_lines }

if needs_clear or needs_rerender then
vim.defer_fn(function()
if needs_clear then
for _, curr in ipairs(api.get_images({ buffer = prev.bufnr })) do
curr:clear(true)
end
elseif needs_rerender then
for _, current_image in ipairs(api.get_images({ window = winid })) do
current_image:render()
end
-- execute deferred clear / rerender
utils.debug("needs_clear", needs_clear, "needs_rerender", needs_rerender)
if needs_rerender then utils.debug("window", winid, "needs rerender") end
local images = (needs_clear and api.get_images({ window = winid, buffer = prev.bufnr }))
or (needs_rerender and api.get_images({ window = winid, buffer = bufnr }))
or {}
vim.defer_fn(function()
if needs_clear then
for _, curr in ipairs(images) do
curr:clear(true)
end
end, 0)
end
else
for _, curr in ipairs(images) do
curr:render()
end
end
end, 0)
end,
})

-- setup autocommands
local group = vim.api.nvim_create_augroup("image.nvim", { clear = true })

-- auto-clear on buffer change
vim.api.nvim_create_autocmd("BufWinEnter", {
group = group,
callback = function()
local has_images = false
for _ in pairs(state.images) do
has_images = true
break
end
if not has_images then return end

local windows = utils.window.get_visible_windows()
local win_buf_map = {}
for _, window in ipairs(windows) do
win_buf_map[window.id] = window.buffer
end

local images = api.get_images()
for _, current_image in ipairs(images) do
local is_window_bound = type(current_image.window) == "number"
local is_window_binding_valid = win_buf_map[current_image.window] ~= nil
local is_buffer_bound = type(current_image.buffer) == "number"
local is_buffer_binding_valid = win_buf_map[current_image.window] == current_image.buffer

local should_clear = false
local shallow = false
if is_window_bound and not is_window_binding_valid then
should_clear = true
elseif is_buffer_bound and not is_buffer_binding_valid then
should_clear = true
shallow = true
end
if should_clear then current_image:clear(shallow) end
end
end,
})

-- auto-clear on window close
vim.api.nvim_create_autocmd("WinClosed", {
group = group,
Expand Down
9 changes: 3 additions & 6 deletions lua/image/integrations/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ local query_buffer_images = function(buffer)
end

---@type fun(ctx: IntegrationContext)
local render = function(ctx)
local render = vim.schedule_wrap(function(ctx)
local windows = utils.window.get_visible_windows()

for _, window in ipairs(windows) do
Expand Down Expand Up @@ -112,14 +112,11 @@ local render = function(ctx)

-- clear previous images
for _, image in ipairs(previous_images) do
if not vim.tbl_contains(new_image_ids, image.id) then
-- utils.debug("[markdown] clearing removed image", image.id)
image:clear()
end
if not vim.tbl_contains(new_image_ids, image.id) then image:clear() end
end
end
end
end
end)

---@type fun(ctx: IntegrationContext)
local setup_autocommands = function(ctx)
Expand Down
5 changes: 3 additions & 2 deletions lua/image/integrations/neorg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ local query_buffer_images = function(buffer)
end

---@type fun(ctx: IntegrationContext)
local render = function(ctx)
local render = vim.schedule_wrap(function(ctx)
local windows = utils.window.get_visible_windows()

for _, window in ipairs(windows) do
Expand Down Expand Up @@ -96,12 +96,13 @@ local render = function(ctx)
end
end

-- clear previous images
for _, image in ipairs(previous_images) do
if not vim.tbl_contains(new_image_ids, image.id) then image:clear() end
end
end
end
end
end)

---@type fun(ctx: IntegrationContext)
local setup_autocommands = function(ctx)
Expand Down
18 changes: 10 additions & 8 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,18 @@ local render = function(image)
if needs_resize then
if image.resize_hash ~= resize_hash then
local resized_image = magick.load_image(image.path)
resized_image:set_format("png")
if resized_image then
resized_image:set_format("png")

-- utils.debug(("resizing image %s to %dx%d"):format(image.path, pixel_width, pixel_height))
resized_image:scale(pixel_width, pixel_height)
local tmp_path = state.tmp_dir .. "/" .. utils.base64.encode(image.id) .. "-resized.png"
resized_image:write(tmp_path)
resized_image:destroy()
-- utils.debug(("resizing image %s to %dx%d"):format(image.path, pixel_width, pixel_height))
resized_image:scale(pixel_width, pixel_height)
local tmp_path = state.tmp_dir .. "/" .. utils.base64.encode(image.id) .. "-resized.png"
resized_image:write(tmp_path)
resized_image:destroy()

image.resized_path = tmp_path
image.resize_hash = resize_hash
image.resized_path = tmp_path
image.resize_hash = resize_hash
end
end
else
image.resized_path = image.path
Expand Down

0 comments on commit 5979f47

Please sign in to comment.