screen: Use `gears.geometry`

This commit is contained in:
Emmanuel Lepage Vallee 2016-05-16 00:30:44 -04:00
parent dd121623b5
commit 1a05d53cab
1 changed files with 12 additions and 26 deletions

View File

@ -16,6 +16,7 @@ local capi =
} }
local util = require("awful.util") local util = require("awful.util")
local object = require("gears.object") local object = require("gears.object")
local grect = require("gears.geometry").rectangle
local function get_screen(s) local function get_screen(s)
return s and capi.screen[s] return s and capi.screen[s]
@ -62,20 +63,7 @@ end
-- @tparam number y Y coordinate of point -- @tparam number y Y coordinate of point
-- @treturn number The squared distance of the screen to the provided point -- @treturn number The squared distance of the screen to the provided point
function screen.object.get_square_distance(self, x, y) function screen.object.get_square_distance(self, x, y)
self = get_screen(self) return grect.get_square_distance(get_screen(self).geometry, x, y)
local geom = self.geometry
local dist_x, dist_y = 0, 0
if x < geom.x then
dist_x = geom.x - x
elseif x >= geom.x + geom.width then
dist_x = x - geom.x - geom.width + 1
end
if y < geom.y then
dist_y = geom.y - y
elseif y >= geom.y + geom.height then
dist_y = y - geom.y - geom.height + 1
end
return dist_x * dist_x + dist_y * dist_y
end end
--- ---
@ -83,20 +71,18 @@ end
-- The number returned can be used as an index into the global -- The number returned can be used as an index into the global
-- `screen` table/object. -- `screen` table/object.
-- @function awful.screen.getbycoord -- @function awful.screen.getbycoord
-- @param x The x coordinate -- @tparam number x The x coordinate
-- @param y The y coordinate -- @tparam number y The y coordinate
-- @treturn ?number The screen index
function screen.getbycoord(x, y) function screen.getbycoord(x, y)
local dist = math.huge local s, sgeos = capi.screen.primary, {}
local s = capi.screen.primary
if s then for scr in capi.screen do
dist = screen.object.get_square_distance(s, x, y) sgeos[scr] = scr.geometry
end
for i in capi.screen do
local d = screen.object.get_square_distance(i, x, y)
if d < dist then
s, dist = capi.screen[i], d
end
end end
s = grect.get_closest_by_coord(sgeos, x, y) or s
return s and s.index return s and s.index
end end