Skip to content

Commit

Permalink
add janky scrolling to newly focused element
Browse files Browse the repository at this point in the history
this should be refined to not actually always scroll even when the element
is already visible, but this works fine for now
  • Loading branch information
BasiqueEvangelist committed Sep 4, 2024
1 parent 11a96c7 commit f605ae7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
}

0 comments on commit f605ae7

Please sign in to comment.