jpayne@68: "Zoom a window to maximum height." jpayne@68: jpayne@68: import re jpayne@68: import sys jpayne@68: import tkinter jpayne@68: jpayne@68: jpayne@68: class WmInfoGatheringError(Exception): jpayne@68: pass jpayne@68: jpayne@68: jpayne@68: class ZoomHeight: jpayne@68: # Cached values for maximized window dimensions, one for each set jpayne@68: # of screen dimensions. jpayne@68: _max_height_and_y_coords = {} jpayne@68: jpayne@68: def __init__(self, editwin): jpayne@68: self.editwin = editwin jpayne@68: self.top = self.editwin.top jpayne@68: jpayne@68: def zoom_height_event(self, event=None): jpayne@68: zoomed = self.zoom_height() jpayne@68: jpayne@68: if zoomed is None: jpayne@68: self.top.bell() jpayne@68: else: jpayne@68: menu_status = 'Restore' if zoomed else 'Zoom' jpayne@68: self.editwin.update_menu_label(menu='options', index='* Height', jpayne@68: label=f'{menu_status} Height') jpayne@68: jpayne@68: return "break" jpayne@68: jpayne@68: def zoom_height(self): jpayne@68: top = self.top jpayne@68: jpayne@68: width, height, x, y = get_window_geometry(top) jpayne@68: jpayne@68: if top.wm_state() != 'normal': jpayne@68: # Can't zoom/restore window height for windows not in the 'normal' jpayne@68: # state, e.g. maximized and full-screen windows. jpayne@68: return None jpayne@68: jpayne@68: try: jpayne@68: maxheight, maxy = self.get_max_height_and_y_coord() jpayne@68: except WmInfoGatheringError: jpayne@68: return None jpayne@68: jpayne@68: if height != maxheight: jpayne@68: # Maximize the window's height. jpayne@68: set_window_geometry(top, (width, maxheight, x, maxy)) jpayne@68: return True jpayne@68: else: jpayne@68: # Restore the window's height. jpayne@68: # jpayne@68: # .wm_geometry('') makes the window revert to the size requested jpayne@68: # by the widgets it contains. jpayne@68: top.wm_geometry('') jpayne@68: return False jpayne@68: jpayne@68: def get_max_height_and_y_coord(self): jpayne@68: top = self.top jpayne@68: jpayne@68: screen_dimensions = (top.winfo_screenwidth(), jpayne@68: top.winfo_screenheight()) jpayne@68: if screen_dimensions not in self._max_height_and_y_coords: jpayne@68: orig_state = top.wm_state() jpayne@68: jpayne@68: # Get window geometry info for maximized windows. jpayne@68: try: jpayne@68: top.wm_state('zoomed') jpayne@68: except tkinter.TclError: jpayne@68: # The 'zoomed' state is not supported by some esoteric WMs, jpayne@68: # such as Xvfb. jpayne@68: raise WmInfoGatheringError( jpayne@68: 'Failed getting geometry of maximized windows, because ' + jpayne@68: 'the "zoomed" window state is unavailable.') jpayne@68: top.update() jpayne@68: maxwidth, maxheight, maxx, maxy = get_window_geometry(top) jpayne@68: if sys.platform == 'win32': jpayne@68: # On Windows, the returned Y coordinate is the one before jpayne@68: # maximizing, so we use 0 which is correct unless a user puts jpayne@68: # their dock on the top of the screen (very rare). jpayne@68: maxy = 0 jpayne@68: maxrooty = top.winfo_rooty() jpayne@68: jpayne@68: # Get the "root y" coordinate for non-maximized windows with their jpayne@68: # y coordinate set to that of maximized windows. This is needed jpayne@68: # to properly handle different title bar heights for non-maximized jpayne@68: # vs. maximized windows, as seen e.g. in Windows 10. jpayne@68: top.wm_state('normal') jpayne@68: top.update() jpayne@68: orig_geom = get_window_geometry(top) jpayne@68: max_y_geom = orig_geom[:3] + (maxy,) jpayne@68: set_window_geometry(top, max_y_geom) jpayne@68: top.update() jpayne@68: max_y_geom_rooty = top.winfo_rooty() jpayne@68: jpayne@68: # Adjust the maximum window height to account for the different jpayne@68: # title bar heights of non-maximized vs. maximized windows. jpayne@68: maxheight += maxrooty - max_y_geom_rooty jpayne@68: jpayne@68: self._max_height_and_y_coords[screen_dimensions] = maxheight, maxy jpayne@68: jpayne@68: set_window_geometry(top, orig_geom) jpayne@68: top.wm_state(orig_state) jpayne@68: jpayne@68: return self._max_height_and_y_coords[screen_dimensions] jpayne@68: jpayne@68: jpayne@68: def get_window_geometry(top): jpayne@68: geom = top.wm_geometry() jpayne@68: m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom) jpayne@68: return tuple(map(int, m.groups())) jpayne@68: jpayne@68: jpayne@68: def set_window_geometry(top, geometry): jpayne@68: top.wm_geometry("{:d}x{:d}+{:d}+{:d}".format(*geometry)) jpayne@68: jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: from unittest import main jpayne@68: main('idlelib.idle_test.test_zoomheight', verbosity=2, exit=False) jpayne@68: jpayne@68: # Add htest?