| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
Application |
| 4 |
=========== |
| 5 |
|
| 6 |
.. seealso:: |
| 7 |
|
| 8 |
For more detailed information about ``Gtk.Application`` checkout the |
| 9 |
:devdocs:`Using GtkApplication <tutorials/application>` tutorial. |
| 10 |
|
| 11 |
|
| 12 |
:class:`Gtk.Application` is the foundation for creating GTK apps. It encompasses |
| 13 |
many repetitive tasks that a modern application needs such as handling multiple |
| 14 |
instances, D-Bus activation, opening files, command line parsing, |
| 15 |
startup/shutdown, menu management, window management, and more. |
| 16 |
|
| 17 |
In this example we will be also using :class:`Gtk.ApplicationWindow`, it |
| 18 |
provides some extra features over regular :class:`Gtk.Window` for main |
| 19 |
application windows. |
| 20 |
|
| 21 |
Actions |
| 22 |
------- |
| 23 |
|
| 24 |
:class:`Gio.Action` is a way to expose any single task your application or widget |
| 25 |
does by a name. These actions can be disabled/enabled at runtime and they can |
| 26 |
either be activated or have a state changed (if they contain state). |
| 27 |
|
| 28 |
The reason to use actions is to separate out the logic from the UI. For example |
| 29 |
this allows using a menubar on OSX and a gear menu on GNOME both simply |
| 30 |
referencing the name of an action. On the main implementation of this, what you will |
| 31 |
be using is :class:`Gio.SimpleAction`, which will be demonstrated later. |
| 32 |
|
| 33 |
Many classes such as :class:`Gio.MenuItem` and :class:`Gtk.Actionable` interface |
| 34 |
implementations like :class:`Gtk.Button` support properties to set an action |
| 35 |
name. |
| 36 |
|
| 37 |
These actions can be grouped together into a :class:`Gio.ActionGroup` and |
| 38 |
when these groups are added to a widget with :meth:`Gtk.Widget.insert_action_group()` |
| 39 |
they will gain a prefix. Such as "win" when added to a :class:`Gtk.ApplicationWindow`. |
| 40 |
You will use the full action name when referencing it such as "app.about" but when |
| 41 |
you create the action it will just be "about" until added to the application. |
| 42 |
|
| 43 |
You can also very easily make keybindings for actions by setting the `accel` |
| 44 |
property in the :class:`Gio.Menu` file or by using :meth:`Gtk.Application.set_accels_for_action()`. |
| 45 |
|
| 46 |
Menus |
| 47 |
----- |
| 48 |
|
| 49 |
Your menus should be defined in XML using :class:`Gio.Menu` and would reference |
| 50 |
the previously mentioned actions you defined. :class:`Gtk.Application` allows you |
| 51 |
to set a menu via :meth:`Gtk.Application.set_menubar()`. If you make use of :class:`Gio.Resource` |
| 52 |
this can automatically load the menu, otherwise you can set them manually. |
| 53 |
A detailed example is shown below. |
| 54 |
|
| 55 |
Command Line |
| 56 |
------------ |
| 57 |
|
| 58 |
When creating your application it takes a flag property of :class:`Gio.ApplicationFlags`. |
| 59 |
Using this you can let it handle everything itself or have more custom behavior. |
| 60 |
|
| 61 |
You can use ``HANDLES_COMMAND_LINE`` to allow custom behavior in :meth:`Gio.Application.do_command_line()`. |
| 62 |
In combination with :meth:`Gio.Application.add_main_option()` to add custom options. |
| 63 |
|
| 64 |
Using ``HANDLES_OPEN`` will do the work of simply taking file arguments for you and |
| 65 |
let you handle it in :meth:`Gio.Application.do_open()`. |
| 66 |
|
| 67 |
If your application is already open these will all be sent to the existing instance |
| 68 |
unless you use ``NON_UNIQUE`` to allow multiple instances. |
| 69 |
|
| 70 |
Example |
| 71 |
------- |
| 72 |
|
| 73 |
.. image:: images/application.png |
| 74 |
|
| 75 |
.. literalinclude:: examples/application.py |
| 76 |
:linenos: |