Skip to content

Commit

Permalink
Merge pull request #248 from b0o/feat-specify-max-size-percentage
Browse files Browse the repository at this point in the history
feat: allow passing max_{width,height}_window_percentage to image factory
  • Loading branch information
3rd authored Dec 5, 2024
2 parents 5f8fcec + a8af48b commit 8b6b2e8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ local from_file = function(path, options, state)
original_path = instance.original_path,
image_width = instance.image_width,
image_height = instance.image_height,
max_width_window_percentage = instance.max_width_window_percentage,
max_height_window_percentage = instance.max_height_window_percentage,
window = opts.window or nil,
buffer = opts.buffer or nil,
geometry = {
Expand Down Expand Up @@ -329,6 +331,8 @@ local from_file = function(path, options, state)
original_path = path,
image_width = dimensions.width,
image_height = dimensions.height,
max_width_window_percentage = opts.max_width_window_percentage,
max_height_window_percentage = opts.max_height_window_percentage,
window = opts.window or nil,
buffer = opts.buffer or nil,
geometry = {
Expand Down
16 changes: 8 additions & 8 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,20 @@ local render = function(image)
bounds.right = bounds.right
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
local max_width_window_percentage = image.max_width_window_percentage or state.options.max_width_window_percentage
local max_height_window_percentage = image.max_height_window_percentage
or state.options.max_height_window_percentage

if type(max_width_window_percentage) == "number" then
width = math.min(
-- original
width,
-- max_window_percentage
math.floor((window.width - global_offsets.x) * state.options.max_width_window_percentage / 100)
math.floor((window.width - global_offsets.x) * max_width_window_percentage / 100)
)
end
if type(state.options.max_height_window_percentage) == "number" then
height = math.min(
height,
math.floor((window.height - global_offsets.y) * state.options.max_height_window_percentage / 100)
)
if type(max_height_window_percentage) == "number" then
height = math.min(height, math.floor((window.height - global_offsets.y) * max_height_window_percentage / 100))
end
end

Expand Down
4 changes: 4 additions & 0 deletions lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
---@field with_virtual_padding? boolean
---@field inline? boolean
---@field namespace? string
---@field max_width_window_percentage? number
---@field max_height_window_percentage? number

---@class ImageBounds
---@field top number
Expand Down Expand Up @@ -99,6 +101,8 @@
---@field original_path string
---@field image_width number
---@field image_height number
---@field max_width_window_percentage? number
---@field max_height_window_percentage? number
---@field window? number
---@field buffer? number
---@field with_virtual_padding? boolean
Expand Down

0 comments on commit 8b6b2e8

Please sign in to comment.