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