scripts/sway/sway-transparency.py

124 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2022-09-24 02:37:07 +00:00
# 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 or use the command line argument -o.
import argparse
import i3ipc
import signal
import sys
import os
import time
2022-09-24 02:37:07 +00:00
from pathlib import Path
from functools import partial
def on_window_focus(opacity, ipc, event):
if os.path.exists(flag):
return
global prev_focused
global prev_workspace
focused_workspace = ipc.get_tree().find_focused()
if focused_workspace == None:
return
focused = event.container
workspace = focused_workspace.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 " + opacity)
prev_focused = focused
prev_workspace = workspace
def show_hide(ipc, opacity):
global flag
if os.path.exists(flag):
os.remove(flag)
else:
opacity = 0
Path(flag).touch()
for workspace in ipc.get_tree().workspaces():
for w in workspace:
if w.focused and opacity != 0:
w.command("opacity 1")
else:
w.command("opacity "+str(opacity))
def fade(ipc, opacity):
global flag
if os.path.exists(flag):
show_hide(ipc, opacity)
return
for i in range(1,100):
for workspace in ipc.get_tree().workspaces():
for w in workspace:
if w.focused:
w.command("opacity "+str(1-(i/100)))
elif float(opacity) >= (i/100):
w.command("opacity "+str(float(opacity)-(i/100)))
time.sleep(0.005)
Path(flag).touch()
2022-09-24 02:37:07 +00:00
def remove_opacity(ipc):
global pid
global flag
for workspace in ipc.get_tree().workspaces():
for w in workspace:
w.command("opacity 1")
for file in [pid, flag]:
if os.path.exists(file):
os.remove(file)
ipc.main_quit()
sys.exit(0)
def set_all(ipc,opacity):
for window in ipc.get_tree():
window.command("opacity " + opacity)
flag = os.environ["HOME"]+"/.spool/sway-hidden"
pid = os.environ["HOME"]+"/.spool/sway-transparency"
2023-04-10 01:35:35 +00:00
opacity = "0.85"
2022-09-24 02:37:07 +00:00
if __name__ == "__main__":
f = open(pid, "w")
try:
f.write(str(os.getpid()))
except AssertionError as error:
print("Failed to write PID to file "+pid+": "+error)
sys.exit(0)
f.close()
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=opacity,
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))
signal.signal(signal.SIGUSR1, lambda signal, frame: show_hide(ipc, opacity))
signal.signal(signal.SIGUSR2, lambda signal, frame: fade(ipc, opacity))
2022-09-24 02:37:07 +00:00
ipc.on("window::focus", partial(on_window_focus, args.opacity))
ipc.main()