-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrestoretabs.py
73 lines (62 loc) · 2.6 KB
/
restoretabs.py
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
import os
from gi.repository import GObject, GLib, Gtk, Gio, Gedit
SETTINGS_SCHEMA = "org.gnome.gedit.plugins.restoretabs"
class RestoreTabsWindowActivatable(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = "RestoreTabsWindowActivatable"
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
self._handlers = []
def do_activate(self):
"""
Connect signal handlers.
"""
handlers = []
handler_id = self.window.connect("delete-event",
self.on_window_delete_event)
self._handlers.append(handler_id)
# temporary handler to catch the first time a window is shown
self._temp_handler = self.window.connect("show", self.on_window_show)
def do_deactivate(self):
"""
Disconect any signal handlers that were added in do_activate().
"""
[self.window.disconnect(handler_id) for handler_id in self._handlers]
def do_update_state(self):
pass
def is_first_window(self):
"""
Return True if the window being added is the first window instance.
"""
app = Gedit.App.get_default()
if len(app.get_windows()) <= 1:
return True
else:
return False
def on_window_delete_event(self, window, event, data=None):
uris = []
for document in window.get_documents():
gfile = document.get_location()
if gfile:
uris.append(gfile.get_uri())
settings = Gio.Settings.new(SETTINGS_SCHEMA)
settings.set_value('uris', GLib.Variant("as", uris))
return False
def on_window_show(self, window, data=None):
"""
Only restore tabs if this window is the first Gedit window instance.
"""
if self.is_first_window():
tab = self.window.get_active_tab()
if tab.get_state() == 0 and not tab.get_document().get_location():
self.window.close_tab(tab)
settings = Gio.Settings.new(SETTINGS_SCHEMA)
uris = settings.get_value('uris')
if uris:
for uri in uris:
location = Gio.file_new_for_uri(uri)
tab = self.window.get_tab_from_location(location)
if not tab:
self.window.create_tab_from_location(location, None, 0,
0, False, True)
self.window.disconnect(self._temp_handler)