diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-01 00:40:12 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-01 00:40:12 +0100 |
commit | 2e8db6560fd8f91d3eafe9c59b38b8d3bcb09eea (patch) | |
tree | 2f0847a9caf7a6b6c3fd0fd495b2cede5d67615e /Libraries | |
parent | 5e19e72a6afa8522e454deae59a50ebc98ceef35 (diff) | |
download | serenity-2e8db6560fd8f91d3eafe9c59b38b8d3bcb09eea.zip |
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/AbstractButton.cpp | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Libraries/LibGUI/AbstractButton.cpp b/Libraries/LibGUI/AbstractButton.cpp index a71ab20ce0..f2846cb78b 100644 --- a/Libraries/LibGUI/AbstractButton.cpp +++ b/Libraries/LibGUI/AbstractButton.cpp @@ -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(); |