contrib: add active and inactive opacity overrides.

This commit is contained in:
Aelfsyg 2022-04-24 17:22:38 +01:00
parent 503d47ea27
commit 023fa263ea

View file

@ -15,13 +15,23 @@ from functools import partial
class Opts:
def __init__(self, args):
self.ignore = args.ignore
self.active_opacity = args.active_opacity
self.active_opacities = json.loads(args.active_opacities)
self.inactive_opacity = args.opacity
self.inactive_opacity = args.inactive_opacity
self.inactive_opacities = json.loads(args.inactive_opacities)
self.ignore = args.ignore
def set_focused_opacity(window, opts):
opacity = opts.active_opacities.get(window.app_id, 1)
def set_opacity(window, opts, active=True):
app = window.app_id
opacity = None
if app in opts.ignore:
opacity = 1.0
else:
if active:
opacity = opts.active_opacities.get(app, opts.active_opacity)
else:
opacity = opts.inactive_opacities.get(app, opts.inactive_opacity)
cmd = "opacity {}".format(opacity)
window.command(cmd)
return
@ -40,12 +50,11 @@ def on_window_focus(opts, ipc, event):
workspace = focused_workspace.workspace().num
if focused.id != prev_focused.id: # https://github.com/swaywm/sway/issues/2859
set_focused_opacity(focused, opts);
set_opacity(focused, opts, active=True);
if workspace == prev_workspace:
if not window.app_id in opts.ignore:
prev_focused.command("opacity " + opts.inactive_opacity)
prev_focused = focused
prev_workspace = workspace
set_opacity(prev_focused, opts, active=False)
prev_focused = focused
prev_workspace = workspace
def remove_opacity(ipc):
@ -57,31 +66,42 @@ def remove_opacity(ipc):
if __name__ == "__main__":
transparency_val = "0.80"
parser = argparse.ArgumentParser(
description="This script allows you to set the transparency of unfocused windows in sway."
description="This script allows you to set the transparency of windows in sway."
)
parser.add_argument(
"--opacity",
"-o",
"--active_opacity",
"-a",
type=str,
default=transparency_val,
help="set opacity value in range 0...1",
default=1,
help="The default opacity for active windows.",
)
parser.add_argument(
"--active-opacities",
"-a",
"-A",
type=str,
default="{}",
help="A dictionary of applications and their active opacities."
)
parser.add_argument(
"--ignore",
"--inactive_opacity",
"-i",
type=str,
default=0.8,
help="The default opacity for inactive windows.",
)
parser.add_argument(
"--inactive_opacities",
"-I",
type=str,
default="{}",
help="A dictionary of applications and their inactive opacities."
)
parser.add_argument(
"--ignore",
type=str,
default=[],
help="List of ignored processes.",
help="List of applications to be ignored.",
nargs="+"
)
args = parser.parse_args()
@ -94,8 +114,7 @@ if __name__ == "__main__":
for window in ipc.get_tree():
if window.focused:
prev_focused = window
elif not window.app_id in opts.ignore:
window.command("opacity " + args.opacity)
set_opacity(window, opts, active=False)
for sig in [signal.SIGINT, signal.SIGTERM]:
signal.signal(sig, lambda signal, frame: remove_opacity(ipc))
ipc.on("window::focus", partial(on_window_focus, opts))