Skip to content

Instantly share code, notes, and snippets.

@SergKolo
Last active March 30, 2024 08:22
Show Gist options
  • Save SergKolo/328548cc3c2b4597d7f7ab91db26709d to your computer and use it in GitHub Desktop.
Save SergKolo/328548cc3c2b4597d7f7ab91db26709d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Author: Serg Kolo
# Date: Oct 3rd, 2016
# Description: Script for aligning the center of
# user's active window with the center of the monitor
# Tested on: Ubuntu 16.04
# Written for: http://askubuntu.com/q/832720/295286
from __future__ import print_function
import gi
gi.require_version('Gdk','3.0')
from gi.repository import Gdk
import subprocess
def get_offset(*args):
command = ['xprop','-notype','_NET_FRAME_EXTENTS',
'-id',str(args[0])
]
out = subprocess.check_output(command)
if 'not found' in out.decode():
return 0
return int(out.decode().strip().split(',')[-2])
def main():
screen = Gdk.Screen.get_default()
window = screen.get_active_window()
window.unmaximize()
window_width = window.get_width()
window_y = window.get_origin()[-1]
print(window_y)
window_monitor = screen.get_monitor_at_window(window)
mon_g = screen.get_monitor_geometry(window_monitor)
print(mon_g.width,mon_g.height)
monitor_center = screen.get_monitor_geometry(window_monitor).width/2
# if centers of window and screen are aligned
# the top left corner will be at screen_center - window_width/2
new_position = monitor_center - window_width /2
# For some reason there is vertical offset necessary
# Apparently this comes form _NET_FRAME_EXTENTS value
offset = get_offset(int(window.get_xid()))
window.move(new_position,window_y-offset)
print(window.get_origin())
if __name__ == '__main__':
main()
@Kvols
Copy link

Kvols commented Dec 12, 2017

Please add
import gi
gi.require_version('Gdk', '3.0')
before
from gi.repository import Gdk
...to avoid warning under Ubuntu 17.04. It does otherwise work great. Thank you! :)

@dmyTRUEk
Copy link

dmyTRUEk commented Dec 4, 2019

On Ubuntu 19.10 have error:

Traceback (most recent call last): File "/bin/center-active-window.py", line 45, in <module> main() File "/bin/center-active-window.py", line 39, in main offset = get_offset(int(window.get_xid())) File "/bin/center-active-window.py", line 20, in get_offset return int(out.decode().strip().split(',')[-2]) IndexError: list index out of range

And some warnings:
/bin/center-active-window.py:11: PyGIWarning: Gdk was imported without specifying a version first. Use gi.require_version('Gdk', '3.0') before import to ensure that the right version gets loaded. from gi.repository import Gdk /bin/center-active-window.py:25: DeprecationWarning: Gdk.Screen.get_active_window is deprecated window = screen.get_active_window() 280 /bin/center-active-window.py:30: DeprecationWarning: Gdk.Screen.get_monitor_at_window is deprecated window_monitor = screen.get_monitor_at_window(window) /bin/center-active-window.py:31: DeprecationWarning: Gdk.Screen.get_monitor_geometry is deprecated monitor_center = screen.get_monitor_geometry(window_monitor).width/2

Plz, fix it ;)

@SergKolo
Copy link
Author

@Kvols Sorry for delayed reply . . .very delayed. This is fixed.

@SergKolo
Copy link
Author

SergKolo commented Dec 10, 2019

@dmyTRUEk Fixed, the error happens because Ubuntu 19.10 uses GNOME, and GNOME for some reason doesn't set _NET_FRAME_EXTENTS property anymore. So xprop fails and returns error in the output. There's a few other problems that I found with this script (for example y centering doesnt work well and panned display needs to be accounted for, even though this is rare nowadays), so I'll try to fix them some time this month and account for couple other X11 properties. Remind me maybe like in January if you don't see any updates on this.

@orlandini
Copy link

Hello @SergKolo,

Thank you for the script! In order to make it work for my setup using two monitors, I had to modify it a little bit.
Basically, I had to write monitor_center = mon_g.x + mon_g.width/2 in order to make take into account where the monitor begins.

I also modified it a bit for centralising in both x and y. I will put in here in case someone find it useful.

def main():

    screen = Gdk.Screen.get_default()
    window = screen.get_active_window()
    window.unmaximize()
    window_width = window.get_width()
    window_height = window.get_height()
    
    window_monitor = screen.get_monitor_at_window(window)
    mon_g = screen.get_monitor_geometry(window_monitor)
    print(mon_g.width,mon_g.height)
    monitor_center_x = mon_g.x + mon_g.width/2
    monitor_center_y = mon_g.y + mon_g.height/2

    # if centers of window and screen are aligned
    new_position_x = monitor_center_x - window_width /2
    new_position_y = monitor_center_y - window_height /2

    window.move(new_position_x,new_position_y)
    print(window.get_origin()) 

@SergKolo
Copy link
Author

SergKolo commented Mar 6, 2020

@orlandini Thank you for contributing what worked for your setup. Just a question, what desktop environment do you use and do you use any docks/panels ?

@orlandini
Copy link

orlandini commented Mar 6, 2020

Hi @SergKolo. I am Ubuntu 18.04 with GNOME 3.28.2. I am mostly using the default settings. To be honest, I have not used linux systems for quite a while, so if there are any other relevant settings, please let me know.

@droid001
Copy link

droid001 commented Jul 8, 2020

Thanks for the script. It works fine under Ubuntu 20.04 apart from the height isn't maintained. For example if I align a window to one side of the screen and then the height is 100%. When centering the window then the height becomes smaller.

@jacques-andre
Copy link

Thank you for this script! Working on Manjaro.

@laoshaw
Copy link

laoshaw commented Dec 8, 2021

@orlandini works well on dual monitors here inside ubuntu 20.04. Thanks! the original script will always center on the 'primary' monitor instead of my 'active'(i.e. the monitor I current am using) monitor.

@UdeRecife
Copy link

I want to thank both of you who created/contributed to this script. The original works for one monitor, and the modified version for however many. Good find. Thank you both.

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