Search code examples
pythonpluginsgedit

Gedit not getting views on window creation (Plugin development)


I am developing a plugin for Gedit.

import gedit

class ReloadOnSave(gedit.Plugin):

    def __init__(self):
        gedit.Plugin.__init__(self)

    def activate(self, window):
        for view in window.get_views():
            self.connect_handlers(view)

    def connect_handlers(self, view):
        print 'Reached here' // This doesnt happen on Gedit startup.

What happens is, when i open up gedit(with any number of tabs open), i don't see 'Reached here'. But if i go to the plugins menu, and disabled and renable my plugin, i will print 'Reached here' (as many times as however many tabs are open)

I also do need get_views(), as i need to use the 'saved' event handler. (ultimately I am trying to do something when a document is saved)

So, why isn't window.get_views() returning any views when Gedit is first opened? (and is only doing so if i disable and renable the plugin)

Also, if i do 'print window.get_views(), same thing will happen. It will print an empty list, but if it disable/re-enable the plugin, i get a list with all the views.


Solution

  • That happens because when your plugin is activated, you don't have any tabs yet. Tabs are created after plugin activation. You might want to listen to the "tab-added" and "tab-removed" signals to fix that.