Skip to content

Commit

Permalink
Fixed widgets believing they have focus while their parent is disabled (
Browse files Browse the repository at this point in the history
closes #226)
  • Loading branch information
texus committed Dec 17, 2023
1 parent 7beac4b commit b89892c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,10 @@ namespace tgui

void Container::setFocused(bool focused)
{
if (m_focusedWidget && (focused != m_focusedWidget->isFocused()))
m_focusedWidget->setFocused(focused);

Widget::setFocused(focused);

if (m_focusedWidget && (m_focused != m_focusedWidget->isFocused()))
m_focusedWidget->setFocused(m_focused);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/CustomWidgetForBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace tgui
void CustomWidgetForBindings::setFocused(bool focused)
{
Widget::setFocused(focused);
implFocusChanged(focused);
implFocusChanged(m_focused);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/SubwidgetContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ namespace tgui

void SubwidgetContainer::setFocused(bool focused)
{
m_container->setFocused(focused);
Widget::setFocused(focused);
m_container->setFocused(m_focused);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 2 additions & 1 deletion src/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,8 @@ namespace tgui
if (m_parent)
m_parent->childWidgetFocused(shared_from_this());

onFocus.emit(this);
if (m_focused) // Might be altered by childWidgetFocused if the parent is disabled
onFocus.emit(this);
}
}
else // Unfocusing widget
Expand Down
11 changes: 8 additions & 3 deletions src/Widgets/EditBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,13 @@ namespace tgui

void EditBox::setFocused(bool focused)
{
const bool wasFocused = m_focused;
ClickableWidget::setFocused(focused);

// Focusing the widget may fail if its parent is disabled
if (focused && !m_focused)
return;

if (m_parentGui)
{
if (focused)
Expand All @@ -416,11 +423,9 @@ namespace tgui
if (m_selChars)
setCaretPosition(m_selEnd);

if (m_focused)
if (wasFocused)
onReturnOrUnfocus.emit(this, m_text);
}

ClickableWidget::setFocused(focused);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 6 additions & 2 deletions src/Widgets/TextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ namespace tgui

void TextArea::setFocused(bool focused)
{
Widget::setFocused(focused);

// Focusing the widget may fail if its parent is disabled
if (focused && !m_focused)
return;

const Vector2f caretPosition = {m_caretPosition.x + m_bordersCached.getLeft() + m_paddingCached.getLeft() - static_cast<float>(m_horizontalScrollbar->getValue()),
m_caretPosition.y + m_bordersCached.getTop() + m_paddingCached.getTop() - static_cast<float>(m_verticalScrollbar->getValue())};

Expand Down Expand Up @@ -438,8 +444,6 @@ namespace tgui
updateSelectionTexts();
}
}

Widget::setFocused(focused);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
23 changes: 23 additions & 0 deletions tests/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,5 +647,28 @@ TEST_CASE("[Widget]")
editBox->setFocused(true);
REQUIRE(!editBox->isFocused());
}

SECTION("Widgets should not be focusable if their parent is disabled (https://github.com/texus/TGUI/issues/226)")
{
tgui::Panel::Ptr panel = tgui::Panel::create();
tgui::EditBox::Ptr editBox = tgui::EditBox::create();
panel->add(editBox);

editBox->setFocused(true);
REQUIRE(editBox->isFocused());

// No widget can be focused while the container is disabled
panel->setEnabled(false);
REQUIRE(!editBox->isFocused());
editBox->setFocused(true);
REQUIRE(!editBox->isFocused());

// If the container is enabled again, it still has no focused widgets
panel->setEnabled(true);
REQUIRE(!panel->isFocused());
REQUIRE(!editBox->isFocused());
editBox->setFocused(true);
REQUIRE(editBox->isFocused());
}
}
}

0 comments on commit b89892c

Please sign in to comment.