Skip to content

Commit 4ad1055

Browse files
committed
Improve editor toggle button
1 parent eee56b6 commit 4ad1055

3 files changed

Lines changed: 85 additions & 26 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2026 TerminalMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.terminalmc.signtweaks.gui.widget;
18+
19+
import net.minecraft.client.gui.components.Button;
20+
import net.minecraft.network.chat.Component;
21+
22+
public class UnfocusingButton extends Button.Plain {
23+
24+
public UnfocusingButton(
25+
int x,
26+
int y,
27+
int width,
28+
int height,
29+
Component message,
30+
OnPress onPress
31+
) {
32+
super(x, y, width, height, message, onPress, Button.DEFAULT_NARRATION);
33+
}
34+
35+
/**
36+
* Why they didn't make this use a field I have no idea.
37+
*/
38+
@Override
39+
public boolean shouldTakeFocusAfterInteraction() {
40+
return false;
41+
}
42+
}

common/src/main/java/dev/terminalmc/signtweaks/mixin/gui/AbstractSignEditScreenMixin.java

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
2020
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
2121
import dev.terminalmc.signtweaks.SignTweaks;
22+
import dev.terminalmc.signtweaks.gui.widget.UnfocusingButton;
2223
import dev.terminalmc.signtweaks.util.inject.ISignScreen;
24+
import net.minecraft.ChatFormatting;
2325
import net.minecraft.client.gui.components.Button;
24-
import net.minecraft.client.gui.components.CycleButton;
26+
import net.minecraft.client.gui.components.Tooltip;
2527
import net.minecraft.client.gui.components.events.GuiEventListener;
2628
import net.minecraft.client.gui.screens.Screen;
2729
import net.minecraft.client.gui.screens.inventory.AbstractSignEditScreen;
2830
import net.minecraft.network.chat.Component;
2931
import net.minecraft.world.level.block.entity.SignBlockEntity;
30-
import org.jetbrains.annotations.NotNull;
3132
import org.spongepowered.asm.mixin.Final;
3233
import org.spongepowered.asm.mixin.Mixin;
3334
import org.spongepowered.asm.mixin.Shadow;
@@ -55,6 +56,9 @@ protected AbstractSignEditScreenMixin(Component title) {
5556
@Shadow
5657
public abstract void onClose();
5758

59+
@Unique
60+
private boolean signTweaks$isReloading;
61+
5862
/**
5963
* On creation of an {@link AbstractSignEditScreen}, resets the value of
6064
* {@link SignTweaks#enhancedEditing} to the default value and stores the original sign text.
@@ -96,8 +100,6 @@ private GuiEventListener wrapAddDoneButton(
96100
int rowCount = 0;
97101
if (options().showActionButtons)
98102
rowCount++;
99-
if (options().showEditorToggleButton)
100-
rowCount++;
101103

102104
int totalWidth = 200;
103105
int buttonHeight = 20;
@@ -154,31 +156,45 @@ private GuiEventListener wrapAddDoneButton(
154156
.bounds(movingX, movingY, buttonWidth, buttonHeight)
155157
.build();
156158
original.call(instance, revertButton);
157-
158-
movingY += rowHeight;
159159
}
160160

161-
// Enhanced editor toggle button
161+
// Add the 'Done' button last.
162162
if (options().showEditorToggleButton) {
163-
CycleButton<@NotNull Boolean> statusButton = CycleButton
164-
.onOffBuilder(SignTweaks.enhancedEditing)
165-
.create(
166-
baseX,
167-
movingY,
168-
totalWidth,
169-
buttonHeight,
170-
localized("button", "enhancedEditing"),
171-
(button, status) -> {
172-
if (SignTweaks.enhancedEditing != status) {
173-
SignTweaks.enhancedEditing = status;
174-
init();
175-
}
176-
}
177-
);
178-
original.call(instance, statusButton);
179-
}
163+
if (doneButton instanceof Button button) {
164+
// reduce width to add space for editor toggle button
165+
button.setWidth(button.getWidth() - spaceX - buttonHeight);
166+
GuiEventListener result = original.call(instance, button);
180167

181-
// Add the 'Done' button last.
168+
// add editor toggle button
169+
UnfocusingButton toggleButton = new UnfocusingButton(
170+
button.getX() + button.getWidth() + spaceX,
171+
button.getY(),
172+
buttonHeight, // square
173+
buttonHeight,
174+
Component.literal("\u270E").withStyle(SignTweaks.enhancedEditing // ✎
175+
? ChatFormatting.GREEN
176+
: ChatFormatting.RED),
177+
(b) -> {
178+
SignTweaks.enhancedEditing = !SignTweaks.enhancedEditing;
179+
init();
180+
}
181+
);
182+
toggleButton.setTooltip(Tooltip.create(localized(
183+
"button",
184+
"enhancedEditing.tooltip." + (SignTweaks.enhancedEditing
185+
? "enabled"
186+
: "disabled")
187+
)));
188+
original.call(instance, toggleButton);
189+
return result;
190+
} else {
191+
SignTweaks.LOG.error(
192+
"Done button has wrong type! Expected '{}', got '{}'",
193+
Button.class,
194+
doneButton.getClass()
195+
);
196+
}
197+
}
182198
return original.call(instance, doneButton);
183199
}
184200

common/src/main/resources/assets/signtweaks/lang/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
"message.signtweaks.copied": "Text copied from sign!",
99

10-
"button.signtweaks.enhancedEditing": "Enhanced Editing",
10+
"button.signtweaks.enhancedEditing.tooltip.enabled": "Enhanced editing enabled.",
11+
"button.signtweaks.enhancedEditing.tooltip.disabled": "Enhanced editing disabled.",
1112
"button.signtweaks.copy": "Copy",
1213
"button.signtweaks.replace": "Replace",
1314
"button.signtweaks.erase": "Erase",

0 commit comments

Comments
 (0)