Improve browser chooser history

This commit is contained in:
crschnick 2024-07-25 04:20:00 +00:00
parent 68cf68ea70
commit 7f62c94881
8 changed files with 59 additions and 42 deletions

View file

@ -29,7 +29,16 @@ public class BrowserSavedStateImpl implements BrowserSavedState {
this.lastSystems = FXCollections.observableArrayList(lastSystems);
}
public static BrowserSavedStateImpl load() {
private static BrowserSavedStateImpl INSTANCE;
public static BrowserSavedState get() {
if (INSTANCE == null) {
INSTANCE = load();
}
return INSTANCE;
}
private static BrowserSavedStateImpl load() {
return AppCache.get("browser-state", BrowserSavedStateImpl.class, () -> {
return new BrowserSavedStateImpl(FXCollections.observableArrayList());
});

View file

@ -43,7 +43,7 @@ public class BrowserWelcomeComp extends SimpleComp {
@Override
protected Region createSimple() {
var state = model.getSavedState();
var state = BrowserSavedStateImpl.get();
var welcome = new BrowserGreetingComp().createSimple();

View file

@ -1,6 +1,7 @@
package io.xpipe.app.browser.fs;
import io.xpipe.app.browser.BrowserSavedState;
import io.xpipe.app.browser.BrowserSavedStateImpl;
import io.xpipe.app.browser.BrowserTransferProgress;
import io.xpipe.app.browser.action.BrowserAction;
import io.xpipe.app.browser.file.BrowserFileListModel;
@ -8,7 +9,6 @@ import io.xpipe.app.browser.file.BrowserFileTransferMode;
import io.xpipe.app.browser.file.BrowserFileTransferOperation;
import io.xpipe.app.browser.file.FileSystemHelper;
import io.xpipe.app.browser.session.BrowserAbstractSessionModel;
import io.xpipe.app.browser.session.BrowserSessionModel;
import io.xpipe.app.browser.session.BrowserSessionTab;
import io.xpipe.app.comp.base.ModalOverlayComp;
import io.xpipe.app.fxcomps.Comp;
@ -24,10 +24,8 @@ import io.xpipe.core.process.ShellDialects;
import io.xpipe.core.process.ShellOpenFunction;
import io.xpipe.core.store.*;
import io.xpipe.core.util.FailableConsumer;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import lombok.Getter;
import lombok.SneakyThrows;
@ -110,13 +108,12 @@ public final class OpenFileSystemModel extends BrowserSessionTab<FileSystemStore
return;
}
var current = getCurrentDirectory();
if (DataStorage.get().getStoreEntries().contains(getEntry().get())
&& savedState != null
&& getCurrentPath().get() != null) {
if (getBrowserModel() instanceof BrowserSessionModel bm) {
bm.getSavedState()
.add(new BrowserSavedState.Entry(getEntry().get().getUuid(), getCurrentPath().get()));
}
&& current != null) {
savedState.cd(current.getPath(), false);
BrowserSavedStateImpl.get().add(new BrowserSavedState.Entry(getEntry().get().getUuid(), current.getPath()));
}
try {
fileSystem.close();
@ -299,7 +296,7 @@ public final class OpenFileSystemModel extends BrowserSessionTab<FileSystemStore
// path = FileSystemHelper.normalizeDirectoryPath(this, path);
filter.setValue(null);
savedState.cd(path);
savedState.cd(path, true);
history.updateCurrent(path);
currentPath.set(path);
loadFilesSync(path);
@ -460,7 +457,7 @@ public final class OpenFileSystemModel extends BrowserSessionTab<FileSystemStore
}
public void initWithDefaultDirectory() {
savedState.cd(null);
savedState.cd(null, false);
history.updateCurrent(null);
}

View file

@ -72,32 +72,36 @@ public class OpenFileSystemSavedState {
AppCache.update("fs-state-" + model.getEntry().get().getUuid(), this);
}
public void cd(String dir) {
public void cd(String dir, boolean delay) {
if (dir == null) {
lastDirectory = null;
return;
}
lastDirectory = dir;
// After 10 seconds
TIMEOUT_TIMER.schedule(
new TimerTask() {
@Override
public void run() {
// Synchronize with platform thread
Platform.runLater(() -> {
if (model.isClosed()) {
return;
}
if (Objects.equals(lastDirectory, dir)) {
updateRecent(dir);
save();
}
});
}
},
10000);
if (delay) {
// After 10 seconds
TIMEOUT_TIMER.schedule(new TimerTask() {
@Override
public void run() {
// Synchronize with platform thread
Platform.runLater(() -> {
if (model.isClosed()) {
return;
}
if (Objects.equals(lastDirectory, dir)) {
updateRecent(dir);
save();
}
});
}
}, 10000);
} else {
updateRecent(dir);
save();
}
}
private void updateRecent(String dir) {

View file

@ -2,14 +2,12 @@ package io.xpipe.app.browser.session;
import io.xpipe.app.util.BooleanScope;
import io.xpipe.app.util.ThreadHelper;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import lombok.Getter;
@Getter

View file

@ -67,6 +67,10 @@ public class BrowserChooserComp extends SimpleComp {
window.close();
});
window.show();
window.setOnHidden(event -> {
model.finishWithoutChoice();
event.consume();
});
ThreadHelper.runAsync(() -> {
model.openFileSystemAsync(store.get(), null, null);
});

View file

@ -65,6 +65,18 @@ public class BrowserFileChooserModel extends BrowserAbstractSessionModel<OpenFil
onFinish.accept(stores);
}
public void finishWithoutChoice() {
synchronized (BrowserFileChooserModel.this) {
var open = selectedEntry.getValue();
if (open != null) {
ThreadHelper.runAsync(() -> {
open.close();
});
}
}
}
public void openFileSystemAsync(
DataStoreEntryRef<? extends FileSystemStore> store,
FailableFunction<OpenFileSystemModel, String, Exception> path,

View file

@ -23,16 +23,11 @@ import java.util.ArrayList;
@Getter
public class BrowserSessionModel extends BrowserAbstractSessionModel<BrowserSessionTab<?>> {
public static final BrowserSessionModel DEFAULT = new BrowserSessionModel(BrowserSavedStateImpl.load());
public static final BrowserSessionModel DEFAULT = new BrowserSessionModel();
private final BrowserTransferModel localTransfersStage = new BrowserTransferModel(this);
private final BrowserSavedState savedState;
private final Property<Boolean> draggingFiles = new SimpleBooleanProperty();
public BrowserSessionModel(BrowserSavedState savedState) {
this.savedState = savedState;
}
public void restoreState(BrowserSavedState state) {
ThreadHelper.runAsync(() -> {
var l = new ArrayList<>(state.getEntries());
@ -62,9 +57,7 @@ public class BrowserSessionModel extends BrowserAbstractSessionModel<BrowserSess
closeSync(o);
}
if (savedState != null) {
savedState.save();
}
BrowserSavedStateImpl.get().save();
}
// Delete all files