Skip to content

Commit 422a969

Browse files
Improve focus handling logic in screen components
Enhance input processing by integrating focus handling checks into key event methods. The focus handler is now consistently retrieved and used, ensuring that only the currently focused component processes key events. This change improves the accuracy and reliability of user interaction with UI components.
1 parent cbe2d62 commit 422a969

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

common/src/main/java/me/pandamods/pandalib/client/screen/BasePLScreen.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,25 @@ public boolean mouseScrolled(double mouseX, double mouseY, double scrollX, doubl
6262

6363
@Override
6464
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
65-
if (this.rootComponent.keyPressed(keyCode, scanCode, modifiers)) return true;
65+
FocusHandler focusHandler = this.rootComponent.getFocusHandler();
66+
if (focusHandler != null && focusHandler.isFocusing() && focusHandler.getFocusedComponent().keyPressed(keyCode, scanCode, modifiers))
67+
return true;
6668
return super.keyPressed(keyCode, scanCode, modifiers);
6769
}
6870

6971
@Override
7072
public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
71-
if (this.rootComponent.keyReleased(keyCode, scanCode, modifiers)) return true;
73+
FocusHandler focusHandler = this.rootComponent.getFocusHandler();
74+
if (focusHandler != null && focusHandler.isFocusing() && focusHandler.getFocusedComponent().keyReleased(keyCode, scanCode, modifiers))
75+
return true;
7276
return super.keyReleased(keyCode, scanCode, modifiers);
7377
}
7478

7579
@Override
7680
public boolean charTyped(char codePoint, int modifiers) {
77-
if (this.rootComponent.charTyped(codePoint, modifiers)) return true;
81+
FocusHandler focusHandler = this.rootComponent.getFocusHandler();
82+
if (focusHandler != null && focusHandler.isFocusing() && focusHandler.getFocusedComponent().charTyped(codePoint, modifiers))
83+
return true;
7884
return super.charTyped(codePoint, modifiers);
7985
}
8086
}

common/src/main/java/me/pandamods/pandalib/client/screen/BaseParentUIComponent.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public void updateChildState(UIComponent uiComponent) {
5656

5757
@Override
5858
public @Nullable FocusHandler getFocusHandler() {
59-
if (this.hasParent()) {
60-
return super.getFocusHandler();
61-
}
62-
return this.focusHandler;
59+
FocusHandler focusHandler = super.getFocusHandler();
60+
if (focusHandler == null)
61+
focusHandler = this.focusHandler;
62+
return focusHandler;
6363
}
6464
}

common/src/main/java/me/pandamods/pandalib/client/screen/utils/FocusHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import me.pandamods.pandalib.client.screen.core.ParentUIComponent;
1616
import me.pandamods.pandalib.client.screen.core.UIComponent;
17+
import org.jetbrains.annotations.Contract;
1718
import org.jetbrains.annotations.Nullable;
1819

1920
public class FocusHandler {

0 commit comments

Comments
 (0)