Remove backgroundImageTypeProperty

This commit is contained in:
yushijinhun
2018-07-17 18:48:53 +08:00
parent bbbe3726f2
commit 846f383f7b
5 changed files with 73 additions and 31 deletions

View File

@@ -29,6 +29,7 @@ import org.hildan.fxgson.creators.ObservableSetCreator;
import org.hildan.fxgson.factories.JavaFxPropertyTypeAdapterFactory; import org.hildan.fxgson.factories.JavaFxPropertyTypeAdapterFactory;
import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer;
import org.jackhuang.hmcl.util.EnumOrdinalDeserializer;
import org.jackhuang.hmcl.util.FileTypeAdapter; import org.jackhuang.hmcl.util.FileTypeAdapter;
import org.jackhuang.hmcl.util.ObservableHelper; import org.jackhuang.hmcl.util.ObservableHelper;
@@ -42,9 +43,11 @@ import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty; import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty; import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
@@ -62,6 +65,7 @@ public final class Config implements Cloneable, Observable {
.registerTypeAdapter(ObservableSet.class, new ObservableSetCreator()) .registerTypeAdapter(ObservableSet.class, new ObservableSetCreator())
.registerTypeAdapter(ObservableMap.class, new ObservableMapCreator()) .registerTypeAdapter(ObservableMap.class, new ObservableMapCreator())
.registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true)) .registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true))
.registerTypeAdapter(EnumBackgroundImage.class, new EnumOrdinalDeserializer<>(EnumBackgroundImage.class)) // backward compatibility for backgroundType
.setPrettyPrinting() .setPrettyPrinting()
.create(); .create();
@@ -77,7 +81,7 @@ public final class Config implements Cloneable, Observable {
public final StringProperty selectedProfile = new SimpleStringProperty(""); public final StringProperty selectedProfile = new SimpleStringProperty("");
@SerializedName("backgroundType") @SerializedName("backgroundType")
public final IntegerProperty backgroundImageType = new SimpleIntegerProperty(0); public final ObjectProperty<EnumBackgroundImage> backgroundImageType = new SimpleObjectProperty<>(EnumBackgroundImage.DEFAULT);
@SerializedName("bgpath") @SerializedName("bgpath")
public final StringProperty backgroundImage = new SimpleStringProperty(); public final StringProperty backgroundImage = new SimpleStringProperty();

View File

@@ -304,31 +304,6 @@ public class Settings {
selectedAccount.get(); selectedAccount.get();
} }
/****************************************
* BACKGROUND *
****************************************/
private final ImmediateObjectProperty<EnumBackgroundImage> backgroundImageType = new ImmediateObjectProperty<EnumBackgroundImage>(this, "backgroundImageType", EnumBackgroundImage.indexOf(ConfigHolder.CONFIG.backgroundImageType.get())) {
@Override
public void invalidated() {
super.invalidated();
ConfigHolder.CONFIG.backgroundImageType.set(get().ordinal());
}
};
public EnumBackgroundImage getBackgroundImageType() {
return backgroundImageType.get();
}
public ImmediateObjectProperty<EnumBackgroundImage> backgroundImageTypeProperty() {
return backgroundImageType;
}
public void setBackgroundImageType(EnumBackgroundImage backgroundImageType) {
this.backgroundImageType.set(backgroundImageType);
}
/**************************************** /****************************************
* THEME * * THEME *
****************************************/ ****************************************/

View File

@@ -220,7 +220,7 @@ public final class Decorator extends StackPane implements TaskExecutorDialogWiza
try { try {
Image background; Image background;
if (Settings.INSTANCE.getBackgroundImageType() == EnumBackgroundImage.DEFAULT) if (ConfigHolder.CONFIG.backgroundImageType.get() == EnumBackgroundImage.DEFAULT)
background = searchBackgroundImage(new Image("/assets/img/background.jpg"), ""); background = searchBackgroundImage(new Image("/assets/img/background.jpg"), "");
else else
background = searchBackgroundImage(new Image("/assets/img/background.jpg"), ConfigHolder.CONFIG.backgroundImage.get()); background = searchBackgroundImage(new Image("/assets/img/background.jpg"), ConfigHolder.CONFIG.backgroundImage.get());

View File

@@ -190,14 +190,14 @@ public final class SettingsPage extends StackPane implements DecoratorPage {
FXUtils.bindString(backgroundItem.getTxtCustom(), ConfigHolder.CONFIG.backgroundImage); FXUtils.bindString(backgroundItem.getTxtCustom(), ConfigHolder.CONFIG.backgroundImage);
backgroundItem.setCustomUserData(EnumBackgroundImage.CUSTOM); backgroundItem.setCustomUserData(EnumBackgroundImage.CUSTOM);
backgroundItem.getGroup().getToggles().stream().filter(it -> it.getUserData() == Settings.INSTANCE.getBackgroundImageType()).findFirst().ifPresent(it -> it.setSelected(true)); backgroundItem.getGroup().getToggles().stream().filter(it -> it.getUserData() == ConfigHolder.CONFIG.backgroundImageType.get()).findFirst().ifPresent(it -> it.setSelected(true));
ConfigHolder.CONFIG.backgroundImage.addListener(onInvalidating(this::initBackgroundItemSubtitle)); ConfigHolder.CONFIG.backgroundImage.addListener(onInvalidating(this::initBackgroundItemSubtitle));
Settings.INSTANCE.backgroundImageTypeProperty().setChangedListener(it -> initBackgroundItemSubtitle()); ConfigHolder.CONFIG.backgroundImageType.addListener(onInvalidating(this::initBackgroundItemSubtitle));
initBackgroundItemSubtitle(); initBackgroundItemSubtitle();
backgroundItem.setToggleSelectedListener(newValue -> backgroundItem.setToggleSelectedListener(newValue ->
Settings.INSTANCE.setBackgroundImageType((EnumBackgroundImage) newValue.getUserData())); ConfigHolder.CONFIG.backgroundImageType.set((EnumBackgroundImage) newValue.getUserData()));
// theme // theme
JFXColorPicker picker = new JFXColorPicker(Color.web(Settings.INSTANCE.getTheme().getColor()), null); JFXColorPicker picker = new JFXColorPicker(Color.web(Settings.INSTANCE.getTheme().getColor()), null);
@@ -214,7 +214,7 @@ public final class SettingsPage extends StackPane implements DecoratorPage {
} }
private void initBackgroundItemSubtitle() { private void initBackgroundItemSubtitle() {
switch (Settings.INSTANCE.getBackgroundImageType()) { switch (ConfigHolder.CONFIG.backgroundImageType.get()) {
case DEFAULT: case DEFAULT:
backgroundItem.setSubtitle(i18n("launcher.background.default")); backgroundItem.setSubtitle(i18n("launcher.background.default"));
break; break;

View File

@@ -0,0 +1,63 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.util;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;
/**
* A deserializer that supports deserializing strings and **numbers** into enums.
*
* @author yushijinhun
*/
public class EnumOrdinalDeserializer<T extends Enum<T>> implements JsonDeserializer<T> {
private Map<String, T> mapping = new HashMap<>();
public EnumOrdinalDeserializer(Class<T> enumClass) {
for (T constant : enumClass.getEnumConstants()) {
mapping.put(String.valueOf(constant.ordinal()), constant);
String name = constant.name();
try {
SerializedName annotation = enumClass.getField(name).getAnnotation(SerializedName.class);
if (annotation != null) {
name = annotation.value();
for (String alternate : annotation.alternate()) {
mapping.put(alternate, constant);
}
}
} catch (NoSuchFieldException e) {
throw new AssertionError(e);
}
mapping.put(name, constant);
}
}
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return mapping.get(json.getAsString());
}
}