From e36f23171bfc7147020d93bbc8c111c1aa9fc25d Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Thu, 4 Feb 2016 01:11:10 -0500 Subject: [PATCH] wibox.widget: Add `set_children` method Provide a generic method to set the widget content --- lib/wibox/layout/align.lua | 14 ++++++++++++++ lib/wibox/layout/constraint.lua | 8 ++++++++ lib/wibox/layout/fixed.lua | 8 ++++++++ lib/wibox/layout/flex.lua | 5 +++++ lib/wibox/layout/margin.lua | 8 ++++++++ lib/wibox/layout/mirror.lua | 8 ++++++++ lib/wibox/layout/rotate.lua | 8 ++++++++ lib/wibox/layout/scroll.lua | 8 ++++++++ lib/wibox/widget/background.lua | 8 ++++++++ lib/wibox/widget/base.lua | 8 ++++++++ 10 files changed, 83 insertions(+) diff --git a/lib/wibox/layout/align.lua b/lib/wibox/layout/align.lua index bc7635cd2..95337aa80 100644 --- a/lib/wibox/layout/align.lua +++ b/lib/wibox/layout/align.lua @@ -171,6 +171,17 @@ function align:get_children() return util.from_sparse {self.first, self.second, self.third} end +--- Replace the layout children +-- This layout only accept three children, all others will be ignored +-- @tparam table children A table composed of valid widgets +function align:set_children(children) + if not children then return self:reset() end + self.first = children[1] + self.second = children[2] + self.third = children[3] + self:emit_signal("widget::layout_changed") +end + --- Fit the align layout into the given space. The align layout will -- ask for the sum of the sizes of its sub-widgets in its direction -- and the largest sized sub widget in the other direction. @@ -243,6 +254,9 @@ local function get_layout(dir, first, second, third) ret:set_second(second) ret:set_third(third) + -- An align layout allow set_children to have empty entries + ret.allow_empty_widget = true + return ret end diff --git a/lib/wibox/layout/constraint.lua b/lib/wibox/layout/constraint.lua index 32aa06a57..9fd4c1d2a 100644 --- a/lib/wibox/layout/constraint.lua +++ b/lib/wibox/layout/constraint.lua @@ -50,6 +50,14 @@ function constraint:get_children() return {self.widget} end +--- Replace the layout children +-- This layout only accept one children, all others will be ignored +-- @tparam table children A table composed of valid widgets +function constraint:set_children(children) + self.widget = children and children[1] + self:emit_signal("widget::layout_changed") +end + --- Set the strategy to use for the constraining. Valid values are 'max', -- 'min' or 'exact'. Throws an error on invalid values. function constraint:set_strategy(val) diff --git a/lib/wibox/layout/fixed.lua b/lib/wibox/layout/fixed.lua index 291c96e3e..0611effd8 100644 --- a/lib/wibox/layout/fixed.lua +++ b/lib/wibox/layout/fixed.lua @@ -110,6 +110,14 @@ function fixed:get_children() return self.widgets end +--- Replace the layout children +-- @tparam table children A table composed of valid widgets +function fixed:set_children(children) + if not children then return self:reset() end + self.widgets = children + self:emit_signal("widget::layout_changed") +end + --- Replace the first instance of `widget` in the layout with `widget2` -- @param widget The widget to replace -- @param widget2 The widget to replace `widget` with diff --git a/lib/wibox/layout/flex.lua b/lib/wibox/layout/flex.lua index a6312e71d..9c0a4b544 100644 --- a/lib/wibox/layout/flex.lua +++ b/lib/wibox/layout/flex.lua @@ -31,6 +31,11 @@ local flex = {} -- @name get_children -- @class function +--- Replace the layout children +-- @tparam table children A table composed of valid widgets +-- @name set_children +-- @class function + --- Add some widgets to the given fixed layout -- @param layout The layout you are modifying. -- @tparam widget ... Widgets that should be added (must at least be one) diff --git a/lib/wibox/layout/margin.lua b/lib/wibox/layout/margin.lua index 1666f85fa..e9b97e752 100644 --- a/lib/wibox/layout/margin.lua +++ b/lib/wibox/layout/margin.lua @@ -78,6 +78,14 @@ function margin:get_children() return {self.widget} end +--- Replace the layout children +-- This layout only accept one children, all others will be ignored +-- @tparam table children A table composed of valid widgets +function margin:set_children(children) + self.widget = children and children[1] + self:emit_signal("widget::layout_changed") +end + --- Set all the margins to val. function margin:set_margins(val) self.left = val diff --git a/lib/wibox/layout/mirror.lua b/lib/wibox/layout/mirror.lua index c98e407fc..a0975459e 100644 --- a/lib/wibox/layout/mirror.lua +++ b/lib/wibox/layout/mirror.lua @@ -64,6 +64,14 @@ function mirror:get_children() return {self.widget} end +--- Replace the layout children +-- This layout only accept one children, all others will be ignored +-- @tparam table children A table composed of valid widgets +function mirror:set_children(children) + self.widget = children and children[1] + self:emit_signal("widget::layout_changed") +end + --- Reset this layout. The widget will be removed and the axes reset. function mirror:reset() self.horizontal = false diff --git a/lib/wibox/layout/rotate.lua b/lib/wibox/layout/rotate.lua index 5d5d3b668..616d2564e 100644 --- a/lib/wibox/layout/rotate.lua +++ b/lib/wibox/layout/rotate.lua @@ -72,6 +72,14 @@ function rotate:get_children() return {self.widget} end +--- Replace the layout children +-- This layout only accept one children, all others will be ignored +-- @tparam table children A table composed of valid widgets +function rotate:set_children(children) + self.widget = children and children[1] + self:emit_signal("widget::layout_changed") +end + --- Reset this layout. The widget will be removed and the rotation reset. function rotate:reset() self.direction = nil diff --git a/lib/wibox/layout/scroll.lua b/lib/wibox/layout/scroll.lua index 44165ea51..d443a4a4d 100644 --- a/lib/wibox/layout/scroll.lua +++ b/lib/wibox/layout/scroll.lua @@ -267,6 +267,14 @@ function scroll:get_children() return {self.widget} end +--- Replace the layout children +-- This layout only accept one children, all others will be ignored +-- @tparam table children A table composed of valid widgets +function scroll:set_children(children) + self.widget = children and children[1] + self:emit_signal("widget::layout_changed") +end + --- Specify the expand mode that is used for extra space. -- @tparam boolean expand If true, the widget is expanded to include the extra -- space. If false, the extra space is simply left empty. diff --git a/lib/wibox/widget/background.lua b/lib/wibox/widget/background.lua index 8ea11954a..a31616a8d 100644 --- a/lib/wibox/widget/background.lua +++ b/lib/wibox/widget/background.lua @@ -101,6 +101,14 @@ function background:get_children() return {self.widget} end +--- Replace the layout children +-- This layout only accept one children, all others will be ignored +-- @tparam table children A table composed of valid widgets +function background:set_children(children) + self.widget = children and children[1] + self:emit_signal("widget::layout_changed") +end + --- Set the background to use function background:set_bg(bg) if bg then diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index 5e324207d..c7ea5acd7 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -80,6 +80,14 @@ function base.widget:get_children() return {} end +--- Replace the layout children +-- The default implementation does nothing, this must be re-implemented by +-- all layout and container widgets. +-- @tparam table children A table composed of valid widgets +function base.widget:set_children(children) + -- Nothing on purpose +end + -- It could have been merged into `get_all_children`, but it's not necessary local function digg_children(ret, tlw) for k, w in ipairs(tlw:get_children()) do