# Viewport Request

**URL:** <https://forum.juce.com/t/viewport-request/7988>\
**Category:** General JUCE discussion\
**Created:** [January 12, 2012, 6:03pm UTC](https://forum.juce.com/t/viewport-request/7988 "2012-01-12T18:03:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shlomi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shlomi/32/99_2.png) [@Shlomi](https://forum.juce.com/u/Shlomi)\
**Post date:** [January 12, 2012, 6:03pm UTC](https://forum.juce.com/t/viewport-request/7988/1 "2012-01-12T18:03:52Z")

</div>

Hi Jules

Would you consider adding setViewPosition (const Point\< int \> &newPosition, bool notifyScrollbarListeners) or scrollToANewPosition(const Point\< int \> &newPosition)?

Thanks

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 12, 2012, 6:15pm UTC](https://forum.juce.com/t/viewport-request/7988/2 "2012-01-12T18:15:04Z")

</div>

What would be the purpose of those?

---

<div class="post-metadata">

**Author:** ![Shlomi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shlomi/32/99_2.png) [@Shlomi](https://forum.juce.com/u/Shlomi)\
**Post date:** [January 12, 2012, 6:33pm UTC](https://forum.juce.com/t/viewport-request/7988/3 "2012-01-12T18:33:17Z")

</div>

I need the listeners of the viewport’s scrollbars to be triggered also in setViewPosition()

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 12, 2012, 7:39pm UTC](https://forum.juce.com/t/viewport-request/7988/4 "2012-01-12T19:39:18Z")

</div>

Ok… but if the scrollbars move, then their listeners will already be triggered, right?

And if you want your listener to be triggered even when a scrollbar _doesn’t_ move, then it shouldn’t be a scrollbar listener at all, it should be listening to any movement of the view component.

---

<div class="post-metadata">

**Author:** ![Shlomi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shlomi/32/99_2.png) [@Shlomi](https://forum.juce.com/u/Shlomi)\
**Post date:** [January 12, 2012, 8:03pm UTC](https://forum.juce.com/t/viewport-request/7988/5 "2012-01-12T20:03:58Z")

</div>

> [@jules](#):
>
> Ok… but if the scrollbars move, then their listeners will already be triggered, right?

No, that’s the problem, setViewPosition() moves the scrollbars but does not trigger the listeners.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 12, 2012, 8:54pm UTC](https://forum.juce.com/t/viewport-request/7988/6 "2012-01-12T20:54:55Z")

</div>

But it calls Scrollbar::setCurrentRange, which will certainly trigger a callback if the value changes… Are you sure?

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 12, 2012, 9:18pm UTC](https://forum.juce.com/t/viewport-request/7988/7 "2012-01-12T21:18:02Z")

</div>

> [@jules](#):
>
> But it calls Scrollbar::setCurrentRange, which will certainly trigger a callback if the value changes… Are you sure?

I remember a problem in the Juce demo from a long time ago. After resizing a list when the scrollbar was in a certain position, the thumb would not disable even though all the elements were in view. Debugging the complex interaction of notifications when attributes of the system changed was really mind-bending. I never did fully understand it.

---

<div class="post-metadata">

**Author:** ![Shlomi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shlomi/32/99_2.png) [@Shlomi](https://forum.juce.com/u/Shlomi)\
**Post date:** [January 13, 2012, 9:10am UTC](https://forum.juce.com/t/viewport-request/7988/8 "2012-01-13T09:10:38Z")

</div>

I tried it with 2 different viewports in my app.

setCurrentRange() does get called and in turn calls triggerAsyncUpdate().  
But for some reason the ScrollBar::handleAsyncUpdate() that triggers the callback does not get called.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 13, 2012, 9:16am UTC](https://forum.juce.com/t/viewport-request/7988/9 "2012-01-13T09:16:22Z")

</div>

> [@](#):
>
> But for some reason the ScrollBar::handleAsyncUpdate() that triggers the callback does not get called.

AFAICT the code is fine, but if you can point out something that’s failing, then let me know.

---

<div class="post-metadata">

**Author:** ![Shlomi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shlomi/32/99_2.png) [@Shlomi](https://forum.juce.com/u/Shlomi)\
**Post date:** [January 13, 2012, 9:27am UTC](https://forum.juce.com/t/viewport-request/7988/10 "2012-01-13T09:27:05Z")

</div>

Maybe it’s the cancelPendingUpdate() inside updateVisibleArea()

```auto
    if (vBarVisible)
    {
        verticalScrollBar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
        verticalScrollBar.setRangeLimits (0.0, contentBounds.getHeight());
        verticalScrollBar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
        verticalScrollBar.setSingleStepSize (singleStepY);
        verticalScrollBar.cancelPendingUpdate();
    }
```

Edit:  
Maybe when you wrote the last two lines in updateVisibleArea()

```auto
    horizontalScrollBar.handleUpdateNowIfNeeded();
    verticalScrollBar.handleUpdateNowIfNeeded();
```

You meant to delay calling the scrollbar’s listeners but you actually canceled them using cancelPendingUpdate()?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 13, 2012, 9:56am UTC](https://forum.juce.com/t/viewport-request/7988/11 "2012-01-13T09:56:48Z")

</div>

Ah, well, I clearly didn’t want that event to fire. The viewport code can be very fragile with feedback loops, so I wouldn’t want to change that, it’d be almost certain to break something in a very subtle way!

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 13, 2012, 6:15pm UTC](https://forum.juce.com/t/viewport-request/7988/12 "2012-01-13T18:15:02Z")

</div>

> [@jules](#):
>
> The viewport code can be very fragile with feedback loops

Ahh…that is exactly what I meant. Is there any way to simplify it?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 13, 2012, 6:29pm UTC](https://forum.juce.com/t/viewport-request/7988/13 "2012-01-13T18:29:08Z")

</div>

> [@TheVinn](#):
>
> [quote=“jules”]The viewport code can be very fragile with feedback loops

Ahh…that is exactly what I meant. Is there any way to simplify it?[/quote]

It’s probably one of the bits of code that I’ve rewritten the most times, and have never quite managed to rationalise it as much as I’d like to. For something that seems so straightforward there are a huge number of edge cases.
