LibGUI: Transfer focus when checking exclusive button programmatically

When calling set_checked(true) on an exclusive button, we will now
transfer focus to the newly checked button if one of its now-unchecked
siblings had focus before.

This makes windows that place initial focus somewhere in a group of
radio buttons look nicer when they show up, since focus will be on
whichever radio button was pre-checked, which may not be the first one
in the group.
This commit is contained in:
Andreas Kling 2021-01-01 00:40:12 +01:00
parent 5e19e72a6a
commit 2e8db6560f
Notes: sideshowbarker 2024-07-19 00:17:36 +09:00

View file

@ -28,6 +28,7 @@
#include <LibCore/Timer.h>
#include <LibGUI/AbstractButton.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
#include <LibGfx/Palette.h>
namespace GUI {
@ -70,8 +71,13 @@ void AbstractButton::set_checked(bool checked)
m_checked = checked;
if (is_exclusive() && checked && parent_widget()) {
bool sibling_had_focus = false;
parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
if (!sibling.is_exclusive() || !sibling.is_checked())
if (!sibling.is_exclusive())
return IterationDecision::Continue;
if (window() && window()->focused_widget() == &sibling)
sibling_had_focus = true;
if (!sibling.is_checked())
return IterationDecision::Continue;
sibling.m_checked = false;
sibling.update();
@ -80,6 +86,8 @@ void AbstractButton::set_checked(bool checked)
return IterationDecision::Continue;
});
m_checked = true;
if (sibling_had_focus)
set_focus(true);
}
update();