fix controller display error

This commit is contained in:
ShirosakiMio 2024-09-09 11:00:56 +08:00
parent 757444c6b4
commit e3c75283f3
2 changed files with 44 additions and 22 deletions

View File

@ -10,6 +10,7 @@ import com.tungsten.fclcore.fakefx.beans.Observable;
import com.tungsten.fclcore.fakefx.beans.property.ReadOnlyListProperty;
import com.tungsten.fclcore.fakefx.beans.property.ReadOnlyListWrapper;
import com.tungsten.fclcore.fakefx.collections.ObservableList;
import com.tungsten.fclcore.task.Schedulers;
import com.tungsten.fclcore.util.Logging;
import com.tungsten.fclcore.util.gson.fakefx.factories.JavaFxPropertyTypeAdapterFactory;
import com.tungsten.fclcore.util.io.FileUtils;
@ -19,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.stream.Collectors;
@ -31,6 +33,8 @@ public class Controllers {
private static final ReadOnlyListWrapper<Controller> controllersWrapper = new ReadOnlyListWrapper<>(controllers);
public static Controller DEFAULT_CONTROLLER;
private static final List<Consumer<Void>> CALLBACKS = new ArrayList<>();
public static void checkControllers() {
if (controllers.contains(null)) {
controllers.remove(null);
@ -98,6 +102,9 @@ public class Controllers {
checkControllers();
initialized = true;
CALLBACKS.forEach(callback -> {
Schedulers.androidUIThread().execute(()->callback.accept(null));
});
}
private static ArrayList<Controller> getControllersFromDisk() {
@ -150,4 +157,12 @@ public class Controllers {
return controllers.stream().filter(it -> it.getName().equals(name)).findFirst().orElse(controllers.get(0));
}
public static void addCallback(Consumer<Void> consumer) {
if (initialized) {
consumer.accept(null);
return;
}
CALLBACKS.add(consumer);
}
}

View File

@ -42,33 +42,14 @@ import com.tungsten.fcllibrary.component.view.FCLUILayout;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.logging.Level;
public class ControllerUI extends FCLCommonUI implements View.OnClickListener {
private final BooleanProperty refreshProperty = new SimpleBooleanProperty(false);
private final ObjectProperty<Controller> selectedController = new SimpleObjectProperty<Controller>() {
{
Controllers.getControllers().addListener(onInvalidating(this::invalidated));
}
@Override
protected void invalidated() {
if (!Controllers.isInitialized()) return;
Controller controller = get();
if (Controllers.getControllers().isEmpty()) {
if (controller != null) {
set(null);
}
} else {
if (!Controllers.getControllers().contains(controller)) {
set(Controllers.getControllers().get(0));
}
}
}
};
private ObjectProperty<Controller> selectedController;
public Controller getSelectedController() {
return selectedController.get();
@ -94,8 +75,34 @@ public class ControllerUI extends FCLCommonUI implements View.OnClickListener {
@Override
public void onCreate() {
super.onCreate();
Controllers.addCallback(unused -> {
init();
});
}
if (Controllers.controllersProperty().size() != 0) {
private void init() {
selectedController = new SimpleObjectProperty<Controller>() {
{
Controllers.getControllers().addListener(onInvalidating(this::invalidated));
}
@Override
protected void invalidated() {
if (!Controllers.isInitialized()) return;
Controller controller = get();
if (Controllers.getControllers().isEmpty()) {
if (controller != null) {
set(null);
}
} else {
if (!Controllers.getControllers().contains(controller)) {
set(Controllers.getControllers().get(0));
}
}
}
};
if (!Controllers.controllersProperty().isEmpty()) {
selectedController.set(Controllers.controllersProperty().get(0));
} else {
selectedController.set(Controllers.DEFAULT_CONTROLLER);