| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.components.events.ContainerEventHandler;
|
| 6 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 7 | import org.jspecify.annotations.Nullable;
|
| 8 |
|
| 9 | @Environment(EnvType.CLIENT)
|
| 10 | public interface ComponentPath {
|
| 11 | static ComponentPath leaf(final GuiEventListener component) {
|
| 12 | return new ComponentPath.Leaf(component);
|
| 13 | }
|
| 14 |
|
| 15 | static @Nullable ComponentPath path(final ContainerEventHandler container, final @Nullable ComponentPath childPath) {
|
| 16 | return childPath == null ? null : new ComponentPath.Path(container, childPath);
|
| 17 | }
|
| 18 |
|
| 19 | static ComponentPath path(final GuiEventListener target, final ContainerEventHandler... containerPath) {
|
| 20 | ComponentPath path = leaf(target);
|
| 21 |
|
| 22 | for (ContainerEventHandler container : containerPath) {
|
| 23 | path = path(container, path);
|
| 24 | }
|
| 25 |
|
| 26 | return path;
|
| 27 | }
|
| 28 |
|
| 29 | GuiEventListener component();
|
| 30 |
|
| 31 | void applyFocus(boolean focused);
|
| 32 |
|
| 33 | GuiEventListener leafComponent();
|
| 34 |
|
| 35 | @Environment(EnvType.CLIENT)
|
| 36 | record Leaf(GuiEventListener component) implements ComponentPath {
|
| 37 | @Override
|
| 38 | public void applyFocus(final boolean focused) {
|
| 39 | this.component.setFocused(focused);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public GuiEventListener leafComponent() {
|
| 44 | return this.component;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Environment(EnvType.CLIENT)
|
| 49 | record Path(ContainerEventHandler component, ComponentPath childPath) implements ComponentPath {
|
| 50 | @Override
|
| 51 | public void applyFocus(final boolean focused) {
|
| 52 | if (!focused) {
|
| 53 | this.component.setFocused(null);
|
| 54 | } else {
|
| 55 | this.component.setFocused(this.childPath.component());
|
| 56 | }
|
| 57 |
|
| 58 | this.childPath.applyFocus(focused);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public GuiEventListener leafComponent() {
|
| 63 | return this.childPath.leafComponent();
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|