Skip to content

Commit

Permalink
Change: Focus on the most relevant text fields when opening various GUIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zailer43 committed Sep 12, 2024
1 parent b2f74e2 commit 03ceaa2
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 16 deletions.
18 changes: 15 additions & 3 deletions src/main/java/fzmm/zailer/me/client/gui/BaseFzmmScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.parsing.UIModel;
import io.wispforest.owo.ui.parsing.UIParsing;
import io.wispforest.owo.ui.util.FocusHandler;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
Expand Down Expand Up @@ -70,16 +71,27 @@ protected void build(StyledFlowLayout rootComponent) {

this.symbolChatCompat.addSymbolChatComponents(this);

this.setupButtonsCallbacks(rootComponent);
this.setup(rootComponent);
rootComponent.child(this.snackBarLayout);
}


@Override
protected void init() {
super.init();
if (FzmmClient.CONFIG.history.automaticallyRecoverScreens() && this instanceof IMementoScreen mementoScreen) {
mementoScreen.getMemento().ifPresent(mementoScreen::restoreMemento);
}

if (this.getRoot().focusHandler() != null) {
this.initFocus(this.getRoot().focusHandler());
}
}

protected void initFocus(FocusHandler focusHandler) {

}

protected abstract void setupButtonsCallbacks(FlowLayout rootComponent);
protected abstract void setup(FlowLayout rootComponent);

@Override
public void removed() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fzmm/zailer/me/client/gui/HistoryScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public HistoryScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
this.contentLayout = rootComponent.childById(FlowLayout.class, CONTENT_ID);
checkNull(this.contentLayout, "flow-layout", CONTENT_ID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public BannerEditorScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
//preview
this.bannerPreview = rootComponent.childById(ItemComponent.class, BANNER_PREVIEW_ID);
checkNull(this.bannerPreview, "flow-layout", BANNER_PREVIEW_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ConvertersScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
this.setTabs(selectedTab);
ScreenTabRow.setup(rootComponent, "tabs", selectedTab);
for (var converterTab : ConvertersTabs.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.core.Surface;
import io.wispforest.owo.ui.util.FocusHandler;
import net.minecraft.client.gui.screen.ConfirmLinkScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
Expand Down Expand Up @@ -57,7 +58,7 @@ public EncryptBookScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
assert this.client != null;
assert this.client.player != null;

Expand Down Expand Up @@ -103,6 +104,11 @@ protected void setupButtonsCallbacks(FlowLayout rootComponent) {
ButtonRow.setup(rootComponent, ButtonRow.getButtonId("faq"), true, this::faqExecute);
}

@Override
protected void initFocus(FocusHandler focusHandler) {
focusHandler.focus(this.messageTextArea, Component.FocusSource.MOUSE_CLICK);
}

private void updateDecryptorProfileList() {
List<TranslationEncryptProfile> decryptorProfiles = getProfiles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.util.FocusHandler;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.client.gui.screen.ConfirmLinkScreen;
import net.minecraft.client.gui.screen.Screen;
Expand Down Expand Up @@ -84,7 +85,7 @@ public HeadGalleryScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
this.page = 1;
this.selectedTags = new HashSet<>();
this.availableTags = new HashSet<>();
Expand Down Expand Up @@ -160,6 +161,11 @@ protected void setupButtonsCallbacks(FlowLayout rootComponent) {
this.setPage(1);
}

@Override
protected void initFocus(FocusHandler focusHandler) {
focusHandler.focus(this.contentSearchField, Component.FocusSource.MOUSE_CLICK);
}

private void categoryButtonExecute(ButtonComponent selectedButton, String category, @Nullable Runnable callback) {
assert this.client != null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.util.FocusHandler;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.ConfirmLinkScreen;
import net.minecraft.client.gui.screen.Screen;
Expand Down Expand Up @@ -103,7 +104,7 @@ public HeadGeneratorScreen(@Nullable Screen parent) {

@Override
@SuppressWarnings("ConstantConditions")
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
this.headComponentEntries = new ArrayList<>();
this.headCompoundComponentEntries = new ArrayList<>();
this.baseSkin = new BufferedImage(SkinPart.MAX_WIDTH, SkinPart.MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Expand Down Expand Up @@ -176,6 +177,11 @@ protected void setupButtonsCallbacks(FlowLayout rootComponent) {
this.updatePreviews();
}

@Override
protected void initFocus(FocusHandler focusHandler) {
focusHandler.focus(this.skinElements.valueField(), Component.FocusSource.MOUSE_CLICK);
}

private void updateCategory(IHeadCategory category) {
this.selectedCategory = category;
this.applyFilters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.wispforest.owo.ui.component.*;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.util.FocusHandler;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -72,7 +73,7 @@ public ImagetextScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
FzmmConfig.Imagetext config = FzmmClient.CONFIG.imagetext;

// image options
Expand Down Expand Up @@ -214,6 +215,11 @@ protected void setupButtonsCallbacks(FlowLayout rootComponent) {
this.setSmallGuiAnimation(this.width);
}

@Override
protected void initFocus(FocusHandler focusHandler) {
focusHandler.focus(this.imageElements.valueField(), Component.FocusSource.MOUSE_CLICK);
}

@SuppressWarnings("unchecked")
private void setTabs(FlowLayout rootComponent, ITabsEnum selectedTab, ITabsEnum[] enumValues, HashMap<String, IScreenTab> tabsHashMap) {
Enum<? extends ITabsEnum> selectedTabEnum = (Enum<? extends ITabsEnum>) selectedTab;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public MainScreen(@Nullable Screen parent) {

@Override
@SuppressWarnings("ConstantConditions")
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
rootComponent.childById(ButtonComponent.class, "config-button")
.onPress(button -> this.client.setScreen(ConfigScreen.create(FzmmClient.CONFIG, this)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public PlayerStatueScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
PlayerEntity player = MinecraftClient.getInstance().player;
assert player != null;
//general
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.TextBoxComponent;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.util.FocusHandler;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -65,7 +67,7 @@ public TextFormatScreen(@Nullable Screen parent) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
this.messagePreviewLabel = rootComponent.childById(LabelComponent.class, MESSAGE_PREVIEW_ID);
BaseFzmmScreen.checkNull(this.messagePreviewLabel, "label", MESSAGE_PREVIEW_ID);

Expand Down Expand Up @@ -102,6 +104,11 @@ protected void setupButtonsCallbacks(FlowLayout rootComponent) {
this.tabCallback(this.getTab(selectedTab, ITextFormatTab.class));
}

@Override
protected void initFocus(FocusHandler focusHandler) {
focusHandler.focus(this.messageTextField, Component.FocusSource.MOUSE_CLICK);
}

private void tabCallback(ITextFormatTab tab) {
this.updateMessagePreview();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CopyTextScreen(@Nullable Screen parent, Text textToCopy) {
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
FlowLayout flowLayout = rootComponent.childById(FlowLayout.class, COPY_BUTTONS_LIST_ID);
if (flowLayout == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AbstractAutoPlacer(String screenPath, String baseScreenTranslationKey, @N
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
assert this.client != null;
assert this.client.player != null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.util.FocusHandler;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.entity.player.PlayerInventory;
Expand Down Expand Up @@ -64,7 +65,7 @@ public SelectItemScreen(@Nullable Screen parent, List<RequestedItem> requestedIt
}

@Override
protected void setupButtonsCallbacks(FlowLayout rootComponent) {
protected void setup(FlowLayout rootComponent) {
assert this.client != null;
assert this.client.player != null;

Expand All @@ -87,7 +88,11 @@ protected void setupButtonsCallbacks(FlowLayout rootComponent) {
this.execute(false);
this.close();
});
}

@Override
protected void initFocus(FocusHandler focusHandler) {
focusHandler.focus(this.searchField, Component.FocusSource.MOUSE_CLICK);
}

private void setupSourceButtons(FlowLayout rootComponent) {
Expand Down

0 comments on commit 03ceaa2

Please sign in to comment.