Skip to content

Commit

Permalink
Add layout-per-window.py script
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPandemic authored and OctopusET committed Dec 22, 2023
1 parent 0a75f6d commit d7b6971
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions layout-per-window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python

# This script keeps track of the active layout for each window.
#
# This script requires i3ipc-python package (install it from a system package
# manager or pip).

import i3ipc

def on_window_focus(ipc: i3ipc.connection.Connection, event: i3ipc.events.WindowEvent):

Check failure on line 10 in layout-per-window.py

View workflow job for this annotation

GitHub Actions / linux

Ruff (I001)

layout-per-window.py:8:1: I001 Import block is un-sorted or un-formatted
global windows, prev_focused

# Save current layout
layouts = {input.identifier : input.xkb_active_layout_index
for input in ipc.get_inputs()}
windows[prev_focused] = layouts

# Restore layout of the newly focused window
if event.container.id in windows:
for (kdb_id, layout_index) in windows[event.container.id].items():
if layout_index != layouts[kdb_id]:
ipc.command(f"input \"{kdb_id}\" xkb_switch_layout {layout_index}")

Check failure on line 22 in layout-per-window.py

View workflow job for this annotation

GitHub Actions / linux

Ruff (Q003)

layout-per-window.py:22:29: Q003 Change outer quotes to avoid escaping inner quotes
break

prev_focused = event.container.id

def on_window_close(ipc: i3ipc.connection.Connection, event: i3ipc.events.WindowEvent):
global windows
if event.container.id in windows:
del(windows[event.container.id])

def on_window(ipc: i3ipc.connection.Connection, event: i3ipc.events.WindowEvent):
if event.change == "focus":
on_window_focus(ipc, event)
elif event.change == "close":
on_window_close(ipc, event)

if __name__ == "__main__":
ipc = i3ipc.Connection()
focused = ipc.get_tree().find_focused()
if focused:
prev_focused = focused.id
else:
prev_focused = None
windows = {}

ipc.on("window", on_window)
ipc.main()

0 comments on commit d7b6971

Please sign in to comment.