The Bug:
Buttons and sliders remain selected after clicking on them.
This issue did not occur in 1.19.3. It first appeared in 23w03a.
Steps to Reproduce:
Navigate to your video settings.
Click on the "Graphics" button and then move your mouse cursor away from it.
Take note as to whether or not buttons and sliders remain selected after clicking on them.
Observed Behavior:
Buttons and sliders remain selected.
Expected Behavior:
Buttons and sliders would not remain selected, just like how they didn't in 1.19.3.
Code Analysis:
Code analysis by @unknown can be found in the duplicate MC-261578:
ContainerEventHandler.java (Mojang mappings, 1.19.4)
@Override
default public boolean mouseClicked(double mouseX, double mouseY, int button) {
for (GuiEventListener guiEventListener : this.children()) {
if (!guiEventListener.mouseClicked(mouseX, mouseY, button)) continue;
this.setFocused(guiEventListener);
if (button == 0) {
this.setDragging(true);
}
return true;
}
return false;
}Here you can see this.setFocused(guiEventListener) is being invoked on every mouse click. Remember, both Screen and ObjectSelectionList.Entry both implement this interface.
This could be fixed with the following code:
@Override
default public boolean mouseClicked(double mouseX, double mouseY, int button) {
for (GuiEventListener child : this.children()) {
if (child.mouseClicked(mouseX, mouseY, button)) {
if (button == InputConstants.MOUSE_BUTTON_LEFT)
this.setDragging(true);
return true;
}
}
return false;
}However, this would break dragging code as mouseDragged is only passed to the focused element. This could be solved by just iterating through the children like usual, as most mouseDragged implementation have a boolean to check if they are dragging anyway.
Linked issues
causes 1
is duplicated by 27
relates to 5
Attachments
Comments 14
This seems more like an intentional change that was added to improve keyboard navigation, specifically when transitioning from mouse to keyboard input. I’ve used it personally in newer versions, most notably when changing the GUI scale which displaces the button from the mouse. Simply making buttons not take focus anymore would result in a regression with that.
Mojang even had a chance to “fix” this in 1.21.9 when they added GuiEventListener#shouldTakeFocusAfterInteraction (which still exists as of 26.3), but they deliberately made it return true by default.
My proposed solution would be to differentiate hovered and focused states, where the focused state is visible only on keyboard navigation, similar to how its done in most websites.
Seems to be fixed on 26.4-snapshot-7 without having to reset the focus like how most suggest. Some may think the focus being kept on click is a bug, but I disagree. It’s better for keyboard navigation and it’s more intuitive (for users who actually make use of accessibility features) since that is how focus works at least on Chrome and Firefox for Windows (see focused and focus-visible CSS pseudo selectors for reference).
Although it is a bit weird that the highlight disappears on mouse move since I guess that technically changes the last input type.
Also appears to affect the Open Pack Folder button (23w06a).
Previously (1.19.3) the button would be deselected when moving the mouse away.