Home Download Buy Blog Forum Support

toggleMenubar v1.1.0

toggleMenubar v1.1.0

Postby Shoozza on Wed Mar 24, 2010 4:24 pm

My first plugin and my first python script that does something useful.
It allows to toggle the menu bars visibility.

I couldn't find a way to do it with sublime api so I implemented it using ctypes.

Changelog v1.1.0:
Added support for remembering toggle state of the menubar and set it at next startup
Modified togglemenubar.tmp save order
Modified save behavior to save toggle state every time a toggle happens
Fixed some local/global variable naming issues

Changelog v1.0.2:
Fixed not using package folder to save tmp file when --data commandline option is used

Changelog v1.0.1:
Fixed toggleMenubar.tmp is now saved in appdata and not everywhere
Fixed only save menu data once when file saving file

Code: Select all
# toggleMenubar plugin v1.1.0 by Shoozza
#
# Forum Thread:
#   http://www.sublimetext.com/forum/viewtopic.php?f=5&t=1080
#
# example keybinding:
#   <binding key="leftalt+m" command="toggleMenubar"/>
#
# Known issues:
#   fullscreen mode menu toggling can cause issues in normal and fullscreen mode

import sublime, sublimeplugin, os
from ctypes import *

firstRun = 1
needSave = 1
show  = 1
hwnd  = windll.User32.GetActiveWindow()
hmenu = windll.User32.GetMenu(hwnd)

def doToggleMenubar():
   if globals()["firstRun"] == 1:
      globals()["firstRun"] = 0
      globals()["hwnd"]  = windll.User32.GetActiveWindow()
      globals()["hmenu"] = windll.User32.GetMenu(globals()["hwnd"])

   if globals()["hmenu"] == 0 or globals()["hwnd"] == 0:
      loadToggleMenubarState()

   newMenu = windll.User32.GetMenu(globals()["hwnd"])
   if newMenu != 0 and newMenu != globals()["hmenu"]:
      globals()["hmenu"] = newMenu
   
   if globals()["show"] == 0:
      windll.User32.SetMenu(globals()["hwnd"], globals()["hmenu"])
      globals()["show"] = 1
   else:
      windll.User32.SetMenu(globals()["hwnd"], 0)
      globals()["show"] = 0      

   globals()["needSave"] = 1

def loadToggleMenubarState():
   if os.path.isfile(sublime.packagesPath()+"\\User\\toggleMenubar.tmp"):
      file = open(sublime.packagesPath()+"\\User\\toggleMenubar.tmp", 'r')
      try:
         globals()["show"]  = int(file.readline())
         globals()["hmenu"] = int(file.readline())
         globals()["hwnd"]  = int(file.readline())
      except:
         pass
      file.close()
   else:
      globals()["hwnd"]  = windll.User32.GetActiveWindow()
      globals()["hmenu"]  = windll.User32.GetMenu(globals()["hwnd"])
      
def saveToggleMenubarState():
   if globals()["needSave"] == 1:
      globals()["needSave"] = 0
      file = open(sublime.packagesPath()+"\\User\\toggleMenubar.tmp", 'w')
      file.write(str(globals()["show"]))
      file.write("\n")
      file.write(str(globals()["hmenu"]))
      file.write("\n")
      file.write(str(globals()["hwnd"]))
      file.close()

def cleanUpToggleMenubarStateFile():
   if os.path.isfile(sublime.packagesPath()+"\\User\\toggleMenubar.tmp"):
      os.remove(sublime.packagesPath()+"\\User\\toggleMenubar.tmp")

class toggleMenubarCommand(sublimeplugin.TextCommand):
   def run(self, view, args):
      doToggleMenubar()
      saveToggleMenubarState()

class toggleMenubarStartup(sublimeplugin.Plugin):
   def onActivated(self, view):
      if globals()["firstRun"] == 1:
         if os.path.isfile(sublime.packagesPath()+"\\User\\toggleMenubar.tmp"):
            file = open(sublime.packagesPath()+"\\User\\toggleMenubar.tmp", 'r')
            try:
               globals()["show"] = int(file.readline())
            except:
               pass
            file.close()
            if globals()["show"] == 0:
               globals()["show"] = 1
               doToggleMenubar()
            else:
               cleanUpToggleMenubarStateFile()


Noticed that the code has spaces instead of tabs on this page and attachments don't work for txt and py.

You can get the latest version here:
(current version v1.1.0) http://pastebin.com/1gUUebzv

(old version v1.0.2) http://pastebin.com/tRQXGqSC
(old version v1.0.1) http://pastebin.com/jzcx6H7x
(old version v1.0.0) http://pastebin.com/vkGUVL0Y
Last edited by Shoozza on Mon Sep 06, 2010 9:49 am, edited 6 times in total.
Shoozza
 
Posts: 7
Joined: Wed Mar 24, 2010 4:11 pm

Re: toggleMenubar

Postby gpfsmurf on Wed Mar 24, 2010 5:12 pm

Nice but it would have been easier to ask Jon to add a 'showMenu' option ;)
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: toggleMenubar

Postby Shoozza on Fri Mar 26, 2010 5:17 pm

Yeah probably. But I just wanted to see if I can do it myself ;)
Shoozza
 
Posts: 7
Joined: Wed Mar 24, 2010 4:11 pm

Re: toggleMenubar

Postby oxman on Sun Aug 29, 2010 6:44 pm

How can I disable the menubar at startup with your plugin ?
oxman
 
Posts: 66
Joined: Sun Aug 29, 2010 10:00 am

Re: toggleMenubar

Postby Shoozza on Mon Sep 06, 2010 9:51 am

oxman wrote:How can I disable the menubar at startup with your plugin ?

Check out the latest version.
I modified the plugin to remember the last state of the menubar so if it's hidden when you close sublime text it will be hidden on the next startup.
Shoozza
 
Posts: 7
Joined: Wed Mar 24, 2010 4:11 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: Google [Bot] and 20 guests