From dd121623b514d67c48b7aa7008ff276b4504d370 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 15 May 2016 23:59:59 -0400 Subject: [PATCH] gears.geometry: Mutualize getbycoord --- lib/gears/geometry.lua | 19 +++++++++++++++++++ lib/wibox/drawable.lua | 24 ++++++++++-------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/gears/geometry.lua b/lib/gears/geometry.lua index b578eea8..8fcf7c47 100644 --- a/lib/gears/geometry.lua +++ b/lib/gears/geometry.lua @@ -57,4 +57,23 @@ function gears.geometry.rectangle.get_closest_by_coord(list, x, y) return ret end +--- Return the rectangle containing the [x, y] point. +-- +-- Note that if multiple element from the geometry list contains the point, the +-- returned result is nondeterministic. +-- +-- @tparam table list A list of geometry tables. +-- @tparam number x The x coordinate +-- @tparam number y The y coordinate +-- @return The key from the closest geometry. In case no result is found, *nil* +-- is returned. +function gears.geometry.rectangle.get_by_coord(list, x, y) + for k, geometry in pairs(list) do + if x >= geometry.x and x < geometry.x + geometry.width + and y >= geometry.y and y < geometry.y + geometry.height then + return k + end + end +end + return gears.geometry diff --git a/lib/wibox/drawable.lua b/lib/wibox/drawable.lua index 9d8627cf..1f2c038e 100644 --- a/lib/wibox/drawable.lua +++ b/lib/wibox/drawable.lua @@ -19,30 +19,26 @@ local color = require("gears.color") local object = require("gears.object") local surface = require("gears.surface") local timer = require("gears.timer") +local grect = require("gears.geometry").rectangle local matrix = require("gears.matrix") local hierarchy = require("wibox.hierarchy") local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1) local drawables = setmetatable({}, { __mode = 'k' }) --- This is awful.screen.getbycoord() which we sadly cannot use from here (cyclic --- dependencies are bad!) -local function screen_getbycoord(x, y) - for i in screen do - local geometry = screen[i].geometry - if x >= geometry.x and x < geometry.x + geometry.width - and y >= geometry.y and y < geometry.y + geometry.height then - return capi.screen[i] - end - end - return capi.screen.primary -end - -- Get the widget context. This should always return the same table (if -- possible), so that our draw and fit caches can work efficiently. local function get_widget_context(self) local geom = self.drawable:geometry() - local s = screen_getbycoord(geom.x, geom.y) + + local sgeos = {} + + for s in capi.screen do + sgeos[s] = s.geometry + end + + local s = grect.get_by_coord(sgeos, geom.x, geom.y) or capi.screen.primary + local context = self._widget_context local dpi = beautiful.xresources.get_dpi(s) if (not context) or context.screen ~= s or context.dpi ~= dpi then