Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: image scroll clipping #235

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function Image:has_extmark_moved()
local moved = extmark[1] ~= self.extmark.row or extmark[2] ~= self.extmark.col
return moved, extmark[1], extmark[2]
end
return false
end

---@param geometry? ImageGeometry
Expand Down
2 changes: 1 addition & 1 deletion lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local default_options = {
},
max_width = nil,
max_height = nil,
max_width_window_percentage = nil,
max_width_window_percentage = 100,
max_height_window_percentage = 50,
kitty_method = "normal",
window_overlap_clear_enabled = false,
Expand Down
39 changes: 20 additions & 19 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ local render = function(image)

-- screen max width/height
width = math.min(width, term_size.screen_cols)
height = math.min(height, term_size.screen_rows)
-- height = math.min(height, term_size.screen_rows)

-- utils.debug(("(1) x: %d, y: %d, width: %d, height: %d y_offset: %d"):format(original_x, original_y, width, height, y_offset))

Expand Down Expand Up @@ -121,7 +121,7 @@ local render = function(image)
-- global max window width/height percentage
if type(state.options.max_width_window_percentage) == "number" then
width =
math.min(width, math.floor((window.width - global_offsets.x) * state.options.max_width_window_percentage / 100))
math.min(width, math.floor((window.width - global_offsets.x) * state.options.max_width_window_percentage / 100))
end
if type(state.options.max_height_window_percentage) == "number" then
height = math.min(
Expand All @@ -148,9 +148,7 @@ local render = function(image)
local absolute_x = original_x + x_offset + window_offset_x
local absolute_y = original_y + y_offset + window_offset_y

if image.with_virtual_padding then
absolute_y = absolute_y + 1
end
if image.with_virtual_padding then absolute_y = absolute_y + 1 end

local prevent_rendering = false

Expand All @@ -163,8 +161,11 @@ local render = function(image)
local botline = win_info.botline

-- bail if out of bounds
if original_y + 1 < topline or original_y > botline then
-- utils.debug("prevent rendering 1", image.id)
if
(image.with_virtual_padding and ((topline == original_y + 2 and topfill == 0) or (topline > original_y + 2)))
or original_y > botline
then
-- utils.debug("prevent rendering 1", image.id, { topline = topline, original_y = original_y })
prevent_rendering = true
end

Expand Down Expand Up @@ -196,7 +197,7 @@ local render = function(image)
-- account for things that push line numbers around
if image.inline then
-- bail if the image is above the top of the window at least by one line
if topfill == 0 and original_y < topline then
if topfill == 0 and original_y < topline - 1 then
-- utils.debug("prevent rendering 2", image.id)
prevent_rendering = true
end
Expand Down Expand Up @@ -319,10 +320,10 @@ local render = function(image)

-- clear out of bounds images
if
absolute_y + height <= bounds.top
or absolute_y >= bounds.bottom + (vim.o.laststatus == 2 and 1 or 0)
or absolute_x + width <= bounds.left
or absolute_x >= bounds.right
absolute_y + height <= bounds.top
or absolute_y >= bounds.bottom + (vim.o.laststatus == 2 and 1 or 0)
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 @@ -443,13 +444,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 @@ -117,6 +117,7 @@
---@field namespace? string
---@field extmark? { id: number, row: number, col: number }
---@field last_modified? number
---@field has_extmark_moved fun (self:Image): (boolean, number?, number?)

-- wish proper generics were a thing here
---@class IntegrationContext
Expand Down
Loading