get_rect_by_dir: Move to `gears.geometry`

Begin to break down `awful.util`
This commit is contained in:
Emmanuel Lepage Vallee 2016-05-16 00:57:15 -04:00
parent 1a05d53cab
commit 1ce92bb550
5 changed files with 83 additions and 67 deletions

View File

@ -11,6 +11,7 @@
local util = require("awful.util")
local spawn = require("awful.spawn")
local object = require("gears.object")
local grect = require("gears.geometry").rectangle
local pairs = pairs
local type = type
local ipairs = ipairs
@ -197,7 +198,7 @@ function client.swap.bydirection(dir, c, stacked)
for i,cl in ipairs(cltbl) do
geomtbl[i] = cl:geometry()
end
local target = util.get_rectangle_in_direction(dir, geomtbl, sel:geometry())
local target = grect.get_in_direction(dir, geomtbl, sel:geometry())
-- If we found a client to swap with, then go for it
if target then

View File

@ -6,7 +6,7 @@
-- @release @AWESOME_VERSION@
-- @submodule client
---------------------------------------------------------------------------
local util = require("awful.util")
local grect = require("gears.geometry").rectangle
local capi =
{
@ -168,7 +168,7 @@ function focus.bydirection(dir, c, stacked)
geomtbl[i] = cl:geometry()
end
local target = util.get_rectangle_in_direction(dir, geomtbl, sel:geometry())
local target = grect.get_in_direction(dir, geomtbl, sel:geometry())
-- If we found a client to focus, then do it.
if target then
@ -200,7 +200,7 @@ function focus.global_bydirection(dir, c, stacked)
for i,cl in ipairs(cltbl) do
geomtbl[i] = cl:geometry()
end
local target = util.get_rectangle_in_direction(dir, geomtbl, scr.geometry)
local target = grect.get_in_direction(dir, geomtbl, scr.geometry)
if target then
cltbl[target]:emit_signal("request::activate",

View File

@ -137,7 +137,7 @@ function screen.focus_bydirection(dir, _screen)
for s in capi.screen do
geomtbl[s] = capi.screen[s].geometry
end
local target = util.get_rectangle_in_direction(dir, geomtbl, sel.geometry)
local target = grect.get_in_direction(dir, geomtbl, sel.geometry)
if target then
return screen.focus(target)
end

View File

@ -20,6 +20,7 @@ local type = type
local rtable = table
local string = string
local lgi = require("lgi")
local grect = require("gears.geometry").rectangle
local Gio = require("lgi").Gio
local Pango = lgi.Pango
local capi =
@ -301,76 +302,18 @@ function util.subsets(set)
return ret
end
--- Return true whether rectangle B is in the right direction
-- compared to rectangle A.
-- @param dir The direction.
-- @param gA The geometric specification for rectangle A.
-- @param gB The geometric specification for rectangle B.
-- @return True if B is in the direction of A.
local function is_in_direction(dir, gA, gB)
if dir == "up" then
return gA.y > gB.y
elseif dir == "down" then
return gA.y < gB.y
elseif dir == "left" then
return gA.x > gB.x
elseif dir == "right" then
return gA.x < gB.x
end
return false
end
--- Calculate distance between two points.
-- i.e: if we want to move to the right, we will take the right border
-- of the currently focused screen and the left side of the checked screen.
-- @param dir The direction.
-- @param _gA The first rectangle.
-- @param _gB The second rectangle.
-- @return The distance between the screens.
local function calculate_distance(dir, _gA, _gB)
local gAx = _gA.x
local gAy = _gA.y
local gBx = _gB.x
local gBy = _gB.y
if dir == "up" then
gBy = _gB.y + _gB.height
elseif dir == "down" then
gAy = _gA.y + _gA.height
elseif dir == "left" then
gBx = _gB.x + _gB.width
elseif dir == "right" then
gAx = _gA.x + _gA.width
end
return math.sqrt(math.pow(gBx - gAx, 2) + math.pow(gBy - gAy, 2))
end
--- Get the nearest rectangle in the given direction. Every rectangle is specified as a table
-- with 'x', 'y', 'width', 'height' keys, the same as client or screen geometries.
-- @deprecated awful.util.get_rectangle_in_direction
-- @param dir The direction, can be either "up", "down", "left" or "right".
-- @param recttbl A table of rectangle specifications.
-- @param cur The current rectangle.
-- @return The index for the rectangle in recttbl closer to cur in the given direction. nil if none found.
-- @see gears.geometry
function util.get_rectangle_in_direction(dir, recttbl, cur)
local dist, dist_min
local target = nil
util.deprecate("gears.geometry.rectangle.get_in_direction")
-- We check each object
for i, rect in pairs(recttbl) do
-- Check geometry to see if object is located in the right direction.
if is_in_direction(dir, cur, rect) then
-- Calculate distance between current and checked object.
dist = calculate_distance(dir, cur, rect)
-- If distance is shorter then keep the object.
if not target or dist < dist_min then
target = i
dist_min = dist
end
end
end
return target
return grect.get_in_direction(dir, recttbl, cur)
end
--- Join all tables given as parameters.

View File

@ -76,4 +76,76 @@ function gears.geometry.rectangle.get_by_coord(list, x, y)
end
end
--- Return true whether rectangle B is in the right direction
-- compared to rectangle A.
-- @param dir The direction.
-- @param gA The geometric specification for rectangle A.
-- @param gB The geometric specification for rectangle B.
-- @return True if B is in the direction of A.
local function is_in_direction(dir, gA, gB)
if dir == "up" then
return gA.y > gB.y
elseif dir == "down" then
return gA.y < gB.y
elseif dir == "left" then
return gA.x > gB.x
elseif dir == "right" then
return gA.x < gB.x
end
return false
end
--- Calculate distance between two points.
-- i.e: if we want to move to the right, we will take the right border
-- of the currently focused screen and the left side of the checked screen.
-- @param dir The direction.
-- @param _gA The first rectangle.
-- @param _gB The second rectangle.
-- @return The distance between the screens.
local function calculate_distance(dir, _gA, _gB)
local gAx = _gA.x
local gAy = _gA.y
local gBx = _gB.x
local gBy = _gB.y
if dir == "up" then
gBy = _gB.y + _gB.height
elseif dir == "down" then
gAy = _gA.y + _gA.height
elseif dir == "left" then
gBx = _gB.x + _gB.width
elseif dir == "right" then
gAx = _gA.x + _gA.width
end
return math.sqrt(math.pow(gBx - gAx, 2) + math.pow(gBy - gAy, 2))
end
--- Get the nearest rectangle in the given direction. Every rectangle is specified as a table
-- with *x*, *y*, *width*, *height* keys, the same as client or screen geometries.
-- @tparam string dir The direction, can be either *up*, *down*, *left* or *right*.
-- @tparam table recttbl A table of rectangle specifications.
-- @tparam table cur The current rectangle.
-- @return The index for the rectangle in recttbl closer to cur in the given direction. nil if none found.
function gears.geometry.rectangle.get_in_direction(dir, recttbl, cur)
local dist, dist_min
local target = nil
-- We check each object
for i, rect in pairs(recttbl) do
-- Check geometry to see if object is located in the right direction.
if is_in_direction(dir, cur, rect) then
-- Calculate distance between current and checked object.
dist = calculate_distance(dir, cur, rect)
-- If distance is shorter then keep the object.
if not target or dist < dist_min then
target = i
dist_min = dist
end
end
end
return target
end
return gears.geometry