Sublime Forum

Make image views act like other views (just the basics)

#1

Can we get Image preview views to act like other views?

For instance, I can have 3 image preview tabs open, and when I call self.window.views(), I get an empty set. This really breaks some plugins. Image views don’t have to function like other views, they can return none or whatever for non-relevant methods, but can we please have them show up as normal views so plugins can handle them appropriately? And maybe give us an easy way to tell if a view is an image view and not a text view.

Currently, if I want to sort tabs or close tabs, or get the active view, sublime will pretend the image tabs don’t exist.

What is even more frustrating, is that the tab context menu actually sends a group and index to underlying window commands for the image view. So you can call a command from the tab context menu on an image view, and it will send the appropriate group and index, but when the plugin tries to call that index in the group, it will get a different view.

I understand that image views are not text views; all I am asking for is the very basics to be exposed so we can target an image view check and set its settings, read its file name etc. Just the basics. Obviously the content specific methods wouldn’t work or wouldn’t be included at all.

1 Like

Dev Build 3067
API Suggestions
#2

While is not a top priority, I would like to support this request! :slight_smile:

1 Like

#3

This is still a big problem!
Anybody found a fix?
Currently many plugins fail in console with NoneType exceptions when displaying an image preview.
I would like to retrieve the filename of the displayed image but the view object is None…

1 Like

#4

There’s some new undocumented API:

window.active_sheet() window.active_sheet_in_group() window.focus_sheet() window.get_sheet_index() window.set_sheet_index() window.sheets() window.sheets_in_group() window.transient_sheet_in_group()
I think the concept is that now a tab is a sheet object that could contain a view object or something else (actually only an image I suppose).
The problem is that there’s no direct link between sheet and view, you must use active_sheet() and active_view() or something like:

[code]>>> window.get_sheet_index(sheet)
(0, 87)

window.get_view_index(view)
(0, 87)[/code]
Hope it helps.

1 Like

#5

Ugh… Sounds like a bit of work getting plugins compliant. But this does look useful. I guess I will add this to my list of todo stuff. I really was hoping for something more straight forward.

1 Like

#6

+1, Supporting this request

1 Like

#7

I have been working to make some of my plugins workaround this annoying functionality, so I wanted to post what I have found.

Apparently, if you are launching a command from a tab context menu, you should be looking at the index as a sheet and not a view. Why? Because image previews are not views, and you will not be able to see them, and they will mess up your indexing.

Now sheets are a worthless class that provide absolutely no useful info except that they will allow you to get the correct index when image previews are present. They have no view pointers, so you can’t easily translate them to views. What you can do is focus the sheet and then get the active view. This is about the only way you have to translate them back to views. Image previews will always yield a “None”. But there is very little you can apply to them anyways.

So here is a simple example showing how to properly get the view index from a tab context menu command. Refocusing the current active sheet is optional, but I use it because if you are using a “is_visible” or “is_enable” method in your WindowCommand, it will focus the tab you right click on to show the menu which is not really the behavior I want. Hopefully this will help some plugin developers on ST3.

[pre=#232628]def get_group_view(window, group, index):
“”"
Get the view at the given index in the given group.
“”"

    active_sheet = window.active_sheet()
    sheets = window.sheets_in_group(int(group))
    if index < len(sheets):
        sheet = sheetsindex]
    else:
        sheet = None
    if sheet is not None:
        window.focus_sheet(sheet)
        view = window.active_view()

    # Refocus the active tab
    window.focus_sheet(active_sheet)

    return view

class MyTestCommand(sublime_plugin.WindowCommand):
def run(self, group=-1, index=-1):
if group >= 0 or index >= 0:
view = get_group_view(self.window, group, index)[/pre]

1 Like

#8

Jon added Sheet.view() in 3068 but is there a way to get the sheet from the view? something like View.sheet()

1 Like

#9

I requested a way to quickly get from view to sheet, but since one has not been developed, you can use this method:

  • Get the id of the view you are interested in

  • Loop through all sheets

  • On each sheet translate to view and get id

  • Compare ids until the sheet.view().id() matches

This is a little annoying but real issue I had was that you could not translate between the two without changing the focused view/sheet. This caused all sorts of focus problems in my TabsExtra plugin. Since Jon has added Sheet.view(), I can translate from sheet to view without chagning focus and from view to sheet without changing focus. So essentially, my real problem is solved, but I would, for convenience, like a view to sheet method as well.

1 Like