From b5a14ba974bd02010b8971c01bea42a852c9b8d7 Mon Sep 17 00:00:00 2001 From: Amir Hosein Farhadi Date: Wed, 14 Dec 2022 23:42:53 +0330 Subject: [PATCH 1/2] fixes the problem of losing focus after changing the workspace --- init.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/init.lua b/init.lua index 16dbda8..4d608b2 100644 --- a/init.lua +++ b/init.lua @@ -24,3 +24,23 @@ client.connect_signal("property::minimized", backham) client.connect_signal("unmanage", backham) --+ attach to closed state + +-------------------------------------------------------- +-- Fixes the problem of losing focus after changing the workspace + +-- Grab focus on first client on screen +function grab_focus() + local all_clients = client.get() + for i, c in pairs(all_clients) do + if c:isvisible() and c.class ~= "Conky" then + client.focus = c + end + end +end + +-- Bind all key numbers to tags. +if tags[screen][i] then + awful.tag.viewonly(tags[screen][i]) + grab_focus() +end + From 1ed23f9bcef38e918b25b0152b0535fd7d9fcbb2 Mon Sep 17 00:00:00 2001 From: Amir Hosein Farhadi Date: Thu, 15 Dec 2022 02:07:49 +0330 Subject: [PATCH 2/2] refactor changed the focus_after_tag_change fn --- init.lua | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/init.lua b/init.lua index 4d608b2..b0f295d 100644 --- a/init.lua +++ b/init.lua @@ -25,22 +25,24 @@ client.connect_signal("property::minimized", backham) client.connect_signal("unmanage", backham) --+ attach to closed state --------------------------------------------------------- --- Fixes the problem of losing focus after changing the workspace - --- Grab focus on first client on screen -function grab_focus() - local all_clients = client.get() - for i, c in pairs(all_clients) do - if c:isvisible() and c.class ~= "Conky" then - client.focus = c - end +local function focus_after_tag_change() + local current_tag = awful.tag.selected(1) + if current_tag == nil then + return nil end -end --- Bind all key numbers to tags. -if tags[screen][i] then - awful.tag.viewonly(tags[screen][i]) - grab_focus() + local table_of_clients = current_tag:clients() + local last_client = table_of_clients[#table_of_clients] + local first_client = table_of_clients[0] or table_of_clients[1] + + local c = last_client ~= nil and last_client or first_client + + if c == nil then + return nil + end + + client.focus = c + c:raise() end +tag.connect_signal("property::selected", focus_after_tag_change) \ No newline at end of file