annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/zoomheight.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 "Zoom a window to maximum height."
jpayne@69 2
jpayne@69 3 import re
jpayne@69 4 import sys
jpayne@69 5 import tkinter
jpayne@69 6
jpayne@69 7
jpayne@69 8 class WmInfoGatheringError(Exception):
jpayne@69 9 pass
jpayne@69 10
jpayne@69 11
jpayne@69 12 class ZoomHeight:
jpayne@69 13 # Cached values for maximized window dimensions, one for each set
jpayne@69 14 # of screen dimensions.
jpayne@69 15 _max_height_and_y_coords = {}
jpayne@69 16
jpayne@69 17 def __init__(self, editwin):
jpayne@69 18 self.editwin = editwin
jpayne@69 19 self.top = self.editwin.top
jpayne@69 20
jpayne@69 21 def zoom_height_event(self, event=None):
jpayne@69 22 zoomed = self.zoom_height()
jpayne@69 23
jpayne@69 24 if zoomed is None:
jpayne@69 25 self.top.bell()
jpayne@69 26 else:
jpayne@69 27 menu_status = 'Restore' if zoomed else 'Zoom'
jpayne@69 28 self.editwin.update_menu_label(menu='options', index='* Height',
jpayne@69 29 label=f'{menu_status} Height')
jpayne@69 30
jpayne@69 31 return "break"
jpayne@69 32
jpayne@69 33 def zoom_height(self):
jpayne@69 34 top = self.top
jpayne@69 35
jpayne@69 36 width, height, x, y = get_window_geometry(top)
jpayne@69 37
jpayne@69 38 if top.wm_state() != 'normal':
jpayne@69 39 # Can't zoom/restore window height for windows not in the 'normal'
jpayne@69 40 # state, e.g. maximized and full-screen windows.
jpayne@69 41 return None
jpayne@69 42
jpayne@69 43 try:
jpayne@69 44 maxheight, maxy = self.get_max_height_and_y_coord()
jpayne@69 45 except WmInfoGatheringError:
jpayne@69 46 return None
jpayne@69 47
jpayne@69 48 if height != maxheight:
jpayne@69 49 # Maximize the window's height.
jpayne@69 50 set_window_geometry(top, (width, maxheight, x, maxy))
jpayne@69 51 return True
jpayne@69 52 else:
jpayne@69 53 # Restore the window's height.
jpayne@69 54 #
jpayne@69 55 # .wm_geometry('') makes the window revert to the size requested
jpayne@69 56 # by the widgets it contains.
jpayne@69 57 top.wm_geometry('')
jpayne@69 58 return False
jpayne@69 59
jpayne@69 60 def get_max_height_and_y_coord(self):
jpayne@69 61 top = self.top
jpayne@69 62
jpayne@69 63 screen_dimensions = (top.winfo_screenwidth(),
jpayne@69 64 top.winfo_screenheight())
jpayne@69 65 if screen_dimensions not in self._max_height_and_y_coords:
jpayne@69 66 orig_state = top.wm_state()
jpayne@69 67
jpayne@69 68 # Get window geometry info for maximized windows.
jpayne@69 69 try:
jpayne@69 70 top.wm_state('zoomed')
jpayne@69 71 except tkinter.TclError:
jpayne@69 72 # The 'zoomed' state is not supported by some esoteric WMs,
jpayne@69 73 # such as Xvfb.
jpayne@69 74 raise WmInfoGatheringError(
jpayne@69 75 'Failed getting geometry of maximized windows, because ' +
jpayne@69 76 'the "zoomed" window state is unavailable.')
jpayne@69 77 top.update()
jpayne@69 78 maxwidth, maxheight, maxx, maxy = get_window_geometry(top)
jpayne@69 79 if sys.platform == 'win32':
jpayne@69 80 # On Windows, the returned Y coordinate is the one before
jpayne@69 81 # maximizing, so we use 0 which is correct unless a user puts
jpayne@69 82 # their dock on the top of the screen (very rare).
jpayne@69 83 maxy = 0
jpayne@69 84 maxrooty = top.winfo_rooty()
jpayne@69 85
jpayne@69 86 # Get the "root y" coordinate for non-maximized windows with their
jpayne@69 87 # y coordinate set to that of maximized windows. This is needed
jpayne@69 88 # to properly handle different title bar heights for non-maximized
jpayne@69 89 # vs. maximized windows, as seen e.g. in Windows 10.
jpayne@69 90 top.wm_state('normal')
jpayne@69 91 top.update()
jpayne@69 92 orig_geom = get_window_geometry(top)
jpayne@69 93 max_y_geom = orig_geom[:3] + (maxy,)
jpayne@69 94 set_window_geometry(top, max_y_geom)
jpayne@69 95 top.update()
jpayne@69 96 max_y_geom_rooty = top.winfo_rooty()
jpayne@69 97
jpayne@69 98 # Adjust the maximum window height to account for the different
jpayne@69 99 # title bar heights of non-maximized vs. maximized windows.
jpayne@69 100 maxheight += maxrooty - max_y_geom_rooty
jpayne@69 101
jpayne@69 102 self._max_height_and_y_coords[screen_dimensions] = maxheight, maxy
jpayne@69 103
jpayne@69 104 set_window_geometry(top, orig_geom)
jpayne@69 105 top.wm_state(orig_state)
jpayne@69 106
jpayne@69 107 return self._max_height_and_y_coords[screen_dimensions]
jpayne@69 108
jpayne@69 109
jpayne@69 110 def get_window_geometry(top):
jpayne@69 111 geom = top.wm_geometry()
jpayne@69 112 m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom)
jpayne@69 113 return tuple(map(int, m.groups()))
jpayne@69 114
jpayne@69 115
jpayne@69 116 def set_window_geometry(top, geometry):
jpayne@69 117 top.wm_geometry("{:d}x{:d}+{:d}+{:d}".format(*geometry))
jpayne@69 118
jpayne@69 119
jpayne@69 120 if __name__ == "__main__":
jpayne@69 121 from unittest import main
jpayne@69 122 main('idlelib.idle_test.test_zoomheight', verbosity=2, exit=False)
jpayne@69 123
jpayne@69 124 # Add htest?