Added function input for enabling/disabling localization p/text

This commit is contained in:
lieght
2025-11-29 12:23:17 +01:00
parent 0ab071693f
commit ec0ce4ea37
3 changed files with 44 additions and 20 deletions

View File

@@ -4,7 +4,6 @@ import javafx.scene.image.ImageView;
import org.toop.framework.resource.resources.ImageAsset;
import org.toop.local.AppContext;
import java.awt.*;
import java.io.File;
import java.util.function.Consumer;
@@ -23,30 +22,38 @@ import javafx.scene.text.Text;
import javafx.util.StringConverter;
public final class Primitive {
public static Text header(String key) {
public static Text header(String key, boolean localize) {
var header = new Text();
header.getStyleClass().add("header");
if (!key.isEmpty()) {
header.setText(AppContext.getString(key));
header.textProperty().bind(AppContext.bindToKey(key));
if (localize) header.setText(AppContext.getString(key)); else header.setText(key);
header.textProperty().bind(AppContext.bindToKey(key, localize));
}
return header;
}
public static Text text(String key) {
public static Text header(String key) {
return header(key, true);
}
public static Text text(String key, boolean localize) {
var text = new Text();
text.getStyleClass().add("text");
if (!key.isEmpty()) {
text.setText(AppContext.getString(key));
text.textProperty().bind(AppContext.bindToKey(key));
if (localize) text.setText(AppContext.getString(key)); else text.setText(key);
text.textProperty().bind(AppContext.bindToKey(key, localize));
}
return text;
}
public static Text text(String key) {
return text(key, true);
}
public static ImageView image(File file) {
ImageAsset imageAsset = new ImageAsset(file);
ImageView imageView = new ImageView(imageAsset.getImage());
@@ -57,13 +64,13 @@ public final class Primitive {
return imageView;
}
public static Button button(String key, Runnable onAction) {
public static Button button(String key, Runnable onAction, boolean localize) {
var button = new Button();
button.getStyleClass().add("button");
if (!key.isEmpty()) {
button.setText(AppContext.getString(key));
button.textProperty().bind(AppContext.bindToKey(key));
if (localize) button.setText(AppContext.getString(key)); else button.setText(key);
button.textProperty().bind(AppContext.bindToKey(key, localize));
}
if (onAction != null) {
@@ -74,13 +81,17 @@ public final class Primitive {
return button;
}
public static TextField input(String promptKey, String text, Consumer<String> onValueChanged) {
public static Button button(String key, Runnable onAction) {
return button(key, onAction, true);
}
public static TextField input(String promptKey, String text, Consumer<String> onValueChanged, boolean localize) {
var input = new TextField();
input.getStyleClass().add("input");
if (!promptKey.isEmpty()) {
input.setPromptText(AppContext.getString(promptKey));
input.promptTextProperty().bind(AppContext.bindToKey(promptKey));
if (localize) input.setPromptText(AppContext.getString(promptKey)); else input.setPromptText(promptKey);
input.promptTextProperty().bind(AppContext.bindToKey(promptKey, localize));
}
input.setText(text);
@@ -93,6 +104,10 @@ public final class Primitive {
return input;
}
public static TextField input(String promptKey, String text, Consumer<String> onValueChanged) {
return input(promptKey, text, onValueChanged, true);
}
public static Slider slider(int min, int max, int value, Consumer<Integer> onValueChanged) {
var slider = new Slider();
slider.getStyleClass().add("slider");

View File

@@ -29,7 +29,7 @@ public final class ServerView extends ViewWidget {
}
private void setupLayout() {
var playerHeader = Primitive.header(user);
var playerHeader = Primitive.header(user, false);
var playerListSection = Primitive.vbox(
playerHeader,
@@ -52,7 +52,7 @@ public final class ServerView extends ViewWidget {
listView.getItems().clear();
for (String player : players) {
var playerButton = Primitive.button(player, () -> onPlayerClicked.accept(player));
var playerButton = Primitive.button(player, () -> onPlayerClicked.accept(player), false);
listView.getItems().add(playerButton);
}
});

View File

@@ -58,10 +58,19 @@ public class AppContext {
return "MISSING RESOURCE";
}
public static StringBinding bindToKey(String key) {
return Bindings.createStringBinding(
public static StringBinding bindToKey(String key, boolean localize) {
if (localize) return Bindings.createStringBinding(
() -> localization.getString(key, locale),
localeProperty
);
return Bindings.createStringBinding(
() -> key
);
}
public static StringBinding bindToKey(String key) {
return bindToKey(key, true);
}
}