More hibernate rework

This commit is contained in:
crschnick
2025-11-01 03:32:41 +00:00
parent 9d2e5e9c75
commit 78a2a5c53d
2 changed files with 32 additions and 11 deletions
@@ -5,8 +5,6 @@ import io.xpipe.app.core.mode.AppOperationMode;
import io.xpipe.app.issue.ErrorEventFactory;
import io.xpipe.app.platform.PlatformState;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.storage.DataStorageUserHandler;
import io.xpipe.app.util.ThreadHelper;
import io.xpipe.core.OsType;
import java.awt.*;
@@ -21,7 +19,18 @@ public class AppDesktopIntegration {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().addAppEventListener(new SystemSleepListener() {
@Override
public void systemAboutToSleep(SystemSleepEvent e) {}
public void systemAboutToSleep(SystemSleepEvent e) {
if (AppPrefs.get() == null) {
return;
}
var b = AppPrefs.get().hibernateBehaviour().getValue();
if (b == null) {
return;
}
b.runOnSleep();
}
@Override
public void systemAwoke(SystemSleepEvent e) {
@@ -34,7 +43,7 @@ public class AppDesktopIntegration {
return;
}
b.run();
b.runOnWake();
}
});
}
@@ -5,20 +5,20 @@ import io.xpipe.app.core.mode.AppOperationMode;
import io.xpipe.app.ext.PrefsChoiceValue;
import io.xpipe.app.storage.DataStorageUserHandler;
import io.xpipe.app.util.ThreadHelper;
import javafx.beans.value.ObservableValue;
import lombok.Getter;
@Getter
public enum HibernateBehaviour implements PrefsChoiceValue {
LOCK_VAULT("lockVault") {
@Override
public void run() {
public void runOnWake() {}
@Override
public void runOnSleep() {
var handler = DataStorageUserHandler.getInstance();
if (handler != null && handler.getActiveUser() != null) {
// If we run this at the same time as the system is waking, there might be exceptions
// because the platform does not like being shut down while still kinda sleeping
// This assures that it will be run later, on system wake
ThreadHelper.runAsync(() -> {
ThreadHelper.sleep(1000);
AppOperationMode.close();
});
}
@@ -27,9 +27,19 @@ public enum HibernateBehaviour implements PrefsChoiceValue {
RESTART("restart") {
@Override
public void run() {
public ObservableValue<String> toTranslatedString() {
return super.toTranslatedString();
}
@Override
public void runOnWake() {
AppRestart.restart();
}
@Override
public void runOnSleep() {
AppOperationMode.switchToAsync(AppOperationMode.BACKGROUND);
}
};
private final String id;
@@ -38,5 +48,7 @@ public enum HibernateBehaviour implements PrefsChoiceValue {
this.id = id;
}
public abstract void run();
public abstract void runOnSleep();
public abstract void runOnWake();
}