MunifTanjim / nui.nvim

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

Layout:update with changing components does not manage them properly #221

Closed jackMort closed 1 year ago

jackMort commented 1 year ago

I want dynamically show/hide settings panel (Popup window which appears as part of layout) but it only shows the empty space, looks like it's not mounted or so? Is this possible?

 local settings_open = false
  chat_input:map("i", "<C-o>", function()
    if settings_open then
      layout:update(Layout.Box({
        Layout.Box(chat_window, { grow = 1 }),
        Layout.Box(chat_input, { size = 3 }),
      }, { dir = "col" }))
    else
      layout:update(Layout.Box({
        Layout.Box({
          Layout.Box(chat_window, { grow = 1 }),
          Layout.Box(chat_input, { size = 3 }),
        }, { dir = "col", grow = 1 }),
        Layout.Box(settings_panel, { size = 30 }), -- Panel added dynamically
      }, { dir = "row" }))
    end
    settings_open = not settings_open
  end, {})
jackMort commented 1 year ago

I also tried another option to mount settings_panel and call :hide()/:show() methods, it works but in my use case I'm want to start with hidden panel, can you help me with that?

jackMort commented 1 year ago

nvm it works, I had to run settings_panel:mount():

   local settings_open = false
  for _, popup in ipairs({ settings_panel, chat_input }) do
    for _, mode in ipairs({ "n", "i" }) do
      popup:map(mode, "<C-o>", function()
        if settings_open then
          layout:update(Layout.Box({
            Layout.Box(chat_window, { grow = 1 }),
            Layout.Box(chat_input, { size = 3 }),
          }, { dir = "col" }))
          settings_panel:hide()
          vim.api.nvim_set_current_win(chat_input.winid)
        else
          layout:update(Layout.Box({
            Layout.Box({
              Layout.Box(chat_window, { grow = 1 }),
              Layout.Box(chat_input, { size = 3 }),
            }, { dir = "col", grow = 1 }),
            Layout.Box(settings_panel, { size = 40 }),
          }, { dir = "row" }))
          settings_panel:show()
          settings_panel:mount()

          vim.api.nvim_set_current_win(settings_panel.winid)
          vim.api.nvim_buf_set_option(settings_panel.bufnr, "modifiable", false)
          vim.api.nvim_win_set_option(settings_panel.winid, "cursorline", true)
        end
        settings_open = not settings_open
      end, {})
    end
  end
MunifTanjim commented 1 year ago

Re-opening.

This should be handled automatically. When you're using Layout you shouldn't need to deal with :mount, :unmount etc. for each individual components.

MunifTanjim commented 1 year ago

@jackMort can you check if https://github.com/MunifTanjim/nui.nvim/pull/222 fixes your issue? In your example, you shouldn't need to mount / show / hide / unmount settings_panel manually. Layout should do it for you automatically.

jackMort commented 1 year ago

Yes, it solves the issue, thank you!