From b2d121aa18e2cac8df4e2d3c5f18872ece294757 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 18 Jan 2016 02:35:28 -0500 Subject: [PATCH] widget.base: Add ':index(w, r)' method This function is optionally recursive. This allow to manipulate abstract layouts without really having to care about the exact hierarchy. --- lib/wibox/widget/base.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index d79642df..5e324207 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -99,6 +99,30 @@ function base.widget:get_all_children() return ret end +--- Get a widex index +-- @param widget The widget to look for +-- @param[opt] recursive Also check sub-widgets +-- @param[opt] ... Aditional widgets to add at the end of the "path" +-- @return The index +-- @return The parent layout +-- @return The path between "self" and "widget" +function base.widget:index(widget, recursive, ...) + local widgets = self:get_children() + + for idx, w in ipairs(widgets) do + if w == widget then + return idx, self, {...} + elseif recursive then + local idx, l, path = w:index(widget, true, self, ...) + if idx and l then + return idx, l, path + end + end + end + + return nil, self, {} +end + -- }}} -- {{{ Caches