diff options
| author | giuseppegadola <giusgadola@gmail.com> | 2023-05-24 14:07:17 +0200 |
|---|---|---|
| committer | giuseppegadola <giusgadola@gmail.com> | 2023-05-24 14:10:17 +0200 |
| commit | 94b4724e031fc3c9b6da19bdef574f44fabcca16 (patch) | |
| tree | 7694e3193d5e527a69785d87889b3f7c13722568 | |
| parent | docs(help): auto generate docs (diff) | |
feat: handle window resizes
Resize the popup when the window is resized.
Related: #32 #11
Fixes #24
| -rw-r--r-- | lua/pets.lua | 6 | ||||
| -rw-r--r-- | lua/pets/animations.lua | 13 | ||||
| -rw-r--r-- | lua/pets/commands.lua | 6 | ||||
| -rw-r--r-- | lua/pets/pet.lua | 8 |
4 files changed, 27 insertions, 6 deletions
diff --git a/lua/pets.lua b/lua/pets.lua index 9ebc71a..c883af9 100644 --- a/lua/pets.lua +++ b/lua/pets.lua @@ -140,4 +140,10 @@ function M.toggle_idle() end end +function M.refresh() + for _, pet in pairs(M.pets) do + pet:refresh_popup() + end +end + return M diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua index 1b685d9..419ac1a 100644 --- a/lua/pets/animations.lua +++ b/lua/pets/animations.lua @@ -184,20 +184,21 @@ end -- @function set horizontal movement per frame based on current action function M.Animation:set_next_col() + local width = self.popup.win_config.width if vim.tbl_contains(self.movements.right.normal, self.current_action) then - if self.col < self.popup.win_config.width - 8 then + if self.col < width - 8 then self.col = self.col + 1 else self.col = M.base_col end elseif vim.tbl_contains(self.movements.right.fast, self.current_action) then - if self.col < self.popup.win_config.width - 8 then + if self.col < width - 8 then self.col = self.col + 2 else self.col = M.base_col end elseif vim.tbl_contains(self.movements.right.slow, self.current_action) then - if self.col < self.popup.win_config.width - 8 then + if self.col < width - 8 then if #self.frames[self.current_action] <= 2 then -- if there is only one frame in the current action if self.repetitions % 2 == 0 then -- then use repetitions as a counter self.col = self.col + 1 @@ -214,13 +215,13 @@ function M.Animation:set_next_col() if self.col > M.base_col then self.col = self.col - 1 else - self.col = self.popup.win_config.width - 8 + self.col = width - 8 end elseif vim.tbl_contains(self.movements.left.fast, self.current_action) then if self.col > M.base_col then self.col = self.col - 2 else - self.col = self.popup.win_config.width - 8 + self.col = width - 8 end elseif vim.tbl_contains(self.movements.left.slow, self.current_action) then if self.col > M.base_col then @@ -234,7 +235,7 @@ function M.Animation:set_next_col() end end else - self.col = self.popup.win_config.width - 8 + self.col = width - 8 end end end diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua index f521710..5b51363 100644 --- a/lua/pets/commands.lua +++ b/lua/pets/commands.lua @@ -1,6 +1,12 @@ local pets = require("pets") local utils = require("pets.utils") +vim.api.nvim_create_autocmd({ "VimResized", "WinResized" }, { + callback = function() + pets.refresh() + end, +}) + vim.api.nvim_create_user_command("PetsNew", function(input) local pet, style = pets.options.default_pet, pets.options.default_style diff --git a/lua/pets/pet.lua b/lua/pets/pet.lua index 6540b1d..47bb2b0 100644 --- a/lua/pets/pet.lua +++ b/lua/pets/pet.lua @@ -15,6 +15,7 @@ function M.Pet.new(name, type, style, user_opts, state) instance.style = style instance.death_animation = user_opts.death_animation instance.state = state + instance.popup_opts = user_opts.popup local wd = debug.getinfo(1).source:sub(2):match("(.*nvim/)") .. "media/" instance.sourcedir = wd .. type .. "/" .. style .. "/" @@ -77,4 +78,11 @@ function M.Pet:set_idle(idle) self.animation:set_state({ idle = idle }) end +function M.Pet:refresh_popup() + self.popup:unmount() + self.popup = require("nui.popup")(self.popup_opts) + self.popup:mount() + self.animation.popup = self.popup +end + return M |