layout: Add separator widget support to 3 layouts

The ratio, fixed and flex layout can now display a widget between
each layout elements.

The align layout was left out because it doesn't support spacing
This commit is contained in:
Emmanuel Lepage Vallee 2017-08-07 16:12:29 -04:00
parent a9c06fb8c5
commit e12c000b97
3 changed files with 96 additions and 10 deletions

View File

@ -23,10 +23,15 @@ local fixed = {}
function fixed:layout(context, width, height) function fixed:layout(context, width, height)
local result = {} local result = {}
local pos,spacing = 0, self._private.spacing 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 for k, v in pairs(self._private.widgets) do
local x, y, w, h, _ local x, y, w, h, _
if self._private.dir == "y" then if is_y then
x, y = 0, pos x, y = 0, pos
w, h = width, height - pos w, h = width, height - pos
if k ~= #self._private.widgets or not self._private.fill_space then 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 pos = pos + w + spacing
end end
if (self._private.dir == "y" and pos-spacing > height) or if (is_y and pos-spacing > height) or
(self._private.dir ~= "y" and pos-spacing > width) then (is_x and pos-spacing > width) then
break break
end 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)) table.insert(result, base.place_widget_at(v, x, y, w, h))
end end
return result return result
@ -194,6 +207,20 @@ function fixed:set(index, widget2)
return true return true
end 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` --- Insert a new widget in the layout at position `index`
-- **Signal:** widget::inserted The arguments are the widget and the index -- **Signal:** widget::inserted The arguments are the widget and the index
-- @tparam number index The position -- @tparam number index The position
@ -308,7 +335,10 @@ function fixed.vertical(...)
return get_layout("y", ...) return get_layout("y", ...)
end end
--- Add spacing between each layout widgets --- Add spacing between each layout widgets.
--
--@DOC_wibox_layout_fixed_spacing_EXAMPLE@
--
-- @property spacing -- @property spacing
-- @tparam number spacing Spacing between widgets. -- @tparam number spacing Spacing between widgets.

View File

@ -50,14 +50,35 @@ local flex = {}
-- @name insert -- @name insert
-- @class function -- @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) function flex:layout(_, width, height)
local result = {} local result = {}
local pos,spacing = 0, self._private.spacing local pos,spacing = 0, self._private.spacing
local num = #self._private.widgets local num = #self._private.widgets
local total_spacing = (spacing*(num-1)) 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 local space_per_item
if self._private.dir == "y" then if is_y then
space_per_item = height / num - total_spacing/num space_per_item = height / num - total_spacing/num
else else
space_per_item = width / num - total_spacing/num 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) space_per_item = math.min(space_per_item, self._private.max_widget_size)
end end
for _, v in pairs(self._private.widgets) do for k, v in pairs(self._private.widgets) do
local x, y, w, h local x, y, w, h
if self._private.dir == "y" then if is_y then
x, y = 0, gmath.round(pos) x, y = 0, gmath.round(pos)
w, h = width, floor(space_per_item) w, h = width, floor(space_per_item)
else else
@ -81,10 +102,17 @@ function flex:layout(_, width, height)
pos = pos + space_per_item + spacing pos = pos + space_per_item + spacing
if (self._private.dir == "y" and pos-spacing >= height) or if (is_y and pos-spacing >= height) or
(self._private.dir ~= "y" and pos-spacing >= width) then (is_x and pos-spacing >= width) then
break break
end 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 end
return result return result

View File

@ -22,6 +22,22 @@ local ratio = {}
--@DOC_fixed_COMMON@ --@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) -- Compute the sum of all ratio (ideally, it should be 1)
local function gen_sum(self, i_s, i_e) local function gen_sum(self, i_s, i_e)
local sum, new_w = 0,0 local sum, new_w = 0,0
@ -86,6 +102,11 @@ function ratio:layout(context, width, height)
local has_stragety = strategy ~= "default" local has_stragety = strategy ~= "default"
local to_redistribute, void_count = 0, 0 local to_redistribute, void_count = 0, 0
local dir = self._private.dir or "x" 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 for k, v in ipairs(self._private.widgets) do
local space, is_void local space, is_void
@ -150,7 +171,7 @@ function ratio:layout(context, width, height)
-- Only the `justify` strategy changes the original widget size. -- Only the `justify` strategy changes the original widget size.
to_redistribute = (strategy == "justify") and to_redistribute or 0 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) local v, x, y, w, h, is_void = unpack(entry)
-- Redistribute the space or move the widgets -- Redistribute the space or move the widgets
@ -167,6 +188,13 @@ function ratio:layout(context, width, height)
end end
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)) table.insert(result, base.place_widget_at(v, x, y, w, h))
end end