Skip to content

Commit

Permalink
Changed implementation of PageUp and PageDown in TextArea as previous…
Browse files Browse the repository at this point in the history
… implementation has issues in certain edge cases
  • Loading branch information
texus committed Oct 3, 2024
1 parent 8ccc474 commit 3fdb812
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
4 changes: 1 addition & 3 deletions src/Widgets/Scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,11 +1193,9 @@ namespace tgui

ScrollbarChildWidget::ScrollbarChildWidget(Orientation orientation)
{
setOrientation(orientation);
if (orientation == Orientation::Horizontal)
{
setOrientation(orientation);
setSize({getSize().y, getSize().x});
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
45 changes: 16 additions & 29 deletions src/Widgets/TextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,18 +1184,14 @@ namespace tgui
void TextArea::moveCaretPageUp()
{
const auto oldSelEnd = m_selEnd;
// Move to the top line when not there already
if (m_selEnd.y != m_topLine)
m_selEnd.y = m_topLine;

const float horiScrollOffset = m_horizontalScrollbar->isShown() ? m_horizontalScrollbar->getSize().y : 0.f;
const auto fullyVisibleLines = static_cast<std::size_t>((getInnerSize().y - m_paddingCached.getTop() - m_paddingCached.getBottom() - horiScrollOffset) / m_lineHeight);
const std::size_t linesToScroll = (fullyVisibleLines >= 2) ? (fullyVisibleLines - 1) : 1;
if (m_selEnd.y > linesToScroll)
m_selEnd.y = m_selEnd.y - linesToScroll;
else
{
// Scroll up when we already where at the top line
const auto visibleLines = static_cast<std::size_t>((getInnerSize().y - m_paddingCached.getTop() - m_paddingCached.getBottom()) / m_lineHeight);
if (m_topLine < visibleLines - 1)
m_selEnd.y = 0;
else
m_selEnd.y = m_topLine - visibleLines + 1;
}
m_selEnd.y = 0;

m_selEnd.x = 0;
if (oldSelEnd != m_selEnd)
Expand All @@ -1207,20 +1203,14 @@ namespace tgui
void TextArea::moveCaretPageDown()
{
const auto oldSelEnd = m_selEnd;
// Move to the bottom line when not there already
if (m_topLine + m_visibleLines > m_lines.size())
m_selEnd.y = m_lines.size() - 1;
else if (m_selEnd.y != m_topLine + m_visibleLines - 1)
m_selEnd.y = m_topLine + m_visibleLines - 1;

const float horiScrollOffset = m_horizontalScrollbar->isShown() ? m_horizontalScrollbar->getSize().y : 0.f;
const auto fullyVisibleLines = static_cast<std::size_t>((getInnerSize().y - m_paddingCached.getTop() - m_paddingCached.getBottom() - horiScrollOffset) / m_lineHeight);
const std::size_t linesToScroll = (fullyVisibleLines >= 2) ? (fullyVisibleLines - 1) : 1;
if (m_selEnd.y + linesToScroll < m_lines.size())
m_selEnd.y = m_selEnd.y + linesToScroll;
else
{
// Scroll down when we already where at the bottom line
const auto visibleLines = static_cast<std::size_t>((getInnerSize().y - m_paddingCached.getTop() - m_paddingCached.getBottom()) / m_lineHeight);
if (m_selEnd.y + visibleLines >= m_lines.size() + 2)
m_selEnd.y = m_lines.size() - 1;
else
m_selEnd.y = m_selEnd.y + visibleLines - 2;
}
m_selEnd.y = m_lines.size() - 1;

m_selEnd.x = m_lines[m_selEnd.y].length();
if (oldSelEnd != m_selEnd)
Expand Down Expand Up @@ -1828,10 +1818,7 @@ namespace tgui
if (m_lineHeight == 0)
return;

float horiScrollOffset = 0.0f;
if (m_horizontalScrollbar->getViewportSize() < m_horizontalScrollbar->getMaximum())
horiScrollOffset = m_horizontalScrollbar->getSize().y;

const float horiScrollOffset = m_horizontalScrollbar->isShown() ? m_horizontalScrollbar->getSize().y : 0.f;
m_visibleLines = std::min(static_cast<std::size_t>((getInnerSize().y - m_paddingCached.getTop() - m_paddingCached.getBottom() - horiScrollOffset) / m_lineHeight), m_lines.size());

// Store which area is visible
Expand All @@ -1844,7 +1831,7 @@ namespace tgui
|| ((m_verticalScrollbar->getValue() % static_cast<unsigned int>(m_lineHeight)) != 0))
m_visibleLines++;
}
else // There is no scrollbar
else // There is no vertical scrollbar
{
m_topLine = 0;
m_visibleLines = std::min(static_cast<std::size_t>((getInnerSize().y - m_paddingCached.getTop() - m_paddingCached.getBottom() - horiScrollOffset) / m_lineHeight), m_lines.size());
Expand Down

0 comments on commit 3fdb812

Please sign in to comment.