MunifTanjim / nui.nvim

UI Component Library for Neovim.
MIT License
1.62k stars 57 forks source link

[Question] Is there any API for navigating layouts? #288

Closed RobinTruax closed 1 year ago

RobinTruax commented 1 year ago

Hi,

Thank you for your plugin! I'm making a tool which initially launches three pop-up windows below the cursor. I'd like the user to start in the first one and navigate between the three with <Tab>.

For a number of reasons, this seems to require a surprising amount of work.

It seems like this would be a common task, so I'm wondering if I'm missing something with my strategy (e.g. maybe I should be using a Table, or some collection of methods for this task), or if this is a feature gap.

MunifTanjim commented 1 year ago

Firstly, handling the usual task of quitting the layout when leaving is much more complex than in the case of a single popup (because, if one just copies/pastes the usual "on BufLeave" autocmd syntax in the wiki, it'll unmount when moving from one window to the next).

local Layout = require("nui.layout")
local Popup = require("nui.popup")

local popup = {
  a = Popup({ border = "single" }),
  b = Popup({ border = "single" }),
  c = Popup({ border = "single" }),
}

local layout = Layout(
  {
    relative = "editor",
    position = "50%",
    size = { height = 10, width = 60 },
  },
  Layout.Box({
    Layout.Box(popup.a, { grow = 1 }),
    Layout.Box(popup.b, { grow = 1 }),
    Layout.Box(popup.c, { grow = 1 }),
  }, { dir = "row" })
)

for _, p in pairs(popup) do
  p:on("BufLeave", function()
    vim.schedule(function()
      local bufnr = vim.api.nvim_get_current_buf()
      for _, lp in pairs(popup) do
        if lp.bufnr == bufnr then
          return
        end
      end
      layout:unmount()
    end)
  end)
end

layout:mount()
MunifTanjim commented 1 year ago

Secondly, it doesn't seem like there's a way to refocus onto a new element of the layout. I understand that I can create a hack to do this by having a function which returns a layout which focuses on pop-up n on mount, and then unmounting/remounting. Yet this erases the data, so I'd have to store it somewhere. Is there an method I missed for fixing this?

I don't think I understand the problem. What exactly are you trying to do? 🤔