From 7a03ccc82a4010577a058516b70bb0905af7ee18 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Sun, 15 Jan 2023 14:55:48 +0800 Subject: [PATCH] feat: add window animation option for window close --- lua/Trans/README.md | 22 ++++++++++++++++++++++ lua/Trans/core/window.lua | 30 +++++++++++++++++++++++++----- lua/Trans/init.lua | 1 + 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lua/Trans/README.md b/lua/Trans/README.md index 6e02658..4999dc9 100644 --- a/lua/Trans/README.md +++ b/lua/Trans/README.md @@ -1,5 +1,20 @@ # 字段说明 + +- [字段说明](#字段说明) + - [本地](#本地) + - [有道](#有道) + - [中英](#中英) + - [百度](#百度) + - [返回结果](#返回结果) + - [彩云小译](#彩云小译) + - [必应](#必应) + - [腾讯翻译君](#腾讯翻译君) + - [阿里翻译](#阿里翻译) + - [火山翻译](#火山翻译) + - [金山词霸](#金山词霸) + + ## 本地 - `word` 查询的字符串 @@ -105,3 +120,10 @@ dict string 中英词典资源 返回中文或英文词典资源,包含音标 ## 腾讯翻译君 +## 阿里翻译 + +## 火山翻译 + +## 金山词霸 + +## Dictionary diff --git a/lua/Trans/core/window.lua b/lua/Trans/core/window.lua index 52d26c6..553f8ea 100644 --- a/lua/Trans/core/window.lua +++ b/lua/Trans/core/window.lua @@ -81,20 +81,18 @@ M.load_hover_opts = function() { 'InsertEnter', 'CursorMoved', 'BufLeave', }, { buffer = 0, once = true, - callback = function() - if api.nvim_win_is_valid(M.id) then - api.nvim_win_close(M.id, true) - end - end, + callback = M.close, }) api.nvim_win_set_option(M.id, 'wrap', M.view ~= 'float') local height = util.get_height(M.bufnr, M.id) if M.height > height then api.nvim_win_set_height(M.id, height) + M.height = height end end + M.load_float_opts = function() vim.keymap.set('n', 'q', function() if api.nvim_win_is_valid(M.id) then @@ -110,6 +108,28 @@ M.load_float_opts = function() end + + +M.close = function() + if api.nvim_win_is_valid(M.id) then + if conf.window.animation then + local function narrow() + if M.height > 1 then + M.height = M.height - 1 + api.nvim_win_set_height(M.id, M.height) + vim.defer_fn(narrow, 13) + else + api.nvim_win_close(M.id, true) + end + end + vim.defer_fn(narrow, 10) + + else + api.nvim_win_close(M.id, true) + end + end +end + M.show = function() M.init(M.view or 'float') M.load_opts() diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index ca3b821..d4b9702 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -8,6 +8,7 @@ M.conf = { }, window = { border = 'rounded', + animation = true, hover = { width = 36, height = 26,