2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
2016-03-17 09:09:16 +01:00
|
|
|
--- Algorithms used to place various drawables.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
2016-03-17 09:09:16 +01:00
|
|
|
-- The functions provided by this module all follow the same arguments
|
|
|
|
-- conventions. This allow:
|
|
|
|
--
|
|
|
|
-- * To use them in various other module as
|
|
|
|
-- [visitor objects](https://en.wikipedia.org/wiki/Visitor_pattern)
|
|
|
|
-- * Turn each function into an API with various common customization parameters.
|
|
|
|
-- * Re-use the same functions for the `mouse`, `client`s, `screen`s and `wibox`es
|
|
|
|
--
|
|
|
|
--
|
2016-04-16 11:49:17 +02:00
|
|
|
--
|
|
|
|
-- It is possible to compose placement function using the `+` or `*` operator:
|
|
|
|
--
|
|
|
|
-- local f = (awful.placement.right + awful.placement.left)
|
|
|
|
-- f(client.focus)
|
|
|
|
--
|
2016-03-17 09:09:16 +01:00
|
|
|
-- ### Common arguments
|
|
|
|
--
|
|
|
|
-- **honor_workarea** (*boolean*):
|
|
|
|
--
|
|
|
|
-- Take workarea into account when placing the drawable (default: false)
|
|
|
|
--
|
|
|
|
-- **honor_padding** (*boolean*):
|
|
|
|
--
|
2016-04-05 05:35:03 +02:00
|
|
|
-- Take the screen padding into account (see `screen.padding`)
|
2016-03-17 09:09:16 +01:00
|
|
|
--
|
|
|
|
-- **tag** (*tag*):
|
|
|
|
--
|
|
|
|
-- Use a tag geometry
|
|
|
|
--
|
|
|
|
-- **margins** (*number* or *table*):
|
|
|
|
--
|
|
|
|
-- A table with left, right, top, bottom keys or a number
|
|
|
|
--
|
|
|
|
-- **parent** (client, wibox, mouse or screen):
|
|
|
|
--
|
|
|
|
-- A parent drawable to use a base geometry
|
|
|
|
--
|
|
|
|
-- **bounding_rect** (table):
|
|
|
|
--
|
|
|
|
-- A bounding rectangle
|
|
|
|
--
|
|
|
|
-- **attach** (*boolean*):
|
|
|
|
--
|
2016-04-15 11:09:05 +02:00
|
|
|
-- **store_geometry** (*boolean*):
|
|
|
|
--
|
|
|
|
-- Keep a single history of each type of placement. It can be restored using
|
|
|
|
-- `awful.placement.restore` by setting the right `context` argument.
|
|
|
|
--
|
2016-03-17 09:09:16 +01:00
|
|
|
-- When either the parent or the screen geometry change, call the placement
|
|
|
|
-- function again.
|
|
|
|
--
|
|
|
|
-- **update_workarea** (*boolean*):
|
|
|
|
--
|
|
|
|
-- If *attach* is true, also update the screen workarea.
|
|
|
|
--
|
|
|
|
-- @author Emmanuel Lepage Vallee <elv1313@gmail.com>
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
2016-03-17 09:09:16 +01:00
|
|
|
-- @copyright 2008 Julien Danjou, Emmanuel Lepage Vallee 2016
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @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
|
|
|
|
2016-02-26 19:08:35 +01:00
|
|
|
local function get_screen(s)
|
|
|
|
return s and capi.screen[s]
|
|
|
|
end
|
|
|
|
|
2016-04-16 11:49:17 +02:00
|
|
|
local wrap_client = nil
|
|
|
|
|
|
|
|
local function compose(w1, w2)
|
|
|
|
return wrap_client(function(...)
|
|
|
|
w1(...)
|
|
|
|
w2(...)
|
|
|
|
return --It make no sense to keep a return value
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
wrap_client = function(f)
|
|
|
|
return setmetatable({is_placement=true}, {
|
|
|
|
__call = function(_,...) return f(...) end,
|
|
|
|
__add = compose, -- Composition is usually defined as +
|
|
|
|
__mul = compose -- Make sense if you think of the functions as matrices
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local placement_private = {}
|
|
|
|
|
|
|
|
-- The module is a proxy in front of the "real" functions.
|
|
|
|
-- This allow syntax like:
|
|
|
|
--
|
|
|
|
-- (awful.placement.no_overlap + awful.placement.no_offscreen)(c)
|
|
|
|
--
|
|
|
|
local placement = setmetatable({}, {
|
|
|
|
__index = placement_private,
|
|
|
|
__newindex = function(_, k, f)
|
|
|
|
placement_private[k] = wrap_client(f)
|
|
|
|
end
|
|
|
|
})
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2016-03-18 06:25:36 +01:00
|
|
|
-- 3x3 matrix of the valid sides and corners
|
|
|
|
local corners3x3 = {{"top_left" , "top" , "top_right" },
|
|
|
|
{"left" , nil , "right" },
|
|
|
|
{"bottom_left", "bottom" , "bottom_right"}}
|
|
|
|
|
|
|
|
-- 2x2 matrix of the valid sides and corners
|
|
|
|
local corners2x2 = {{"top_left" , "top_right" },
|
|
|
|
{"bottom_left", "bottom_right"}}
|
|
|
|
|
2016-03-18 07:22:45 +01:00
|
|
|
-- Compute the new `x` and `y`.
|
|
|
|
-- The workarea position need to be applied by the caller
|
|
|
|
local align_map = {
|
|
|
|
top_left = function(_ , _ , _ , _ ) return {x=0 , y=0 } end,
|
|
|
|
top_right = function(sw, _ , dw, _ ) return {x=sw-dw , y=0 } end,
|
|
|
|
bottom_left = function(_ , sh, _ , dh) return {x=0 , y=sh-dh } end,
|
|
|
|
bottom_right = function(sw, sh, dw, dh) return {x=sw-dw , y=sh-dh } end,
|
|
|
|
left = function(_ , sh, _ , dh) return {x=0 , y=sh/2-dh/2} end,
|
|
|
|
right = function(sw, sh, dw, dh) return {x=sw-dw , y=sh/2-dh/2} end,
|
|
|
|
top = function(sw, _ , dw, _ ) return {x=sw/2-dw/2, y=0 } end,
|
|
|
|
bottom = function(sw, sh, dw, dh) return {x=sw/2-dw/2, y=sh-dh } end,
|
|
|
|
centered = function(sw, sh, dw, dh) return {x=sw/2-dw/2, y=sh/2-dh/2} end,
|
|
|
|
center_vertical = function(_ , sh, _ , dh) return {x= nil , y=sh-dh } end,
|
|
|
|
center_horizontal = function(sw, _ , dw, _ ) return {x=sw/2-dw/2, y= nil } end,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Store function -> keys
|
|
|
|
local reverse_align_map = {}
|
|
|
|
|
2016-04-15 11:09:05 +02:00
|
|
|
--- Add a context to the arguments.
|
|
|
|
-- This function extend the argument table. The context is used by some
|
|
|
|
-- internal helper methods. If there already is a context, it has priority and
|
|
|
|
-- is kept.
|
|
|
|
local function add_context(args, context)
|
|
|
|
return setmetatable({context = (args or {}).context or context }, {__index=args})
|
|
|
|
end
|
|
|
|
|
|
|
|
local data = setmetatable({}, { __mode = 'k' })
|
|
|
|
|
|
|
|
--- Store a drawable geometry (per context) in a weak table.
|
|
|
|
-- @param d The drawin
|
|
|
|
-- @tparam string reqtype The context.
|
|
|
|
local function store_geometry(d, reqtype)
|
|
|
|
if not data[d] then data[d] = {} end
|
|
|
|
if not data[d][reqtype] then data[d][reqtype] = {} end
|
|
|
|
data[d][reqtype] = d:geometry()
|
|
|
|
data[d][reqtype].screen = d.screen
|
|
|
|
end
|
|
|
|
|
2016-03-18 06:25:36 +01:00
|
|
|
--- Get the area covered by a drawin.
|
|
|
|
-- @param d The drawin
|
|
|
|
-- @tparam[opt=nil] table new_geo A new geometry
|
|
|
|
-- @tparam[opt=false] boolean ignore_border_width Ignore the border
|
|
|
|
-- @treturn The drawin's area.
|
|
|
|
local function area_common(d, new_geo, ignore_border_width)
|
|
|
|
-- The C side expect no arguments, nil isn't valid
|
|
|
|
local geometry = new_geo and d:geometry(new_geo) or d:geometry()
|
|
|
|
local border = ignore_border_width and 0 or d.border_width or 0
|
|
|
|
geometry.x = geometry.x - border
|
|
|
|
geometry.y = geometry.y - border
|
|
|
|
geometry.width = geometry.width + 2 * border
|
|
|
|
geometry.height = geometry.height + 2 * border
|
|
|
|
return geometry
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get (and optionally set) an object geometry.
|
|
|
|
-- Some elements, such as `mouse` and `screen` don't have a `:geometry()`
|
|
|
|
-- methods.
|
|
|
|
-- @param obj An object
|
|
|
|
-- @tparam table args the method arguments
|
|
|
|
-- @tparam[opt=nil] table new_geo A new geometry to replace the existing one
|
|
|
|
-- @tparam[opt=false] boolean ignore_border_width Ignore the border
|
|
|
|
-- @treturn table A table with *x*, *y*, *width* and *height*.
|
|
|
|
local function geometry_common(obj, args, new_geo, ignore_border_width)
|
2016-04-15 11:09:05 +02:00
|
|
|
|
|
|
|
-- Store the current geometry in a singleton-memento
|
|
|
|
if args.store_geometry and new_geo and args.context then
|
|
|
|
store_geometry(obj, args.context)
|
|
|
|
end
|
|
|
|
|
2016-03-18 06:25:36 +01:00
|
|
|
-- It's a mouse
|
|
|
|
if obj.coords then
|
|
|
|
local coords = new_geo and obj.coords(new_geo) or obj.coords()
|
|
|
|
return {x=coords.x, y=coords.y, width=0, height=0}
|
|
|
|
elseif obj.geometry then
|
|
|
|
local geo = obj.geometry
|
|
|
|
|
|
|
|
-- It is either a drawable or something that implement its API
|
|
|
|
if type(geo) == "function" then
|
|
|
|
local dgeo = area_common(obj, new_geo, ignore_border_width)
|
|
|
|
|
|
|
|
-- Apply the margins
|
|
|
|
if args.margins then
|
|
|
|
local delta = type(args.margins) == "table" and args.margins or {
|
|
|
|
left = args.margins , right = args.margins,
|
|
|
|
top = args.margins , bottom = args.margins
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
x = dgeo.x + (delta.left or 0),
|
|
|
|
y = dgeo.y + (delta.top or 0),
|
|
|
|
width = dgeo.width - (delta.left or 0) - (delta.right or 0),
|
|
|
|
height = dgeo.height - (delta.top or 0) - (delta.bottom or 0),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return dgeo
|
|
|
|
end
|
|
|
|
|
|
|
|
-- It is a screen, it doesn't support setting new sizes.
|
2016-04-05 05:35:03 +02:00
|
|
|
return obj:get_bounding_geometry(args)
|
2016-03-18 06:25:36 +01:00
|
|
|
else
|
|
|
|
assert(false, "Invalid object")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the parent geometry from the standardized arguments API shared by all
|
|
|
|
-- `awful.placement` methods.
|
|
|
|
-- @param obj A screen or a drawable
|
|
|
|
-- @tparam table args the method arguments
|
|
|
|
-- @treturn table A table with *x*, *y*, *width* and *height*.
|
|
|
|
local function get_parent_geometry(obj, args)
|
|
|
|
if args.bounding_rect then
|
|
|
|
return args.bounding_rect
|
|
|
|
elseif args.parent then
|
|
|
|
return geometry_common(args.parent, args)
|
|
|
|
elseif obj.screen then
|
|
|
|
return geometry_common(obj.screen, args)
|
|
|
|
else
|
|
|
|
return geometry_common(capi.screen[capi.mouse.screen], args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Move a point into an area.
|
|
|
|
-- This doesn't change the *width* and *height* values, allowing the target
|
|
|
|
-- area to be smaller than the source one.
|
|
|
|
-- @tparam table source The (larger) geometry to move `target` into
|
|
|
|
-- @tparam table target The area to move into `source`
|
|
|
|
-- @treturn table A table with *x* and *y* keys
|
|
|
|
local function move_into_geometry(source, target)
|
|
|
|
local ret = {x = target.x, y = target.y}
|
|
|
|
|
|
|
|
-- Horizontally
|
|
|
|
if ret.x < source.x then
|
|
|
|
ret.x = source.x
|
|
|
|
elseif ret.x > source.x + source.width then
|
|
|
|
ret.x = source.x + source.width - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Vertically
|
|
|
|
if ret.y < source.y then
|
|
|
|
ret.y = source.y
|
|
|
|
elseif ret.y > source.y + source.height then
|
|
|
|
ret.y = source.y + source.height - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2016-03-18 07:22:45 +01:00
|
|
|
-- Update the workarea
|
|
|
|
local function wibox_update_strut(d, position)
|
|
|
|
-- If the drawable isn't visible, remove the struts
|
|
|
|
if not d.visible then
|
|
|
|
d:struts { left = 0, right = 0, bottom = 0, top = 0 }
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Detect horizontal or vertical drawables
|
|
|
|
local geo = area_common(d)
|
|
|
|
local vertical = geo.width < geo.height
|
|
|
|
|
|
|
|
-- Look into the `position` string to find the relevants sides to crop from
|
|
|
|
-- the workarea
|
|
|
|
local struts = { left = 0, right = 0, bottom = 0, top = 0 }
|
|
|
|
|
|
|
|
if vertical then
|
|
|
|
for _, v in ipairs {"right", "left"} do
|
|
|
|
if (not position) or position:match(v) then
|
|
|
|
struts[v] = geo.width
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for _, v in ipairs {"top", "bottom"} do
|
|
|
|
if (not position) or position:match(v) then
|
|
|
|
struts[v] = geo.height
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Update the workarea
|
|
|
|
d:struts(struts)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Pin a drawable to a placement function.
|
|
|
|
-- Automatically update the position when the size change.
|
|
|
|
-- All other arguments will be passed to the `position` function (if any)
|
|
|
|
-- @tparam[opt=client.focus] drawable d A drawable (like `client`, `mouse`
|
|
|
|
-- or `wibox`)
|
|
|
|
-- @param position_f A position name (see `align`) or a position function
|
|
|
|
-- @tparam[opt={}] table args Other arguments
|
|
|
|
local function attach(d, position_f, args)
|
|
|
|
args = args or {}
|
|
|
|
|
|
|
|
if not args.attach then return end
|
|
|
|
|
|
|
|
d = d or capi.client.focus
|
|
|
|
if not d then return end
|
|
|
|
|
|
|
|
if type(position_f) == "string" then
|
|
|
|
position_f = placement[position_f]
|
|
|
|
end
|
|
|
|
|
|
|
|
if not position_f then return end
|
|
|
|
|
|
|
|
local function tracker()
|
|
|
|
position_f(d, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
d:connect_signal("property::width" , tracker)
|
|
|
|
d:connect_signal("property::height", tracker)
|
|
|
|
|
|
|
|
tracker()
|
|
|
|
|
|
|
|
if args.update_workarea then
|
|
|
|
local function tracker_struts()
|
|
|
|
--TODO this is too fragile and doesn't work with all methods.
|
|
|
|
wibox_update_strut(d, reverse_align_map[position_f])
|
|
|
|
end
|
|
|
|
|
|
|
|
d:connect_signal("property::geometry" , tracker_struts)
|
|
|
|
d:connect_signal("property::visible" , tracker_struts)
|
|
|
|
|
|
|
|
tracker_struts()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If there is a parent drawable, screen or mouse, also track it
|
|
|
|
local parent = args.parent or d.screen
|
|
|
|
if parent then
|
|
|
|
args.parent:connect_signal("property::geometry" , tracker)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2016-03-18 06:25:36 +01:00
|
|
|
--- Move a drawable to the closest corner of the parent geometry (such as the
|
|
|
|
-- screen).
|
|
|
|
--
|
|
|
|
-- Valid arguments include the common ones and:
|
|
|
|
--
|
|
|
|
-- * **include_sides**: Also include the left, right, top and bottom positions
|
|
|
|
--
|
|
|
|
--@DOC_awful_placement_closest_mouse_EXAMPLE@
|
|
|
|
-- @tparam[opt=client.focus] drawable d A drawable (like `client`, `mouse`
|
|
|
|
-- or `wibox`)
|
|
|
|
-- @tparam[opt={}] table args The arguments
|
|
|
|
-- @treturn string The corner name
|
|
|
|
function placement.closest_corner(d, args)
|
2016-04-15 11:09:05 +02:00
|
|
|
args = add_context(args, "closest_corner")
|
2016-03-18 06:25:36 +01:00
|
|
|
d = d or capi.client.focus
|
|
|
|
|
|
|
|
local sgeo = get_parent_geometry(d, args)
|
|
|
|
local dgeo = geometry_common(d, args)
|
|
|
|
|
|
|
|
local pos = move_into_geometry(sgeo, dgeo)
|
|
|
|
|
|
|
|
local corner_i, corner_j, n
|
|
|
|
|
|
|
|
-- Use the product of 3 to get the closest point in a NxN matrix
|
|
|
|
local function f(_n, mat)
|
|
|
|
n = _n
|
|
|
|
corner_i = -math.ceil( ( (sgeo.x - pos.x) * n) / sgeo.width )
|
|
|
|
corner_j = -math.ceil( ( (sgeo.y - pos.y) * n) / sgeo.height )
|
|
|
|
return mat[corner_j + 1][corner_i + 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Turn the area into a grid and snap to the cloest point. This size of the
|
|
|
|
-- grid will increase the accuracy. A 2x2 matrix only include the corners,
|
|
|
|
-- at 3x3, this include the sides too technically, a random size would work,
|
|
|
|
-- but without corner names.
|
|
|
|
local grid_size = args.include_sides and 3 or 2
|
|
|
|
|
|
|
|
-- If the point is in the center, use the closest corner
|
2016-04-10 09:22:23 +02:00
|
|
|
local corner = grid_size == 3 and f(3, corners3x3) or f(2, corners2x2)
|
2016-03-18 06:25:36 +01:00
|
|
|
|
|
|
|
-- Transpose the corner back to the original size
|
|
|
|
local new_args = setmetatable({position = corner}, {__index=args})
|
2016-04-16 11:49:17 +02:00
|
|
|
placement_private.align(d, new_args)
|
2016-03-18 06:25:36 +01:00
|
|
|
|
|
|
|
return corner
|
2015-10-24 08:44:50 +02:00
|
|
|
end
|
|
|
|
|
2015-08-14 03:02:52 +02:00
|
|
|
--- Place the client so no part of it will be outside the screen (workarea).
|
2016-03-17 09:09:16 +01:00
|
|
|
--@DOC_awful_placement_no_offscreen_EXAMPLE@
|
2015-08-14 03:02:52 +02:00
|
|
|
-- @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)
|
2016-04-16 11:49:17 +02:00
|
|
|
--HACK necessary for composition to work. The API will be changed soon
|
|
|
|
if type(screen) == "table" then
|
|
|
|
screen = nil
|
|
|
|
end
|
|
|
|
|
2016-02-07 15:02:25 +01:00
|
|
|
c = c or capi.client.focus
|
2016-03-18 06:25:36 +01:00
|
|
|
local geometry = area_common(c)
|
2016-02-26 19:08:35 +01:00
|
|
|
screen = get_screen(screen or c.screen or a_screen.getbycoord(geometry.x, geometry.y))
|
|
|
|
local screen_geometry = 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.
|
2016-03-17 09:09:16 +01:00
|
|
|
--@DOC_awful_placement_no_overlap_EXAMPLE@
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @param c The client.
|
2012-06-12 20:13:09 +02:00
|
|
|
function placement.no_overlap(c)
|
2016-03-17 09:09:16 +01:00
|
|
|
c = c or capi.client.focus
|
2016-03-18 06:25:36 +01:00
|
|
|
local geometry = area_common(c)
|
2016-02-26 19:08:35 +01:00
|
|
|
local screen = get_screen(c.screen or a_screen.getbycoord(geometry.x, geometry.y))
|
2016-02-27 09:24:19 +01:00
|
|
|
local cls = client.visible(screen)
|
2012-01-15 11:57:47 +01:00
|
|
|
local curlay = layout.get()
|
2016-02-26 19:08:35 +01:00
|
|
|
local areas = { screen.workarea }
|
2016-02-07 15:02:25 +01:00
|
|
|
for _, cl in pairs(cls) do
|
2016-03-14 07:21:29 +01:00
|
|
|
if cl ~= c and cl.type ~= "desktop" and (cl.floating or curlay == layout.suit.floating) then
|
2016-03-18 06:25:36 +01:00
|
|
|
areas = area_remove(areas, area_common(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 }
|
2016-02-07 15:02:25 +01:00
|
|
|
for _, 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
|
2016-02-07 15:02:25 +01:00
|
|
|
for _, r in ipairs(areas) do
|
2008-09-29 16:49:18 +02:00
|
|
|
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.
|
2016-03-17 09:09:16 +01:00
|
|
|
--@DOC_awful_placement_under_mouse_EXAMPLE@
|
2008-09-29 16:49:18 +02:00
|
|
|
-- @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)
|
2016-02-07 15:02:25 +01:00
|
|
|
c = c or capi.client.focus
|
2016-03-18 06:25:36 +01:00
|
|
|
local c_geometry = area_common(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.
|
2016-03-17 09:09:16 +01:00
|
|
|
--@DOC_awful_placement_next_to_mouse_EXAMPLE@
|
2015-08-02 19:36:56 +02:00
|
|
|
-- @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)
|
2016-03-18 06:25:36 +01:00
|
|
|
local c_geometry = area_common(c)
|
2015-10-24 08:44:50 +02:00
|
|
|
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
|
|
|
|
|
2016-02-07 15:02:25 +01:00
|
|
|
local x, y
|
|
|
|
|
2015-08-02 19:36:56 +02:00
|
|
|
-- Prefer it to be on the right.
|
2016-02-07 15:02:25 +01:00
|
|
|
x = m_coords.x + offset
|
2015-08-02 19:36:56 +02:00
|
|
|
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
|
|
|
|
|
2016-03-18 07:22:45 +01:00
|
|
|
--- Move the drawable (client or wibox) `d` to a screen position or side.
|
|
|
|
--
|
|
|
|
-- Supported args.positions are:
|
|
|
|
--
|
|
|
|
-- * top_left
|
|
|
|
-- * top_right
|
|
|
|
-- * bottom_left
|
|
|
|
-- * bottom_right
|
|
|
|
-- * left
|
|
|
|
-- * right
|
|
|
|
-- * top
|
|
|
|
-- * bottom
|
|
|
|
-- * centered
|
|
|
|
-- * center_vertical
|
|
|
|
-- * center_horizontal
|
|
|
|
--
|
|
|
|
--@DOC_awful_placement_align_EXAMPLE@
|
|
|
|
-- @tparam drawable d A drawable (like `client`, `mouse` or `wibox`)
|
|
|
|
-- @tparam[opt={}] table args Other arguments
|
|
|
|
function placement.align(d, args)
|
2016-04-15 11:09:05 +02:00
|
|
|
args = add_context(args, "align")
|
2016-03-18 07:22:45 +01:00
|
|
|
d = d or capi.client.focus
|
|
|
|
|
|
|
|
if not d or not args.position then return end
|
|
|
|
|
|
|
|
local sgeo = get_parent_geometry(d, args)
|
|
|
|
local dgeo = geometry_common(d, args)
|
|
|
|
local bw = d.border_width or 0
|
|
|
|
|
|
|
|
local pos = align_map[args.position](
|
|
|
|
sgeo.width ,
|
|
|
|
sgeo.height,
|
|
|
|
dgeo.width ,
|
|
|
|
dgeo.height
|
|
|
|
)
|
|
|
|
|
|
|
|
geometry_common(d, args, {
|
|
|
|
x = (pos.x and math.ceil(sgeo.x + pos.x) or dgeo.x) + bw ,
|
|
|
|
y = (pos.y and math.ceil(sgeo.y + pos.y) or dgeo.y) + bw ,
|
|
|
|
width = math.ceil(dgeo.width ) - 2*bw,
|
|
|
|
height = math.ceil(dgeo.height ) - 2*bw,
|
|
|
|
})
|
|
|
|
|
|
|
|
attach(d, placement[args.position], args)
|
|
|
|
end
|
|
|
|
|
2016-03-18 07:32:12 +01:00
|
|
|
-- Add the alias functions
|
|
|
|
for k in pairs(align_map) do
|
|
|
|
placement[k] = function(d, args)
|
2016-04-15 11:09:05 +02:00
|
|
|
args = add_context(args, k)
|
|
|
|
args.position = k
|
2016-04-16 11:49:17 +02:00
|
|
|
placement_private.align(d, args)
|
2009-02-14 20:41:57 +01:00
|
|
|
end
|
2016-03-18 07:32:12 +01:00
|
|
|
reverse_align_map[placement[k]] = k
|
2009-02-13 23:56:26 +01:00
|
|
|
end
|
|
|
|
|
2016-03-18 07:32:12 +01:00
|
|
|
-- Add the documentation for align alias
|
2009-09-13 14:50:53 +02:00
|
|
|
|
2016-03-18 07:32:12 +01:00
|
|
|
---@DOC_awful_placement_top_left_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_top_right_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_bottom_left_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_bottom_right_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_left_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_right_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_top_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_bottom_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_centered_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_center_vertical_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_center_horizontal_EXAMPLE@
|
2009-09-13 14:50:53 +02:00
|
|
|
|
2016-03-15 05:18:42 +01:00
|
|
|
--- Stretch a drawable in a specific direction.
|
|
|
|
-- Valid args:
|
|
|
|
--
|
|
|
|
-- * **direction**: The stretch direction (*left*, *right*, *up*, *down*) or
|
|
|
|
-- a table with multiple directions.
|
|
|
|
--
|
|
|
|
--@DOC_awful_placement_stretch_EXAMPLE@
|
|
|
|
-- @tparam[opt=client.focus] drawable d A drawable (like `client` or `wibox`)
|
|
|
|
-- @tparam[opt={}] table args The arguments
|
|
|
|
function placement.stretch(d, args)
|
2016-04-15 11:09:05 +02:00
|
|
|
args = add_context(args, "stretch")
|
2016-03-15 05:18:42 +01:00
|
|
|
|
|
|
|
d = d or capi.client.focus
|
|
|
|
if not d or not args.direction then return end
|
|
|
|
|
|
|
|
-- In case there is multiple directions, call `stretch` for each of them
|
|
|
|
if type(args.direction) == "table" then
|
|
|
|
for _, dir in ipairs(args.direction) do
|
2016-04-15 11:09:05 +02:00
|
|
|
args.direction = dir
|
2016-04-16 11:49:17 +02:00
|
|
|
placement_private.stretch(dir, args)
|
2016-03-15 05:18:42 +01:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local sgeo = get_parent_geometry(d, args)
|
|
|
|
local dgeo = geometry_common(d, args)
|
|
|
|
local ngeo = geometry_common(d, args, nil, true)
|
|
|
|
local bw = d.border_width or 0
|
|
|
|
|
|
|
|
if args.direction == "left" then
|
|
|
|
ngeo.x = sgeo.x + bw
|
|
|
|
ngeo.width = dgeo.width + (dgeo.x - ngeo.x)
|
|
|
|
elseif args.direction == "right" then
|
|
|
|
ngeo.width = sgeo.width - ngeo.x - bw
|
|
|
|
elseif args.direction == "up" then
|
|
|
|
ngeo.y = sgeo.y + bw
|
|
|
|
ngeo.height = dgeo.height + (dgeo.y - ngeo.y)
|
|
|
|
elseif args.direction == "down" then
|
|
|
|
ngeo.height = sgeo.height - dgeo.y - bw
|
|
|
|
else
|
|
|
|
assert(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Avoid negative sizes if args.parent isn't compatible
|
|
|
|
ngeo.width = math.max(args.minimim_width or 1, ngeo.width )
|
|
|
|
ngeo.height = math.max(args.minimim_height or 1, ngeo.height)
|
|
|
|
|
|
|
|
geometry_common(d, args, ngeo)
|
|
|
|
|
|
|
|
attach(d, placement["stretch_"..args.direction], args)
|
|
|
|
end
|
|
|
|
|
2016-03-15 07:09:32 +01:00
|
|
|
-- Add the alias functions
|
|
|
|
for _,v in ipairs {"left", "right", "up", "down"} do
|
|
|
|
placement["stretch_"..v] = function(d, args)
|
2016-04-15 11:09:05 +02:00
|
|
|
args = add_context(args, "stretch_"..v)
|
|
|
|
args.direction = v
|
2016-04-16 11:49:17 +02:00
|
|
|
placement_private.stretch(d, args)
|
2016-03-15 07:09:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@DOC_awful_placement_stretch_left_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_stretch_right_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_stretch_up_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_stretch_down_EXAMPLE@
|
|
|
|
|
2016-03-24 04:01:52 +01:00
|
|
|
--- Maximize a drawable horizontally, vertically or both.
|
|
|
|
-- Valid args:
|
|
|
|
--
|
|
|
|
-- * *axis*:The axis (vertical or horizontal). If none is
|
|
|
|
-- specified, then the drawable will be maximized on both axis.
|
|
|
|
--
|
|
|
|
--@DOC_awful_placement_maximize_EXAMPLE@
|
|
|
|
-- @tparam[opt=client.focus] drawable d A drawable (like `client` or `wibox`)
|
|
|
|
-- @tparam[opt={}] table args The arguments
|
|
|
|
function placement.maximize(d, args)
|
2016-04-15 11:09:05 +02:00
|
|
|
args = add_context(args, "maximize")
|
2016-03-24 04:01:52 +01:00
|
|
|
d = d or capi.client.focus
|
|
|
|
|
|
|
|
if not d then return end
|
|
|
|
|
|
|
|
local sgeo = get_parent_geometry(d, args)
|
|
|
|
local ngeo = geometry_common(d, args, nil, true)
|
|
|
|
local bw = d.border_width or 0
|
|
|
|
|
|
|
|
if (not args.axis) or args.axis :match "vertical" then
|
|
|
|
ngeo.y = sgeo.y + bw
|
|
|
|
ngeo.height = sgeo.height - 2*bw
|
|
|
|
end
|
|
|
|
|
|
|
|
if (not args.axis) or args.axis :match "horizontal" then
|
|
|
|
ngeo.x = sgeo.x + bw
|
|
|
|
ngeo.width = sgeo.width - 2*bw
|
|
|
|
end
|
|
|
|
|
|
|
|
geometry_common(d, args, ngeo)
|
|
|
|
|
|
|
|
attach(d, placement.maximize, args)
|
|
|
|
end
|
|
|
|
|
2016-03-24 04:56:28 +01:00
|
|
|
-- Add the alias functions
|
|
|
|
for _, v in ipairs {"vertically", "horizontally"} do
|
|
|
|
placement["maximize_"..v] = function(d2, args)
|
2016-04-15 11:09:05 +02:00
|
|
|
args = add_context(args, "maximize_"..v)
|
|
|
|
args.axis = v
|
2016-04-16 11:49:17 +02:00
|
|
|
placement_private.maximize(d2, args)
|
2016-03-24 04:56:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@DOC_awful_placement_maximize_vertically_EXAMPLE@
|
|
|
|
|
|
|
|
---@DOC_awful_placement_maximize_horizontally_EXAMPLE@
|
|
|
|
|
2016-04-15 11:09:05 +02:00
|
|
|
--- Restore the geometry.
|
|
|
|
-- @tparam[opt=client.focus] drawable d A drawable (like `client` or `wibox`)
|
|
|
|
-- @tparam[opt={}] table args The arguments
|
|
|
|
-- @treturn boolean If the geometry was restored
|
|
|
|
function placement.restore(d, args)
|
|
|
|
if not args or not args.context then return false end
|
|
|
|
d = d or capi.client.focus
|
|
|
|
|
|
|
|
if not data[d] then return false end
|
|
|
|
|
|
|
|
local memento = data[d][args.context]
|
|
|
|
|
|
|
|
if not memento then return false end
|
|
|
|
|
|
|
|
memento.screen = nil --TODO use it
|
|
|
|
d:geometry(memento)
|
|
|
|
return true
|
|
|
|
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
|