rotate: Use the draw_widget() function
[awesome.git] / lib / wibox / layout / rotate.lua.in
blob7b6a6035740c5188e3f40133dcd04a5bec1b1f08
1 ---------------------------------------------------------------------------
2 -- @author Uli Schlachter
3 -- @copyright 2010 Uli Schlachter
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 local error = error
8 local pairs = pairs
9 local pi = math.pi
10 local type = type
11 local setmetatable = setmetatable
12 local tostring = tostring
13 local base = require("wibox.layout.base")
14 local widget_base = require("wibox.widget.base")
16 -- wibox.layout.rotate
17 local rotate = { mt = {} }
19 local function transform(layout, width, height)
20 local dir = layout:get_direction()
21 if dir == "east" or dir == "west" then
22 return height, width
23 end
24 return width, height
25 end
27 --- Draw this layout
28 function rotate.draw(layout, wibox, cr, width, height)
29 if not layout.widget then return { width = 0, height = 0 } end
31 local dir = layout:get_direction()
33 if dir == "west" then
34 cr:rotate(pi / 2)
35 cr:translate(0, -width)
36 elseif dir == "south" then
37 cr:rotate(pi)
38 cr:translate(-width, -height)
39 elseif dir == "east" then
40 cr:rotate(3 * pi / 2)
41 cr:translate(-height, 0)
42 end
44 -- Since we rotated, we might have to swap width and height.
45 -- transform() does that for us.
46 base.draw_widget(wibox, cr, layout.widget, 0, 0, transform(layout, width, height))
47 end
49 --- Fit this layout into the given area
50 function rotate.fit(layout, width, height)
51 if not layout.widget then
52 return 0, 0
53 end
54 return transform(layout, layout.widget:fit(transform(layout, width, height)))
55 end
57 --- Set the widget that this layout rotates.
58 function rotate.set_widget(layout, widget)
59 if layout.widget then
60 layout.widget:disconnect_signal("widget::updated", layout._emit_updated)
61 end
62 if widget then
63 widget_base.check_widget(widget)
64 widget:connect_signal("widget::updated", layout._emit_updated)
65 end
66 layout.widget = widget
67 layout._emit_updated()
68 end
70 --- Reset this layout. The widget will be removed and the rotation reset.
71 function rotate.reset(layout)
72 layout.direction = nil
73 layout:set_widget(nil)
74 end
76 --- Set the direction of this rotating layout. Valid values are "north", "east",
77 -- "south" and "west". On an invalid value, this function will throw an error.
78 function rotate.set_direction(layout, dir)
79 local allowed = {
80 north = true,
81 east = true,
82 south = true,
83 west = true
86 if not allowed[dir] then
87 error("Invalid direction for rotate layout: " .. tostring(dir))
88 end
90 layout.direction = dir
91 layout._emit_updated()
92 end
94 --- Get the direction of this rotating layout
95 function rotate.get_direction(layout)
96 return layout.direction or "north"
97 end
99 --- Returns a new rotate layout. A rotate layout rotates a given widget. Use
100 -- :set_widget() to set the widget and :set_direction() for the direction.
101 -- The default direction is "north" which doesn't change anything.
102 local function new()
103 local ret = widget_base.make_widget()
105 for k, v in pairs(rotate) do
106 if type(v) == "function" then
107 ret[k] = v
111 ret._emit_updated = function()
112 ret:emit_signal("widget::updated")
115 return ret
118 function rotate.mt:__call(...)
119 return new(...)
122 return setmetatable(rotate, rotate.mt)
124 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80