Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
127 lines (100 sloc) 3.86 KB
# gEdit-Toggle-Menu - Add missing show or hide menu bar feature in gEdit.
# Copyright (C) 2013 Intars Students
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from gi.repository import Gtk, GObject, Gedit, Gdk
# Toggle menu item XML
ui_manager_xml = """
<ui>
<menubar name="MenuBar">
<menu name="ViewMenu" action="View">
<placeholder name="ViewOps_2">
<separator/>
<menuitem name="toggleMenuBar" action="toggleMenuBar"/>
</placeholder>
</menu>
</menubar>
</ui>
"""
class toggleMenuInstance:
def __init__(self, plugin, window):
self._window = window
self._plugin = plugin
# Find location of Toggle Menu config file (if it's possible)
try: self._config = os.path.expanduser('~')+"/.toggleMenu"
except: self._config = ""
# Get gEdit main menu bar object
self._manager = self._window.get_ui_manager()
self._menuBar = self._manager.get_widget("/ui/MenuBar")
self._menuVisible = True
# Append custom menu item and key shortcut to toggle menu bar
toggleMenuBar = (
'toggleMenuBar',
None,
_("Toggle Menu"),
'<Ctrl><Alt>M',
_("Show or Hide Menu Bar"),
self.toggle
)
self._action_group = Gtk.ActionGroup("toggleMenuBar")
self._action_group.add_actions([toggleMenuBar])
self._manager.insert_action_group(self._action_group, -1)
self._ui_id = self._manager.add_ui_from_string(ui_manager_xml)
# Auto start with menu hidden?
try:
if os.path.exists(self._config):
self._menuBar.hide()
self._menuVisible = False
except: pass
# Track gedit window for sudden fullscreen movements
self._trackWindow_id = self._window.connect("window-state-event", self.trackWindow)
def trackWindow(self, window, callback_data):
if not callback_data.new_window_state & Gdk.WindowState.FULLSCREEN and callback_data.changed_mask & Gdk.WindowState.FULLSCREEN:
# If menu wasn't visible before fullscreen, hide it again (with fake toggle)
if not self._menuVisible:
self._menuVisible = True
self.toggle("_")
# Show menu, remove custom menu item & keyboard shortcut
def deactivate(self):
self._menuBar.show()
self._manager.remove_ui(self._ui_id)
self._manager.ensure_update()
self._window.disconnect(self._trackWindow_id)
del self
# Toggle menu bar and save current status in file (kinda)
def toggle(self, action):
#if self._menuBar.flags() & Gtk.VISIBLE:
if self._menuVisible == True:
self._menuBar.hide()
self._menuVisible = False
try: open(self._config, 'w').close()
except: pass
else:
self._menuBar.show()
self._menuVisible = True
try: os.unlink(self._config)
except: pass
# Basic gedit plugin structure (nothing interesting)
class toggleMenuPlugin(GObject.Object, Gedit.WindowActivatable):
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
self._instances = {}
def do_activate(self):
self._instances[self.window] = toggleMenuInstance(self, self.window)
def do_deactivate(self):
self._instances[self.window].deactivate()
del self._instances[self.window]
def update_ui(self):
pass