mojira.dev
MC-259387

Buttons and sliders remain selected after clicking on them

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:

  1. Navigate to your video settings.

  2. Click on the "Graphics" button and then move your mouse cursor away from it.

  3. 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

MC-296135 I clicked the difficulty changer button, and the borderof the button remained white Resolved MC-274485 Game Menu Buttons keep highlighted after I switch into a new menu Resolved MC-274165 After interacting, UI buttons stay highlighted Resolved MC-275709 Pressed button in Options menu freeze after clicking them, and staying highlighted Resolved MC-277676 Minecraft Widget "bug" Resolved

Attachments

Comments 14

Also appears to affect the Open Pack Folder button (23w06a).

Previously (1.19.3) the button would be deselected when moving the mouse away.

Can confirm: 23w13a.

Can confirm: 23w17a.

Can confirm in 1.20 Release Candidate 1.

4 more comments

can confirm in 24w45a

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.

It is indeed fixed in 26.4 snapshot 7.

Avoma

(Unassigned)

Confirmed

Platform

Normal

UI

23w04a, 23w06a, 23w07a, 1.19.4 Pre-release 2, 1.19.4 Release Candidate 1, , 1.21.4, 1.21.5, 1.21.11, 26.2, 26.3 Snapshot 7

Retrieved