From 539825a928c69cbf1e569f02279998d5c25cdcaf Mon Sep 17 00:00:00 2001 From: cubewhy Date: Sat, 13 Jan 2024 22:38:28 +0800 Subject: [PATCH] switch theme (default only) --- .../celestial/gui/pages/GuiSettings.java | 64 +++++++++++++++---- .../resources/languages/launcher.properties | 5 +- .../languages/launcher_en.properties | 5 +- .../languages/launcher_zh.properties | 5 +- 4 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java b/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java index 6585572e..6657731a 100644 --- a/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java +++ b/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java @@ -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; @@ -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) -> { @@ -114,6 +115,19 @@ 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)); @@ -121,12 +135,12 @@ private void initGui() { panel.add(panelUnclaimed); } - private void addUnclaimed(JPanel basePanel, JsonObject json) { + private void addUnclaimed(JPanel basePanel, @NotNull JsonObject json) { for (Map.Entry 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()) { @@ -143,20 +157,30 @@ private void addUnclaimed(JPanel basePanel, JsonObject json) { } } - private @NotNull JPanel getSimplePanel(JsonObject json, String key, @NotNull JsonPrimitive value) { + private JComboBox getAutoSaveComboBox(JsonObject json, String key, @NotNull List items) { + JComboBox cb = new JComboBox<>(); + for (String item : items) { + cb.addItem(item); + } + cb.setSelectedItem(json.get(key).getAsString()); + cb.addActionListener((e) -> { + JComboBox source = (JComboBox) 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)); @@ -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(); diff --git a/src/main/resources/languages/launcher.properties b/src/main/resources/languages/launcher.properties index 20eac325..fafe5d3d 100644 --- a/src/main/resources/languages/launcher.properties +++ b/src/main/resources/languages/launcher.properties @@ -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 \ No newline at end of file +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: \ No newline at end of file diff --git a/src/main/resources/languages/launcher_en.properties b/src/main/resources/languages/launcher_en.properties index 20eac325..fafe5d3d 100644 --- a/src/main/resources/languages/launcher_en.properties +++ b/src/main/resources/languages/launcher_en.properties @@ -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 \ No newline at end of file +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: \ No newline at end of file diff --git a/src/main/resources/languages/launcher_zh.properties b/src/main/resources/languages/launcher_zh.properties index e21a74a2..555147d3 100644 --- a/src/main/resources/languages/launcher_zh.properties +++ b/src/main/resources/languages/launcher_zh.properties @@ -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 \ No newline at end of file +gui.settings.args.remove.confirm=确认移除%s +gui.settings.launcher=启动器 +gui.settings.launcher.data-sharing=数据分享 +gui.settings.launcher.theme=主题: \ No newline at end of file