2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
2015-02-25 11:18:53 +01:00
|
|
|
-- @classmod wibox.layout.align
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local table = table
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
2014-01-02 09:09:08 +01:00
|
|
|
local floor = math.floor
|
2015-06-14 15:57:33 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
local align = {}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-06-14 16:03:29 +02:00
|
|
|
--- Calculate the layout of an align layout.
|
2015-08-08 13:18:54 +02:00
|
|
|
-- @param context The context in which we are drawn.
|
2013-01-05 16:12:47 +01:00
|
|
|
-- @param width The available width.
|
|
|
|
-- @param height The available height.
|
2015-06-14 16:03:29 +02:00
|
|
|
function align:layout(context, width, height)
|
|
|
|
local result = {}
|
|
|
|
|
2014-01-04 20:10:01 +01:00
|
|
|
-- Draw will have to deal with all three align modes and should work in a
|
|
|
|
-- way that makes sense if one or two of the widgets are missing (if they
|
|
|
|
-- are all missing, it won't draw anything.) It should also handle the case
|
|
|
|
-- where the fit something that isn't set to expand (for instance the
|
|
|
|
-- outside widgets when the expand mode is "inside" or any of the widgets
|
|
|
|
-- when the expand mode is "none" wants to take up more space than is
|
|
|
|
-- allowed.
|
2010-10-06 12:42:56 +02:00
|
|
|
local size_first = 0
|
2014-01-04 20:10:01 +01:00
|
|
|
-- start with all the space given by the parent, subtract as we go along
|
2014-01-02 09:09:08 +01:00
|
|
|
local size_remains = self.dir == "y" and height or width
|
2015-08-13 15:42:39 +02:00
|
|
|
-- This is only set & used if expand ~= "inside" and we have second width.
|
|
|
|
-- It contains the size allocated to the second widget.
|
|
|
|
local size_second
|
2014-01-02 09:09:08 +01:00
|
|
|
|
2014-01-04 20:10:01 +01:00
|
|
|
-- we will prioritize the middle widget unless the expand mode is "inside"
|
|
|
|
-- if it is, we prioritize the first widget by not doing this block also,
|
|
|
|
-- if the second widget doesn't exist, we will prioritise the first one
|
|
|
|
-- instead
|
2014-01-02 09:09:08 +01:00
|
|
|
if self._expand ~= "inside" and self.second then
|
2015-09-17 15:01:50 +02:00
|
|
|
local w, h = base.fit_widget(self, context, self.second, width, height)
|
2015-08-13 15:42:39 +02:00
|
|
|
size_second = self.dir == "y" and h or w
|
2014-01-04 20:10:01 +01:00
|
|
|
-- if all the space is taken, skip the rest, and draw just the middle
|
|
|
|
-- widget
|
2014-01-02 09:09:08 +01:00
|
|
|
if size_second >= size_remains then
|
2015-06-14 15:57:33 +02:00
|
|
|
return { base.place_widget_at(self.second, 0, 0, width, height) }
|
2014-01-02 09:09:08 +01:00
|
|
|
else
|
2014-01-04 20:10:01 +01:00
|
|
|
-- the middle widget is sized first, the outside widgets are given
|
|
|
|
-- the remaining space if available we will draw later
|
2014-01-02 09:09:08 +01:00
|
|
|
size_remains = floor((size_remains - size_second) / 2)
|
|
|
|
end
|
|
|
|
end
|
2013-01-05 16:12:47 +01:00
|
|
|
if self.first then
|
2012-06-16 21:12:26 +02:00
|
|
|
local w, h, _ = width, height, nil
|
2014-01-04 20:10:01 +01:00
|
|
|
-- we use the fit function for the "inside" and "none" modes, but
|
|
|
|
-- ignore it for the "outside" mode, which will force it to expand
|
|
|
|
-- into the remaining space
|
|
|
|
if self._expand ~= "outside" then
|
2014-01-02 09:09:08 +01:00
|
|
|
if self.dir == "y" then
|
2015-09-17 15:01:50 +02:00
|
|
|
_, h = base.fit_widget(self, context, self.first, width, size_remains)
|
2014-01-02 09:09:08 +01:00
|
|
|
size_first = h
|
2014-01-04 20:10:01 +01:00
|
|
|
-- for "inside", the third widget will get a chance to use the
|
|
|
|
-- remaining space, then the middle widget. For "none" we give
|
|
|
|
-- the third widget the remaining space if there was no second
|
|
|
|
-- widget to take up any space (as the first if block is skipped
|
|
|
|
-- if this is the case)
|
2014-01-02 09:09:08 +01:00
|
|
|
if self._expand == "inside" or not self.second then
|
|
|
|
size_remains = size_remains - h
|
|
|
|
end
|
|
|
|
else
|
2015-09-17 15:01:50 +02:00
|
|
|
w, _ = base.fit_widget(self, context, self.first, size_remains, height)
|
2014-01-02 09:09:08 +01:00
|
|
|
size_first = w
|
|
|
|
if self._expand == "inside" or not self.second then
|
|
|
|
size_remains = size_remains - w
|
|
|
|
end
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
else
|
2014-01-02 09:09:08 +01:00
|
|
|
if self.dir == "y" then
|
|
|
|
h = size_remains
|
|
|
|
else
|
|
|
|
w = size_remains
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2015-06-14 15:57:33 +02:00
|
|
|
table.insert(result, base.place_widget_at(self.first, 0, 0, w, h))
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2014-01-04 20:10:01 +01:00
|
|
|
-- size_remains will be <= 0 if first used all the space
|
2014-01-02 09:09:08 +01:00
|
|
|
if self.third and size_remains > 0 then
|
|
|
|
local w, h, _ = width, height, nil
|
|
|
|
if self._expand ~= "outside" then
|
|
|
|
if self.dir == "y" then
|
2015-09-17 15:01:50 +02:00
|
|
|
_, h = base.fit_widget(self, context, self.third, width, size_remains)
|
2014-01-04 20:10:01 +01:00
|
|
|
-- give the middle widget the rest of the space for "inside" mode
|
2014-01-02 09:09:08 +01:00
|
|
|
if self._expand == "inside" then
|
|
|
|
size_remains = size_remains - h
|
|
|
|
end
|
|
|
|
else
|
2015-09-17 15:01:50 +02:00
|
|
|
w, _ = base.fit_widget(self, context, self.third, size_remains, height)
|
2014-01-02 09:09:08 +01:00
|
|
|
if self._expand == "inside" then
|
|
|
|
size_remains = size_remains - w
|
|
|
|
end
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
else
|
2014-01-02 09:09:08 +01:00
|
|
|
if self.dir == "y" then
|
|
|
|
h = size_remains
|
|
|
|
else
|
|
|
|
w = size_remains
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2014-01-02 09:09:08 +01:00
|
|
|
local x, y = width - w, height - h
|
2015-06-14 15:57:33 +02:00
|
|
|
table.insert(result, base.place_widget_at(self.third, x, y, w, h))
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2014-01-04 20:10:01 +01:00
|
|
|
-- here we either draw the second widget in the space set aside for it
|
|
|
|
-- in the beginning, or in the remaining space, if it is "inside"
|
2014-01-02 09:09:08 +01:00
|
|
|
if self.second and size_remains > 0 then
|
|
|
|
local x, y, w, h = 0, 0, width, height
|
|
|
|
if self._expand == "inside" then
|
|
|
|
if self.dir == "y" then
|
|
|
|
h = size_remains
|
|
|
|
x, y = 0, size_first
|
|
|
|
else
|
|
|
|
w = size_remains
|
|
|
|
x, y = size_first, 0
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
else
|
2014-01-02 09:09:08 +01:00
|
|
|
if self.dir == "y" then
|
2015-09-17 15:01:50 +02:00
|
|
|
_, h = base.fit_widget(self, context, self.second, width, size_second)
|
2014-01-02 09:09:08 +01:00
|
|
|
y = floor( (height - h)/2 )
|
|
|
|
else
|
2015-09-17 15:01:50 +02:00
|
|
|
w, _ = base.fit_widget(self, context, self.second, size_second, height)
|
2014-01-02 09:09:08 +01:00
|
|
|
x = floor( (width -w)/2 )
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2015-06-14 15:57:33 +02:00
|
|
|
table.insert(result, base.place_widget_at(self.second, x, y, w, h))
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2015-06-14 16:03:29 +02:00
|
|
|
return result
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the layout's first widget. This is the widget that is at the left/top
|
2012-11-18 20:44:03 +01:00
|
|
|
function align:set_first(widget)
|
2015-06-21 14:53:16 +02:00
|
|
|
if self.first == widget then
|
|
|
|
return
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.first = widget
|
2015-06-14 16:03:29 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the layout's second widget. This is the centered one.
|
2012-11-18 20:44:03 +01:00
|
|
|
function align:set_second(widget)
|
2015-06-21 14:53:16 +02:00
|
|
|
if self.second == widget then
|
|
|
|
return
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.second = widget
|
2015-06-14 16:03:29 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the layout's third widget. This is the widget that is at the right/bottom
|
2012-11-18 20:44:03 +01:00
|
|
|
function align:set_third(widget)
|
2015-06-21 14:53:16 +02:00
|
|
|
if self.third == widget then
|
|
|
|
return
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.third = widget
|
2015-06-14 16:03:29 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2013-01-05 16:12:49 +01:00
|
|
|
--- Fit the align layout into the given space. The align layout will
|
2014-01-04 16:10:37 +01:00
|
|
|
-- ask for the sum of the sizes of its sub-widgets in its direction
|
|
|
|
-- and the largest sized sub widget in the other direction.
|
2015-08-08 13:43:35 +02:00
|
|
|
-- @param context The context in which we are fit.
|
2013-01-05 16:12:49 +01:00
|
|
|
-- @param orig_width The available width.
|
|
|
|
-- @param orig_height The available height.
|
2015-08-08 13:43:35 +02:00
|
|
|
function align:fit(context, orig_width, orig_height)
|
2014-01-04 16:10:37 +01:00
|
|
|
local used_in_dir = 0
|
|
|
|
local used_in_other = 0
|
2013-01-05 16:12:49 +01:00
|
|
|
|
|
|
|
for k, v in pairs{self.first, self.second, self.third} do
|
2015-09-17 15:01:50 +02:00
|
|
|
local w, h = base.fit_widget(self, context, v, orig_width, orig_height)
|
2013-01-05 16:12:49 +01:00
|
|
|
|
|
|
|
local max = self.dir == "y" and w or h
|
2014-01-04 16:10:37 +01:00
|
|
|
if max > used_in_other then
|
|
|
|
used_in_other = max
|
2013-01-05 16:12:49 +01:00
|
|
|
end
|
2014-01-04 16:10:37 +01:00
|
|
|
|
|
|
|
used_in_dir = used_in_dir + (self.dir == "y" and h or w)
|
2013-01-05 16:12:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if self.dir == "y" then
|
2014-01-04 16:10:37 +01:00
|
|
|
return used_in_other, used_in_dir
|
2013-01-05 16:12:49 +01:00
|
|
|
end
|
2014-01-04 16:10:37 +01:00
|
|
|
return used_in_dir, used_in_other
|
2013-01-05 16:12:49 +01:00
|
|
|
end
|
2015-06-14 16:03:29 +02:00
|
|
|
|
2014-01-02 09:09:08 +01:00
|
|
|
--- Set the expand mode which determines how sub widgets expand to take up
|
2015-10-14 00:21:29 +02:00
|
|
|
-- unused space.
|
|
|
|
--
|
|
|
|
-- @tparam[opt=inside] string mode How to use unused space.
|
|
|
|
--
|
|
|
|
-- * "inside" - Default option. Size of outside widgets is determined using
|
|
|
|
-- their fit function. Second, middle, or center widget expands to fill
|
|
|
|
-- remaining space.
|
|
|
|
-- * "outside" - Center widget is sized using its fit function and placed in
|
|
|
|
-- the center of the allowed space. Outside widgets expand (or contract) to
|
|
|
|
-- fill remaining space on their side.
|
|
|
|
-- * "none" - All widgets are sized using their fit function, drawn to only the
|
|
|
|
-- returned space, or remaining space, whichever is smaller. Center widget
|
|
|
|
-- gets priority.
|
2014-01-02 09:09:08 +01:00
|
|
|
function align:set_expand(mode)
|
|
|
|
if mode == "none" or mode == "outside" then
|
|
|
|
self._expand = mode
|
|
|
|
else
|
|
|
|
self._expand = "inside"
|
|
|
|
end
|
2015-06-14 16:03:29 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2014-01-02 09:09:08 +01:00
|
|
|
end
|
2013-01-05 16:12:49 +01:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function align:reset()
|
2010-10-06 12:42:56 +02:00
|
|
|
for k, v in pairs({ "first", "second", "third" }) do
|
2012-11-18 20:44:03 +01:00
|
|
|
self[v] = nil
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2015-06-14 16:03:29 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2015-09-28 21:00:49 +02:00
|
|
|
local function get_layout(dir, first, second, third)
|
2015-06-14 15:57:33 +02:00
|
|
|
local ret = base.make_widget()
|
2013-01-05 16:12:47 +01:00
|
|
|
ret.dir = dir
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
for k, v in pairs(align) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-02 09:09:08 +01:00
|
|
|
ret:set_expand("inside")
|
2015-09-28 21:00:49 +02:00
|
|
|
ret:set_first(first)
|
|
|
|
ret:set_second(second)
|
|
|
|
ret:set_third(third)
|
2014-01-02 09:09:08 +01:00
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns a new horizontal align layout. An align layout can display up to
|
|
|
|
-- three widgets. The widget set via :set_left() is left-aligned. :set_right()
|
|
|
|
-- sets a widget which will be right-aligned. The remaining space between those
|
|
|
|
-- two will be given to the widget set via :set_middle().
|
2015-09-28 21:00:49 +02:00
|
|
|
-- @tparam[opt] widget left Widget to be put to the left.
|
|
|
|
-- @tparam[opt] widget middle Widget to be put to the middle.
|
|
|
|
-- @tparam[opt] widget right Widget to be put to the right.
|
|
|
|
function align.horizontal(left, middle, right)
|
|
|
|
local ret = get_layout("x", left, middle, right)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
ret.set_left = ret.set_first
|
|
|
|
ret.set_middle = ret.set_second
|
|
|
|
ret.set_right = ret.set_third
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns a new vertical align layout. An align layout can display up to
|
|
|
|
-- three widgets. The widget set via :set_top() is top-aligned. :set_bottom()
|
|
|
|
-- sets a widget which will be bottom-aligned. The remaining space between those
|
|
|
|
-- two will be given to the widget set via :set_middle().
|
2015-09-28 21:00:49 +02:00
|
|
|
-- @tparam[opt] widget top Widget to be put to the top.
|
|
|
|
-- @tparam[opt] widget middle Widget to be put to the middle.
|
|
|
|
-- @tparam[opt] widget bottom Widget to be put to the right.
|
|
|
|
function align.vertical(top, middle, bottom)
|
|
|
|
local ret = get_layout("y", top, middle, bottom)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
ret.set_top = ret.set_first
|
|
|
|
ret.set_middle = ret.set_second
|
|
|
|
ret.set_bottom = ret.set_third
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
return align
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|