From 19261c8cbe07a90492829e6f1149b22f69a6cf32 Mon Sep 17 00:00:00 2001 From: DrPandemic Date: Tue, 4 Aug 2020 21:45:24 -0400 Subject: [PATCH] Add type hinting, moved things to main and use f string --- contrib/keyboard-layout-per-window.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/contrib/keyboard-layout-per-window.py b/contrib/keyboard-layout-per-window.py index 7b4258e46..2b16a8f8d 100755 --- a/contrib/keyboard-layout-per-window.py +++ b/contrib/keyboard-layout-per-window.py @@ -7,11 +7,7 @@ import i3ipc -ipc = i3ipc.Connection() -prev_focused = ipc.get_tree().find_focused().id -windows = {} - -def on_window_focus(ipc, event): +def on_window_focus(ipc: i3ipc.connection.Connection, event: i3ipc.events.WindowEvent): global windows, prev_focused # Save current layout @@ -23,21 +19,26 @@ def on_window_focus(ipc, event): 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("""input "{}" xkb_switch_layout {}""".format( - kdb_id, layout_index)) + ipc.command(f"input \"{kdb_id}\" xkb_switch_layout {layout_index}") + break prev_focused = event.container.id -def on_window_close(ipc, event): +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, event): +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) -ipc.on("window", on_window) -ipc.main() +if __name__ == "__main__": + ipc = i3ipc.Connection() + prev_focused = ipc.get_tree().find_focused().id + windows = {} + + ipc.on("window", on_window) + ipc.main()