Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Sublime text toggle menu plugin with ctypes

By: a guest on Sep 6th, 2010  |  syntax: Python  |  size: 2.72 KB  |  views: 242  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # toggleMenubar plugin v1.1.0 by Shoozza
  2. #
  3. # Forum Thread:
  4. #       http://www.sublimetext.com/forum/viewtopic.php?f=5&t=1080
  5. #
  6. # example keybinding:
  7. #       <binding key="leftalt+m" command="toggleMenubar"/>
  8. #
  9. # Known issues:
  10. #       fullscreen mode menu toggling can cause issues in normal and fullscreen mode
  11.  
  12. import sublime, sublimeplugin, os
  13. from ctypes import *
  14.  
  15. firstRun = 1
  16. needSave = 1
  17. show  = 1
  18. hwnd  = windll.User32.GetActiveWindow()
  19. hmenu = windll.User32.GetMenu(hwnd)
  20.  
  21. def doToggleMenubar():
  22.         if globals()["firstRun"] == 1:
  23.                 globals()["firstRun"] = 0
  24.                 globals()["hwnd"]  = windll.User32.GetActiveWindow()
  25.                 globals()["hmenu"] = windll.User32.GetMenu(globals()["hwnd"])
  26.  
  27.         if globals()["hmenu"] == 0 or globals()["hwnd"] == 0:
  28.                 loadToggleMenubarState()
  29.  
  30.         newMenu = windll.User32.GetMenu(globals()["hwnd"])
  31.         if newMenu != 0 and newMenu != globals()["hmenu"]:
  32.                 globals()["hmenu"] = newMenu
  33.        
  34.         if globals()["show"] == 0:
  35.                 windll.User32.SetMenu(globals()["hwnd"], globals()["hmenu"])
  36.                 globals()["show"] = 1
  37.         else:
  38.                 windll.User32.SetMenu(globals()["hwnd"], 0)
  39.                 globals()["show"] = 0          
  40.  
  41.         globals()["needSave"] = 1
  42.  
  43. def loadToggleMenubarState():
  44.         if os.path.isfile(sublime.packagesPath()+"\\User\\toggleMenubar.tmp"):
  45.                 file = open(sublime.packagesPath()+"\\User\\toggleMenubar.tmp", 'r')
  46.                 try:
  47.                         globals()["show"]  = int(file.readline())
  48.                         globals()["hmenu"] = int(file.readline())
  49.                         globals()["hwnd"]  = int(file.readline())
  50.                 except:
  51.                         pass
  52.                 file.close()
  53.         else:
  54.                 globals()["hwnd"]  = windll.User32.GetActiveWindow()
  55.                 globals()["hmenu"]  = windll.User32.GetMenu(globals()["hwnd"])
  56.                
  57. def saveToggleMenubarState():
  58.         if globals()["needSave"] == 1:
  59.                 globals()["needSave"] = 0
  60.                 file = open(sublime.packagesPath()+"\\User\\toggleMenubar.tmp", 'w')
  61.                 file.write(str(globals()["show"]))
  62.                 file.write("\n")
  63.                 file.write(str(globals()["hmenu"]))
  64.                 file.write("\n")
  65.                 file.write(str(globals()["hwnd"]))
  66.                 file.close()
  67.  
  68. def cleanUpToggleMenubarStateFile():
  69.         if os.path.isfile(sublime.packagesPath()+"\\User\\toggleMenubar.tmp"):
  70.                 os.remove(sublime.packagesPath()+"\\User\\toggleMenubar.tmp")
  71.  
  72. class toggleMenubarCommand(sublimeplugin.TextCommand):
  73.         def run(self, view, args):
  74.                 doToggleMenubar()
  75.                 saveToggleMenubarState()
  76.  
  77. class toggleMenubarStartup(sublimeplugin.Plugin):
  78.         def onActivated(self, view):
  79.                 if globals()["firstRun"] == 1:
  80.                         if os.path.isfile(sublime.packagesPath()+"\\User\\toggleMenubar.tmp"):
  81.                                 file = open(sublime.packagesPath()+"\\User\\toggleMenubar.tmp", 'r')
  82.                                 try:
  83.                                         globals()["show"] = int(file.readline())
  84.                                 except:
  85.                                         pass
  86.                                 file.close()
  87.                                 if globals()["show"] == 0:
  88.                                         globals()["show"] = 1
  89.                                         doToggleMenubar()
  90.                                 else:
  91.                                         cleanUpToggleMenubarStateFile()