Skip to content

Commit

Permalink
fix: disable decoration provider handling on focus lost (when needed)
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Oct 12, 2023
1 parent 60d7fc6 commit 725eccb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
7 changes: 7 additions & 0 deletions lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ local state = {
extmarks_namespace = vim.api.nvim_create_namespace("image.nvim"),
remote_cache = {},
tmp_dir = vim.fn.tempname(),
disable_decorator_handling = false,
}

---@type API
Expand Down Expand Up @@ -75,6 +76,8 @@ api.setup = function(options)
local window_history = {}
vim.api.nvim_set_decoration_provider(state.extmarks_namespace, {
on_win = vim.schedule_wrap(function(_, winid, bufnr, topline, botline)
if state.disable_decorator_handling then return false end

if not vim.api.nvim_win_is_valid(winid) then return false end
if not vim.api.nvim_buf_is_valid(bufnr) then return false end

Expand Down Expand Up @@ -232,6 +235,8 @@ api.setup = function(options)
state.options.editor_only_render_when_focused
or (utils.tmux.is_tmux and utils.tmux.get_window_id() ~= initial_tmux_window_id)
then
state.disable_decorator_handling = true

local images = api.get_images()
for _, current_image in ipairs(images) do
if current_image.is_rendered then
Expand All @@ -249,6 +254,8 @@ api.setup = function(options)
callback = function() -- auto-clear images when windows and buffers change
-- utils.debug("FocusGained")

state.disable_decorator_handling = false

vim.schedule_wrap(function()
for _, current_image in ipairs(images_to_restore_on_focus) do
current_image:render()
Expand Down
28 changes: 13 additions & 15 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local render = function(image)
local image_rows = math.floor(image.image_height / term_size.cell_height)
local image_columns = math.floor(image.image_width / term_size.cell_width)

-- utils.debug(("renderer.render() %s"):format(image.original_path))
-- utils.debug(("renderer.render() %s %d"):format(image.id, image.internal_id))

local original_x = image.geometry.x or 0
local original_y = image.geometry.y or 0
Expand Down Expand Up @@ -111,9 +111,7 @@ local render = function(image)
bounds.bottom = bounds.bottom + global_offsets.y
bounds.left = bounds.left + global_offsets.x
bounds.right = bounds.right
if utils.offsets.get_border_shape(window.id).left > 0 then
bounds.right = bounds.right + 1
end
if utils.offsets.get_border_shape(window.id).left > 0 then bounds.right = bounds.right + 1 end

-- global max window width/height percentage
if type(state.options.max_width_window_percentage) == "number" then
Expand Down Expand Up @@ -240,10 +238,10 @@ local render = function(image)

-- clear out of bounds images
if
absolute_y + height <= bounds.top
or absolute_y >= bounds.bottom
or absolute_x + width <= bounds.left
or absolute_x >= bounds.right
absolute_y + height <= bounds.top
or absolute_y >= bounds.bottom
or absolute_x + width <= bounds.left
or absolute_x >= bounds.right
then
if image.is_rendered then
-- utils.debug("deleting out of bounds image", { id = image.id, x = absolute_x, y = absolute_y, width = width, height = height, bounds = bounds })
Expand Down Expand Up @@ -363,13 +361,13 @@ local render = function(image)
end

if
image.is_rendered
and image.rendered_geometry.x == rendered_geometry.x
and image.rendered_geometry.y == rendered_geometry.y
and image.rendered_geometry.width == rendered_geometry.width
and image.rendered_geometry.height == rendered_geometry.height
and image.crop_hash == initial_crop_hash
and image.resize_hash == initial_resize_hash
image.is_rendered
and image.rendered_geometry.x == rendered_geometry.x
and image.rendered_geometry.y == rendered_geometry.y
and image.rendered_geometry.width == rendered_geometry.width
and image.rendered_geometry.height == rendered_geometry.height
and image.crop_hash == initial_crop_hash
and image.resize_hash == initial_resize_hash
then
-- utils.debug("skipping render", image.id)
return true
Expand Down
1 change: 1 addition & 0 deletions lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
---@field extmarks_namespace any
---@field remote_cache { [string]: string }
---@field tmp_dir string
---@field disable_decorator_handling boolean

---@class DocumentIntegrationOptions
---@field enabled? boolean
Expand Down

0 comments on commit 725eccb

Please sign in to comment.