From f605ae7f82f31189be69e8b5da20dbd58bb5489e Mon Sep 17 00:00:00 2001 From: Basique Date: Wed, 4 Sep 2024 17:26:44 +0300 Subject: [PATCH] add janky scrolling to newly focused element this should be refined to not actually always scroll even when the element is already visible, but this works fine for now --- .../limelight/impl/ui/SearchFocusHandler.java | 29 +++++++++++++++++++ .../impl/ui/SearchRootFlowLayout.java | 10 +++++++ 2 files changed, 39 insertions(+) create mode 100644 src/client/java/io/wispforest/limelight/impl/ui/SearchFocusHandler.java diff --git a/src/client/java/io/wispforest/limelight/impl/ui/SearchFocusHandler.java b/src/client/java/io/wispforest/limelight/impl/ui/SearchFocusHandler.java new file mode 100644 index 0000000..4e2a60c --- /dev/null +++ b/src/client/java/io/wispforest/limelight/impl/ui/SearchFocusHandler.java @@ -0,0 +1,29 @@ +package io.wispforest.limelight.impl.ui; + +import io.wispforest.owo.ui.container.ScrollContainer; +import io.wispforest.owo.ui.core.Component; +import io.wispforest.owo.ui.core.ParentComponent; +import io.wispforest.owo.ui.util.FocusHandler; +import org.jetbrains.annotations.Nullable; + +public class SearchFocusHandler extends FocusHandler { + public SearchFocusHandler(ParentComponent root) { + super(root); + } + + @Override + public void focus(@Nullable Component component, Component.FocusSource source) { + super.focus(component, source); + + if (component == null) return; + + ParentComponent current = component.parent(); + while (current != null) { + if (current instanceof ScrollContainer scroll) { + scroll.scrollTo(component); + } + + current = current.parent(); + } + } +} diff --git a/src/client/java/io/wispforest/limelight/impl/ui/SearchRootFlowLayout.java b/src/client/java/io/wispforest/limelight/impl/ui/SearchRootFlowLayout.java index dc13906..d948bc4 100644 --- a/src/client/java/io/wispforest/limelight/impl/ui/SearchRootFlowLayout.java +++ b/src/client/java/io/wispforest/limelight/impl/ui/SearchRootFlowLayout.java @@ -1,6 +1,7 @@ package io.wispforest.limelight.impl.ui; import io.wispforest.owo.ui.container.FlowLayout; +import io.wispforest.owo.ui.core.ParentComponent; import io.wispforest.owo.ui.core.Sizing; import org.lwjgl.glfw.GLFW; @@ -23,4 +24,13 @@ public boolean onKeyPress(int keyCode, int scanCode, int modifiers) { return this.keyPressEvents.sink().onKeyPress(keyCode, scanCode, modifiers); } + + @Override + public void mount(ParentComponent parent, int x, int y) { + super.mount(parent, x, y); + + if (parent == null) { + this.focusHandler = new SearchFocusHandler(this); + } + } }