Last active
January 14, 2025 14:48
-
-
Save SidharthArya/f4d80c246793eb61be0ae928c9184406 to your computer and use it in GitHub Desktop.
Sway Windows Manager Alt Tab behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import json | |
import subprocess | |
direction=bool(sys.argv[1] == 't' or sys.argv[1] == 'T') | |
swaymsg = subprocess.run(['swaymsg', '-t', 'get_tree'], stdout=subprocess.PIPE) | |
data = json.loads(swaymsg.stdout) | |
current = data["nodes"][1]["current_workspace"] | |
workspace = int(data["nodes"][1]["current_workspace"])-1 | |
roi = data["nodes"][1]["nodes"][workspace] | |
temp = roi | |
windows = list() | |
def getNextWindow(): | |
if focus < len(windows) - 1: | |
return focus+1 | |
else: | |
return 0 | |
def getPrevWindow(): | |
if focus > 0: | |
return focus-1 | |
else: | |
return len(windows)-1 | |
def makelist(temp): | |
for nodes in "floating_nodes", "nodes": | |
for i in range(len(temp[nodes])): | |
if temp[nodes][i]["name"] is None: | |
makelist(temp[nodes][i]) | |
else: | |
windows.append(temp[nodes][i]) | |
def focused(temp_win): | |
for i in range(len(temp_win)): | |
if temp_win[i]["focused"] == True: | |
return i | |
makelist(temp) | |
# print(len(windows)) | |
focus = focused(windows) | |
if str(sys.argv[1]) == 't' or str(sys.argv[1]) == 'T': | |
print(windows[getNextWindow()]["id"]) | |
else: | |
print(windows[getPrevWindow()]["id"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bindsym $mod+tab exec swaymsg [con_id=$(swaymsg -t get_tree | ~/.config/sway/alttab t)] focus | |
bindsym $mod+shift+tab exec swaymsg [con_id=$(swaymsg -t get_tree | ~/.config/sway/alttab f)] focus |
There is quite some room for simplification by using the i3ipc python module:
#!/usr/bin/env python3
import i3ipc
import sys
def main():
sway = i3ipc.Connection()
for workspace in [ws for output in sway.get_tree().nodes for ws in output.nodes]:
focus = workspace.find_focused()
if focus is None:
continue
descendants = [d for d in workspace.descendants() if d.name is not None]
focus = descendants.index(focus)
focus = (focus + 1 if sys.argv[1] == "next" else focus - 1) % len(descendants)
sway.command(f"[con_id={descendants[focus].id}] focus")
sys.exit()
if __name__ == '__main__':
main()
Does it work on hyprland as well?
for hyprland, hyprswitch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will this work in i3?