Last active
March 30, 2024 08:22
-
-
Save SergKolo/328548cc3c2b4597d7f7ab91db26709d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Thank you for this script! Working on Manjaro.
@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.
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
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.