Skip to content

Commit

Permalink
ScrollablePanel::getInnerSize() now always subtracts the scrollbar wi…
Browse files Browse the repository at this point in the history
…dth when a scrollbar is shown (instead of only when policy is set to Always), allowing e.g. widgets with 100% width to become smaller when the vertical scrollbar becomes visible
  • Loading branch information
texus committed Jul 14, 2024
1 parent 85128a5 commit 2f6a1f8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ TGUI 1.4 (TBD)
- Added MaxValue getter to Scrollbar
- Added ScrollbarMaxValue getters to widgets with a scrollbar
- Added getPixelsPerPoint() to BackendRenderTarget
- Replace VerticalScroll with Orientation in Slider, Scrollbar and SpinButton
- Inner size of ScrollablePanel now depends on shown scrollbars
- Replaced VerticalScroll with Orientation in Slider, Scrollbar and SpinButton
- Multiple fixes to EditBoxSlider widget


Expand Down
3 changes: 3 additions & 0 deletions include/TGUI/Widgets/ScrollablePanel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ TGUI_MODULE_EXPORT namespace tgui
unsigned int m_verticalScrollAmount = 0;
unsigned int m_horizontalScrollAmount = 0;

bool m_verticalScrollbarWasVisibleOnSizeUpdate = false;
bool m_horizontalScrollbarWasVisibleOnSizeUpdate = false;

std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;

Expand Down
30 changes: 28 additions & 2 deletions src/Widgets/ScrollablePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ namespace tgui
m_horizontalScrollbar {other.m_horizontalScrollbar},
m_verticalScrollbarPolicy {other.m_verticalScrollbarPolicy},
m_horizontalScrollbarPolicy {other.m_horizontalScrollbarPolicy},
m_verticalScrollAmount {other.m_verticalScrollAmount},
m_horizontalScrollAmount {other.m_horizontalScrollAmount},
m_verticalScrollbarWasVisibleOnSizeUpdate{other.m_verticalScrollbarWasVisibleOnSizeUpdate},
m_horizontalScrollbarWasVisibleOnSizeUpdate{other.m_horizontalScrollbarWasVisibleOnSizeUpdate},
m_connectedPositionCallbacks{},
m_connectedSizeCallbacks {}
{
Expand All @@ -83,6 +87,10 @@ namespace tgui
m_horizontalScrollbar {std::move(other.m_horizontalScrollbar)},
m_verticalScrollbarPolicy {std::move(other.m_verticalScrollbarPolicy)},
m_horizontalScrollbarPolicy {std::move(other.m_horizontalScrollbarPolicy)},
m_verticalScrollAmount {std::move(other.m_verticalScrollAmount)},
m_horizontalScrollAmount {std::move(other.m_horizontalScrollAmount)},
m_verticalScrollbarWasVisibleOnSizeUpdate{std::move(other.m_verticalScrollbarWasVisibleOnSizeUpdate)},
m_horizontalScrollbarWasVisibleOnSizeUpdate{std::move(other.m_horizontalScrollbarWasVisibleOnSizeUpdate)},
m_connectedPositionCallbacks{std::move(other.m_connectedPositionCallbacks)},
m_connectedSizeCallbacks {std::move(other.m_connectedSizeCallbacks)}
{
Expand All @@ -108,6 +116,10 @@ namespace tgui
m_horizontalScrollbar = other.m_horizontalScrollbar;
m_verticalScrollbarPolicy = other.m_verticalScrollbarPolicy;
m_horizontalScrollbarPolicy = other.m_horizontalScrollbarPolicy;
m_verticalScrollAmount = other.m_verticalScrollAmount;
m_horizontalScrollAmount = other.m_horizontalScrollAmount;
m_verticalScrollbarWasVisibleOnSizeUpdate = other.m_verticalScrollbarWasVisibleOnSizeUpdate;
m_horizontalScrollbarWasVisibleOnSizeUpdate = other.m_horizontalScrollbarWasVisibleOnSizeUpdate;

disconnectAllChildWidgets();

Expand All @@ -133,6 +145,10 @@ namespace tgui
m_horizontalScrollbar = std::move(other.m_horizontalScrollbar);
m_verticalScrollbarPolicy = std::move(other.m_verticalScrollbarPolicy);
m_horizontalScrollbarPolicy = std::move(other.m_horizontalScrollbarPolicy);
m_verticalScrollAmount = std::move(other.m_verticalScrollAmount);
m_horizontalScrollAmount = std::move(other.m_horizontalScrollAmount);
m_verticalScrollbarWasVisibleOnSizeUpdate = other.m_verticalScrollbarWasVisibleOnSizeUpdate;
m_horizontalScrollbarWasVisibleOnSizeUpdate = other.m_horizontalScrollbarWasVisibleOnSizeUpdate;
Panel::operator=(std::move(other));

disconnectAllChildWidgets();
Expand Down Expand Up @@ -192,6 +208,9 @@ namespace tgui

void ScrollablePanel::setSize(const Layout2d& size)
{
m_horizontalScrollbarWasVisibleOnSizeUpdate = m_horizontalScrollbar->isShown();
m_verticalScrollbarWasVisibleOnSizeUpdate = m_verticalScrollbar->isShown();

Panel::setSize(size);
updateScrollbars();
}
Expand Down Expand Up @@ -302,9 +321,9 @@ namespace tgui
Vector2f ScrollablePanel::getInnerSize() const
{
Vector2f size = Panel::getInnerSize();
if (m_verticalScrollbarPolicy == Scrollbar::Policy::Always)
if (m_verticalScrollbar->isShown())
size.x -= std::min(size.x, getScrollbarWidth());
if (m_horizontalScrollbarPolicy == Scrollbar::Policy::Always)
if (m_horizontalScrollbar->isShown())
size.y -= std::min(size.y, getScrollbarWidth());
return size;
}
Expand Down Expand Up @@ -872,6 +891,13 @@ namespace tgui

if (m_horizontalScrollAmount == 0)
setHorizontalScrollAmount(0);

if ((m_horizontalScrollbarWasVisibleOnSizeUpdate != m_horizontalScrollbar->isShown()) || (m_verticalScrollbarWasVisibleOnSizeUpdate != m_verticalScrollbar->isShown()))
{
m_horizontalScrollbarWasVisibleOnSizeUpdate = m_horizontalScrollbar->isShown();
m_verticalScrollbarWasVisibleOnSizeUpdate = m_verticalScrollbar->isShown();
recalculateBoundSizeLayouts();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
30 changes: 30 additions & 0 deletions tests/Widgets/ScrollablePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ TEST_CASE("[ScrollablePanel]")
REQUIRE(panel->getContentSize() == tgui::Vector2f{200, 100});
}

SECTION("Inner size")
{
panel->setSize(202, 102);
panel->getRenderer()->setBorders({1});

auto widget1 = tgui::Button::create();
widget1->setPosition({0, 0});
widget1->setSize({"100%", "100%"});
panel->add(widget1);

auto widget2 = tgui::Button::create();
widget2->setPosition({20, 30});
widget2->setSize({50, 50});
panel->add(widget2);

REQUIRE(widget1->getSize() == tgui::Vector2f{200, 100});

widget2->setPosition({180, 50 - panel->getScrollbarWidth()});
REQUIRE(widget1->getSize() == tgui::Vector2f{200, 100 - panel->getScrollbarWidth()});

widget2->setPosition({40, 90});
REQUIRE(widget1->getSize() == tgui::Vector2f{200 - panel->getScrollbarWidth(), 100});

widget2->setPosition({250, 120});
REQUIRE(widget1->getSize() == tgui::Vector2f{200 - panel->getScrollbarWidth(), 100 - panel->getScrollbarWidth()});

panel->remove(widget2);
REQUIRE(widget1->getSize() == tgui::Vector2f{200, 100});
}

SECTION("Events / Signals")
{
unsigned int mousePressedCount = 0;
Expand Down

0 comments on commit 2f6a1f8

Please sign in to comment.