1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
18 local client
= require("awful.client")
19 local layout
= require("awful.layout")
21 --- Places client according to special criteria.
22 module("awful.placement")
24 -- Check if an area intersect another area.
26 -- @param b The other area.
27 -- @return True if they intersect, false otherwise.
28 local function area_intersect_area(a
, b
)
29 return (b
.x
< a
.x
+ a
.width
30 and b
.x
+ b
.width
> a
.x
31 and b
.y
< a
.y
+ a
.height
32 and b
.y
+ b
.height
> a
.y
)
35 -- Get the intersect area between a and b.
37 -- @param b The other area.
38 -- @return The intersect area.
39 local function area_intersect_area_get(a
, b
)
41 g
.x
= math
.max(a
.x
, b
.x
)
42 g
.y
= math
.max(a
.y
, b
.y
)
43 g
.width
= math
.min(a
.x
+ a
.width
, b
.x
+ b
.width
) - g
.x
44 g
.height
= math
.min(a
.y
+ a
.height
, b
.y
+ b
.height
) - g
.y
48 -- Remove an area from a list, splitting the space between several area that
50 -- @param areas Table of areas.
51 -- @param elem Area to remove.
52 -- @return The new area list.
53 local function area_remove(areas
, elem
)
54 for i
= #areas
, 1, -1 do
55 -- Check if the 'elem' intersect
56 if area_intersect_area(areas
[i
], elem
) then
58 local r
= table.remove(areas
, i
)
59 local inter
= area_intersect_area_get(r
, elem
)
65 width
= inter
.x
- r
.x
,
75 height
= inter
.y
- r
.y
79 if inter
.x
+ inter
.width
< r
.x
+ r
.width
then
81 x
= inter
.x
+ inter
.width
,
83 width
= (r
.x
+ r
.width
) - (inter
.x
+ inter
.width
),
88 if inter
.y
+ inter
.height
< r
.y
+ r
.height
then
91 y
= inter
.y
+ inter
.height
,
93 height
= (r
.y
+ r
.height
) - (inter
.y
+ inter
.height
)
102 --- Place the client so no part of it will be outside the screen.
103 -- @param c The client.
104 -- @return The new client geometry.
105 function no_offscreen(c
)
106 local c
= c
or capi
.client
.focus
107 local geometry
= c
:geometry()
108 local screen_geometry
= capi
.screen
[c
.screen
].workarea
110 if geometry
.x
+ geometry
.width
> screen_geometry
.x
+ screen_geometry
.width
then
111 geometry
.x
= screen_geometry
.x
+ screen_geometry
.width
- geometry
.width
112 elseif geometry
.x
< screen_geometry
.x
then
113 geometry
.x
= screen_geometry
.x
116 if geometry
.y
+ geometry
.height
> screen_geometry
.y
+ screen_geometry
.height
then
117 geometry
.y
= screen_geometry
.y
+ screen_geometry
.height
- geometry
.height
118 elseif geometry
.y
< screen_geometry
.y
then
119 geometry
.y
= screen_geometry
.y
125 --- Place the client where there's place available with minimum overlap.
126 -- @param c The client.
127 function no_overlap(c
)
128 local cls
= client
.visible(c
.screen
)
129 local curlay
= layout
.get()
130 local areas
= { capi
.screen
[c
.screen
].workarea
}
131 local geometry
= c
:geometry()
132 for i
, cl
in pairs(cls
) do
133 if cl
~= c
and (client
.floating
.get(cl
) or curlay
== layout
.suit
.floating
) then
134 areas
= area_remove(areas
, cl
:geometry())
138 -- Look for available space
140 local new
= { x
= geometry
.x
, y
= geometry
.y
, width
= 0, height
= 0 }
141 for i
, r
in ipairs(areas
) do
142 if r
.width
>= geometry
.width
143 and r
.height
>= geometry
.height
144 and r
.width
* r
.height
> new
.width
* new
.height
then
150 -- We did not foudn an area with enough space for our size:
151 -- just take the biggest available one and go in
153 for i
, r
in ipairs(areas
) do
154 if r
.width
* r
.height
> new
.width
* new
.height
then
160 -- Restore height and width
161 new
.width
= geometry
.width
162 new
.height
= geometry
.height
164 return c
:geometry(new
)
167 --- Place the client under the mouse.
168 -- @param c The client.
169 -- @return The new client geometry.
170 function under_mouse(c
)
171 local c
= c
or capi
.client
.focus
172 local c_geometry
= c
:geometry()
173 local m_coords
= capi
.mouse
.coords()
174 return c
:geometry({ x
= m_coords
.x
- c_geometry
.width
/ 2,
175 y
= m_coords
.y
- c_geometry
.height
/ 2 })
178 --- Place the client centered with respect to a parent or the clients screen.
179 -- @param c The client.
180 -- @param p The parent (optional, nil for screen centering).
181 -- @return The new client geometry.
182 function centered(c
, p
)
183 local c
= c
or capi
.client
.focus
184 local c_geometry
= c
:geometry()
187 s_geometry
= p
:geometry()
189 s_geometry
= capi
.screen
[c
.screen
].geometry
191 return c
:geometry({ x
= s_geometry
.x
+ (s_geometry
.width
- c_geometry
.width
) / 2,
192 y
= s_geometry
.y
+ (s_geometry
.height
- c_geometry
.height
) / 2 })
195 --- Place the client centered on the horizontal axis with respect to a parent or the clients screen.
196 -- @param c The client.
197 -- @param p The parent (optional, nil for screen centering).
198 -- @return The new client geometry.
199 function center_horizontal(c
, p
)
200 local c
= c
or capi
.client
.focus
201 local c_geometry
= c
:geometry()
204 s_geometry
= p
:geometry()
206 s_geometry
= capi
.screen
[c
.screen
].geometry
208 return c
:geometry({ x
= s_geometry
.x
+ (s_geometry
.width
- c_geometry
.width
) / 2 })
211 --- Place the client centered on the vertical axis with respect to a parent or the clients screen.
212 -- @param c The client.
213 -- @param p The parent (optional, nil for screen centering).
214 -- @return The new client geometry.
215 function center_vertical(c
, p
)
216 local c
= c
or capi
.client
.focus
217 local c_geometry
= c
:geometry()
220 s_geometry
= p
:geometry()
222 s_geometry
= capi
.screen
[c
.screen
].geometry
224 return c
:geometry({ y
= s_geometry
.y
+ (s_geometry
.height
- c_geometry
.height
) / 2 })
228 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80