Skip to content

Commit

Permalink
feat(menu): add new method root() to find root menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekaboo committed Jan 17, 2025
1 parent 415701d commit 7b8b13a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 41 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,7 @@ appearance of the fuzzy finder interface.
or mouse.column <= 0
or mouse.winrow > #menu.entries
then
-- Find the root menu
while menu and menu.prev_menu do
menu = menu.prev_menu
end
menu = menu:root() --[[@as dropbar_menu_t]]
if menu then
menu:finish_preview(true)
menu:update_hover_hl()
Expand Down Expand Up @@ -958,9 +955,9 @@ the symbols:
}),
sym:merge({
on_click = function()
local current_menu = symbol.menu
while current_menu and current_menu.prev_menu do
current_menu = current_menu.prev_menu
local root_menu = symbol.menu and symbol.menu:root()
if root_menu then
root_menu:close(false)
end
if current_menu then
current_menu:close(false)
Expand Down Expand Up @@ -1774,6 +1771,7 @@ Declared and defined in [`lua/dropbar/menu.lua`](lua/dropbar/menu.lua).
| ------ | ------ |
| `dropbar_menu_t:new(opts: dropbar_menu_t?): dropbar_menu_t` | constructor of `dropbar_menu_t` |
| `dropbar_menu_t:del()` | destructor of `dropbar_menu_t` |
| `dropbar_menu_t:root(): dropbar_menu_t?` | get the root menu (menu without `prev_menu`) |
| `dropbar_menu_t:eval_win_configs()` | evaluate window configurations `dropbar_menu_t.win_configs` and store the result in `dropbar_menu_t._win_configs` |
| `dropbar_menu_t:get_component_at(pos: integer[], look_ahead: boolean?): dropbar_symbol_t?, { start: integer, end: integer }?` | get the component<sub>[`dropbar_symbol_t`](#dropbar_symbol_t)</sub> at position `pos` and its range it occupies in the entry it belongs to |
| `dropbar_menu_t:click_at(pos: integer[], min_width: integer?, n_clicks: integer?, button: string?, modifiers: string?)` | simulate a click at `pos` in the menu |
Expand Down
21 changes: 11 additions & 10 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,7 @@ appearance of the fuzzy finder interface.
or mouse.column <= 0
or mouse.winrow > #menu.entries
then
-- Find the root menu
while menu and menu.prev_menu do
menu = menu.prev_menu
end
menu = menu:root() --[[@as dropbar_menu_t]]
if menu then
menu:finish_preview(true)
menu:update_hover_hl()
Expand Down Expand Up @@ -887,12 +884,9 @@ the symbols:
}),
sym:merge({
on_click = function()
local current_menu = symbol.menu
while current_menu and current_menu.prev_menu do
current_menu = current_menu.prev_menu
end
if current_menu then
current_menu:close(false)
local root_menu = symbol.menu and symbol.menu:root()
if root_menu then
root_menu:close(false)
end
sym:jump()
end,
Expand Down Expand Up @@ -2242,6 +2236,13 @@ dropbar_menu_t:del() *dropbar_menu_t:del()*

Destructor of `dropbar_menu_t`

dropbar_menu_t:root() *dropbar_menu_t:root()*

Get the root menu (menu without `prev_menu`)

Returns ~
(`dropbar_menu_t`?): the new menu

*dropbar_menu_t:eval_win_configs()*
dropbar_menu_t:eval_win_configs()

Expand Down
14 changes: 4 additions & 10 deletions lua/dropbar/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,9 @@ M.opts = {
}),
sym:merge({
on_click = function()
local current_menu = symbol.menu
while current_menu and current_menu.prev_menu do
current_menu = current_menu.prev_menu
end
if current_menu then
current_menu:close(false)
local root_menu = symbol.menu and symbol.menu:root()
if root_menu then
root_menu:close(false)
end
sym:jump()
end,
Expand Down Expand Up @@ -649,10 +646,7 @@ M.opts = {
or mouse.column <= 0
or mouse.winrow > (#menu.entries + 1)
then
-- Find the root menu
while menu and menu.prev_menu do
menu = menu.prev_menu
end
menu = menu:root() --[[@as dropbar_menu_t]]
if menu then
menu:finish_preview(true)
menu:update_hover_hl()
Expand Down
24 changes: 10 additions & 14 deletions lua/dropbar/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ function dropbar_menu_t:del()
end
end

---Retrieves the root menu (first menu opened from winbar)
---@return dropbar_menu_t?
function dropbar_menu_t:root()
local current = self
while current and current.prev_menu do
current = current.prev_menu
end
return current
end

---Evaluate window configurations
---Side effects: update self._win_configs
---@return nil
Expand Down Expand Up @@ -304,20 +314,6 @@ function dropbar_menu_t:click_at(pos, min_width, n_clicks, button, modifiers)
end
end

---Retrieves the root window of the menu.
---If `self.prev_menu` is nil then this `self.prev_win`.
---Otherwise, it is the root window of `self.prev_menu`.
---@return integer?
function dropbar_menu_t:root_win()
local current = self
local win = self.prev_win
while current and current.prev_menu do
win = current.prev_menu.prev_win
current = current.prev_menu
end
return win
end

---"Click" the component in the dropbar menu
---Side effects: update self.clicked_at
---@param symbol dropbar_symbol_t
Expand Down

0 comments on commit 7b8b13a

Please sign in to comment.