File: session.rst

package info (click to toggle)
gnome-builder 49.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,412 kB
  • sloc: ansic: 270,495; xml: 3,485; python: 1,863; javascript: 82; makefile: 65; sh: 21; cpp: 14
file content (53 lines) | stat: -rw-r--r-- 1,555 bytes parent folder | download | duplicates (3)
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
################
Session Tracking
################

Some plugins may want to save state when the user closes Builder.
The `Ide.SessionAddin` allows for saving and restoring state of an `Ide.Page` when a project is closed or re-opened.

.. code-block:: python3

   # my_plugin.py

   import gi

   from gi.repository import GObject
   from gi.repository import Gio
   from gi.repository import Ide

   class MySessionAddin(Ide.Object, Ide.SessionAddin):

       def get_autosave_properties(self):
           return ['uri', 'current-directory']

       def can_save_page(self, page):
           return issubclass(page, My.CustomPage)

       def do_save_page_async(self, page, cancellable, callback, data):
           # Create our async task
           task = Ide.Task.new(self, cancellable, callback)

           # State is saved as a variant
           task.result = GLib.Variant.new_int(123)

           # Now complete task
           task.return_boolean(True)

       def do_save_finish(self, task):
           if task.propagate_boolean():
               return task.result

       def do_restore_page_async(self, state, cancellable, callback, data):
           # Create our async task
           task = Ide.Task.new(self, cancellable, callback)

           # state is a GLib.Variant matching what we saved
           # unpack state here

           my_page = My.CustomPage.new(data_from_state)

           # Now complete task
           task.return_pointer(my_page)

       def do_restore_finish(self, task):
           return task.propagate_pointer()