blob: cd50c91c183e493a9daf76ecd7ad17ede0675d1b [file] [log] [blame]
wohlganger58fc71c2017-09-10 16:19:47 -05001"Zoom a window to maximum height."
David Scherer7aced172000-08-15 01:13:23 +00002
3import re
4import sys
Tal Einat5bff3c82019-06-17 22:41:00 +03005import tkinter
Guido van Rossum36e0a922007-07-20 04:05:57 +00006
Tal Einat5bff3c82019-06-17 22:41:00 +03007
8class WmInfoGatheringError(Exception):
9 pass
David Scherer7aced172000-08-15 01:13:23 +000010
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040011
David Scherer7aced172000-08-15 01:13:23 +000012class ZoomHeight:
Tal Einat5bff3c82019-06-17 22:41:00 +030013 # Cached values for maximized window dimensions, one for each set
14 # of screen dimensions.
15 _max_height_and_y_coords = {}
David Scherer7aced172000-08-15 01:13:23 +000016
David Scherer7aced172000-08-15 01:13:23 +000017 def __init__(self, editwin):
18 self.editwin = editwin
Tal Einat5bff3c82019-06-17 22:41:00 +030019 self.top = self.editwin.top
David Scherer7aced172000-08-15 01:13:23 +000020
Terry Jan Reedy4d921582018-06-19 19:12:52 -040021 def zoom_height_event(self, event=None):
Tal Einat5bff3c82019-06-17 22:41:00 +030022 zoomed = self.zoom_height()
23
24 if zoomed is None:
25 self.top.bell()
26 else:
27 menu_status = 'Restore' if zoomed else 'Zoom'
28 self.editwin.update_menu_label(menu='options', index='* Height',
29 label=f'{menu_status} Height')
30
Serhiy Storchaka213ce122017-06-27 07:02:32 +030031 return "break"
David Scherer7aced172000-08-15 01:13:23 +000032
Tal Einat5bff3c82019-06-17 22:41:00 +030033 def zoom_height(self):
34 top = self.top
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040035
Tal Einat5bff3c82019-06-17 22:41:00 +030036 width, height, x, y = get_window_geometry(top)
37
38 if top.wm_state() != 'normal':
39 # Can't zoom/restore window height for windows not in the 'normal'
40 # state, e.g. maximized and full-screen windows.
41 return None
42
43 try:
44 maxheight, maxy = self.get_max_height_and_y_coord()
45 except WmInfoGatheringError:
46 return None
47
48 if height != maxheight:
49 # Maximize the window's height.
50 set_window_geometry(top, (width, maxheight, x, maxy))
51 return True
52 else:
53 # Restore the window's height.
54 #
55 # .wm_geometry('') makes the window revert to the size requested
56 # by the widgets it contains.
57 top.wm_geometry('')
58 return False
59
60 def get_max_height_and_y_coord(self):
61 top = self.top
62
63 screen_dimensions = (top.winfo_screenwidth(),
64 top.winfo_screenheight())
65 if screen_dimensions not in self._max_height_and_y_coords:
66 orig_state = top.wm_state()
67
68 # Get window geometry info for maximized windows.
69 try:
70 top.wm_state('zoomed')
71 except tkinter.TclError:
72 # The 'zoomed' state is not supported by some esoteric WMs,
73 # such as Xvfb.
74 raise WmInfoGatheringError(
75 'Failed getting geometry of maximized windows, because ' +
76 'the "zoomed" window state is unavailable.')
77 top.update()
78 maxwidth, maxheight, maxx, maxy = get_window_geometry(top)
79 if sys.platform == 'win32':
80 # On Windows, the returned Y coordinate is the one before
81 # maximizing, so we use 0 which is correct unless a user puts
82 # their dock on the top of the screen (very rare).
83 maxy = 0
84 maxrooty = top.winfo_rooty()
85
86 # Get the "root y" coordinate for non-maximized windows with their
87 # y coordinate set to that of maximized windows. This is needed
88 # to properly handle different title bar heights for non-maximized
89 # vs. maximized windows, as seen e.g. in Windows 10.
90 top.wm_state('normal')
91 top.update()
92 orig_geom = get_window_geometry(top)
93 max_y_geom = orig_geom[:3] + (maxy,)
94 set_window_geometry(top, max_y_geom)
95 top.update()
96 max_y_geom_rooty = top.winfo_rooty()
97
98 # Adjust the maximum window height to account for the different
99 # title bar heights of non-maximized vs. maximized windows.
100 maxheight += maxrooty - max_y_geom_rooty
101
102 self._max_height_and_y_coords[screen_dimensions] = maxheight, maxy
103
104 set_window_geometry(top, orig_geom)
105 top.wm_state(orig_state)
106
107 return self._max_height_and_y_coords[screen_dimensions]
108
109
110def get_window_geometry(top):
David Scherer7aced172000-08-15 01:13:23 +0000111 geom = top.wm_geometry()
112 m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom)
Tal Einat5bff3c82019-06-17 22:41:00 +0300113 return tuple(map(int, m.groups()))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114
Tal Einat5bff3c82019-06-17 22:41:00 +0300115
116def set_window_geometry(top, geometry):
117 top.wm_geometry("{:d}x{:d}+{:d}+{:d}".format(*geometry))
Terry Jan Reedy4d921582018-06-19 19:12:52 -0400118
119
120if __name__ == "__main__":
121 from unittest import main
122 main('idlelib.idle_test.test_zoomheight', verbosity=2, exit=False)
123
124 # Add htest?