MunifTanjim / nui.nvim

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

QuitPre event not working if set after popup is mounted #236

Closed xeluxee closed 1 year ago

xeluxee commented 1 year ago

Consider the following working code:

local nui_popup = require("nui.popup")
local nui_event = require("nui.utils.autocmd").event

local popup = nui_popup({
    enter = true,
    border = {
        style = "rounded",
    },
    position = "50%",
    size = "50%",
})

popup:on(nui_event.QuitPre, function()
    print("QuitPre autocommand")
end)

popup:mount()
vim.api.nvim_buf_set_lines(popup.bufnr, 0, 1, false, { "Hello World" })

When I close the popup with :q the callback is executed and "QuitPre autocommand" is printed


But when the autocommand is created after popup is mounted the callback doesn't work:

local nui_popup = require("nui.popup")
local nui_event = require("nui.utils.autocmd").event

local popup = nui_popup({
    enter = true,
    border = {
        style = "rounded",
    },
    position = "50%",
    size = "50%",
})

popup:mount()

popup:on(nui_event.QuitPre, function()
    print("QuitPre autocommand")
end)

vim.api.nvim_buf_set_lines(popup.bufnr, 0, 1, false, { "Hello World" })

When I close the popup with :q nothing happens

MunifTanjim commented 1 year ago

@xeluxee can you check if https://github.com/MunifTanjim/nui.nvim/pull/237 solves the issue for you?

Also if you want to organize multiple popups in a specific layout, you can try nui.layout for that. It already supports the desired behavior for :quit command. If one of the popup in a layout is quitted, the whole layout is unmounted.

xeluxee commented 1 year ago

can you check if https://github.com/MunifTanjim/nui.nvim/pull/237 solves the issue for you?

Thanks, it solves my issue.

Also if you want to organize multiple popups in a specific layout, you can try nui.layout for that. It already supports the desired behavior for :quit command. If one of the popup in a layout is quitted, the whole layout is unmounted.

I wrote my code before nui.layout existed. Soon or later I'll rewrite it using layouts.