2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local ipairs = ipairs
|
|
|
|
local pairs = pairs
|
|
|
|
local math = math
|
|
|
|
local table = table
|
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
screen = screen,
|
|
|
|
mouse = mouse,
|
|
|
|
}
|
|
|
|
local client = require("awful.client")
|
|
|
|
local layout = require("awful.layout")
|
|
|
|
|
|
|
|
--- Placement module for awful
|
|
|
|
module("awful.placement")
|
|
|
|
|
2009-01-08 12:25:45 +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
|
|
|
|
|
2009-01-08 12:25:45 +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
|
|
|
|
|
2009-01-08 12:25:45 +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
|
|
|
|
|
|
|
|
--- Place the client without it being outside the screen.
|
|
|
|
-- @param c The client.
|
|
|
|
function no_offscreen(c)
|
2008-12-12 00:01:43 +01:00
|
|
|
local geometry = c:geometry()
|
2008-09-29 16:49:18 +02:00
|
|
|
local screen_geometry = capi.screen[c.screen].workarea
|
|
|
|
|
|
|
|
if geometry.x + geometry.width > screen_geometry.x + screen_geometry.width then
|
|
|
|
geometry.x = screen_geometry.x + screen_geometry.width - geometry.width
|
|
|
|
elseif geometry.x < screen_geometry.x then
|
|
|
|
geometry.x = screen_geometry.x
|
|
|
|
end
|
|
|
|
|
|
|
|
if geometry.y + geometry.height > screen_geometry.y + screen_geometry.height then
|
|
|
|
geometry.y = screen_geometry.y + screen_geometry.height - geometry.height
|
|
|
|
elseif geometry.y < screen_geometry.y then
|
|
|
|
geometry.y = screen_geometry.y
|
|
|
|
end
|
|
|
|
|
2008-12-12 00:01:43 +01:00
|
|
|
c:geometry(geometry)
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Place the client where there's place available with minimum overlap.
|
|
|
|
-- @param c The client.
|
|
|
|
function no_overlap(c)
|
|
|
|
local cls = client.visible(c.screen)
|
2009-01-09 12:39:27 +01:00
|
|
|
local curlay = layout.get()
|
2008-09-29 16:49:18 +02:00
|
|
|
local areas = { capi.screen[c.screen].workarea }
|
2008-10-21 15:31:52 +02:00
|
|
|
local geometry = c:geometry()
|
2008-09-29 16:49:18 +02:00
|
|
|
for i, cl in pairs(cls) do
|
2009-01-09 12:39:27 +01:00
|
|
|
if cl ~= c and (client.floating.get(cl) or curlay == layout.suit.floating) then
|
2008-12-12 00:01:43 +01:00
|
|
|
areas = area_remove(areas, cl:geometry())
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- We did not foudn an area with enough space for our size:
|
|
|
|
-- 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
|
|
|
|
2008-10-21 15:31:52 +02:00
|
|
|
c:geometry(new)
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Place the client under the mouse.
|
|
|
|
-- @param c The client.
|
|
|
|
function under_mouse(c)
|
2008-10-21 15:31:52 +02:00
|
|
|
local c_geometry = c:geometry()
|
2008-09-29 16:49:18 +02:00
|
|
|
local m_coords = capi.mouse.coords()
|
2008-10-21 15:31:52 +02:00
|
|
|
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
|
|
|
|
|
2009-02-13 23:56:26 +01:00
|
|
|
--- Place the client centered in the screen.
|
|
|
|
-- @param c The client.
|
|
|
|
function centered(c)
|
|
|
|
local c_geometry = c:geometry()
|
|
|
|
local s_geometry = capi.screen[c.screen].geometry
|
|
|
|
c:geometry({ x = s_geometry.x + (s_geometry.width - c_geometry.width) / 2,
|
|
|
|
y = s_geometry.y + (s_geometry.height - c_geometry.height) / 2 })
|
|
|
|
end
|
|
|
|
|
2008-09-29 16:49:18 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|