I'm working on a Sublime Text 3 plugin, which displays images in a view. Those images are created by an jupyter kernel, therefore the size of the images depend on the code producing them. Since I create really big images, they are to big to fit in the view.
I would like to limit the size to the image to the size of the view, like scale it to 100% of the view size. Is there any way to get the size of the current view and add this to the minihtml implementation of ST3?
I've tested width: 100% which just used a fraction of the actual views size.
I think the API endpoint you want here is view.viewport_extent()
; it returns back a vector
that represents the width and height of the view itself, which is a tuple of (dip, dip)
(i.e. device independent pixels).
The px
size in minihtml is (I believe) device independent as well, so that it's possible to generate HTML without knowing the screen size or DPI scale of the display in your plugin.
Thus by using this API method you can calculate an appropriate width
and height
on the image tag (making sure you constrain the aspect ratio if that's needed) to size it as desired.
Something to note is that the size you get is literally the full viewport size, so in cases where there's a vertical scrollbar using up a bit of the horizontal space of the window, you'll find that making something that wide will actually make a horizontal scrollbar appear because a small part of the image is under the scrollbar. Thus you may want to trim the size back a little bit.
view.line_height()
and view.em_width()
may also come in handy in this sort of situation; they tell you in dip
how tall or wide text is based on the font in the view, in case you want to size images based on the font size,