2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-20 13:02:39 +02:00
|
|
|
--- Places client according to special criteria.
|
|
|
|
--
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:39 +02:00
|
|
|
-- @module awful.placement
|
2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local ipairs = ipairs
|
|
|
|
local pairs = pairs
|
|
|
|
local math = math
|
|
|
|
local table = table
|
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
screen = screen,
|
|
|
|
mouse = mouse,
|
2009-02-13 23:57:40 +01:00
|
|
|
client = client
|
2008-09-29 16:49:18 +02:00
|
|
|
}
|
|
|
|
local client = require("awful.client")
|
|
|
|
local layout = require("awful.layout")
|
2012-01-15 11:57:47 +01:00
|
|
|
local a_screen = require("awful.screen")
|
2015-08-02 19:36:56 +02:00
|
|
|
local dpi = require("beautiful").xresources.apply_dpi
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
local placement = {}
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Check if an area intersect another area.
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @param a The area.
|
|
|
|
-- @param b The other area.
|
|
|
|
-- @return True if they intersect, false otherwise.
|
|
|
|
local function area_intersect_area(a, b)
|
|
|
|
return (b.x < a.x + a.width
|
|
|
|
and b.x + b.width > a.x
|
|
|
|
and b.y < a.y + a.height
|
|
|
|
and b.y + b.height > a.y)
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Get the intersect area between a and b.
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @param a The area.
|
|
|
|
-- @param b The other area.
|
|
|
|
-- @return The intersect area.
|
|
|
|
local function area_intersect_area_get(a, b)
|
|
|
|
local g = {}
|
|
|
|
g.x = math.max(a.x, b.x)
|
|
|
|
g.y = math.max(a.y, b.y)
|
|
|
|
g.width = math.min(a.x + a.width, b.x + b.width) - g.x
|
|
|
|
g.height = math.min(a.y + a.height, b.y + b.height) - g.y
|
|
|
|
return g
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Remove an area from a list, splitting the space between several area that
|
2008-09-29 16:49:18 +02:00
|
|
|
-- can overlap.
|
|
|
|
-- @param areas Table of areas.
|
|
|
|
-- @param elem Area to remove.
|
|
|
|
-- @return The new area list.
|
|
|
|
local function area_remove(areas, elem)
|
2008-10-02 12:12:42 +02:00
|
|
|
for i = #areas, 1, -1 do
|
2008-09-29 16:49:18 +02:00
|
|
|
-- Check if the 'elem' intersect
|
2008-10-02 12:12:42 +02:00
|
|
|
if area_intersect_area(areas[i], elem) then
|
2008-09-29 16:49:18 +02:00
|
|
|
-- It does? remove it
|
2008-10-02 12:12:42 +02:00
|
|
|
local r = table.remove(areas, i)
|
2008-09-29 16:49:18 +02:00
|
|
|
local inter = area_intersect_area_get(r, elem)
|
|
|
|
|
|
|
|
if inter.x > r.x then
|
2008-10-02 12:12:42 +02:00
|
|
|
table.insert(areas, {
|
2008-09-29 16:49:18 +02:00
|
|
|
x = r.x,
|
|
|
|
y = r.y,
|
|
|
|
width = inter.x - r.x,
|
|
|
|
height = r.height
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
if inter.y > r.y then
|
2008-10-02 12:12:42 +02:00
|
|
|
table.insert(areas, {
|
2008-09-29 16:49:18 +02:00
|
|
|
x = r.x,
|
|
|
|
y = r.y,
|
|
|
|
width = r.width,
|
|
|
|
height = inter.y - r.y
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
if inter.x + inter.width < r.x + r.width then
|
2008-10-02 12:12:42 +02:00
|
|
|
table.insert(areas, {
|
2008-09-29 16:49:18 +02:00
|
|
|
x = inter.x + inter.width,
|
|
|
|
y = r.y,
|
|
|
|
width = (r.x + r.width) - (inter.x + inter.width),
|
|
|
|
height = r.height
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
if inter.y + inter.height < r.y + r.height then
|
2008-10-02 12:12:42 +02:00
|
|
|
table.insert(areas, {
|
2008-09-29 16:49:18 +02:00
|
|
|
x = r.x,
|
|
|
|
y = inter.y + inter.height,
|
|
|
|
width = r.width,
|
|
|
|
height = (r.y + r.height) - (inter.y + inter.height)
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-02 12:12:42 +02:00
|
|
|
return areas
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2015-10-24 08:44:50 +02:00
|
|
|
--- Get the area covered by a client.
|
|
|
|
-- @client c The client.
|
|
|
|
-- @treturn The client's area.
|
|
|
|
local function get_area(c)
|
|
|
|
local geometry = c:geometry()
|
|
|
|
local border = c.border_width or 0
|
|
|
|
geometry.width = geometry.width + 2 * border
|
|
|
|
geometry.height = geometry.height + 2 * border
|
|
|
|
return geometry
|
|
|
|
end
|
|
|
|
|
2015-08-14 03:02:52 +02:00
|
|
|
--- Place the client so no part of it will be outside the screen (workarea).
|
|
|
|
-- @client c The client.
|
2015-09-16 10:39:12 +02:00
|
|
|
-- @tparam[opt=client's screen] integer screen The screen.
|
2015-08-14 03:02:52 +02:00
|
|
|
-- @treturn table The new client geometry.
|
2015-09-16 10:39:12 +02:00
|
|
|
function placement.no_offscreen(c, screen)
|
2009-02-13 23:57:40 +01:00
|
|
|
local c = c or capi.client.focus
|
2015-10-24 08:44:50 +02:00
|
|
|
local geometry = get_area(c)
|
2015-09-16 10:39:12 +02:00
|
|
|
local screen = screen or c.screen or a_screen.getbycoord(geometry.x, geometry.y)
|
2012-01-15 11:57:47 +01:00
|
|
|
local screen_geometry = capi.screen[screen].workarea
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2015-10-24 08:44:50 +02:00
|
|
|
if geometry.x + geometry.width > screen_geometry.x + screen_geometry.width then
|
|
|
|
geometry.x = screen_geometry.x + screen_geometry.width - geometry.width
|
2015-02-14 21:55:23 +01:00
|
|
|
end
|
|
|
|
if geometry.x < screen_geometry.x then
|
2008-09-29 16:49:18 +02:00
|
|
|
geometry.x = screen_geometry.x
|
|
|
|
end
|
|
|
|
|
2015-10-24 08:44:50 +02:00
|
|
|
if geometry.y + geometry.height > screen_geometry.y + screen_geometry.height then
|
|
|
|
geometry.y = screen_geometry.y + screen_geometry.height - geometry.height
|
2015-02-14 21:55:23 +01:00
|
|
|
end
|
|
|
|
if geometry.y < screen_geometry.y then
|
2008-09-29 16:49:18 +02:00
|
|
|
geometry.y = screen_geometry.y
|
|
|
|
end
|
|
|
|
|
2015-10-24 08:43:22 +02:00
|
|
|
return c:geometry({ x = geometry.x, y = geometry.y })
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Place the client where there's place available with minimum overlap.
|
|
|
|
-- @param c The client.
|
2012-06-12 20:13:09 +02:00
|
|
|
function placement.no_overlap(c)
|
2015-10-24 08:44:50 +02:00
|
|
|
local geometry = get_area(c)
|
2012-01-15 11:57:47 +01:00
|
|
|
local screen = c.screen or a_screen.getbycoord(geometry.x, geometry.y)
|
|
|
|
local cls = client.visible(screen)
|
|
|
|
local curlay = layout.get()
|
|
|
|
local areas = { capi.screen[screen].workarea }
|
2008-09-29 16:49:18 +02:00
|
|
|
for i, cl in pairs(cls) do
|
2010-09-03 17:43:36 +02:00
|
|
|
if cl ~= c and cl.type ~= "desktop" and (client.floating.get(cl) or curlay == layout.suit.floating) then
|
2015-10-24 08:44:50 +02:00
|
|
|
areas = area_remove(areas, get_area(cl))
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Look for available space
|
|
|
|
local found = false
|
2008-10-21 15:31:52 +02:00
|
|
|
local new = { x = geometry.x, y = geometry.y, width = 0, height = 0 }
|
2008-09-29 16:49:18 +02:00
|
|
|
for i, r in ipairs(areas) do
|
2008-12-12 00:01:43 +01:00
|
|
|
if r.width >= geometry.width
|
|
|
|
and r.height >= geometry.height
|
2008-09-29 16:49:18 +02:00
|
|
|
and r.width * r.height > new.width * new.height then
|
|
|
|
found = true
|
|
|
|
new = r
|
2011-03-01 17:52:00 +01:00
|
|
|
-- Check if the client's current position is available
|
|
|
|
-- and prefer that one (why move it around pointlessly?)
|
|
|
|
if geometry.x >= r.x
|
|
|
|
and geometry.y >= r.y
|
|
|
|
and geometry.x + geometry.width <= r.x + r.width
|
|
|
|
and geometry.y + geometry.height <= r.y + r.height then
|
|
|
|
new.x = geometry.x
|
|
|
|
new.y = geometry.y
|
|
|
|
end
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-01 17:52:00 +01:00
|
|
|
-- We did not find an area with enough space for our size:
|
2008-09-29 16:49:18 +02:00
|
|
|
-- just take the biggest available one and go in
|
|
|
|
if not found then
|
|
|
|
for i, r in ipairs(areas) do
|
|
|
|
if r.width * r.height > new.width * new.height then
|
|
|
|
new = r
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Restore height and width
|
2008-10-21 15:31:52 +02:00
|
|
|
new.width = geometry.width
|
|
|
|
new.height = geometry.height
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2015-10-24 08:43:22 +02:00
|
|
|
return c:geometry({ x = new.x, y = new.y })
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Place the client under the mouse.
|
|
|
|
-- @param c The client.
|
2009-02-13 23:57:40 +01:00
|
|
|
-- @return The new client geometry.
|
2012-06-12 20:13:09 +02:00
|
|
|
function placement.under_mouse(c)
|
2009-02-13 23:57:40 +01:00
|
|
|
local c = c or capi.client.focus
|
2015-10-24 08:44:50 +02:00
|
|
|
local c_geometry = get_area(c)
|
2008-09-29 16:49:18 +02:00
|
|
|
local m_coords = capi.mouse.coords()
|
2009-02-13 23:57:40 +01:00
|
|
|
return c:geometry({ x = m_coords.x - c_geometry.width / 2,
|
|
|
|
y = m_coords.y - c_geometry.height / 2 })
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2015-08-02 19:36:56 +02:00
|
|
|
--- Place the client next to the mouse.
|
|
|
|
--
|
|
|
|
-- It will place `c` next to the mouse pointer, trying the following positions
|
|
|
|
-- in this order: right, left, above and below.
|
|
|
|
-- @client[opt=focused] c The client.
|
|
|
|
-- @tparam[opt=apply_dpi(5)] integer offset The offset from the mouse position.
|
|
|
|
-- @return The new client geometry.
|
|
|
|
function placement.next_to_mouse(c, offset)
|
|
|
|
c = c or capi.client.focus
|
|
|
|
offset = offset or dpi(5)
|
2015-10-24 08:44:50 +02:00
|
|
|
local c_geometry = get_area(c)
|
|
|
|
local c_width = c_geometry.width
|
|
|
|
local c_height = c_geometry.height
|
2015-08-02 19:36:56 +02:00
|
|
|
local m_coords = capi.mouse.coords()
|
|
|
|
local screen_geometry = capi.screen[capi.mouse.screen].workarea
|
|
|
|
|
|
|
|
-- Prefer it to be on the right.
|
|
|
|
local x = m_coords.x + offset
|
|
|
|
if x + c_width > screen_geometry.width then
|
|
|
|
-- Then to the left.
|
|
|
|
x = m_coords.x - c_width - offset
|
|
|
|
end
|
|
|
|
if x < screen_geometry.x then
|
|
|
|
-- Then above.
|
|
|
|
x = m_coords.x - math.ceil(c_width / 2)
|
|
|
|
y = m_coords.y - c_height - offset
|
|
|
|
if y < screen_geometry.y then
|
|
|
|
-- Finally below.
|
|
|
|
y = m_coords.y + offset
|
|
|
|
end
|
|
|
|
else
|
|
|
|
y = m_coords.y - math.ceil(c_height / 2)
|
|
|
|
end
|
|
|
|
return c:geometry({ x = x, y = y })
|
|
|
|
end
|
|
|
|
|
2009-02-14 20:41:57 +01:00
|
|
|
--- Place the client centered with respect to a parent or the clients screen.
|
2009-02-13 23:56:26 +01:00
|
|
|
-- @param c The client.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] p The parent (nil for screen centering).
|
2009-02-13 23:57:40 +01:00
|
|
|
-- @return The new client geometry.
|
2012-06-12 20:13:09 +02:00
|
|
|
function placement.centered(c, p)
|
2009-02-13 23:57:40 +01:00
|
|
|
local c = c or capi.client.focus
|
2015-10-24 08:44:50 +02:00
|
|
|
local c_geometry = get_area(c)
|
2012-01-15 11:57:47 +01:00
|
|
|
local screen = c.screen or a_screen.getbycoord(c_geometry.x, c_geometry.y)
|
2009-02-14 20:41:57 +01:00
|
|
|
local s_geometry
|
|
|
|
if p then
|
2015-10-24 08:44:50 +02:00
|
|
|
s_geometry = get_area(p)
|
2009-02-14 20:41:57 +01:00
|
|
|
else
|
2012-01-15 11:57:47 +01:00
|
|
|
s_geometry = capi.screen[screen].geometry
|
2009-02-14 20:41:57 +01:00
|
|
|
end
|
2009-02-13 23:57:40 +01:00
|
|
|
return c:geometry({ x = s_geometry.x + (s_geometry.width - c_geometry.width) / 2,
|
|
|
|
y = s_geometry.y + (s_geometry.height - c_geometry.height) / 2 })
|
2009-02-13 23:56:26 +01:00
|
|
|
end
|
|
|
|
|
2009-09-13 14:50:53 +02:00
|
|
|
--- Place the client centered on the horizontal axis with respect to a parent or the clients screen.
|
|
|
|
-- @param c The client.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] p The parent (nil for screen centering).
|
2009-09-13 14:50:53 +02:00
|
|
|
-- @return The new client geometry.
|
2012-06-12 20:13:09 +02:00
|
|
|
function placement.center_horizontal(c, p)
|
2009-09-13 14:50:53 +02:00
|
|
|
local c = c or capi.client.focus
|
2015-10-24 08:44:50 +02:00
|
|
|
local c_geometry = get_area(c)
|
2012-01-15 11:57:47 +01:00
|
|
|
local screen = c.screen or a_screen.getbycoord(c_geometry.x, c_geometry.y)
|
2009-09-13 14:50:53 +02:00
|
|
|
local s_geometry
|
|
|
|
if p then
|
2015-10-24 08:44:50 +02:00
|
|
|
s_geometry = get_area(p)
|
2009-09-13 14:50:53 +02:00
|
|
|
else
|
2012-01-15 11:57:47 +01:00
|
|
|
s_geometry = capi.screen[screen].geometry
|
2009-09-13 14:50:53 +02:00
|
|
|
end
|
|
|
|
return c:geometry({ x = s_geometry.x + (s_geometry.width - c_geometry.width) / 2 })
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Place the client centered on the vertical axis with respect to a parent or the clients screen.
|
|
|
|
-- @param c The client.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] p The parent (nil for screen centering).
|
2009-09-13 14:50:53 +02:00
|
|
|
-- @return The new client geometry.
|
2012-06-12 20:13:09 +02:00
|
|
|
function placement.center_vertical(c, p)
|
2009-09-13 14:50:53 +02:00
|
|
|
local c = c or capi.client.focus
|
2015-10-24 08:44:50 +02:00
|
|
|
local c_geometry = get_area(c)
|
2012-01-15 11:57:47 +01:00
|
|
|
local screen = c.screen or a_screen.getbycoord(c_geometry.x, c_geometry.y)
|
2009-09-13 14:50:53 +02:00
|
|
|
local s_geometry
|
|
|
|
if p then
|
2015-10-24 08:44:50 +02:00
|
|
|
s_geometry = get_area(p)
|
2009-09-13 14:50:53 +02:00
|
|
|
else
|
2012-01-15 11:57:47 +01:00
|
|
|
s_geometry = capi.screen[screen].geometry
|
2009-09-13 14:50:53 +02:00
|
|
|
end
|
|
|
|
return c:geometry({ y = s_geometry.y + (s_geometry.height - c_geometry.height) / 2 })
|
|
|
|
end
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
return placement
|
2009-09-13 14:50:53 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|