zoomheight.py 4.1 KB

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