From 9cb60b81300b07128976a0bcabb5535ad2b1ba91 Mon Sep 17 00:00:00 2001 From: Christoph Mertz Date: Sun, 5 Mar 2017 04:16:15 +0100 Subject: [PATCH] awful.screen: extract screen:get_next_in_direction() (#1597) This extracts the code for finding the next screen from focus_bydirection to a separate method on the screen object. The main reason was to use the finding code without actually changing the screen focus but this should incidentally make the code slightly easier to test as well since both concerns can be tested in isolation. Signed-off-by: Christoph Mertz --- lib/awful/screen.lua | 35 ++++++++++++++++++++++++++--------- lib/gears/geometry.lua | 2 +- spec/awful/screen_spec.lua | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/lib/awful/screen.lua b/lib/awful/screen.lua index 89a88bff..3627df9b 100644 --- a/lib/awful/screen.lua +++ b/lib/awful/screen.lua @@ -122,6 +122,28 @@ function screen.focus(_screen) end end +--- Get the next screen in a specific direction. +-- +-- This gets the next screen relative to this one in +-- the specified direction. +-- +-- @function screen:get_next_in_direction +-- @param self Screen. +-- @param dir The direction, can be either "up", "down", "left" or "right". +function screen.object.get_next_in_direction(self, dir) + local sel = get_screen(self) + if not sel then + return + end + + local geomtbl = {} + for s in capi.screen do + geomtbl[s] = s.geometry + end + + return grect.get_in_direction(dir, geomtbl, sel.geometry) +end + --- Move the focus to a screen in a specific direction. -- -- This moves the mouse pointer to the last known position on the new screen, @@ -131,15 +153,10 @@ end -- @param _screen Screen. function screen.focus_bydirection(dir, _screen) local sel = get_screen(_screen or screen.focused()) - if sel then - local geomtbl = {} - for s in capi.screen do - geomtbl[s] = s.geometry - end - local target = grect.get_in_direction(dir, geomtbl, sel.geometry) - if target then - return screen.focus(target) - end + local target = sel:get_next_in_direction(dir) + + if target then + return target:focus() end end diff --git a/lib/gears/geometry.lua b/lib/gears/geometry.lua index 2b1f2048..a5144356 100644 --- a/lib/gears/geometry.lua +++ b/lib/gears/geometry.lua @@ -117,7 +117,7 @@ local function calculate_distance(dir, _gA, _gB) gAx = _gA.x + _gA.width end - return math.sqrt(math.pow(gBx - gAx, 2) + math.pow(gBy - gAy, 2)) + return math.sqrt((gBx - gAx) ^ 2 + (gBy - gAy) ^ 2) end --- Get the nearest rectangle in the given direction. Every rectangle is specified as a table diff --git a/spec/awful/screen_spec.lua b/spec/awful/screen_spec.lua index 7a9a31ee..cc5a4994 100644 --- a/spec/awful/screen_spec.lua +++ b/spec/awful/screen_spec.lua @@ -76,6 +76,40 @@ describe("awful.screen", function() end) end) + describe("get_next_in_direction", function() + before_each(function() + fake_screens = { + { geometry = { x = 0, y = 0, width = 1, height = 1 }, index = 1 }, + { geometry = { x = 1, y = 0, width = 1, height = 1 }, index = 2 }, + { geometry = { x = 0, y = 1, width = 1, height = 1 }, index = 3 }, + { geometry = { x = 1, y = 1, width = 1, height = 1 }, index = 4 }, + } + end) + + it("gets screens to the left", function() + assert.is.equal(fake_screens[1], ascreen.object.get_next_in_direction(fake_screens[2], "left")) + end) + + it("gets screens to the right", function() + assert.is.equal(fake_screens[2], ascreen.object.get_next_in_direction(fake_screens[1], "right")) + end) + + it("gets screens to the top", function() + assert.is.equal(fake_screens[2], ascreen.object.get_next_in_direction(fake_screens[4], "up")) + end) + + it("gets screens to the bottom", function() + assert.is.equal(fake_screens[4], ascreen.object.get_next_in_direction(fake_screens[2], "down")) + end) + + it("gets no screens if none exist in the direction", function() + assert.is_nil(ascreen.object.get_next_in_direction(fake_screens[2], "up")) + assert.is_nil(ascreen.object.get_next_in_direction(fake_screens[4], "down")) + assert.is_nil(ascreen.object.get_next_in_direction(fake_screens[2], "right")) + assert.is_nil(ascreen.object.get_next_in_direction(fake_screens[1], "left")) + end) + end) + describe("no screens", function() before_each(function() fake_screens = {}