优化陶瓦联机的复制邀请码功能 (#4687)

This commit is contained in:
Glavo
2025-10-18 22:18:14 +08:00
committed by GitHub
parent 1ab414e702
commit 94ca81035b
5 changed files with 21 additions and 24 deletions

View File

@@ -1411,12 +1411,16 @@ public final class FXUtils {
} }
public static void copyText(String text) { public static void copyText(String text) {
copyText(text, i18n("message.copied"));
}
public static void copyText(String text, @Nullable String toastMessage) {
ClipboardContent content = new ClipboardContent(); ClipboardContent content = new ClipboardContent();
content.putString(text); content.putString(text);
Clipboard.getSystemClipboard().setContent(content); Clipboard.getSystemClipboard().setContent(content);
if (!Controllers.isStopped()) { if (toastMessage != null && !Controllers.isStopped()) {
Controllers.showToast(i18n("message.copied")); Controllers.showToast(toastMessage);
} }
} }

View File

@@ -34,11 +34,8 @@ import javafx.scene.Cursor;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority; import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
@@ -342,6 +339,7 @@ public class TerracottaControllerPage extends StackPane {
return; return;
} else { } else {
String cs = hostOK.getCode(); String cs = hostOK.getCode();
copyCode(cs);
statusProperty.set(i18n("terracotta.status.host_ok")); statusProperty.set(i18n("terracotta.status.host_ok"));
progressProperty.set(1); progressProperty.set(1);
@@ -350,36 +348,24 @@ public class TerracottaControllerPage extends StackPane {
code.setAlignment(Pos.CENTER); code.setAlignment(Pos.CENTER);
{ {
Label desc = new Label(i18n("terracotta.status.host_ok.code")); Label desc = new Label(i18n("terracotta.status.host_ok.code"));
{ desc.setMouseTransparent(true);
ClipboardContent cp = new ClipboardContent();
cp.putString(cs);
Clipboard.getSystemClipboard().setContent(cp);
}
// FIXME: The implementation to display Room Code is ambiguous. Consider using a clearer JavaFX Element in the future. Label label = new Label(cs);
TextField label = new TextField(cs); label.setMouseTransparent(true);
label.setEditable(false); label.setStyle("-fx-font-size: 24");
label.setFocusTraversable(false);
label.setAlignment(Pos.CENTER); label.setAlignment(Pos.CENTER);
label.setStyle("-fx-background-color: transparent; -fx-border-color: transparent;");
VBox.setMargin(label, new Insets(10, 0, 10, 0)); VBox.setMargin(label, new Insets(10, 0, 10, 0));
label.setScaleX(1.8);
label.setScaleY(1.8);
holder.add(FXUtils.onWeakChange(label.selectedTextProperty(), string -> {
if (string != null && !string.isEmpty() && !cs.equals(string)) {
label.selectAll();
}
}));
code.getChildren().setAll(desc, label); code.getChildren().setAll(desc, label);
} }
FXUtils.onClicked(code, () -> FXUtils.copyText(cs)); code.setCursor(Cursor.HAND);
FXUtils.onClicked(code, () -> copyCode(cs));
LineButton copy = LineButton.of(); LineButton copy = LineButton.of();
copy.setLeftIcon(SVG.CONTENT_COPY); copy.setLeftIcon(SVG.CONTENT_COPY);
copy.setTitle(i18n("terracotta.status.host_ok.code.copy")); copy.setTitle(i18n("terracotta.status.host_ok.code.copy"));
copy.setSubtitle(i18n("terracotta.status.host_ok.code.desc")); copy.setSubtitle(i18n("terracotta.status.host_ok.code.desc"));
FXUtils.onClicked(copy, () -> FXUtils.copyText(cs)); FXUtils.onClicked(copy, () -> copyCode(cs));
LineButton back = LineButton.of(); LineButton back = LineButton.of();
back.setLeftIcon(SVG.ARROW_BACK); back.setLeftIcon(SVG.ARROW_BACK);
@@ -601,6 +587,10 @@ public class TerracottaControllerPage extends StackPane {
return locals; return locals;
} }
private void copyCode(String code) {
FXUtils.copyText(code, i18n("terracotta.status.host_ok.code.copy.toast"));
}
private static final class LineButton extends RipplerContainer { private static final class LineButton extends RipplerContainer {
private final WeakListenerHolder holder = new WeakListenerHolder(); private final WeakListenerHolder holder = new WeakListenerHolder();

View File

@@ -1460,6 +1460,7 @@ terracotta.status.host_starting.back=This will stop creating the room.
terracotta.status.host_ok=Room created terracotta.status.host_ok=Room created
terracotta.status.host_ok.code=Invitation code (Copied) terracotta.status.host_ok.code=Invitation code (Copied)
terracotta.status.host_ok.code.copy=Copy invitation code terracotta.status.host_ok.code.copy=Copy invitation code
terracotta.status.host_ok.code.copy.toast=Invitation code has been copied to clipboard
terracotta.status.host_ok.code.desc=Please remind your friends to select Guest mode in HMCL - Multiplayer or PCL CE and enter this invitation code. terracotta.status.host_ok.code.desc=Please remind your friends to select Guest mode in HMCL - Multiplayer or PCL CE and enter this invitation code.
terracotta.status.host_ok.back=This will also close the room, other guests will leave and cannot rejoin. terracotta.status.host_ok.back=This will also close the room, other guests will leave and cannot rejoin.
terracotta.status.guest_starting=Joining room terracotta.status.guest_starting=Joining room

View File

@@ -1248,6 +1248,7 @@ terracotta.status.host_starting.back=這將會取消建立房間。
terracotta.status.host_ok=已建立房間 terracotta.status.host_ok=已建立房間
terracotta.status.host_ok.code=邀請碼 (已自動複製到剪貼簿) terracotta.status.host_ok.code=邀請碼 (已自動複製到剪貼簿)
terracotta.status.host_ok.code.copy=複製邀請碼 terracotta.status.host_ok.code.copy=複製邀請碼
terracotta.status.host_ok.code.copy.toast=已將邀請碼複製到剪貼簿
terracotta.status.host_ok.code.desc=請提醒您的朋友在 HMCL 或 PCL CE 多人遊戲功能中選擇房客模式,並輸入該邀請碼。 terracotta.status.host_ok.code.desc=請提醒您的朋友在 HMCL 或 PCL CE 多人遊戲功能中選擇房客模式,並輸入該邀請碼。
terracotta.status.host_ok.back=這將同時徹底關閉房間,其他房客將退出並不再能重新加入該房間。 terracotta.status.host_ok.back=這將同時徹底關閉房間,其他房客將退出並不再能重新加入該房間。
terracotta.status.guest_starting=正在加入房間 terracotta.status.guest_starting=正在加入房間

View File

@@ -1258,6 +1258,7 @@ terracotta.status.host_starting.back=这将会取消创建房间。
terracotta.status.host_ok=已启动房间 terracotta.status.host_ok=已启动房间
terracotta.status.host_ok.code=邀请码 (已自动复制到剪贴板) terracotta.status.host_ok.code=邀请码 (已自动复制到剪贴板)
terracotta.status.host_ok.code.copy=复制邀请码 terracotta.status.host_ok.code.copy=复制邀请码
terracotta.status.host_ok.code.copy.toast=已将邀请码复制到剪贴板
terracotta.status.host_ok.code.desc=请提醒您的朋友在 HMCL 或 PCL CE 多人联机功能中选择房客模式,并输入该邀请码。 terracotta.status.host_ok.code.desc=请提醒您的朋友在 HMCL 或 PCL CE 多人联机功能中选择房客模式,并输入该邀请码。
terracotta.status.host_ok.back=这将同时彻底关闭房间,其他房客将退出并不再能重新加入该房间。 terracotta.status.host_ok.back=这将同时彻底关闭房间,其他房客将退出并不再能重新加入该房间。
terracotta.status.guest_starting=正在加入房间 terracotta.status.guest_starting=正在加入房间