macosx.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. """
  2. A number of functions that enhance IDLE on macOS.
  3. """
  4. from os.path import expanduser
  5. import plistlib
  6. from sys import platform # Used in _init_tk_type, changed by test.
  7. import tkinter
  8. ## Define functions that query the Mac graphics type.
  9. ## _tk_type and its initializer are private to this section.
  10. _tk_type = None
  11. def _init_tk_type():
  12. """ Initialize _tk_type for isXyzTk functions.
  13. This function is only called once, when _tk_type is still None.
  14. """
  15. global _tk_type
  16. if platform == 'darwin':
  17. # When running IDLE, GUI is present, test/* may not be.
  18. # When running tests, test/* is present, GUI may not be.
  19. # If not, guess most common. Does not matter for testing.
  20. from idlelib.__init__ import testing
  21. if testing:
  22. from test.support import requires, ResourceDenied
  23. try:
  24. requires('gui')
  25. except ResourceDenied:
  26. _tk_type = "cocoa"
  27. return
  28. root = tkinter.Tk()
  29. ws = root.tk.call('tk', 'windowingsystem')
  30. if 'x11' in ws:
  31. _tk_type = "xquartz"
  32. elif 'aqua' not in ws:
  33. _tk_type = "other"
  34. elif 'AppKit' in root.tk.call('winfo', 'server', '.'):
  35. _tk_type = "cocoa"
  36. else:
  37. _tk_type = "carbon"
  38. root.destroy()
  39. else:
  40. _tk_type = "other"
  41. return
  42. def isAquaTk():
  43. """
  44. Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon).
  45. """
  46. if not _tk_type:
  47. _init_tk_type()
  48. return _tk_type == "cocoa" or _tk_type == "carbon"
  49. def isCarbonTk():
  50. """
  51. Returns True if IDLE is using a Carbon Aqua Tk (instead of the
  52. newer Cocoa Aqua Tk).
  53. """
  54. if not _tk_type:
  55. _init_tk_type()
  56. return _tk_type == "carbon"
  57. def isCocoaTk():
  58. """
  59. Returns True if IDLE is using a Cocoa Aqua Tk.
  60. """
  61. if not _tk_type:
  62. _init_tk_type()
  63. return _tk_type == "cocoa"
  64. def isXQuartz():
  65. """
  66. Returns True if IDLE is using an OS X X11 Tk.
  67. """
  68. if not _tk_type:
  69. _init_tk_type()
  70. return _tk_type == "xquartz"
  71. def readSystemPreferences():
  72. """
  73. Fetch the macOS system preferences.
  74. """
  75. if platform != 'darwin':
  76. return None
  77. plist_path = expanduser('~/Library/Preferences/.GlobalPreferences.plist')
  78. try:
  79. with open(plist_path, 'rb') as plist_file:
  80. return plistlib.load(plist_file)
  81. except OSError:
  82. return None
  83. def preferTabsPreferenceWarning():
  84. """
  85. Warn if "Prefer tabs when opening documents" is set to "Always".
  86. """
  87. if platform != 'darwin':
  88. return None
  89. prefs = readSystemPreferences()
  90. if prefs and prefs.get('AppleWindowTabbingMode') == 'always':
  91. return (
  92. 'WARNING: The system preference "Prefer tabs when opening'
  93. ' documents" is set to "Always". This will cause various problems'
  94. ' with IDLE. For the best experience, change this setting when'
  95. ' running IDLE (via System Preferences -> Dock).'
  96. )
  97. return None
  98. ## Fix the menu and related functions.
  99. def addOpenEventSupport(root, flist):
  100. """
  101. This ensures that the application will respond to open AppleEvents, which
  102. makes is feasible to use IDLE as the default application for python files.
  103. """
  104. def doOpenFile(*args):
  105. for fn in args:
  106. flist.open(fn)
  107. # The command below is a hook in aquatk that is called whenever the app
  108. # receives a file open event. The callback can have multiple arguments,
  109. # one for every file that should be opened.
  110. root.createcommand("::tk::mac::OpenDocument", doOpenFile)
  111. def hideTkConsole(root):
  112. try:
  113. root.tk.call('console', 'hide')
  114. except tkinter.TclError:
  115. # Some versions of the Tk framework don't have a console object
  116. pass
  117. def overrideRootMenu(root, flist):
  118. """
  119. Replace the Tk root menu by something that is more appropriate for
  120. IDLE with an Aqua Tk.
  121. """
  122. # The menu that is attached to the Tk root (".") is also used by AquaTk for
  123. # all windows that don't specify a menu of their own. The default menubar
  124. # contains a number of menus, none of which are appropriate for IDLE. The
  125. # Most annoying of those is an 'About Tck/Tk...' menu in the application
  126. # menu.
  127. #
  128. # This function replaces the default menubar by a mostly empty one, it
  129. # should only contain the correct application menu and the window menu.
  130. #
  131. # Due to a (mis-)feature of TkAqua the user will also see an empty Help
  132. # menu.
  133. from tkinter import Menu
  134. from idlelib import mainmenu
  135. from idlelib import window
  136. closeItem = mainmenu.menudefs[0][1][-2]
  137. # Remove the last 3 items of the file menu: a separator, close window and
  138. # quit. Close window will be reinserted just above the save item, where
  139. # it should be according to the HIG. Quit is in the application menu.
  140. del mainmenu.menudefs[0][1][-3:]
  141. mainmenu.menudefs[0][1].insert(6, closeItem)
  142. # Remove the 'About' entry from the help menu, it is in the application
  143. # menu
  144. del mainmenu.menudefs[-1][1][0:2]
  145. # Remove the 'Configure Idle' entry from the options menu, it is in the
  146. # application menu as 'Preferences'
  147. del mainmenu.menudefs[-3][1][0:2]
  148. menubar = Menu(root)
  149. root.configure(menu=menubar)
  150. menu = Menu(menubar, name='window', tearoff=0)
  151. menubar.add_cascade(label='Window', menu=menu, underline=0)
  152. def postwindowsmenu(menu=menu):
  153. end = menu.index('end')
  154. if end is None:
  155. end = -1
  156. if end > 0:
  157. menu.delete(0, end)
  158. window.add_windows_to_menu(menu)
  159. window.register_callback(postwindowsmenu)
  160. def about_dialog(event=None):
  161. "Handle Help 'About IDLE' event."
  162. # Synchronize with editor.EditorWindow.about_dialog.
  163. from idlelib import help_about
  164. help_about.AboutDialog(root)
  165. def config_dialog(event=None):
  166. "Handle Options 'Configure IDLE' event."
  167. # Synchronize with editor.EditorWindow.config_dialog.
  168. from idlelib import configdialog
  169. # Ensure that the root object has an instance_dict attribute,
  170. # mirrors code in EditorWindow (although that sets the attribute
  171. # on an EditorWindow instance that is then passed as the first
  172. # argument to ConfigDialog)
  173. root.instance_dict = flist.inversedict
  174. configdialog.ConfigDialog(root, 'Settings')
  175. def help_dialog(event=None):
  176. "Handle Help 'IDLE Help' event."
  177. # Synchronize with editor.EditorWindow.help_dialog.
  178. from idlelib import help
  179. help.show_idlehelp(root)
  180. root.bind('<<about-idle>>', about_dialog)
  181. root.bind('<<open-config-dialog>>', config_dialog)
  182. root.createcommand('::tk::mac::ShowPreferences', config_dialog)
  183. if flist:
  184. root.bind('<<close-all-windows>>', flist.close_all_callback)
  185. # The binding above doesn't reliably work on all versions of Tk
  186. # on macOS. Adding command definition below does seem to do the
  187. # right thing for now.
  188. root.createcommand('exit', flist.close_all_callback)
  189. if isCarbonTk():
  190. # for Carbon AquaTk, replace the default Tk apple menu
  191. menu = Menu(menubar, name='apple', tearoff=0)
  192. menubar.add_cascade(label='IDLE', menu=menu)
  193. mainmenu.menudefs.insert(0,
  194. ('application', [
  195. ('About IDLE', '<<about-idle>>'),
  196. None,
  197. ]))
  198. if isCocoaTk():
  199. # replace default About dialog with About IDLE one
  200. root.createcommand('tkAboutDialog', about_dialog)
  201. # replace default "Help" item in Help menu
  202. root.createcommand('::tk::mac::ShowHelp', help_dialog)
  203. # remove redundant "IDLE Help" from menu
  204. del mainmenu.menudefs[-1][1][0]
  205. def fixb2context(root):
  206. '''Removed bad AquaTk Button-2 (right) and Paste bindings.
  207. They prevent context menu access and seem to be gone in AquaTk8.6.
  208. See issue #24801.
  209. '''
  210. root.unbind_class('Text', '<B2>')
  211. root.unbind_class('Text', '<B2-Motion>')
  212. root.unbind_class('Text', '<<PasteSelection>>')
  213. def setupApp(root, flist):
  214. """
  215. Perform initial OS X customizations if needed.
  216. Called from pyshell.main() after initial calls to Tk()
  217. There are currently three major versions of Tk in use on OS X:
  218. 1. Aqua Cocoa Tk (native default since OS X 10.6)
  219. 2. Aqua Carbon Tk (original native, 32-bit only, deprecated)
  220. 3. X11 (supported by some third-party distributors, deprecated)
  221. There are various differences among the three that affect IDLE
  222. behavior, primarily with menus, mouse key events, and accelerators.
  223. Some one-time customizations are performed here.
  224. Others are dynamically tested throughout idlelib by calls to the
  225. isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which
  226. are initialized here as well.
  227. """
  228. if isAquaTk():
  229. hideTkConsole(root)
  230. overrideRootMenu(root, flist)
  231. addOpenEventSupport(root, flist)
  232. fixb2context(root)
  233. if __name__ == '__main__':
  234. from unittest import main
  235. main('idlelib.idle_test.test_macosx', verbosity=2)