2018-10-20 21:43:54 +03:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
2022-10-03 11:25:59 +02:00
|
|
|
# This script requires i3ipc-python package (install it from a system package manager or pip).
|
|
|
|
|
# Use with --help flag for usage isntructions.
|
2018-10-20 21:43:54 +03:00
|
|
|
|
2019-12-05 13:15:41 +01:00
|
|
|
import argparse
|
2018-10-20 21:43:54 +03:00
|
|
|
import i3ipc
|
2019-12-05 13:15:41 +01:00
|
|
|
import signal
|
|
|
|
|
import sys
|
2020-01-09 10:50:52 -08:00
|
|
|
from functools import partial
|
2018-10-20 21:43:54 +03:00
|
|
|
|
2022-10-03 11:25:59 +02:00
|
|
|
def on_window_focus(args, ipc, event):
|
2018-10-20 21:43:54 +03:00
|
|
|
global prev_focused
|
|
|
|
|
|
2021-02-16 11:38:57 +01:00
|
|
|
focused_workspace = ipc.get_tree().find_focused()
|
|
|
|
|
|
|
|
|
|
if focused_workspace == None:
|
|
|
|
|
return
|
|
|
|
|
|
2019-12-05 13:15:41 +01:00
|
|
|
focused = event.container
|
|
|
|
|
|
2022-10-02 22:15:36 +02:00
|
|
|
# on_window_focus not called only when focused is changed,
|
|
|
|
|
# but also when a window is moved
|
|
|
|
|
if focused.id != prev_focused.id:
|
2022-10-03 11:25:59 +02:00
|
|
|
focused.command("opacity " + args.active_opacity)
|
|
|
|
|
prev_focused.command("opacity " + args.inactive_opacity)
|
2019-12-05 13:15:41 +01:00
|
|
|
prev_focused = focused
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_opacity(ipc):
|
2022-09-10 18:23:23 +00:00
|
|
|
tree = ipc.get_tree()
|
|
|
|
|
for workspace in tree.workspaces():
|
|
|
|
|
for window in workspace:
|
|
|
|
|
window.command("opacity 1")
|
|
|
|
|
for window in tree.scratchpad():
|
|
|
|
|
window.command("opacity 1")
|
2019-12-05 13:15:41 +01:00
|
|
|
ipc.main_quit()
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-10-03 11:25:59 +02:00
|
|
|
default_inactive_opacity = "0.80"
|
|
|
|
|
default_active_opacity = "1.0"
|
2019-12-05 13:15:41 +01:00
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2022-10-03 11:25:59 +02:00
|
|
|
description="This script allows you to set the transparency of focused and unfocused windows in sway."
|
2019-12-05 13:15:41 +01:00
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
2022-10-03 11:25:59 +02:00
|
|
|
"--inactive-opacity",
|
|
|
|
|
"-i",
|
2019-12-05 13:15:41 +01:00
|
|
|
type=str,
|
2022-10-03 11:25:59 +02:00
|
|
|
default=default_inactive_opacity,
|
|
|
|
|
help="value between 0 and 1 denoting opacity for inactive windows",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--active-opacity",
|
|
|
|
|
"-a",
|
|
|
|
|
type=str,
|
|
|
|
|
default=default_active_opacity,
|
|
|
|
|
help="value between 0 and 1 denoting opacity for active windows",
|
2019-12-05 13:15:41 +01:00
|
|
|
)
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
ipc = i3ipc.Connection()
|
|
|
|
|
prev_focused = None
|
|
|
|
|
|
|
|
|
|
for window in ipc.get_tree():
|
|
|
|
|
if window.focused:
|
|
|
|
|
prev_focused = window
|
|
|
|
|
else:
|
2022-10-03 11:25:59 +02:00
|
|
|
window.command("opacity " + args.inactive_opacity)
|
2019-12-05 13:15:41 +01:00
|
|
|
for sig in [signal.SIGINT, signal.SIGTERM]:
|
|
|
|
|
signal.signal(sig, lambda signal, frame: remove_opacity(ipc))
|
2022-10-03 11:25:59 +02:00
|
|
|
ipc.on("window::focus", partial(on_window_focus, args))
|
2022-09-10 18:23:23 +00:00
|
|
|
ipc.main()
|