2012-05-15 00:39:54 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author dodo
|
|
|
|
-- @copyright 2012 dodo
|
|
|
|
-- @release @AWESOME_VERSION@
|
2015-02-25 11:18:53 +01:00
|
|
|
-- @classmod wibox.layout.mirror
|
2012-05-15 00:39:54 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local type = type
|
|
|
|
local error = error
|
|
|
|
local pairs = pairs
|
|
|
|
local ipairs = ipairs
|
|
|
|
local setmetatable = setmetatable
|
2015-06-14 16:32:58 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2015-09-16 13:57:38 +02:00
|
|
|
local matrix = require("gears.matrix")
|
2012-05-15 00:39:54 +02:00
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
local mirror = { mt = {} }
|
2012-05-15 00:39:54 +02:00
|
|
|
|
2015-06-14 16:32:58 +02:00
|
|
|
--- Layout this layout
|
2016-02-29 22:09:18 +01:00
|
|
|
function mirror:layout(_, width, height)
|
2015-08-08 13:18:54 +02:00
|
|
|
if not self.widget then return end
|
2012-05-15 00:39:54 +02:00
|
|
|
|
2015-09-16 13:57:38 +02:00
|
|
|
local m = matrix.identity
|
2012-05-15 00:39:54 +02:00
|
|
|
local t = { x = 0, y = 0 } -- translation
|
|
|
|
local s = { x = 1, y = 1 } -- scale
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.horizontal then
|
2012-05-15 00:39:54 +02:00
|
|
|
t.x = width
|
|
|
|
s.x = -1
|
|
|
|
end
|
2016-02-29 22:09:18 +01:00
|
|
|
if self.vertical then
|
|
|
|
t.y = height
|
|
|
|
s.y = -1
|
|
|
|
end
|
2015-09-16 13:57:38 +02:00
|
|
|
m = m:translate(t.x, t.y)
|
|
|
|
m = m:scale(s.x, s.y)
|
2012-05-15 00:39:54 +02:00
|
|
|
|
2016-02-29 22:09:18 +01:00
|
|
|
return { base.place_widget_via_matrix(self.widget, m, width, height) }
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit this layout into the given area
|
2015-08-08 13:43:35 +02:00
|
|
|
function mirror:fit(context, ...)
|
2012-11-18 20:44:03 +01:00
|
|
|
if not self.widget then
|
2012-05-15 00:39:54 +02:00
|
|
|
return 0, 0
|
|
|
|
end
|
2015-09-17 15:01:50 +02:00
|
|
|
return base.fit_widget(self, context, self.widget, ...)
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the widget that this layout mirrors.
|
|
|
|
-- @param widget The widget to mirror
|
2012-11-18 20:44:03 +01:00
|
|
|
function mirror:set_widget(widget)
|
2012-05-15 00:39:54 +02:00
|
|
|
if widget then
|
2015-06-14 16:32:58 +02:00
|
|
|
base.check_widget(widget)
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.widget = widget
|
2015-06-14 16:32:58 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
|
|
|
|
2016-01-16 07:13:07 +01:00
|
|
|
--- Get the number of children element
|
|
|
|
-- @treturn table The children
|
|
|
|
function mirror:get_children()
|
|
|
|
return {self.widget}
|
|
|
|
end
|
|
|
|
|
2016-02-04 07:11:10 +01:00
|
|
|
--- 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)
|
2016-02-27 13:55:34 +01:00
|
|
|
self:set_widget(children[1])
|
2016-02-04 07:11:10 +01:00
|
|
|
end
|
|
|
|
|
2012-05-15 00:39:54 +02:00
|
|
|
--- Reset this layout. The widget will be removed and the axes reset.
|
2012-11-18 20:44:03 +01:00
|
|
|
function mirror:reset()
|
|
|
|
self.horizontal = false
|
|
|
|
self.vertical = false
|
|
|
|
self:set_widget(nil)
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the reflection of this mirror layout.
|
|
|
|
-- @param reflection a table which contains new values for horizontal and/or vertical (booleans)
|
2012-11-18 20:44:03 +01:00
|
|
|
function mirror:set_reflection(reflection)
|
2012-05-15 00:39:54 +02:00
|
|
|
if type(reflection) ~= 'table' then
|
|
|
|
error("Invalid type of reflection for mirror layout: " ..
|
|
|
|
type(reflection) .. " (should be a table)")
|
|
|
|
end
|
|
|
|
for _, ref in ipairs({"horizontal", "vertical"}) do
|
|
|
|
if reflection[ref] ~= nil then
|
2012-11-18 20:44:03 +01:00
|
|
|
self[ref] = reflection[ref]
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
|
|
|
end
|
2015-06-14 16:32:58 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the reflection of this mirror layout.
|
|
|
|
-- @return a table of booleans with the keys "horizontal", "vertical".
|
2012-11-18 20:44:03 +01:00
|
|
|
function mirror:get_reflection()
|
|
|
|
return { horizontal = self.horizontal, vertical = self.vertical }
|
2012-05-15 00:39:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns a new mirror layout. A mirror layout mirrors a given widget. Use
|
|
|
|
-- :set_widget() to set the widget and
|
|
|
|
-- :set_horizontal() and :set_vertical() for the direction.
|
|
|
|
-- horizontal and vertical are by default false which doesn't change anything.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] widget The widget to display.
|
|
|
|
-- @param[opt] reflection A table describing the reflection to apply.
|
2013-03-10 13:46:28 +01:00
|
|
|
local function new(widget, reflection)
|
2015-06-14 16:32:58 +02:00
|
|
|
local ret = base.make_widget()
|
2012-05-15 00:39:54 +02:00
|
|
|
ret.horizontal = false
|
|
|
|
ret.vertical = false
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
for k, v in pairs(mirror) do
|
2012-05-15 00:39:54 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-10 13:46:28 +01:00
|
|
|
ret:set_widget(widget)
|
|
|
|
ret:set_reflection(reflection or {})
|
|
|
|
|
2012-05-15 00:39:54 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
function mirror.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(mirror, mirror.mt)
|
2012-05-15 00:39:54 +02:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|