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.rotate
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local error = error
|
|
|
|
local pairs = pairs
|
|
|
|
local pi = math.pi
|
|
|
|
local type = type
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local tostring = tostring
|
2015-06-14 15:57:33 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2015-09-16 13:57:38 +02:00
|
|
|
local matrix = require("gears.matrix")
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
local rotate = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
local function transform(layout, width, height)
|
|
|
|
local dir = layout:get_direction()
|
|
|
|
if dir == "east" or dir == "west" then
|
|
|
|
return height, width
|
|
|
|
end
|
|
|
|
return width, height
|
|
|
|
end
|
|
|
|
|
2015-06-14 16:35:01 +02:00
|
|
|
--- Layout this layout
|
|
|
|
function rotate:layout(context, width, height)
|
2015-07-17 18:23:55 +02:00
|
|
|
if not self.widget or not self.widget.visible then
|
2015-08-08 13:18:54 +02:00
|
|
|
return
|
2015-07-17 18:23:55 +02:00
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
local dir = self:get_direction()
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-09-16 13:57:38 +02:00
|
|
|
local m = matrix.identity
|
2010-10-06 12:42:56 +02:00
|
|
|
if dir == "west" then
|
2015-09-16 13:57:38 +02:00
|
|
|
m = m:rotate(pi / 2)
|
|
|
|
m = m:translate(0, -width)
|
2010-10-06 12:42:56 +02:00
|
|
|
elseif dir == "south" then
|
2015-09-16 13:57:38 +02:00
|
|
|
m = m:rotate(pi)
|
|
|
|
m = m:translate(-width, -height)
|
2010-10-06 12:42:56 +02:00
|
|
|
elseif dir == "east" then
|
2015-09-16 13:57:38 +02:00
|
|
|
m = m:rotate(3 * pi / 2)
|
|
|
|
m = m:translate(-height, 0)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Since we rotated, we might have to swap width and height.
|
|
|
|
-- transform() does that for us.
|
2015-06-14 16:35:01 +02:00
|
|
|
return { base.place_widget_via_matrix(self.widget, m, transform(self, width, height)) }
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit this layout into the given area
|
2015-08-08 13:43:35 +02:00
|
|
|
function rotate:fit(context, width, height)
|
2012-11-18 20:44:03 +01:00
|
|
|
if not self.widget then
|
2010-10-06 12:42:56 +02:00
|
|
|
return 0, 0
|
|
|
|
end
|
2015-09-17 15:01:50 +02:00
|
|
|
return transform(self, base.fit_widget(self, context, self.widget, transform(self, width, height)))
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the widget that this layout rotates.
|
2012-11-18 20:44:03 +01:00
|
|
|
function rotate:set_widget(widget)
|
2010-10-06 12:42:56 +02:00
|
|
|
if widget then
|
2015-06-14 15:57:33 +02:00
|
|
|
base.check_widget(widget)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.widget = widget
|
2015-06-14 16:35:01 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-01-16 07:13:07 +01:00
|
|
|
--- Get the number of children element
|
|
|
|
-- @treturn table The children
|
|
|
|
function rotate:get_children()
|
|
|
|
return {self.widget}
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Reset this layout. The widget will be removed and the rotation reset.
|
2012-11-18 20:44:03 +01:00
|
|
|
function rotate:reset()
|
|
|
|
self.direction = nil
|
|
|
|
self:set_widget(nil)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the direction of this rotating layout. Valid values are "north", "east",
|
|
|
|
-- "south" and "west". On an invalid value, this function will throw an error.
|
2012-11-18 20:44:03 +01:00
|
|
|
function rotate:set_direction(dir)
|
2010-10-06 12:42:56 +02:00
|
|
|
local allowed = {
|
|
|
|
north = true,
|
|
|
|
east = true,
|
|
|
|
south = true,
|
|
|
|
west = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if not allowed[dir] then
|
|
|
|
error("Invalid direction for rotate layout: " .. tostring(dir))
|
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
self.direction = dir
|
2015-06-14 16:35:01 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the direction of this rotating layout
|
2012-11-18 20:44:03 +01:00
|
|
|
function rotate:get_direction()
|
|
|
|
return self.direction or "north"
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns a new rotate layout. A rotate layout rotates a given widget. Use
|
|
|
|
-- :set_widget() to set the widget and :set_direction() for the direction.
|
|
|
|
-- The default direction is "north" which doesn't change anything.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] widget The widget to display.
|
|
|
|
-- @param[opt] dir The direction to rotate to.
|
2013-01-05 20:52:00 +01:00
|
|
|
local function new(widget, dir)
|
2015-06-14 15:57:33 +02:00
|
|
|
local ret = base.make_widget()
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
for k, v in pairs(rotate) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-05 20:52:00 +01:00
|
|
|
ret:set_widget(widget)
|
|
|
|
ret:set_direction(dir or "north")
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
function rotate.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(rotate, rotate.mt)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|