Welcome to a new installment of the plugins tutorial! Todays issue will cover menu modifications. This allows you to add new menu options for features you introduce with your plugin.
Extending a menu in PyGI
Now how do we extend a Liferea menu? First we get the menu from the GtkBuilder:
self.menu = self.shell.get_property("builder").get_object("tools_menu")
Now we can append a new menu action:
toolsmenu.append ('Menu Entry Title', 'app.MyPluginMenuEntry')
Finally we have to create an action matching app.MyPluginMenuEntry
:
action = Gio.SimpleAction.new ('MyPluginMenuEntry', None)
action.connect("activate", self._menu_callback)
self._app = Liferea.Shell.get_window().get_application ()
self._app.add_action (action)
Available Menus
You probably noticed the ToolsMenu
and MainwindowMenubar
ids in the XML snippet above. Here is a list of all menu ids in 1.14 which also can be found in glade/liferea_menu.ui.
submenu id | Description |
---|---|
feed_menu | All actions regarding subscriptions and the feed list |
item_menu | All actions regarding the item list |
view_menu | All actions controlling reading layout |
tools_menu | The menu to access preferences and update status |
help_menu | The place to go for help |
Complete Solution
Everything put together in a plugin could look like this:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Liferea, Gtk
class AppActivatable(GObject.Object, Liferea.ShellActivatable):
__gtype_name__ = "MyPluginAppActivatable"
shell = GObject.property(type=Liferea.Shell)
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
action = Gio.SimpleAction.new ('InstallPlugins', None)
action.connect("activate", self._run)
self._app = self.shell.get_window().get_application ()
self._app.add_action (action)
toolsmenu = self.shell.get_property("builder").get_object ("tools_menu")
toolsmenu.append (_('Plugins'), 'app.InstallPlugins')
def do_deactivate(self):
self._browser = None
self._app.remove_action ('InstallPlugins')
self.app = None
def _run(self, action, data=None):
print("Menu action triggered")
For a real world example have a look at https://github.com/lwindolf/liferea/blob/master/plugins/plugin-installer.py
Related
Also check out the other plugin tutorial posts
- Plugin Tutorial Part 1 (Plugin Boiler Plate)
- Plugin Tutorial Part 2 (Acessing and modify UI elements)
- Plugin Tutorial Part 3 (Adding menu elements)
- Plugin Tutorial Part 4 (Using GTK inspector)
- Plugin Tutorial Part 5 (Enabling translations for plugins)