-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoggleMenu.py
More file actions
126 lines (100 loc) · 3.86 KB
/
toggleMenu.py
File metadata and controls
126 lines (100 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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