Skip to content

Commit

Permalink
switch theme (default only)
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Jan 13, 2024
1 parent b951e90 commit 539825a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
64 changes: 51 additions & 13 deletions src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.List;

import static org.cubewhy.celestial.Celestial.config;
import static org.cubewhy.celestial.Celestial.f;
Expand Down Expand Up @@ -97,7 +98,7 @@ private void initGui() {

JPanel p3 = new JPanel();
p3.add(new JLabel(f.getString("gui.settings.jvm.wrapper")));
JTextField wrapperInput = getAutoSaveTextField("wrapper", config.getValue("wrapper").getAsJsonPrimitive(), config.getConfig());
JTextField wrapperInput = getAutoSaveTextField("wrapper", config.getConfig());
p3.add(wrapperInput);
JButton btnSetVMArgs = new JButton(f.getString("gui.settings.jvm.args"));
btnSetVMArgs.addActionListener((e) -> {
Expand All @@ -114,19 +115,32 @@ private void initGui() {
claim("game"); // config in GuiVersionSelect
claim("javaagents"); // config in GuiAddonManager

JPanel panelLauncher = new JPanel();
panelLauncher.setLayout(new VerticalFlowLayout(VerticalFlowLayout.LEFT));
panelLauncher.setBorder(new TitledBorder(null, f.getString("gui.settings.launcher"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.orange));
panelLauncher.add(getAutoSaveCheckBox(config.getConfig(), "data-sharing", f.getString("gui.settings.launcher.data-sharing")));

JPanel p4 = new JPanel();
p4.add(new JLabel(f.getString("gui.settings.launcher.theme")));
p4.add(getAutoSaveComboBox(config.getConfig(), "theme", List.of(new String[]{"dark", "light"})));
panelLauncher.add(p4);

claim("data-sharing", panelLauncher);
claim("theme");

JPanel panelUnclaimed = new JPanel();
panelUnclaimed.setBorder(new TitledBorder(null, f.getString("gui.settings.unclaimed"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.orange));
panelUnclaimed.setLayout(new VerticalFlowLayout(VerticalFlowLayout.LEFT));
addUnclaimed(panelUnclaimed, config.getConfig());
panel.add(panelUnclaimed);
}

private void addUnclaimed(JPanel basePanel, JsonObject json) {
private void addUnclaimed(JPanel basePanel, @NotNull JsonObject json) {
for (Map.Entry<String, JsonElement> s : json.entrySet()) {
if (!claimed.contains(s.getKey())) {
// unclaimed
if (s.getValue().isJsonPrimitive()) {
JPanel p = getSimplePanel(json, s.getKey(), s.getValue().getAsJsonPrimitive());
JPanel p = getSimplePanel(json, s.getKey());
basePanel.add(p);
}
if (s.getValue().isJsonObject()) {
Expand All @@ -143,20 +157,30 @@ private void addUnclaimed(JPanel basePanel, JsonObject json) {
}
}

private @NotNull JPanel getSimplePanel(JsonObject json, String key, @NotNull JsonPrimitive value) {
private JComboBox<String> getAutoSaveComboBox(JsonObject json, String key, @NotNull List<String> items) {
JComboBox<String> cb = new JComboBox<>();
for (String item : items) {
cb.addItem(item);
}
cb.setSelectedItem(json.get(key).getAsString());
cb.addActionListener((e) -> {
JComboBox<String> source = (JComboBox<String>) e.getSource();
String v = (String) source.getSelectedItem();
json.addProperty(key, v);
config.save();
});
return cb;
}

private @NotNull JPanel getSimplePanel(@NotNull JsonObject json, String key) {
JPanel panel = new JPanel();
JsonPrimitive value = json.getAsJsonPrimitive(key);
if (value.isBoolean()) {
JCheckBox cb = new JCheckBox(key);
cb.setSelected(value.getAsBoolean());
cb.addActionListener((e) -> {
JCheckBox source = (JCheckBox) e.getSource();
json.addProperty(key, source.isSelected());
config.save();
});
JCheckBox cb = getAutoSaveCheckBox(json, key, key);
panel.add(cb);
} else if (value.isString()) {
panel.add(new JLabel(key));
JTextField input = getAutoSaveTextField(key, value, json);
JTextField input = getAutoSaveTextField(key, json);
panel.add(input);
} else if (value.isNumber()) {
panel.add(new JLabel(key));
Expand All @@ -178,7 +202,21 @@ private void addUnclaimed(JPanel basePanel, JsonObject json) {
}

@NotNull
private static JTextField getAutoSaveTextField(String key, @NotNull JsonPrimitive value, JsonObject json) {
private static JCheckBox getAutoSaveCheckBox(@NotNull JsonObject json, String key, String text) {
JCheckBox cb = new JCheckBox(text);
JsonPrimitive value = json.getAsJsonPrimitive(key);
cb.setSelected(value.getAsBoolean());
cb.addActionListener((e) -> {
JCheckBox source = (JCheckBox) e.getSource();
json.addProperty(key, source.isSelected());
config.save();
});
return cb;
}

@NotNull
private static JTextField getAutoSaveTextField(String key, @NotNull JsonObject json) {
JsonPrimitive value = json.getAsJsonPrimitive(key);
JTextField input = new JTextField(value.getAsString());
input.addActionListener((e) -> {
JTextField source = (JTextField) e.getSource();
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/languages/launcher.properties
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@ gui.settings.args.title=Args Settings
gui.settings.args.add=Add
gui.settings.args.remove=Remove
gui.settings.args.add.message=Input parameters:
gui.settings.args.remove.confirm=Confirm removal of %s
gui.settings.args.remove.confirm=Confirm removal of %s
gui.settings.launcher=Launcher
gui.settings.launcher.data-sharing=Data sharing
gui.settings.launcher.theme=Theme:
5 changes: 4 additions & 1 deletion src/main/resources/languages/launcher_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@ gui.settings.args.title=Args Settings
gui.settings.args.add=Add
gui.settings.args.remove=Remove
gui.settings.args.add.message=Input parameters:
gui.settings.args.remove.confirm=Confirm removal of %s
gui.settings.args.remove.confirm=Confirm removal of %s
gui.settings.launcher=Launcher
gui.settings.launcher.data-sharing=Data sharing
gui.settings.launcher.theme=Theme:
5 changes: 4 additions & 1 deletion src/main/resources/languages/launcher_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,7 @@ gui.settings.args.title=参数设置
gui.settings.args.add=添加参数
gui.settings.args.remove=移除参数
gui.settings.args.add.message=输入参数:
gui.settings.args.remove.confirm=确认移除%s
gui.settings.args.remove.confirm=确认移除%s
gui.settings.launcher=启动器
gui.settings.launcher.data-sharing=数据分享
gui.settings.launcher.theme=主题:

0 comments on commit 539825a

Please sign in to comment.