| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.Blaze3D;
|
| 4 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 5 | import java.net.URI;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.gui.components.Button;
|
| 10 | import net.minecraft.client.gui.components.StringWidget;
|
| 11 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 12 | import net.minecraft.network.chat.CommonComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.MutableComponent;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class ConfirmLinkScreen extends ConfirmScreen {
|
| 19 | private static final Component WARNING_TEXT = Component.translatable("chat.link.warning").withColor(-13108);
|
| 20 | private static final int BUTTON_WIDTH = 100;
|
| 21 | private final URI url;
|
| 22 | private final boolean showWarning;
|
| 23 |
|
| 24 | public ConfirmLinkScreen(final BooleanConsumer callback, final URI url, final boolean trusted) {
|
| 25 | this(callback, confirmMessage(trusted), Component.literal(url.toString()), url, trusted ? CommonComponents.GUI_CANCEL : CommonComponents.GUI_NO, trusted);
|
| 26 | }
|
| 27 |
|
| 28 | public ConfirmLinkScreen(final BooleanConsumer callback, final Component title, final URI url, final boolean trusted) {
|
| 29 | this(callback, title, confirmMessage(trusted, url.toString()), url, trusted ? CommonComponents.GUI_CANCEL : CommonComponents.GUI_NO, trusted);
|
| 30 | }
|
| 31 |
|
| 32 | public ConfirmLinkScreen(
|
| 33 | final BooleanConsumer callback, final Component title, final Component message, final URI url, final Component noButtonComponent, final boolean trusted
|
| 34 | ) {
|
| 35 | super(callback, title, message);
|
| 36 | this.yesButtonComponent = trusted ? CommonComponents.GUI_OPEN_IN_BROWSER : CommonComponents.GUI_YES;
|
| 37 | this.noButtonComponent = noButtonComponent;
|
| 38 | this.showWarning = !trusted;
|
| 39 | this.url = url;
|
| 40 | }
|
| 41 |
|
| 42 | protected static MutableComponent confirmMessage(final boolean trusted, final String url) {
|
| 43 | return confirmMessage(trusted).append(CommonComponents.SPACE).append(Component.literal(url));
|
| 44 | }
|
| 45 |
|
| 46 | protected static MutableComponent confirmMessage(final boolean trusted) {
|
| 47 | return Component.translatable(trusted ? "chat.link.confirmTrusted" : "chat.link.confirm");
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void addAdditionalText() {
|
| 52 | if (this.showWarning) {
|
| 53 | this.layout.addChild(new StringWidget(WARNING_TEXT, this.font));
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void addButtons(final LinearLayout buttonLayout) {
|
| 59 | this.yesButton = buttonLayout.addChild(Button.builder(this.yesButtonComponent, button -> this.callback.accept(true)).width(100).build());
|
| 60 | buttonLayout.addChild(Button.builder(CommonComponents.GUI_COPY_TO_CLIPBOARD, button -> {
|
| 61 | this.copyToClipboard();
|
| 62 | this.callback.accept(false);
|
| 63 | }).width(100).build());
|
| 64 | this.noButton = buttonLayout.addChild(Button.builder(this.noButtonComponent, button -> this.callback.accept(false)).width(100).build());
|
| 65 | }
|
| 66 |
|
| 67 | public void copyToClipboard() {
|
| 68 | this.minecraft.keyboardHandler.setClipboard(this.url.toString());
|
| 69 | }
|
| 70 |
|
| 71 | public static void confirmLinkNow(final @Nullable Screen parentScreen, final URI uri, final boolean trusted) {
|
| 72 | Minecraft minecraft = Minecraft.getInstance();
|
| 73 | minecraft.gui.setScreen(new ConfirmLinkScreen(shouldOpen -> {
|
| 74 | if (shouldOpen) {
|
| 75 | Blaze3D.openUri(uri);
|
| 76 | }
|
| 77 |
|
| 78 | minecraft.gui.setScreen(parentScreen);
|
| 79 | }, uri, trusted));
|
| 80 | }
|
| 81 |
|
| 82 | public static void confirmLinkNow(final @Nullable Screen parentScreen, final URI uri) {
|
| 83 | confirmLinkNow(parentScreen, uri, true);
|
| 84 | }
|
| 85 |
|
| 86 | public static Button.OnPress confirmLink(final Screen parentScreen, final URI uri, final boolean trusted) {
|
| 87 | return button -> confirmLinkNow(parentScreen, uri, trusted);
|
| 88 | }
|
| 89 |
|
| 90 | public static Button.OnPress confirmLink(final Screen parentScreen, final URI uri) {
|
| 91 | return confirmLink(parentScreen, uri, true);
|
| 92 | }
|
| 93 | }
|
| 94 |
|