MCPcopy
hub / github.com/Sathvik-Rao/ClipCascade / center_window

Function center_window

ClipCascade_Desktop/src/utils/window_manager.py:6–45  ·  view source on GitHub ↗

Center a Tk/Toplevel window on the primary monitor.

(win, max_height=None)

Source from the content-addressed store, hash-verified

4
5
6def center_window(win, max_height=None):
7 """Center a Tk/Toplevel window on the primary monitor."""
8 logging.debug(f"Centering window with max_height={max_height}")
9 win.update_idletasks()
10 window_width = win.winfo_width()
11 window_height = win.winfo_height()
12 if max_height:
13 window_height = min(window_height, max_height)
14 logging.debug(f"Window dimensions: {window_width}x{window_height}")
15 primary_x, primary_y = 0, 0
16 primary_w, primary_h = win.winfo_screenwidth(), win.winfo_screenheight()
17 try:
18 if PLATFORM.startswith(LINUX):
19 logging.debug("Detecting monitors on Linux platform")
20 import subprocess
21
22 result = subprocess.run(
23 ["xrandr", "--query"], capture_output=True, text=True, timeout=2
24 )
25 for line in result.stdout.splitlines():
26 if " connected primary" in line:
27 m = re.search(r"(\d+)x(\d+)\+(\d+)\+(\d+)", line)
28 if m:
29 primary_w = int(m.group(1))
30 primary_h = int(m.group(2))
31 primary_x = int(m.group(3))
32 primary_y = int(m.group(4))
33 logging.debug(
34 f"Primary monitor: position=({primary_x}, {primary_y}), size={primary_w}x{primary_h}"
35 )
36 break
37 except Exception as e:
38 logging.warning(f"Failed to detect primary monitor: {e}. Using fallback dimensions.")
39 x = primary_x + (primary_w - window_width) // 2
40 y = primary_y + (primary_h - window_height) // 2
41 x = max(primary_x, x)
42 y = max(primary_y, y)
43 logging.debug(f"Calculated window position: ({x}, {y})")
44 win.geometry(f"{window_width}x{window_height}+{x}+{y}")
45 logging.debug(f"Applied geometry: {window_width}x{window_height}+{x}+{y}")

Callers 3

_configure_windowMethod · 0.90
__init__Method · 0.90
_toggle_extra_configMethod · 0.90

Calls 1

runMethod · 0.45

Tested by

no test coverage detected