feat: add auto resize animation
This commit is contained in:
@@ -2,7 +2,7 @@ local Trans = require('Trans')
|
||||
|
||||
|
||||
---@class TransBackend
|
||||
---@field query fun(TransData): TransResult
|
||||
---@field query fun(data: TransData)---@async
|
||||
---@field opts TransBackendOpts
|
||||
---@field no_wait? boolean whether need to wait for the result
|
||||
---@field name string @backend name
|
||||
|
||||
@@ -14,8 +14,6 @@ end
|
||||
---@param _start? integer start line index
|
||||
---@param _end? integer end line index
|
||||
function buffer:del(_start, _end)
|
||||
-- FIXME :
|
||||
|
||||
---@diagnostic disable-next-line: cast-local-type
|
||||
_start = _start or '$'
|
||||
fn.deletebufline(self.bufnr, _start, _end)
|
||||
@@ -102,18 +100,6 @@ function buffer:add_highlight(linenr, hl_group, col_start, col_end, ns)
|
||||
api.nvim_buf_add_highlight(self.bufnr, ns or -1, hl_group, linenr, col_start, col_end or -1)
|
||||
end
|
||||
|
||||
---Calculate buffer content display height
|
||||
---@param width integer
|
||||
---@return integer height
|
||||
function buffer:height(width)
|
||||
local lines = self:lines()
|
||||
local height = 0
|
||||
for _, line in ipairs(lines) do
|
||||
height = height + math.max(1, (math.ceil(line:width() / width)))
|
||||
end
|
||||
return height
|
||||
end
|
||||
|
||||
---Get buffer line count
|
||||
---@return integer
|
||||
function buffer:line_count()
|
||||
|
||||
@@ -3,7 +3,6 @@ local util = Trans.util
|
||||
|
||||
local function init_opts(opts)
|
||||
opts = opts or {}
|
||||
---@type TransMode
|
||||
opts.mode = opts.mode or ({
|
||||
n = 'normal',
|
||||
v = 'visual',
|
||||
@@ -19,9 +18,12 @@ end
|
||||
---@return TransResult? @return nil if no result
|
||||
local function do_query(data)
|
||||
-- HACK :Rewrite this function to support multi requests
|
||||
|
||||
---@type TransFrontend
|
||||
local frontend = data.frontend
|
||||
local result = data.result
|
||||
for _, backend in ipairs(data.backends) do
|
||||
---@cast backend TransBackend
|
||||
local name = backend.name
|
||||
if backend.no_wait then
|
||||
backend.query(data)
|
||||
@@ -32,11 +34,13 @@ local function do_query(data)
|
||||
|
||||
|
||||
if type(result[name]) == 'table' then
|
||||
---@diagnostic disable-next-line: return-type-mismatch
|
||||
return result[name]
|
||||
else
|
||||
result[name] = nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -68,7 +72,7 @@ end
|
||||
|
||||
|
||||
---@class Trans
|
||||
---@field translate fun(opts: { word: string, mode: string?}) Translate string core function
|
||||
---@field translate fun(opts: { frontend: string?, mode: string?}?) Translate string core function
|
||||
return function(opts)
|
||||
coroutine.wrap(process)(opts)
|
||||
end
|
||||
|
||||
@@ -77,6 +77,44 @@ function M.is_English(str)
|
||||
return true
|
||||
end
|
||||
|
||||
---Calculates the height of the text to be displayed
|
||||
---@param lines string[] text to be displayed
|
||||
---@param width integer width of the window
|
||||
---@return integer height display height
|
||||
function M.display_height(lines, width)
|
||||
local height = 0
|
||||
for _, line in ipairs(lines) do
|
||||
height = height + math.max(1, (math.ceil(line:width() / width)))
|
||||
end
|
||||
return height
|
||||
end
|
||||
|
||||
---Calculates the width of the text to be displayed
|
||||
---@param lines string[] text to be displayed
|
||||
---@return integer width display width
|
||||
function M.display_width(lines)
|
||||
local width = 0
|
||||
for _, line in ipairs(lines) do
|
||||
width = math.max(line:width(), width)
|
||||
end
|
||||
return width
|
||||
end
|
||||
|
||||
---Calculates the height and width of the text to be displayed
|
||||
---@param lines string[] text to be displayed
|
||||
---@param width integer width of the window
|
||||
---@return { height: integer, width: integer } _ display height and width
|
||||
function M.display_size(lines, width)
|
||||
local ds_height, ds_width = 0, 0
|
||||
for _, line in ipairs(lines) do
|
||||
local wid = line:width()
|
||||
ds_height = ds_height + math.max(1, (math.ceil(wid / width)))
|
||||
ds_width = math.max(wid, ds_width)
|
||||
end
|
||||
|
||||
return { height = ds_height, width = ds_width }
|
||||
end
|
||||
|
||||
---@class Trans
|
||||
---@field util TransUtil
|
||||
return M
|
||||
|
||||
@@ -5,10 +5,10 @@ local Trans = require("Trans")
|
||||
local window = {}
|
||||
|
||||
---Change window attached buffer
|
||||
---@param buf TransBuffer
|
||||
function window:set_buf(buf)
|
||||
api.nvim_win_set_buf(self.winid, buf.bufnr)
|
||||
self.buf = buf
|
||||
---@param buffer TransBuffer
|
||||
function window:set_buf(buffer)
|
||||
api.nvim_win_set_buf(self.winid, buffer.bufnr)
|
||||
self.buffer = buffer
|
||||
end
|
||||
|
||||
---Check window valid
|
||||
@@ -54,10 +54,17 @@ function window:height()
|
||||
return api.nvim_win_get_height(self.winid)
|
||||
end
|
||||
|
||||
-- TODO :
|
||||
-- function window:adjust()
|
||||
---Auto adjust window size
|
||||
---@param height? integer max height
|
||||
function window:adjust_height(height)
|
||||
local display_height = Trans.util.display_height(self.buffer:lines(), self:width())
|
||||
height = height and math.min(display_height, height) or display_height
|
||||
|
||||
-- end
|
||||
self:smooth_expand({
|
||||
field = 'height',
|
||||
to = height
|
||||
})
|
||||
end
|
||||
|
||||
---Expand window [width | height] value
|
||||
---@param opts
|
||||
@@ -88,16 +95,8 @@ end
|
||||
---@param opts
|
||||
---|{ width: integer, height: integer }
|
||||
function window:resize(opts)
|
||||
local width = opts[1]
|
||||
local height = opts[2]
|
||||
|
||||
|
||||
if self:width() ~= width then
|
||||
self:smooth_expand {
|
||||
field = 'width',
|
||||
to = width
|
||||
}
|
||||
end
|
||||
local width = opts.width
|
||||
local height = opts.height
|
||||
|
||||
|
||||
if self:height() ~= height then
|
||||
@@ -106,6 +105,13 @@ function window:resize(opts)
|
||||
to = height
|
||||
}
|
||||
end
|
||||
|
||||
if self:width() ~= width then
|
||||
self:smooth_expand {
|
||||
field = 'width',
|
||||
to = width
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
---Try to close window with animation?
|
||||
|
||||
Reference in New Issue
Block a user