Skip to content

Commit

Permalink
Use getmousepos() if mousemoveevent is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Aug 9, 2023
1 parent 3c68a81 commit 3b0e3ca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
38 changes: 26 additions & 12 deletions lua/piemenu/view/background.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,28 @@ function Background.open(name, position)
vim.wo[window_id].winblend = 100
vim.wo[window_id].scrolloff = 0
vim.wo[window_id].sidescrolloff = 0
vim.api.nvim_win_set_cursor(window_id, { position[1] + 1, position[2] - 1 })

-- NOTE: show and move cursor to the window by <LeftDrag>
vim.cmd.redraw()
local get_position
if vim.o.mousemoveevent then
get_position = function()
local mouse_pos = vim.fn.getmousepos()
return {
mouse_pos.screenrow,
mouse_pos.screencol - 1,
}
end
else
vim.api.nvim_win_set_cursor(window_id, { position[1] + 1, position[2] - 1 })
-- NOTE: show and move cursor to the window by <LeftDrag>
vim.cmd.redraw()
get_position = function()
Background._click()
if not vim.api.nvim_win_is_valid(window_id) then
return nil
end
return vim.api.nvim_win_get_cursor(window_id)
end
end

vim.api.nvim_create_autocmd({ "WinLeave", "TabLeave", "BufLeave" }, {
buffer = bufnr,
Expand All @@ -54,7 +72,11 @@ function Background.open(name, position)
end,
})

local tbl = { window_id = window_id, _ns = ns }
local tbl = {
window_id = window_id,
get_position = get_position,
_ns = ns,
}
return setmetatable(tbl, Background)
end

Expand All @@ -63,14 +85,6 @@ function Background.close(self)
vim.api.nvim_set_decoration_provider(self._ns, {})
end

function Background.click(self)
self:_click()
if not vim.api.nvim_win_is_valid(self.window_id) then
return nil
end
return vim.api.nvim_win_get_cursor(self.window_id)
end

local mouse = vim.api.nvim_eval('"\\<LeftMouse>"')
-- replace on testing
function Background._click()
Expand Down
4 changes: 2 additions & 2 deletions lua/piemenu/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ function View.highlight(self)
if mouse_is_on_tabline() then
return
end
local position = self._background:click()
local position = self._background:get_position()
if not position then
return
end
self._tiles:activate(position)
end

function View.finish(self)
local position = self._background:click()
local position = self._background:get_position()
if not position then
return
end
Expand Down
22 changes: 22 additions & 0 deletions spec/lua/piemenu/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ describe("piemenu.highlight()", function()

assert.exists_highlighted_window("PiemenuCurrent")
end)

it("highlights a menu if mouse cursor is in area with mousemoveevent", function()
vim.o.mousemoveevent = true

piemenu.register("default", {
animation = { duration = 0 },
radius = 5.0,
menus = {
{
text = "text A",
action = function() end,
},
},
})

piemenu.start("default", { position = { vim.o.lines / 2, vim.o.columns / 2 } })

vim.api.nvim_input_mouse("left", "press", "", 0, math.floor(vim.o.lines / 2), vim.o.columns)
piemenu.highlight()

assert.exists_highlighted_window("PiemenuCurrent")
end)
end)

describe("piemenu.finish()", function()
Expand Down

0 comments on commit 3b0e3ca

Please sign in to comment.