Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI: Fix focus cycle through window #103967

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,9 @@ Control *Control::find_next_valid_focus() const {

Control *from = const_cast<Control *>(this);

// Index of the current `Control` subtree within the containing `Window`
int window_next = -1;

while (true) {
// Find next child.

Expand Down Expand Up @@ -2121,6 +2124,25 @@ Control *Control::find_next_valid_focus() const {
}
next_child = next_child->data.parent_control;
}

Window *win = next_child == nullptr ? nullptr : next_child->data.parent_window;
if (win) { // Cycle through `Control` subtrees of the parent window
if (window_next == -1) {
window_next = next_child->get_index();
ERR_FAIL_INDEX_V(window_next, win->get_child_count(), nullptr);
}

for (int i = 1; i < win->get_child_count() + 1; i++) {
int next = Math::wrapi(window_next + i, 0, win->get_child_count());
Control *c = Object::cast_to<Control>(win->get_child(next));
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
continue;
}
window_next = next;
next_child = c;
break;
}
}
}
}

Expand Down Expand Up @@ -2172,6 +2194,9 @@ Control *Control::find_prev_valid_focus() const {

Control *from = const_cast<Control *>(this);

// Index of the current `Control` subtree within the containing `Window`
int window_prev = -1;

while (true) {
// Find prev child.

Expand All @@ -2180,7 +2205,28 @@ Control *Control::find_prev_valid_focus() const {
if (from->is_set_as_top_level() || !from->data.parent_control) {
// Find last of the children.

prev_child = _prev_control(from); // Wrap start here.
Window *win = from->data.parent_window;
if (win) { // Cycle through `Control` subtrees of the parent window
if (window_prev == -1) {
window_prev = from->get_index();
ERR_FAIL_INDEX_V(window_prev, win->get_child_count(), nullptr);
}

for (int i = 1; i < win->get_child_count() + 1; i++) {
int prev = Math::wrapi(window_prev - i, 0, win->get_child_count());
Control *c = Object::cast_to<Control>(win->get_child(prev));
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
continue;
}
window_prev = prev;
prev_child = _prev_control(c);
break;
}
}

if (!prev_child) {
prev_child = _prev_control(from); // Wrap start here.
}

} else {
for (int i = (from->get_index() - 1); i >= 0; i--) {
Expand Down
2 changes: 1 addition & 1 deletion scene/gui/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ AcceptDialog::AcceptDialog() {
message_label->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
add_child(message_label, false, INTERNAL_MODE_FRONT);

add_child(buttons_hbox, false, INTERNAL_MODE_FRONT);
add_child(buttons_hbox, false, INTERNAL_MODE_BACK);

buttons_hbox->add_spacer();
ok_button = memnew(Button);
Expand Down
56 changes: 53 additions & 3 deletions tests/scene/test_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ TEST_CASE("[SceneTree][Control] Focus") {
}

TEST_CASE("[SceneTree][Control] Find next/prev valid focus") {
Node *intermediate = memnew(Node);
Control *ctrl = memnew(Control);
SceneTree::get_singleton()->get_root()->add_child(ctrl);
intermediate->add_child(ctrl);
SceneTree::get_singleton()->get_root()->add_child(intermediate);

SUBCASE("[SceneTree][Control] In FOCUS_CLICK mode") {
ctrl->set_focus_mode(Control::FocusMode::FOCUS_CLICK);
Expand All @@ -161,9 +163,56 @@ TEST_CASE("[SceneTree][Control] Find next/prev valid focus") {
CHECK_UNARY(ctrl->has_focus());
}

SUBCASE("[SceneTree][Control] Has a sibling control but the parent node is not a control") {
SUBCASE("[SceneTree][Control] Has a sibling control and the parent is a window") {
Control *ctrl1 = memnew(Control);
Control *ctrl2 = memnew(Control);
Control *ctrl3 = memnew(Control);
Window *win = SceneTree::get_singleton()->get_root();

ctrl1->set_focus_mode(Control::FocusMode::FOCUS_ALL);
ctrl2->set_focus_mode(Control::FocusMode::FOCUS_ALL);
ctrl3->set_focus_mode(Control::FocusMode::FOCUS_ALL);

ctrl2->add_child(ctrl3);
win->add_child(ctrl1);
win->add_child(ctrl2);

SUBCASE("[SceneTree][Control] Focus Next") {
ctrl1->grab_focus();
CHECK_UNARY(ctrl1->has_focus());

SEND_GUI_ACTION("ui_focus_next");
CHECK_UNARY(ctrl2->has_focus());

SEND_GUI_ACTION("ui_focus_next");
CHECK_UNARY(ctrl3->has_focus());

SEND_GUI_ACTION("ui_focus_next");
CHECK_UNARY(ctrl1->has_focus());
}

SUBCASE("[SceneTree][Control] Focus Prev") {
ctrl1->grab_focus();
CHECK_UNARY(ctrl1->has_focus());

SEND_GUI_ACTION("ui_focus_prev");
CHECK_UNARY(ctrl3->has_focus());

SEND_GUI_ACTION("ui_focus_prev");
CHECK_UNARY(ctrl2->has_focus());

SEND_GUI_ACTION("ui_focus_prev");
CHECK_UNARY(ctrl1->has_focus());
}

memdelete(ctrl3);
memdelete(ctrl1);
memdelete(ctrl2);
}

SUBCASE("[SceneTree][Control] Has a sibling control but the parent node is not a control or window") {
Control *other_ctrl = memnew(Control);
SceneTree::get_singleton()->get_root()->add_child(other_ctrl);
intermediate->add_child(other_ctrl);

SUBCASE("[SceneTree][Control] Has a sibling control with FOCUS_ALL") {
other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
Expand Down Expand Up @@ -898,6 +947,7 @@ TEST_CASE("[SceneTree][Control] Find next/prev valid focus") {
}

memdelete(ctrl);
memdelete(intermediate);
}

TEST_CASE("[SceneTree][Control] Anchoring") {
Expand Down