Scroll percentage of viewports?

14 views
Skip to first unread message

Hugo Hörnquist

unread,
Jun 10, 2021, 6:03:51 PM6/10/21
to Brick Users

Is there any way to check how far along a viewport is scrolled? I both want to display a scrollbar to the side, and change <space> from scroll down to next entry when at the bottom of an "entry".

Jonathan Daugherty

unread,
Jun 10, 2021, 6:58:20 PM6/10/21
to Hugo Hörnquist, Brick Users
> Is there any way to check how far along a viewport is scrolled? I
> both want to display a scrollbar to the side, and change <space> from
> scroll down to next entry when at the bottom of an "entry".

The short answer is: yes, but with a caveat.

The long answer:

You can get the data for a viewport in EventM with lookupViewport:

https://hackage.haskell.org/package/brick-0.62/docs/Brick-Main.html#v:lookupViewport

The issue is that the viewport data you get will be current *as of the
last rendering*. That means it'll be possibly stale by the time you use
it the next time you draw the UI, because the viewport state is
determined at rendering time. If the viewport data said the scroll
position was at 50%, that might be horribly wrong if the next redraw of
the UI happens because of a resize that radically alters the viewport
width and thus the scroll percentage, say.

I don't have a great way to deal with this problem which is why I've
avoided trying to display viewport info in my own Brick programs.
Instead I sometimes use my knowledge of the viewport content to indicate
a scroll position, like "X of Y total items", such as when I have a list
of elements. That way I avoid measuring things in terms of screen space
so I avoid this problem. I hope that is helpful.

--
Jonathan Daugherty

Jonathan Daugherty

unread,
Jun 13, 2021, 11:34:09 AM6/13/21
to Hugo Hörnquist, Hugo Hörnquist, Brick Users
> Even so I seem to only get the size of the "scroll" widget. My draw
> function basically looks like:
> viewport "TextViewport" Vertical $ reportExtent "limit" $ {- actual componenent -}
> and calling the appropriate lookup{Viewport,Extent} gives
> VP {_vpLeft = 0, _vpTop = 71, _vpSize = (143,18)}
> Extent {extentName = RT "limit", extentUpperLeft = Location {loc = (0,20)}, extentSize = (143,18)}
> Which tells me how many lines down I have scrolled, and the size of
> the "scroll" widget. I however still need the size of the actuall
> component.

It turns out I was wrong and the viewport size was not available
anywhere. I've just put out a new release of brick (0.63) that adds a
new field to the Viewport type, _vpContentSize (lens: vpContentSize),
that will give you the size of the contents of the viewport. Give that a
try!

--
Jonathan Daugherty

Jonathan Daugherty

unread,
Jun 13, 2021, 11:36:48 AM6/13/21
to Hugo Hörnquist, Brick Users
> It turns out I was wrong and the viewport size was not available
> anywhere.

I meant *content* size. :)

--
Jonathan Daugherty

Jonathan Daugherty

unread,
Jun 21, 2021, 11:49:28 PM6/21/21
to Hugo Hörnquist, Brick Users
> Thanks for the patch! Works wonders (with the caveat of evaluation
> time).

You're welcome, I'm glad it helped!

--
Jonathan Daugherty
Reply all
Reply to author
Forward
0 new messages