Skip to content

Instantly share code, notes, and snippets.

@artemave
Last active November 26, 2022 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemave/df7cb3e796ca16022368aabeff2cecec to your computer and use it in GitHub Desktop.
Save artemave/df7cb3e796ca16022368aabeff2cecec to your computer and use it in GitHub Desktop.
i3wm remember window floating mode

When plugged in, this script remembers and restores window floating mode. So that if you don't want certain windows to go fullscreen (e.g. arandr, volume control, etc.) you can simply switch them into floating mode once and it will always start floating. You can of course do this in i3 config, but it's more hustle.

Install

  1. pip install i3ipc

  2. Add this to ~/.config/i3/config:

exec --no-startup-id ~/.config/i3/auto_floating.py
#!/usr/bin/env python3
import os
from i3ipc import Connection
from i3ipc import Event
i3 = Connection()
os.system('touch ~/.config/i3/.auto_floating_windows')
def cacheKey(container):
props = [
container.window_class,
container.window_instance,
container.window_role,
container.window_title,
]
# Filter out None values
props = [p for p in props if isinstance(p, str)]
return '\t'.join(props)
def on_window_new(i3, e):
key = cacheKey(e.container)
if len(key) == 0:
return
with open(os.path.expanduser('~/.config/i3/.auto_floating_windows'), 'r') as f:
if key in f.read():
i3.command('[con_id=%s] floating enable' % e.container.id)
def on_window_floating(_, e):
key = cacheKey(e.container)
if len(key) == 0:
return
with open(os.path.expanduser('~/.config/i3/.auto_floating_windows'), 'r+') as f:
lines = f.read().splitlines()
if e.container.floating == 'user_on' and key not in lines:
lines.append(key)
if e.container.floating == 'user_off' and key in lines:
lines.remove(key)
f.seek(0)
f.truncate()
f.write("\n".join(lines))
i3.on(Event.WINDOW_FLOATING, on_window_floating)
i3.on(Event.WINDOW_NEW, on_window_new)
i3.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment