metro
metro

Reputation: 185

Switch between first sheet(tab) and previous sheet on sublime text

I am trying to program a plugin for myself to switch tab automatically. When I change to other window, the first tab will be shown. When I change back to sublime, the editng tab will show up. I got the following code, which can switch to the first tab. But when I get back to sublime text , it hangs , instead of switching back the tab. Anything I have miss understand with sheets? Thanks!

import sublime
import sublime_plugin



class ShowSheets(sublime_plugin.EventListener):
    """
    Display the name of the current project in the status bar.
    """
    orisheet = 0



    def show_1st(self,view):    
        if view.window() is None:
            return  
        self.orisheet = view.window().active_sheet()
        firstsheet = view.window().sheets()
        view.window().focus_sheet(firstsheet[0])

    def show_original(self,view):
        if view.window() is None:
            return
        if self.orisheet == 0:
            return    
        view.window().focus_sheet(self.orisheet)


    def on_activated(self, view):
        self.show_original (view)

    def on_deactivated(self, view):
        self.show_1st (view)    

Upvotes: 0

Views: 102

Answers (1)

Keith Hall
Keith Hall

Reputation: 16105

This is because you have created an infinite callback loop - on_activated and on_deactivated are called when a tab gains or loses the focus.

Thus, when the ST window loses the focus, on_deactivated runs once. Then, when the ST window regains the focus, it calls on_activated once. Then, your code switches the active tab, so on_deactivated and on_activated are executed again, and the cycle repeats indefinitely.

Probably you could work around this by checking which tab is already active, and don't set the focus to the currently active tab. But, you may find that your code will just prevent you from ever being able to switch tab, because there is no real way to tell when a Window loses focus vs. when the active tab has changed.

Upvotes: 1

Related Questions