Facebook
From xor512, 3 Years ago, written in Lua.
Embed
Download Paste or View Raw
Hits: 292
  1. -- {{{ Mine
  2.  
  3.     -- {{{ From https://www.reddit.com/r/awesomewm/comments/gd4qxl/get_index_of_client_in_tasklist/ with some modifications
  4.     local function fixed_indexing_filter(c)
  5.         if not c then
  6.             return false
  7.         end
  8.         if not (c.skip_taskbar or c.hidden or c.type == "splash" or c.type == "dock" or c.type == "desktop") then
  9.             return true
  10.         end
  11.         return false
  12.     end
  13.  
  14.     local function focused_screen_current_tag_client_iterator()
  15.         local focused_screen = awful.screen.focused()
  16.         local selected_tag_filter = function (c) return c.first_tag == focused_screen.selected_tag end
  17.         local first_client_taskbar_idx = 1
  18.         return awful.client.iterate(selected_tag_filter, first_client_taskbar_idx, focused_screen)
  19.     end
  20.  
  21.     -- Returns client index in the taskbar (first on the left will have index = 1)
  22.     local function client_to_taskbar_idx(client)
  23.         local client_taskbar_idx = 0
  24.         for c in focused_screen_current_tag_client_iterator() do
  25.             if fixed_indexing_filter(c) then
  26.                 client_taskbar_idx = client_taskbar_idx + 1
  27.                 if (c == client) then
  28.                     return client_taskbar_idx
  29.                 end
  30.             end
  31.         end
  32.  
  33.         -- if no client is focused (e.g. all are minimized) behave as if 1st is focused
  34.         return 1
  35.     end
  36.  
  37.     -- Returns client which is at taskbar_idx position in the taskbar
  38.     function taskbar_idx_to_client(taskbar_idx)
  39.         local client_taskbar_idx = 0
  40.         for c in focused_screen_current_tag_client_iterator() do
  41.             if fixed_indexing_filter(c) then
  42.                 client_taskbar_idx = client_taskbar_idx + 1
  43.                 if (client_taskbar_idx == taskbar_idx) then
  44.                     return c
  45.                 end
  46.             end
  47.         end
  48.  
  49.         return nil
  50.     end
  51.     --
  52.     -- }}}
  53.    
  54.     -- Returns total number of clients in the currently selected tag
  55.     local function selected_tag_number_of_clients()
  56.         local number_of_clients = 0
  57.         for c in focused_screen_current_tag_client_iterator() do
  58.             if fixed_indexing_filter(c) then
  59.                  number_of_clients = number_of_clients + 1
  60.             end
  61.         end
  62.         return number_of_clients
  63.     end
  64.  
  65.     -- Switches focus to the client of index in the taskbar = taskbar_idx:
  66.     --    * client with 1st taskbar item will have index 1
  67.     --    * client with 2nd taskbar item will have index 2
  68.     --    * etc
  69.     local function client_focus_by_taskbar_idx(taskbar_idx)
  70.         -- User wants to focus a non-existing client, e.g. there are only 2 opened in
  71.         -- focused screen selected tag clients and user wants to focus 3rd one, as there is
  72.         -- no 3rd one just do nothing
  73.         if taskbar_idx < 1 or taskbar_idx > selected_tag_number_of_clients() then
  74.             return
  75.         end
  76.      
  77.  
  78.         -- unminimize clint to focus in case it is minizied (won't harm for not-minimized)
  79.         local client_to_focus = taskbar_idx_to_client(taskbar_idx)
  80.         client_to_focus:emit_signal( -- client_to_focus cannot be null because of the sanity check above
  81.             "request::activate",
  82.             "tasklist",
  83.             {raise = true}
  84.         )
  85.  
  86.         -- now focus to it
  87.         local focused_client_taskbar_idx = client_to_taskbar_idx(client.focus)
  88.         local relative_idx = taskbar_idx - focused_client_taskbar_idx            
  89.         awful.client.focus.byidx(relative_idx)
  90.     end
  91.     -- }}}
  92.  
  93. -- }}}
  94.  
  95.     -- {{{ Mine: added/changes navigation for clients
  96.  
  97.         -- {{{ changed (was according to stack, now according to taskbar position):
  98.         --     moving between windows left/right (as in tasklist)
  99.         awful.key({ modkey,           }, "j",
  100.             function () awful.client.focus.byidx(-1) end,
  101.             {description = "next window to the left by index", group = "client"}
  102.         ),
  103.         awful.key({ modkey,           }, "k",
  104.             function () awful.client.focus.byidx(1) end,
  105.             {description = "next window to the right by index", group = "client"}
  106.         ), -- }}}
  107.  
  108.         -- {{{ added (browser-tab-like switching of clients):
  109.         --     moving between windows with Mod4-Fn (as between tags with Mod4-n)
  110.         awful.key({ modkey,           }, "F1",
  111.             function () client_focus_by_taskbar_idx(1) end,
  112.             {description = "focus client 1 in taskbar", group = "client"}
  113.         ),
  114.         awful.key({ modkey,           }, "F2",
  115.             function () client_focus_by_taskbar_idx(2) end,
  116.             {description = "focus client 2 in taskbar", group = "client"}
  117.         ),
  118.         awful.key({ modkey,           }, "F3",
  119.             function () client_focus_by_taskbar_idx(3) end,
  120.             {description = "focus client 3 in taskbar", group = "client"}
  121.         ),
  122.         awful.key({ modkey,           }, "F4",
  123.             function () client_focus_by_taskbar_idx(4) end,
  124.             {description = "focus client 4 in taskbar", group = "client"}
  125.         ),
  126.         awful.key({ modkey,           }, "F5",
  127.             function () client_focus_by_taskbar_idx(5) end,
  128.             {description = "focus client 5 in taskbar", group = "client"}
  129.         ),
  130.         awful.key({ modkey,           }, "F6",
  131.             function () client_focus_by_taskbar_idx(6) end,
  132.             {description = "focus client 6 in taskbar", group = "client"}
  133.         ),
  134.         awful.key({ modkey,           }, "F7",
  135.             function () client_focus_by_taskbar_idx(7) end,
  136.             {description = "focus client 7 in taskbar", group = "client"}
  137.         ),
  138.         awful.key({ modkey,           }, "F8",
  139.             function () client_focus_by_taskbar_idx(8) end,
  140.             {description = "focus client 8 in taskbar", group = "client"}
  141.         ),
  142.         awful.key({ modkey,           }, "F9",
  143.             function () client_focus_by_taskbar_idx(9) end,
  144.             {description = "focus client 9 in taskbar", group = "client"}
  145.         ),
  146.         awful.key({ modkey,           }, "F10",
  147.             function () client_focus_by_taskbar_idx(10) end,
  148.             {description = "focus client 10 in taskbar", group = "client"}
  149.         ),
  150.         awful.key({ modkey,           }, "F11",
  151.             function () client_focus_by_taskbar_idx(11) end,
  152.             {description = "focus client 11 in taskbar", group = "client"}
  153.         ),
  154.         awful.key({ modkey,           }, "F12",
  155.             function () client_focus_by_taskbar_idx(12) end,
  156.             {description = "focus client 12 in taskbar", group = "client"}
  157.         ),
  158.         -- }}}
  159.  
  160.     -- }}}