Home Download Buy Blog Forum Support

Setting view index after creation

Setting view index after creation

Postby glycerine on Thu Jan 10, 2013 5:51 pm

Hi,

I've already asked some questions about my "moving tabs to the right" plugin (and have received some wonderful answers, thanks for that.)

I was wondering if there is a way to set the view_index of a newly created view. I'm using on_new(self,view) (and have tried on_activated) to get a reference to the newly created view, but its view index at that point in time is always set to -1. Changing it with window.set_view_index(view, group, rightIndex) does nothing. It appears the view's index is set by SublimeText after these events fire. There may be a good reason for this, but in my case it makes things difficult.

Is there a way to change the index of a newly created view? If there isn't, is there a polite way to ask Jon to add this to the API?

I'll include some samplecode from my plugin so you can understand what I mean.

Code: Select all
class TabrightEvent(sublime_plugin.EventListener):

   def move_tab_right(self,view):
      if not view.settings().get("Tabright_processed"):
         window = sublime.active_window()
           view.settings().set("Tabright_processed", True)
         group,index = window.get_view_index(view)
         rightIndex = len(window.views_in_group(group)) - 1
         window.set_view_index(view, group, rightIndex)
         window.focus_group(group)
         window.focus_view(view)

   def on_new(self,view):
      self.move_tab_right(view)


Thanks in advance!
glycerine
 
Posts: 22
Joined: Thu Dec 15, 2011 11:16 pm

Re: Setting view index after creation

Postby glycerine on Thu Jan 10, 2013 6:25 pm

I discovered I could use this trick to delay setting the index until after the new view was made visible. It seems a bit hacky. Any comments or insights are still welcome.

Code: Select all
   def on_new(self,view):
       def callback(view=view):
         return self.move_tab_right(view)
       sublime.set_timeout(callback, 10)
glycerine
 
Posts: 22
Joined: Thu Dec 15, 2011 11:16 pm

Re: Setting view index after creation

Postby adzenith on Thu Jan 10, 2013 11:36 pm

I think you'll find that set_timeout is indeed the generally accepted way to work around issues like these. A lot of times it works even if the timeout is 0.
adzenith
 
Posts: 1213
Joined: Mon Oct 19, 2009 9:12 pm

Re: Setting view index after creation

Postby glycerine on Fri Jan 11, 2013 5:37 pm

Thanks for the comment! I'm using this now.
Do you reckon this is "expected" or simply hacking around a problem with the API? I'm sure Jon has a lot on his plate as it is, but adding some "post" events would make for much more future-proof plugins, I'd say.
glycerine
 
Posts: 22
Joined: Thu Dec 15, 2011 11:16 pm

Re: Setting view index after creation

Postby FichteFoll on Sat Jan 19, 2013 4:59 pm

This is probably just a "missing feature" out of many. Related: http://sublimetext.userecho.com/topic/1 ... -no-window
FichteFoll
 
Posts: 377
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany


Return to Plugin Development

Who is online

Users browsing this forum: roycyt and 2 guests