NvChad / menu

Menu plugin for neovim ( supports nested menus ) made using volt
GNU General Public License v3.0
335 stars 5 forks source link

Allow reopen #9

Closed Zeioth closed 3 days ago

Zeioth commented 4 days ago

DEMO: https://www.youtube.com/watch?v=foVP_ElIbtM

Explanation

This code respect the behavior the native neovim right click menu has.

-- function to create the right click menu.
-- @param is_mouse boolean
local function right_click_menu(is_mouse)
  -- If the window already exists, close it before opening another one.
  for _, win in ipairs(vim.api.nvim_list_wins()) do
    local buf = vim.api.nvim_win_get_buf(win)
    if vim.bo[buf].filetype == "NvMenu" then
      vim.api.nvim_win_close(win, true)
    end
  end

  if is_mouse == nil then is_mouse = true end
  local options = vim.bo.ft == "NvimTree" and "nvimtree" or menu_opts
  require("menu").open(options, { mouse = is_mouse, border = true })
end

maps.n["<C-t>"] =
{ function() right_click_menu(false) end, desc = "Open right click menu" }
maps.v["<C-t>"] =
{ function() right_click_menu(false) end, desc = "Open right click menu" }

maps.n["<RightMouse>"] = { right_click_menu, desc = "Open right click menu" }
maps.v["<RightMouse>"] = { right_click_menu, desc = "Open right click menu" }
Zeioth commented 4 days ago

I'm not sure it's possible to do this on NvMenu side with the current architecture, so for now maybe it's better to let it to the user.

siduck commented 4 days ago

a menu in general shouldnt reopen at multiple sites. Look at how menus work in ides, only one at once.

epheien commented 3 days ago

a menu in general shouldnt reopen at multiple sites. Look at how menus work in ides, only one at once.

I agree with this PR because the right-click menu behavior of VSCode is consistent with the video behavior of this PR.

siduck commented 3 days ago

a menu in general shouldnt reopen at multiple sites. Look at how menus work in ides, only one at once.

I agree with this PR because the right-click menu behavior of VSCode is consistent with the video behavior of this PR.

i dont get it, i already close the menu on left click if menu's opened btw

siduck commented 3 days ago

like who in his right mind would have multiple menus opened at the same time? I dont get it

epheien commented 3 days ago

like who in his right mind would have multiple menus opened at the same time? I dont get it

This PR is not the behavior you described.

The meaning of this PR is that every time we need to open menu, the old menu (if any) is closed and the menu is reopened.

The biggest advantage of doing this is that every time the menu is opened, it will follow the latest position of the mouse.

Zeioth commented 3 days ago

like who in his right mind would have multiple menus opened at the same time? I dont get it

This is just a proposal, it's cool if you don't agree.

siduck commented 3 days ago

honestly i dont understand, whats wrong with current one? it just doesnt let users open multiple menus at once.

The biggest advantage of doing this is that every time the menu is opened, it will follow the latest position of the mouse

In IDES like vscode etc and everywhere, you first close the existing menu by clicking anywhere and then right click to where you want...

epheien commented 3 days ago

In IDES like vscode etc and everywhere, you first close the existing menu by clicking anywhere and then right click to where you want...

In fact, the usage scenario is that when the menu has already been opened, the user will not need to click to close the menu. Instead, when the mouse reaches a new position, they will directly right-click to open the menu again. At this point, the menu will open near the mouse (the old menu will automatically close first).

The opening logic of the right-click menu in the current GUI is like this.

siduck commented 3 days ago

https://github.com/user-attachments/assets/2becf123-ef42-40cb-a6de-fced7656e6e6

tried with vscode, the user has to press right click twice to make it open on new location

epheien commented 3 days ago

tried with vscode, the user has to press right click twice to make it open on new location

Then I finally understand why you misunderstood. It seems that the Linux desktop is the behavior you described, but macOS and Windows are the behavior I described.

So I mentioned earlier that PR requires the menu to provide check and close API and let the user decide their final behavior.

siduck commented 3 days ago

anyways, to check if the menu is opened, the fastest would be its state

local menu_opened = #require("menu.state").bufids > 0