From 6235423544b7e3feab5a6775cd4d06a3b171dd54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Thu, 5 Dec 2019 13:15:41 +0100 Subject: [PATCH] Improve transparency script Transparency gets reset when the script is terminated. Added command line option to set transparency strength without changing the script. Added support for multiple displays. --- contrib/inactive-windows-transparency.py | 70 ++++++++++++++++++------ 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/contrib/inactive-windows-transparency.py b/contrib/inactive-windows-transparency.py index af26c0176..15e2a91b8 100755 --- a/contrib/inactive-windows-transparency.py +++ b/contrib/inactive-windows-transparency.py @@ -3,26 +3,62 @@ # This script requires i3ipc-python package (install it from a system package manager # or pip). # It makes inactive windows transparent. Use `transparency_val` variable to control -# transparency strength in range of 0…1. +# transparency strength in range of 0…1 or use the command line argument -o. +import argparse import i3ipc +import signal +import sys -transparency_val = '0.8'; -ipc = i3ipc.Connection() -prev_focused = None -for window in ipc.get_tree(): - if window.focused: - prev_focused = window - else: - window.command('opacity ' + transparency_val) - -def on_window_focus(ipc, focused): +def on_window_focus(ipc, event): global prev_focused - if focused.container.id != prev_focused.id: # https://github.com/swaywm/sway/issues/2859 - focused.container.command('opacity 1') - prev_focused.command('opacity ' + transparency_val) - prev_focused = focused.container + global prev_workspace -ipc.on("window::focus", on_window_focus) -ipc.main() + focused = event.container + workspace = ipc.get_tree().find_focused().workspace().num + + if focused.id != prev_focused.id: # https://github.com/swaywm/sway/issues/2859 + focused.command("opacity 1") + if workspace == prev_workspace: + prev_focused.command("opacity " + transparency_val) + prev_focused = focused + prev_workspace = workspace + + +def remove_opacity(ipc): + for workspace in ipc.get_tree().workspaces(): + for w in workspace: + w.command("opacity 1") + ipc.main_quit() + sys.exit(0) + + +if __name__ == "__main__": + transparency_val = "0.80" + + parser = argparse.ArgumentParser( + description="This script allows you to set the transparency of unfocused windows in sway." + ) + parser.add_argument( + "--opacity", + "-o", + type=str, + default=transparency_val, + help="set opacity value in range 0...1", + ) + args = parser.parse_args() + + ipc = i3ipc.Connection() + prev_focused = None + prev_workspace = ipc.get_tree().find_focused().workspace().num + + for window in ipc.get_tree(): + if window.focused: + prev_focused = window + else: + window.command("opacity " + args.opacity) + for sig in [signal.SIGINT, signal.SIGTERM]: + signal.signal(sig, lambda signal, frame: remove_opacity(ipc)) + ipc.on("window::focus", on_window_focus) + ipc.main()