Coverage for zoomheight.py: 19%

66 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 13:22 -0700

1"Zoom a window to maximum height." 

2 

3import re 

4import sys 

5import tkinter 

6 

7 

8class WmInfoGatheringError(Exception): 

9 pass 

10 

11 

12class ZoomHeight: 

13 # Cached values for maximized window dimensions, one for each set 

14 # of screen dimensions. 

15 _max_height_and_y_coords = {} 

16 

17 def __init__(self, editwin): 

18 self.editwin = editwin 

19 self.top = self.editwin.top 

20 

21 def zoom_height_event(self, event=None): 

22 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 

31 return "break" 

32 

33 def zoom_height(self): 

34 top = self.top 

35 

36 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): 

111 geom = top.wm_geometry() 

112 m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom) 

113 return tuple(map(int, m.groups())) 

114 

115 

116def set_window_geometry(top, geometry): 

117 top.wm_geometry("{:d}x{:d}+{:d}+{:d}".format(*geometry)) 

118 

119 

120if __name__ == "__main__": 120 ↛ 121line 120 didn't jump to line 121, because the condition on line 120 was never true

121 from unittest import main 

122 main('idlelib.idle_test.test_zoomheight', verbosity=2, exit=False) 

123 

124 # Add htest?