Viewport, viewedComponent and position offset


#1

Hi,
I’m using a viewport to display a list of elements which are positioned using flexbox. I use a container component to hold the flexitems. I was trying to add a padding to the whole container component by shrinking it’s width and offsetting the position. The problem is, that the viewport always reset the position to (0, 0). Is this wanted behaviour? I find it irritating.
I can work around this issue by adding a second container inside the other one but it feels wrong.


#2

PIP:

/*******************************************************************************
 The block below describes the properties of this PIP. A PIP is a short snippet
 of code that can be read by the Projucer and used to generate a JUCE project.

 BEGIN_JUCE_PIP_METADATA

  name:             Viewport Position Offset

  dependencies:     juce_core, juce_data_structures, juce_events, juce_graphics, juce_gui_basics
  exporters:        xcode_mac

  moduleFlags:      JUCE_STRICT_REFCOUNTEDPOINTER=1

  type:             Component
  mainClass:        MyComponent

 END_JUCE_PIP_METADATA

*******************************************************************************/

#pragma once

class MyComponent : public Component {

public:

    MyComponent()
    {
        addAndMakeVisible(viewport);
        viewport.setViewedComponent(&container, false);
        setSize(600, 400);
    }

    ~MyComponent()
    {
    }

    void paint (Graphics& g) override {
        g.setColour(Colours::white);
        g.fillRect(viewport.getLocalBounds());
        g.setColour(Colours::red);
        g.fillRect(container.getLocalBounds());
    }

    void resized() override {
        viewport.setBounds(getLocalBounds());
        container.setBounds(getLocalBounds().reduced(30));
    }


private:
    Viewport viewport;
    Component container;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyComponent)
};

#3

Having a second container is the way to go.

If the bounds of the viewed container are changed like that then the Viewport has to realign things or all of the positioning/scrolling functions would break.


Viewport allowHorizontalScrolling