From 9803af1b1fb778ae723c0f2886a506dbd6fd8745 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 2 Aug 2015 19:36:56 +0200 Subject: [PATCH] Add awful.placement.next_to_mouse --- lib/awful/placement.lua | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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).