Skip to content

Commit

Permalink
feat(menu): scrollbar customization options
Browse files Browse the repository at this point in the history
Adds the ability to hide the scrollbar's background, or
to disable it entirely.
  • Loading branch information
willothy committed Dec 8, 2023
1 parent 5ca5e7f commit 034efbe
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 19 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ https://github.com/Bekaboo/dropbar.nvim/assets/76579810/e8c1ac26-0321-4762-9975-
right = 1,
},
},
-- Menu scrollbar options
scrollbar = {
enable = true,
-- The background / gutter of the scrollbar
-- When false, only the scrollbar thumb is shown.
background = true
},
---@type table<string, string|function|table<string, string|function>>
keymaps = {
['<LeftMouse>'] = function()
Expand Down Expand Up @@ -515,8 +522,13 @@ https://github.com/Bekaboo/dropbar.nvim/assets/76579810/e8c1ac26-0321-4762-9975-
---@param menu dropbar_menu_t
col = function(menu)
if menu.prev_menu then
return menu.prev_menu._win_configs.width
+ (menu.prev_menu.scrollbar and 1 or 0)
local offset = 0
if
menu.prev_menu.scrollbar and M.opts.menu.scrollbar.background
then
offset = 1
end
return menu.prev_menu._win_configs.width + offset
end
local mouse = vim.fn.getmousepos()
local bar = utils.bar.get({ win = menu.prev_win })
Expand Down Expand Up @@ -1123,6 +1135,18 @@ menu:
end
},
```

- `opts.menu.scrollbar`: `table<string, boolean>`
- Scrollbar configuration for the menu.
- Default:
```lua
{
enable = true,
-- if false, only the scrollbar thumb will be shown
background = true
}
```

- `opts.menu.win_configs`: `table<string, dropbar_menu_win_config_opts_t>`
- Window configurations for the menu, see `:h nvim_open_win()`
- Each config key in `opts.menu.win_configs` accepts either a plain value
Expand Down
10 changes: 10 additions & 0 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,16 @@ the menu:
end
},
<
- `opts.menu.scrollbar`: `table<string, boolean>`
- Scrollbar configuration for the menu.
- Default: >lua
{
enable = true,
-- if false, only the scrollbar thumb will be shown
background = true
}
<

- `opts.menu.win_configs`: `table<string, dropbar_menu_win_config_opts_t>`
- Window configurations for the menu, see `:h nvim_open_win()`
- Each config key in `opts.menu.win_configs` accepts either a plain value
Expand Down
14 changes: 12 additions & 2 deletions lua/dropbar/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ M.opts = {
right = 1,
},
},
-- Menu scrollbar options
scrollbar = {
enable = true,
-- The background / gutter of the scrollbar
-- When false, only the thumb is shown.
background = true,
},
---@type table<string, string|function|table<string, string|function>>
keymaps = {
['<LeftMouse>'] = function()
Expand Down Expand Up @@ -281,8 +288,11 @@ M.opts = {
---@param menu dropbar_menu_t
col = function(menu)
if menu.prev_menu then
return menu.prev_menu._win_configs.width
+ (menu.prev_menu.scrollbar and 1 or 0)
local offset = 0
if menu.prev_menu.scrollbar and M.opts.menu.scrollbar.background then
offset = 1
end
return menu.prev_menu._win_configs.width + offset
end
local mouse = vim.fn.getmousepos()
local bar = utils.bar.get({ win = menu.prev_win })
Expand Down
39 changes: 24 additions & 15 deletions lua/dropbar/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ end
---@field prev_cursor integer[]? previous cursor position
---@field symbol_previewed dropbar_symbol_t? symbol being previewed
---@field fzf_state fzf_state_t? fuzzy-finding state, or nil if not currently fuzzy-finding
---@field scrollbar { thumb: integer, sbar: integer }? scrollbar window handlers
---@field scrollbar { thumb: integer, background: integer }? scrollbar window handlers
local dropbar_menu_t = {}
dropbar_menu_t.__index = dropbar_menu_t

Expand Down Expand Up @@ -538,6 +538,7 @@ function dropbar_menu_t:update_scrollbar()
or not self.buf
or not vim.api.nvim_win_is_valid(self.win)
or not vim.api.nvim_buf_is_valid(self.buf)
or not configs.opts.menu.scrollbar.enable
then
return
end
Expand Down Expand Up @@ -568,26 +569,31 @@ function dropbar_menu_t:update_scrollbar()
self.scrollbar = {}

local win_configs = {
row = 0,
col = menu_win_configs.width,
row = offset,
col = menu_win_configs.width
- (configs.opts.menu.scrollbar.background and 0 or 1),
width = 1,
height = menu_win_configs.height,
style = 'minimal',
border = 'none',
relative = 'win',
win = self.win,
zindex = menu_win_configs.zindex,
zindex = menu_win_configs.zindex
+ (configs.opts.menu.scrollbar.background and 0 or 1),
}
self.scrollbar.sbar = vim.api.nvim_open_win(
vim.api.nvim_create_buf(false, true),
false,
win_configs
)
vim.wo[self.scrollbar.sbar].winhl = 'NormalFloat:DropBarMenuSbar'

win_configs.row = offset
win_configs.height = thumb_height
win_configs.zindex = menu_win_configs.zindex + 1
if configs.opts.menu.scrollbar.background then
win_configs.row = 0
win_configs.height = menu_win_configs.height
win_configs.zindex = win_configs.zindex - 1
self.scrollbar.background = vim.api.nvim_open_win(
vim.api.nvim_create_buf(false, true),
false,
win_configs
)
vim.wo[self.scrollbar.background].winhl = 'NormalFloat:DropBarMenuSbar'
end

self.scrollbar.thumb = vim.api.nvim_open_win(
vim.api.nvim_create_buf(false, true),
false,
Expand All @@ -607,8 +613,11 @@ function dropbar_menu_t:close_scrollbar()
if vim.api.nvim_win_is_valid(self.scrollbar.thumb) then
vim.api.nvim_win_close(self.scrollbar.thumb, true)
end
if vim.api.nvim_win_is_valid(self.scrollbar.sbar) then
vim.api.nvim_win_close(self.scrollbar.sbar, true)
if
self.scrollbar.background
and vim.api.nvim_win_is_valid(self.scrollbar.background)
then
vim.api.nvim_win_close(self.scrollbar.background, true)
end
self.scrollbar = nil
end
Expand Down

0 comments on commit 034efbe

Please sign in to comment.