Skip to content

Commit

Permalink
port plugin api to GMenu
Browse files Browse the repository at this point in the history
This is broken now that we use Gmenu instead of arcane modelbuttons.

The maintained plugins have been ported and I can confirm that they work
correctly.

The exception is hamster which I didn't port yet since it uses them in
a non trivial way, modifying them (which is a lot more conveluted with
gmenu). Will do that after GTK4, and after I can figure out how to run
that plugin without dbus errors

sendEmail is a bit different, in this case I used an action group to
associate to the correct open editor, instead of a global action.
I used the style "editor" (component) "_plugin_", "email" - the name of
the plugin. Also, I have done this after I travelled back in time from
the very far feature where I realised that this is needed, and that you
need to not use '.' in the action group name or GTK will crash.
  • Loading branch information
ranchester2 authored and diegogangl committed May 1, 2022
1 parent 72d916b commit 188b5f6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 33 deletions.
40 changes: 31 additions & 9 deletions GTG/core/plugins/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import pickle
import logging
from gi.repository import GLib
from GTG.core.dirs import plugin_configuration_dir

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -146,21 +147,42 @@ def remove_active_selection_changed_callback(self, plugin_class):
def add_menu_item(self, item):
"""Adds a menu entry to the menu of the Task Browser or Task Editor.
@param item: The Gtk.MenuItem that is going to be added.
@param item: The Gio.MenuItem that is going to be added.
"""
menu_box = self.__builder.get_object('menu_box')
menu_box.add(item)
menu_box.reorder_child(item, 1)
menu_box.show_all()
_, _, menu = self.__builder.get_object(
'editor_menu' if self.is_editor() else 'main_menu'
).iterate_item_links(0).get_next()
menu.append_item(item)

def remove_menu_item(self, item):
"""Remove a menu entry to the menu of the Task Browser or Task Editor.
@param item: The Gtk.MenuItem that is going to be removed.
@param item: The Gio.MenuItem that is going to be removed.
"""

menu_box = self.__builder.get_object('menu_box')
menu_box.remove(item)
# you cannot remove items by identity since there values are simply copied
# when adding a new one. A reliable solution is to instead find the first one
# with the same label as the given one.
_, _, menu = self.__builder.get_object(
'editor_menu' if self.is_editor() else 'main_menu'
# all menu items are added to the first section
).iterate_item_links(0).get_next()

length = menu.get_n_items()
i = 0
while i < length:
name = ''.join(menu.get_item_attribute_value(
i,
'label',
GLib.VariantType.new('s')).get_string()
)

if name == ''.join(item.get_attribute_value(
'label',
GLib.VariantType.new('s')).get_string()
):
menu.remove(i)
return
i += 1

def add_widget_to_taskeditor(self, widget):
"""Adds a widget to the bottom of the task editor dialog
Expand Down
8 changes: 3 additions & 5 deletions GTG/plugins/dev_console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def __init__(self):
self.window = None
self.terminal = None

self.menu_btn = Gtk.ModelButton.new()
self.menu_btn.props.text = _('Developer Console')
self.menu_btn.connect('clicked', self.open_window)
self.menu_item = Gio.MenuItem.new('Developer Console', 'app.plugin.open_console')

# Set prompt.
sys.ps1 = ">>> "
Expand All @@ -80,7 +78,7 @@ def activate(self, api: PluginAPI) -> None:
"""Plugin is activated."""

self.api = api
self.api.add_menu_item(self.menu_btn)
self.api.add_menu_item(self.menu_item)

namespace = GTGNamespace(self.api.get_view_manager())
self.window = Gtk.Window()
Expand Down Expand Up @@ -129,7 +127,7 @@ def welcome_message(self, namespace):
def deactivate(self, api: PluginAPI) -> None:
"""Deactivates the plugin."""

api.remove_menu_item(self.menu_btn)
api.remove_menu_item(self.menu_item)


def open_window(self, widget=None, unsued=None) -> None:
Expand Down
12 changes: 6 additions & 6 deletions GTG/plugins/export/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import webbrowser
import logging

from gi.repository import GObject, Gtk, GdkPixbuf, GLib
from gi.repository import GObject, Gtk, GdkPixbuf, GLib, Gio

from gettext import gettext as _
from GTG.plugins.export.task_str import get_task_wrappers
Expand Down Expand Up @@ -147,10 +147,10 @@ def get_selected_tasks(self):
# GTK FUNCTIONS ###############################################################
def _init_gtk(self):
""" Initialize all the GTK widgets """
self.menu_item = Gtk.ModelButton()
self.menu_item.props.text = _("Export the tasks currently listed")
self.menu_item.connect('clicked', self.show_dialog)
self.menu_item.show()
self.menu_item = Gio.MenuItem.new(_("Export the tasks currently listed"), "app.plugin.open_export")
open_action = Gio.SimpleAction.new('plugin.open_export', None)
open_action.connect('activate', self.show_dialog)
self.plugin_api.get_view_manager().add_action(open_action)
self.plugin_api.add_menu_item(self.menu_item)

builder = Gtk.Builder()
Expand Down Expand Up @@ -196,7 +196,7 @@ def _gtk_deactivate(self):
""" Remove Menu item for this plugin """
self.plugin_api.remove_menu_item(self.menu_item)

def show_dialog(self, widget):
def show_dialog(self, action, param):
""" Show dialog with options for export """
parent_window = self.plugin_api.get_ui().get_window()
self.export_dialog.set_transient_for(parent_window)
Expand Down
25 changes: 19 additions & 6 deletions GTG/plugins/send_email/sendEmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,37 @@

class SendEmailPlugin():

ACTION_GROUP_PREF = "editor_plugin_email"

def onTaskOpened(self, plugin_api):
"""
Adds the button when a task is opened.
"""
self.plugin_api = plugin_api
group = Gio.SimpleActionGroup()
send_action = Gio.SimpleAction.new("send_as_email", None)
send_action.connect("activate", self._on_send_activate, plugin_api)
group.add_action(send_action)
plugin_api.get_ui().window.insert_action_group(self.ACTION_GROUP_PREF, group)

self.menu_item = Gio.MenuItem.new(
_("Send via email"), ".".join([self.ACTION_GROUP_PREF, "send_as_email"])
)
plugin_api.add_menu_item(self.menu_item)

self.menu_item = Gtk.ModelButton.new()
self.menu_item.props.text = _("Send via email")
self.menu_item.connect("clicked", self.onTbTaskButton, plugin_api)
self.plugin_api.add_menu_item(self.menu_item)
def onTaskClosed(self, plugin_api):
"""
Removes the button when a task is closed.
"""
plugin_api.get_ui().window.insert_action_group(self.ACTION_GROUP_PREF, None)
plugin_api.remove_menu_item(self.menu_item)

def deactivate(self, plugin_api):
"""
Desactivates the plugin.
"""
pass

def onTbTaskButton(self, widget, plugin_api):
def _on_send_activate(self, action, param, plugin_api):
"""
When the user presses the button.
"""
Expand Down
11 changes: 6 additions & 5 deletions GTG/plugins/untouched_tasks/untouchedTasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import logging
from gettext import gettext as _

from gi.repository import Gtk
from gi.repository import Gtk, Gio

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,9 +59,7 @@ def __init__(self):
}

self.builder.connect_signals(SIGNAL_CONNECTIONS_DIC)
self.menu_item = Gtk.ModelButton.new()
self.menu_item.props.text = _("Add @untouched tag")
self.menu_item.connect("clicked", self.add_untouched_tag)
self.menu_item = Gio.MenuItem.new(_("Add @untouched tag"), "app.plugin.add_untouched_tag")

def activate(self, plugin_api):
self.plugin_api = plugin_api
Expand All @@ -71,6 +69,9 @@ def activate(self, plugin_api):
self.preferences_load()
self.preferences_apply()
# add menu item
self.add_untouched_action = Gio.SimpleAction.new("plugin.add_untouched_tag", None)
self.add_untouched_action.connect('activate', self.add_untouched_tag)
self.plugin_api.get_view_manager().add_action(self.add_untouched_action)
self.plugin_api.add_menu_item(self.menu_item)

def deactivate(self, plugin_api):
Expand All @@ -92,7 +93,7 @@ def cancel_autopurge(self):
log.debug("Automatic untouched tasks check cancelled")
self.timer.cancel()

def add_untouched_tag(self, widget=None):
def add_untouched_tag(self, action=None, param=None):
# If no tag is picked up from preferences
tag_name = self.pref_tag_name.get_text()
if not tag_name:
Expand Down
4 changes: 2 additions & 2 deletions docs/contributors/plugin development guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ A helloword plugin is packed along with GTG for example purposes. You can check

| **Method** | **Description** |
|------------|-----------------|
|add_menu_item(item) | Adds a menu to the Plugin Menu in the menu bar of the task browser. <br> **item** is the gtk.MenuItem that is going to be added. |
|remove_menu_item(item) | Removes a menu from the Plugin Menu in the menu bar of the task browser. <br> **item** is the gtk.MenuItem that is going to be removed. |
|add_menu_item(item) | Adds a menu to the Plugin Menu in the menu bar of the task browser. <br> **item** is the gio.MenuItem that is going to be added. |
|remove_menu_item(item) | Removes a menu from the Plugin Menu in the menu bar of the task browser. <br> **item** is the gio.MenuItem that is going to be removed. |
|add_toolbar_item(item) | Adds a button to the task browser's toolbar. <br> **item** is the gtk.ToolButton that is going to be added to the toolbar. <br> **Returns**: a integer that represents the position of the item in the toolbar. |
|remove_toolbar_item(item, n=None) | Removes a toolbar button from the task browser's toolbar. <br> **item** is the gtk.ToolButton that is going to be removed. <br> **n** is the position of the item to be removed. It's useful to remove gtk.SeparatorToolItem(). ie, remove_toolbar_item(None, 14) |
|add_task_toolbar_item(item) | Adds a button to the task editor's toolbar. <br> **item** is the gtk.ToolButton that is going to be added to the toolbar. |
Expand Down

0 comments on commit 188b5f6

Please sign in to comment.