Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: select window by left mouse click #106

Merged
merged 3 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ require 'window-picker'.setup({

-- This section contains picker specific configurations
picker_config = {
-- whether should select window by clicking left mouse button on it
handle_mouse_click = false,
statusline_winbar_picker = {
-- You can change the display string in status bar.
-- It supports '%' printf style. Such as `return char .. ': %f'` to display
Expand Down
2 changes: 2 additions & 0 deletions lua/window-picker/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ local config = {

-- This section contains picker specific configurations
picker_config = {
-- whether should select window by clicking left mouse button on it
handle_mouse_click = false,
statusline_winbar_picker = {
-- You can change the display string in status bar.
-- It supports '%' printf style. Such as `return char .. ': %f'` to display
Expand Down
7 changes: 6 additions & 1 deletion lua/window-picker/pickers/window-picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function M:set_config(config)
self.show_prompt = config.show_prompt
self.prompt_message = config.prompt_message
self.autoselect_one = config.filter_rules.autoselect_one
self.handle_mouse_click = config.picker_config.handle_mouse_click
return self
end

Expand Down Expand Up @@ -68,12 +69,16 @@ function M:pick_window()
print(self.prompt_message)
end

local char = util.get_user_input_char()
local char = util.get_user_input_char(self.handle_mouse_click)

vim.cmd.redraw()

self.hint:clear()

if char == '<LeftMouse>' then
return vim.fn.getmousepos().winid
end

if char then
window = self:_find_matching_win_for_char(char, windows)
end
Expand Down
7 changes: 6 additions & 1 deletion lua/window-picker/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ function M.map_find(tbl, match_func)
end
end

function M.get_user_input_char()
function M.get_user_input_char(handle_mouse_click)
local ok, c = pcall(vim.fn.getchar)

if not ok then
return
end

while type(c) ~= 'number' do
local ok2, translated_key = pcall(vim.fn.keytrans, c)
if ok2 and handle_mouse_click and translated_key == '<LeftMouse>' then
return translated_key
end

c = vim.fn.getchar()
end

Expand Down