Skip to content

Instantly share code, notes, and snippets.

@ramnes
Created February 5, 2021 22:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnes/a9c10e45801a17ef69e172f276629e06 to your computer and use it in GitHub Desktop.
Save ramnes/a9c10e45801a17ef69e172f276629e06 to your computer and use it in GitHub Desktop.
Qtile hook to center all floating windows, until more sensible defaults or a better solution are available
@hook.subscribe.float_change
def center_window():
client = qtile.current_window
if not client.floating:
return
screen_rect = qtile.current_screen.get_rect()
center_x = screen_rect.x + screen_rect.width / 2
center_y = screen_rect.y + screen_rect.height / 2
x = center_x - client.width / 2
y = center_y - client.height / 2
# don't go off the right...
x = min(x, screen_rect.x + screen_rect.width - client.width)
# or left...
x = max(x, screen_rect.x)
# or bottom...
y = min(y, screen_rect.y + screen_rect.height - client.height)
# or top
y = max(y, screen_rect.y)
client.x = int(round(x))
client.y = int(round(y))
qtile.current_group.layout_all()
@stefur
Copy link

stefur commented Aug 18, 2021

Thank you so much for this! I use it in my config across all machines, works beautifully.

However I encountered a small problem with it when trying to fullscreen YouTube videos (for example) in a browser. It would maximize the video, but the bar would not hide, e.g. not completely fullscreen.
From my understanding a fullscreen video is also floating, which meant that the hook to center floating windows was interfering.

So on line 4 I did a very small adjustment to check if the window is fullscreening:

if not client.floating or client.fullscreen:
        return

Now fullscreen videos work properly and floating videos get centered as they should.
I am not a programmer by trade so I don't know if there is a better solution, but maybe this helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment