Skip to content

Commit

Permalink
feat(utils/window): per instance scrollbar config
Browse files Browse the repository at this point in the history
fixes #113
  • Loading branch information
max397574 committed Oct 21, 2024
1 parent 8ba3ccc commit 6a44585
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lua/care/menu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function Menu.new()
self.ns = vim.api.nvim_create_namespace("care")
self.config = require("care.config").options
self.index = 0
self.menu_window = require("care.utils.window").new()
self.docs_window = require("care.utils.window").new()
self.menu_window = require("care.utils.window").new({ scrollbar = self.config.ui.menu.scrollbar })
self.docs_window = require("care.utils.window").new({ scrollbar = self.config.ui.docs_view.scrollbar })
self.ghost_text = require("care.ghost_text").new()
self.reversed = false
return self
Expand Down
7 changes: 6 additions & 1 deletion lua/care/types/window.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
--- Utility class for working with windows in care
---@class care.window
--- Creates a new instance of the menu window
---@field new fun(): care.window
---@field new fun(config: care.window.config): care.window
---@field winnr? integer
--- Instance of the care config
---@field config care.config
---@field buf integer
--- Whether the window is currently opened above or below the cursor
---@field position? "above"|"below"
--- Configuration for the scrollbar of the window
---@field scrollbar_config care.config.scrollbar
--- Data for the scrollbar of the window
---@field scrollbar {win: integer, buf: integer}
--- The maximum available height where the window is currently open
Expand Down Expand Up @@ -49,3 +51,6 @@
---@field width_with_border integer
---@field height_with_border integer
---@field total_lines integer

---@class care.window.config
---@field scrollbar care.config.scrollbar
20 changes: 9 additions & 11 deletions lua/care/utils/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
---@diagnostic disable-next-line: missing-fields
local Window = {}

function Window.new()
function Window.new(config)
---@type care.window
local self = setmetatable({}, { __index = Window })
self.winnr = nil
Expand All @@ -15,6 +15,7 @@ function Window.new()
self.scrollbar.win = nil
self.max_height = nil
self.current_scroll = 1
self.scrollbar_config = config.scrollbar
self.scrollbar.buf = vim.api.nvim_create_buf(false, true)
return self
end
Expand Down Expand Up @@ -49,20 +50,20 @@ function Window:open_cursor_relative(width, wanted_height, offset, config)

local needed_space = math.min(needed_height, config.max_height)
local position = "below"
local config_position = config.position
local config_pos = config.position
local height
if config_position == "auto" then
if config_pos == "auto" then
if space_below < needed_space then
position = "above"
if space_above < needed_space then
position = space_above > space_below and "above" or "below"
end
end
height = math.min(wanted_height, (position == "below" and space_below or space_above) - border_space)
elseif config_position == "below" then
elseif config_pos == "below" then
position = "below"
height = math.min(wanted_height, space_below - border_space)
elseif config_position == "above" then
elseif config_pos == "above" then
position = "above"
height = math.min(wanted_height, space_above - border_space)
end
Expand Down Expand Up @@ -207,8 +208,7 @@ function Window:open_scrollbar_win(width, height)
self.scrollbar.win = nil
end
local menu_pos = vim.api.nvim_win_get_position(self.winnr)
-- TODO: check correct config here, can also be docs_view
if self.config.ui.menu.scrollbar.enabled then
if self.scrollbar_config.enabled then
self.scrollbar.win = vim.api.nvim_open_win(self.scrollbar.buf, false, {
height = height,
relative = "cursor",
Expand Down Expand Up @@ -248,8 +248,7 @@ function Window:draw_scrollbar()

for i = 1, scrollbar_height do
vim.api.nvim_buf_set_extmark(self.scrollbar.buf, self.ns, i - 1, 0, {
-- TODO: check correct config here, can also be docs_view
virt_text = { { self.config.ui.menu.scrollbar.character, "@care.scrollbar.thumb" } },
virt_text = { { self.scrollbar_config.character, "@care.scrollbar.thumb" } },
virt_text_pos = "overlay",
})
end
Expand All @@ -266,8 +265,7 @@ function Window:draw_scrollbar()
width = 1,
height = scrollbar_height,
row = menu_pos_NE[1] + scrollbar_offset + (win_data.has_border and 1 or 0),
-- TODO: check correct config here, can also be docs_view
col = menu_pos_NE[2] + self.config.ui.menu.scrollbar.offset + 1,
col = menu_pos_NE[2] + self.scrollbar_config.offset + 1,
hide = false,
})
end
Expand Down

0 comments on commit 6a44585

Please sign in to comment.