diff --git a/README.md b/README.md index 5e12e756..ab6cdd9d 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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) @@ -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[`dropbar_symbol_t`](#dropbar_symbol_t) 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 | diff --git a/doc/dropbar.txt b/doc/dropbar.txt index 49920e5a..76bfb8ed 100644 --- a/doc/dropbar.txt +++ b/doc/dropbar.txt @@ -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() @@ -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, @@ -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() diff --git a/lua/dropbar/configs.lua b/lua/dropbar/configs.lua index 5a6ffb06..e8ed8026 100644 --- a/lua/dropbar/configs.lua +++ b/lua/dropbar/configs.lua @@ -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, @@ -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() diff --git a/lua/dropbar/menu.lua b/lua/dropbar/menu.lua index 410d266c..72e505cf 100644 --- a/lua/dropbar/menu.lua +++ b/lua/dropbar/menu.lua @@ -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 @@ -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