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
|
|
|
|
local base = require("wibox.layout.base")
|
|
|
|
local widget_base = require("wibox.widget.base")
|
|
|
|
|
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
|
|
|
|
|
|
|
|
--- Draw this layout
|
2012-11-18 20:44:03 +01:00
|
|
|
function rotate:draw(wibox, cr, width, height)
|
|
|
|
if not self.widget then return { width = 0, height = 0 } 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
|
|
|
|
|
|
|
if dir == "west" then
|
|
|
|
cr:rotate(pi / 2)
|
|
|
|
cr:translate(0, -width)
|
|
|
|
elseif dir == "south" then
|
|
|
|
cr:rotate(pi)
|
|
|
|
cr:translate(-width, -height)
|
|
|
|
elseif dir == "east" then
|
|
|
|
cr:rotate(3 * pi / 2)
|
|
|
|
cr:translate(-height, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Since we rotated, we might have to swap width and height.
|
|
|
|
-- transform() does that for us.
|
2012-11-18 20:44:03 +01:00
|
|
|
base.draw_widget(wibox, cr, self.widget, 0, 0, transform(self, width, height))
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit this layout into the given area
|
2012-12-05 17:35:08 +01:00
|
|
|
function rotate:fit(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
|
2013-08-21 12:26:47 +02:00
|
|
|
return transform(self, base.fit_widget(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)
|
|
|
|
if self.widget then
|
|
|
|
self.widget:disconnect_signal("widget::updated", self._emit_updated)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
if widget then
|
|
|
|
widget_base.check_widget(widget)
|
2012-11-18 20:44:03 +01:00
|
|
|
widget:connect_signal("widget::updated", self._emit_updated)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.widget = widget
|
|
|
|
self._emit_updated()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- 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
|
|
|
|
self._emit_updated()
|
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)
|
2010-10-06 12:42:56 +02:00
|
|
|
local ret = widget_base.make_widget()
|
|
|
|
|
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
|
|
|
|
|
|
|
|
ret._emit_updated = function()
|
|
|
|
ret:emit_signal("widget::updated")
|
|
|
|
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
|