From b6f1ee86463c7dce36e4840238f58e7f485e836e Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 15 Dec 2012 14:17:06 +0100 Subject: [PATCH] bindings: enhance the way the Pidgin conversation window is selected There is now some cycle stuff. This is still not perfect since it will bring some random window. It would be better to be able to choose the appropriate one. --- rc/bindings.lua | 71 +++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/rc/bindings.lua b/rc/bindings.lua index bb7a0e4..65d91b4 100644 --- a/rc/bindings.lua +++ b/rc/bindings.lua @@ -80,36 +80,49 @@ end -- Toggle pidgin conversation window local toggle_pidgin = toggle_window( - function () - local cls = client.get() - local focus = client.focus - local rule = { class = "Pidgin", - role = "conversation" } - -- Score. We want a Pidgin window. Then: - -- 1. Urgent, visible, not focused - -- 2. Urgent, not visible, not focused. - -- 3. Not urgent, not visible, not focused - -- 4. Focused - -- 5. Visible, not focused - local function score(cl) - if not awful.rules.match(cl, rule) then return -10 end + (function () + local adding = true + local choose = function() + local cls = client.get() + local focus = client.focus + local rule = { class = "Pidgin", + role = "conversation" } + -- Score. We want a Pidgin window. Then: + -- 1. Urgent, visible, not focused + -- 2. Urgent, not visible, not focused. + -- 3. Not urgent, not visible, not focused + -- 4. Focused + -- 5. Visible, not focused + local function score(cl) + if not awful.rules.match(cl, rule) then return -10 end - local urgent = cl.urgent - local focused = (focus == cl) - local visible = cl:isvisible() - if urgent and visible and not focused then return 100 end - if urgent and not visible and not focused then return 90 end - if not urgent and not visible then return 80 end - if focused then return 50 end - return 10 - end - table.sort(cls, function(c1, c2) - local s1 = score(c1) - local s2 = score(c2) - return s1 > s2 - end) - if awful.rules.match(cls[1], rule) then return cls[1] end - end) + local urgent = cl.urgent + local focused = (focus == cl) + local visible = cl:isvisible() + if urgent and visible and not focused then return 100 end + if urgent and not visible and not focused then return 90 end + if not adding and focused then return 80 end + if adding and not urgent and not visible then return 70 end + if focused then return 50 end + if not urgent and not visible then return 40 end + return 10 + end + table.sort(cls, function(c1, c2) + local s1 = score(c1) + local s2 = score(c2) + return s1 > s2 + end) + local candidate = cls[1] + if candidate == nil or not awful.rules.match(candidate, rule) then return nil end + + -- Maybe we need to switch direction + if candidate == focus and adding then adding = false + elseif candidate ~= focus and not adding then adding = true end + + return candidate + end + return choose + end)()) -- Toggle urgent window local toggle_urgent = toggle_window(awful.client.urgent.get)