Created
March 30, 2025 23:39
-
-
Save ecoologic/1b79dec267bcdb8cc774d8fb18ea72db to your computer and use it in GitHub Desktop.
Linux mate: move windows between monitors (for shortcuts)
This file contains hidden or 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 | |
# This script moves the active window to the other monitor. | |
# From (customised) https://askubuntu.com/questions/804211/create-keyboard-shortcuts-to-move-windows-to-different-monitors-without-compiz | |
# The size of your monitor. | |
THRESHOLD_X = 3840 | |
import gi | |
gi.require_version('Gdk', '3.0') | |
gi.require_version('Gtk', '3.0') | |
gi.require_version('Wnck', '3.0') | |
from gi.repository import Gdk, Gtk, Wnck | |
def test_window_movement(): | |
screen = Wnck.Screen.get_default() | |
screen.force_update() | |
Gtk.main_iteration_do(False) # Process any pending events | |
active_window = screen.get_active_window() | |
print("active window geometry", active_window.get_geometry().xp) | |
old_x = active_window.get_geometry().xp | |
if old_x >= THRESHOLD_X: | |
new_x = 0 | |
else: | |
new_x = THRESHOLD_X | |
print(f"Moving window from {old_x} to {new_x}") | |
active_window.set_geometry( | |
Wnck.WindowGravity.STATIC, | |
(Wnck.WindowMoveResizeMask.X | | |
Wnck.WindowMoveResizeMask.Y | | |
Wnck.WindowMoveResizeMask.WIDTH | | |
Wnck.WindowMoveResizeMask.HEIGHT), | |
new_x, 0, | |
2400, 1600 | |
) | |
screen.force_update() | |
Gtk.main_iteration_do(False) # Process any pending events | |
print("active window geometry", active_window.get_geometry()) | |
if __name__ == "__main__": | |
test_window_movement() | |
# geometry = active_window.get_geometry() | |
# display = Gdk.Display.get_default() | |
# n_monitors = display.get_n_monitors() | |
# monitor_geometries = list(map( | |
# lambda i: { | |
# 'x': (monitor := display.get_monitor(i)).get_geometry().x, | |
# 'y': monitor.get_geometry().y, | |
# 'width': monitor.get_geometry().width, | |
# 'height': monitor.get_geometry().height | |
# }, | |
# range(n_monitors) | |
# )) | |
# current_monitor_index = 0 | |
# for i, geom in enumerate(monitor_geometries): | |
# if (geometry.xp >= geom['x'] and | |
# geometry.xp < geom['x'] + geom['width'] and | |
# geometry.yp >= geom['y'] and | |
# geometry.yp < geom['y'] + geom['height']): | |
# current_monitor_index = i | |
# break | |
# target_monitor_index = (current_monitor_index + 1) % len(monitor_geometries) | |
# target_monitor = monitor_geometries[target_monitor_index] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment