Gedit panels: Move content of bottom and side panel

Disclaimer: It’s the first time I ask a question specifically on gedit plugin development here. If this is not the right place, I apologize. If there is a better place to ask such a question, please let me know!

I’m working on a plugin for gedit, gedit-reST-plugin, which renders a document in either gedit’s bottom panel or the side panel. Which location is used is at the user’s discretion (via the plugin’s preferences).

Displaying content and adding the container window to either the bottom panel or the side panel works reliably. What I can’t get to work is to remove the container from one panel and add add it to the other.

  1. When the preferences dialog is created, the instance gets a reference of the parent window passed. I want to use that later to conveniently access instance members.

  2. When the user changes the display target, I invoke the methods of the parent reference to remove and re-add the panel. Interestingly, at that occasion when I call the _parent reference’s methods the instance members are None for some reason even though they were initialized earlier.

    Traceback (most recent call last):
      File "/home/peter/.local/share/gedit/plugins/reST/config.py", line 90, in on_button_toggled
        self._parent.do_deactivate()
      File "/home/peter/.local/share/gedit/plugins/reST/__init__.py", line 66, in do_deactivate
        self.html_container.clear_view()
    AttributeError: 'NoneType' object has no attribute 'clear_view'
    

Can someone spot what I do wrong?

How do I remove the container correctly and/or move it to the other panel?

1 Like

I haven’t tried your code to be sure, but I’m reasonably certain that when the preferences window is created, it is instantiating a new instance of your plugin class (separate from the instance of the parent window), and so when RestructuredtextConfigWidget(self) is called, self is the preferences window, not the parent window.

(Your plugin class inherits from both Gedit.WindowActivatable and PeasGtk.Configurable but it is possible to have two separate classes that inherit from each and gedit (or libpeas, I’m not sure) will instantiate the right class when necessary.)

This current approach also isn’t scalable; if there are two or more main windows open at the same time, only one will be affected by the settings change.

I suggest each instance of your plugin class (which is associated with a main window, since it inherits from Gedit.WindowActivatable) listen to changes on a settings object (it can be their own instance) and react by adjusting its own main window.

That’s a good point, thanks for the hint! :+1:

Do you know of an example piece of source code that does this? That might be helpful.

(I can still only post two links per post, apologies in advance.)

As @ebassi mentioned, connecting to the changed signal is the easiest way to be notified of settings changes.

In the Python Console plugin, you can see the plugin class connects to self._settings’s changed signal, then in the signal handler (on_settings_changed(), further down the same file) it reconfigures based on the updated settings.

You can also connect to changed::x that is only triggered when the x key is changed. I suggest reading the documentation for more details.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.