127 lines
3.6 KiB
Python
Executable File
127 lines
3.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# 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
|
|
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)
|
|
for workspace in ipc.get_tree().workspaces():
|
|
for w in workspace:
|
|
w.command("opacity 0")
|
|
Path(flag).touch()
|
|
|
|
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"
|
|
opacity = "0.9"
|
|
|
|
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))
|
|
ipc.on("window::focus", partial(on_window_focus, args.opacity))
|
|
ipc.main()
|
|
|