diff --git a/lib/awful/placement.lua b/lib/awful/placement.lua index 0aa3c58aa..0c6338a76 100644 --- a/lib/awful/placement.lua +++ b/lib/awful/placement.lua @@ -21,6 +21,7 @@ local capi = local client = require("awful.client") local layout = require("awful.layout") local a_screen = require("awful.screen") +local dpi = require("beautiful").xresources.apply_dpi local placement = {} @@ -192,6 +193,42 @@ function placement.under_mouse(c) y = m_coords.y - c_geometry.height / 2 }) end +--- 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. +-- @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) + local c_geometry = c:geometry() + local c_width = c_geometry.width + (c.border_width * 2) + local c_height = c_geometry.height + (c.border_width * 2) + local m_coords = capi.mouse.coords() + local screen_geometry = capi.screen[capi.mouse.screen].workarea + + -- Prefer it to be on the right. + local x = m_coords.x + offset + 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 + --- Place the client centered with respect to a parent or the clients screen. -- @param c The client. -- @param[opt] p The parent (nil for screen centering). diff --git a/lib/awful/tooltip.lua b/lib/awful/tooltip.lua index 2f4b12483..e26326db1 100644 --- a/lib/awful/tooltip.lua +++ b/lib/awful/tooltip.lua @@ -73,8 +73,7 @@ local data = setmetatable({}, { __mode = 'k' }) -- Place the tooltip on the screen. -- @tparam tooltip self A tooltip object. local function place(self) - a_placement.under_mouse(self.wibox) - a_placement.no_offscreen(self.wibox) + a_placement.next_to_mouse(self.wibox) end -- Place the tooltip under the mouse.