Skip to content

Commit

Permalink
feat(mouse): add hacky support for scrolling yazi (opt-in)
Browse files Browse the repository at this point in the history
This is currently experimental. The users that want to opt in can set
`enable_mouse_support` to `true` in their configuration.
  • Loading branch information
mikavilpas committed Apr 25, 2024
1 parent ad4f8a2 commit 83619ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function M.default()
open_for_directories = false,
chosen_file_path = '/tmp/yazi_filechosen',
events_file_path = '/tmp/yazi.nvim.events.txt',
enable_mouse_support = false,
open_file_function = openers.open_file,
set_keymappings_function = M.default_set_keymappings_function,
hooks = {
Expand Down
1 change: 1 addition & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
---@field public open_for_directories? boolean
---@field public chosen_file_path? string "the path to a temporary file that will be created by yazi to store the chosen file path"
---@field public events_file_path? string "the path to a temporary file that will be created by yazi to store events"
---@field public enable_mouse_support? boolean
---@field public open_file_function? fun(chosen_file: string, config: YaziConfig): nil "a function that will be called when a file is chosen in yazi"
---@field public set_keymappings_function? fun(buffer: integer, config: YaziConfig): nil "the function that will set the keymappings for the yazi floating window. It will be called after the floating window is created."
---@field public hooks? YaziConfigHooks
Expand Down
30 changes: 25 additions & 5 deletions lua/yazi/window.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
local M = {}

---@class YaziFloatingWindow
---@class (exact) YaziFloatingWindow
---@field new fun(config: YaziConfig): YaziFloatingWindow
---@field win integer floating_window_id
---@field content_buffer integer
---@field config YaziConfig
---@field private cleanup fun(): nil
local YaziFloatingWindow = {}
---@diagnostic disable-next-line: inject-field
YaziFloatingWindow.__index = YaziFloatingWindow

M.YaziFloatingWindow = YaziFloatingWindow
Expand All @@ -19,6 +22,8 @@ function YaziFloatingWindow.new(config)
end

function YaziFloatingWindow:close()
pcall(self.cleanup)

if
vim.api.nvim_buf_is_valid(self.content_buffer)
and vim.api.nvim_buf_is_loaded(self.content_buffer)
Expand Down Expand Up @@ -55,6 +60,8 @@ function YaziFloatingWindow:open_and_display()
local yazi_buffer = vim.api.nvim_create_buf(false, true)
-- create file window, enter the window, and use the options defined in opts
local win = vim.api.nvim_open_win(yazi_buffer, true, opts)
self.win = win
self.content_buffer = yazi_buffer

vim.bo[yazi_buffer].filetype = 'yazi'

Expand All @@ -64,16 +71,29 @@ function YaziFloatingWindow:open_and_display()
vim.cmd('setlocal winhl=NormalFloat:YaziFloat')
vim.cmd('set winblend=' .. self.config.yazi_floating_window_winblend)

vim.api.nvim_create_autocmd('WinLeave', {
if self.config.enable_mouse_support == true then
-- Disable nvim mouse support so that yazi can handle mouse events instead
local original_mouse_settings = vim.o.mouse
vim.api.nvim_create_autocmd({ 'TermEnter', 'WinEnter' }, {
buffer = yazi_buffer,
callback = function()
vim.api.nvim_set_option_value('mouse', '', {})
end,
})

self.cleanup = function()
-- Restore mouse mode on exiting
vim.api.nvim_set_option_value('mouse', original_mouse_settings, {})
end
end

vim.api.nvim_create_autocmd({ 'WinLeave', 'TermLeave' }, {
buffer = yazi_buffer,
callback = function()
self:close()
end,
})

self.win = win
self.content_buffer = yazi_buffer

return self
end

Expand Down

0 comments on commit 83619ea

Please sign in to comment.