diff --git a/lib/wibox/layout/fixed.lua b/lib/wibox/layout/fixed.lua index 4d7fac69..95b91e6a 100644 --- a/lib/wibox/layout/fixed.lua +++ b/lib/wibox/layout/fixed.lua @@ -23,10 +23,15 @@ local fixed = {} function fixed:layout(context, width, height) local result = {} local pos,spacing = 0, self._private.spacing + local spacing_widget = self._private.spacing_widget + local is_y = self._private.dir == "y" + local is_x = not is_y + local abspace = math.abs(spacing) + local spoffset = spacing < 0 and 0 or spacing for k, v in pairs(self._private.widgets) do local x, y, w, h, _ - if self._private.dir == "y" then + if is_y then x, y = 0, pos w, h = width, height - pos if k ~= #self._private.widgets or not self._private.fill_space then @@ -42,10 +47,18 @@ function fixed:layout(context, width, height) pos = pos + w + spacing end - if (self._private.dir == "y" and pos-spacing > height) or - (self._private.dir ~= "y" and pos-spacing > width) then + if (is_y and pos-spacing > height) or + (is_x and pos-spacing > width) then break end + + -- Add the spacing widget + if k > 1 and abspace > 0 and spacing_widget then + table.insert(result, base.place_widget_at( + spacing_widget, is_x and (x - spoffset) or x, is_y and (y - spoffset) or y, + is_x and abspace or w, is_y and abspace or h + )) + end table.insert(result, base.place_widget_at(v, x, y, w, h)) end return result @@ -194,6 +207,20 @@ function fixed:set(index, widget2) return true end +--- The widget used to fill the spacing between the layout elements. +-- +-- By default, no widget is used. +-- +--@DOC_wibox_layout_fixed_spacing_widget_EXAMPLE@ +-- +-- @property spacing_widget +-- @param widget + +function fixed:set_spacing_widget(wdg) + self._private.spacing_widget = base.make_widget_from_value(wdg) + self:emit_signal("widget::layout_changed") +end + --- Insert a new widget in the layout at position `index` -- **Signal:** widget::inserted The arguments are the widget and the index -- @tparam number index The position @@ -308,7 +335,10 @@ function fixed.vertical(...) return get_layout("y", ...) end ---- Add spacing between each layout widgets +--- Add spacing between each layout widgets. +-- +--@DOC_wibox_layout_fixed_spacing_EXAMPLE@ +-- -- @property spacing -- @tparam number spacing Spacing between widgets. diff --git a/lib/wibox/layout/flex.lua b/lib/wibox/layout/flex.lua index e66896f8..797705e7 100644 --- a/lib/wibox/layout/flex.lua +++ b/lib/wibox/layout/flex.lua @@ -50,14 +50,35 @@ local flex = {} -- @name insert -- @class function +--- The widget used to fill the spacing between the layout elements. +-- +-- By default, no widget is used. +-- +--@DOC_wibox_layout_flex_spacing_widget_EXAMPLE@ +-- +-- @property spacing_widget +-- @param widget + +--- Add spacing between each layout widgets. +-- +--@DOC_wibox_layout_flex_spacing_EXAMPLE@ +-- +-- @property spacing +-- @tparam number spacing Spacing between widgets. + function flex:layout(_, width, height) local result = {} local pos,spacing = 0, self._private.spacing local num = #self._private.widgets local total_spacing = (spacing*(num-1)) + local spacing_widget = self._private.spacing_widget + local abspace = math.abs(spacing) + local spoffset = spacing < 0 and 0 or spacing + local is_y = self._private.dir == "y" + local is_x = not is_y local space_per_item - if self._private.dir == "y" then + if is_y then space_per_item = height / num - total_spacing/num else space_per_item = width / num - total_spacing/num @@ -67,9 +88,9 @@ function flex:layout(_, width, height) space_per_item = math.min(space_per_item, self._private.max_widget_size) end - for _, v in pairs(self._private.widgets) do + for k, v in pairs(self._private.widgets) do local x, y, w, h - if self._private.dir == "y" then + if is_y then x, y = 0, gmath.round(pos) w, h = width, floor(space_per_item) else @@ -81,10 +102,17 @@ function flex:layout(_, width, height) pos = pos + space_per_item + spacing - if (self._private.dir == "y" and pos-spacing >= height) or - (self._private.dir ~= "y" and pos-spacing >= width) then + if (is_y and pos-spacing >= height) or + (is_x and pos-spacing >= width) then break end + + if k > 1 and spacing ~= 0 and spacing_widget then + table.insert(result, base.place_widget_at( + spacing_widget, is_x and (x - spoffset) or x, is_y and (y - spoffset) or y, + is_x and abspace or w, is_y and abspace or h + )) + end end return result diff --git a/lib/wibox/layout/ratio.lua b/lib/wibox/layout/ratio.lua index a3a30f82..04828a6a 100644 --- a/lib/wibox/layout/ratio.lua +++ b/lib/wibox/layout/ratio.lua @@ -22,6 +22,22 @@ local ratio = {} --@DOC_fixed_COMMON@ +--- The widget used to fill the spacing between the layout elements. +-- +-- By default, no widget is used. +-- +--@DOC_wibox_layout_ratio_spacing_widget_EXAMPLE@ +-- +-- @property spacing_widget +-- @param widget + +--- Add spacing between each layout widgets. +-- +--@DOC_wibox_layout_ratio_spacing_EXAMPLE@ +-- +-- @property spacing +-- @tparam number spacing Spacing between widgets. + -- Compute the sum of all ratio (ideally, it should be 1) local function gen_sum(self, i_s, i_e) local sum, new_w = 0,0 @@ -86,6 +102,11 @@ function ratio:layout(context, width, height) local has_stragety = strategy ~= "default" local to_redistribute, void_count = 0, 0 local dir = self._private.dir or "x" + local spacing_widget = self._private.spacing_widget + local abspace = math.abs(spacing) + local spoffset = spacing < 0 and 0 or spacing + local is_y = self._private.dir == "y" + local is_x = not is_y for k, v in ipairs(self._private.widgets) do local space, is_void @@ -150,7 +171,7 @@ function ratio:layout(context, width, height) -- Only the `justify` strategy changes the original widget size. to_redistribute = (strategy == "justify") and to_redistribute or 0 - for _, entry in ipairs(preliminary_results) do + for k, entry in ipairs(preliminary_results) do local v, x, y, w, h, is_void = unpack(entry) -- Redistribute the space or move the widgets @@ -167,6 +188,13 @@ function ratio:layout(context, width, height) end end + if k > 1 and abspace > 0 and spacing_widget then + table.insert(result, base.place_widget_at( + spacing_widget, is_x and (x - spoffset) or x, is_y and (y - spoffset) or y, + is_x and abspace or w, is_y and abspace or h + )) + end + table.insert(result, base.place_widget_at(v, x, y, w, h)) end