mirror of
https://github.com/RGBCube/serenity
synced 2025-07-14 17:57:34 +00:00
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:
parent
5e19e72a6a
commit
2e8db6560f
1 changed files with 9 additions and 1 deletions
|
@ -28,6 +28,7 @@
|
||||||
#include <LibCore/Timer.h>
|
#include <LibCore/Timer.h>
|
||||||
#include <LibGUI/AbstractButton.h>
|
#include <LibGUI/AbstractButton.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
|
#include <LibGUI/Window.h>
|
||||||
#include <LibGfx/Palette.h>
|
#include <LibGfx/Palette.h>
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
@ -70,8 +71,13 @@ void AbstractButton::set_checked(bool checked)
|
||||||
m_checked = checked;
|
m_checked = checked;
|
||||||
|
|
||||||
if (is_exclusive() && checked && parent_widget()) {
|
if (is_exclusive() && checked && parent_widget()) {
|
||||||
|
bool sibling_had_focus = false;
|
||||||
parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
|
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;
|
return IterationDecision::Continue;
|
||||||
sibling.m_checked = false;
|
sibling.m_checked = false;
|
||||||
sibling.update();
|
sibling.update();
|
||||||
|
@ -80,6 +86,8 @@ void AbstractButton::set_checked(bool checked)
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
m_checked = true;
|
m_checked = true;
|
||||||
|
if (sibling_had_focus)
|
||||||
|
set_focus(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
update();
|
update();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue