More file loading fixes [stage]

This commit is contained in:
crschnick 2023-04-29 18:51:39 +00:00
parent f2d11c548a
commit ae42b07759
4 changed files with 58 additions and 44 deletions

View file

@ -12,7 +12,7 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
final class FileBrowserNavigationHistory {
final class FileBrowserHistory {
private final IntegerProperty cursor = new SimpleIntegerProperty(0);
private final List<String> history = new ArrayList<>();
@ -25,7 +25,7 @@ final class FileBrowserNavigationHistory {
return history.size() > 0 ? history.get(cursor.get()) : null;
}
public void cd(String s) {
public void updateCurrent(String s) {
if (s == null) {
return;
}

View file

@ -19,6 +19,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.function.Predicate;
import java.util.stream.Stream;
@Getter
final class FileListModel {
@ -31,8 +32,8 @@ final class FileListModel {
private final OpenFileSystemModel fileSystemModel;
private final Property<Comparator<FileSystem.FileEntry>> comparatorProperty =
new SimpleObjectProperty<>(FILE_TYPE_COMPARATOR);
private final Property<List<FileSystem.FileEntry>> all = new SimpleObjectProperty<>(List.of());
private final Property<List<FileSystem.FileEntry>> shown = new SimpleObjectProperty<>(List.of());
private final Property<List<FileSystem.FileEntry>> all = new SimpleObjectProperty<>(new ArrayList<>());
private final Property<List<FileSystem.FileEntry>> shown = new SimpleObjectProperty<>(new ArrayList<>());
private final ObjectProperty<Predicate<FileSystem.FileEntry>> predicateProperty =
new SimpleObjectProperty<>(path -> true);
private final ObservableList<FileSystem.FileEntry> selected = FXCollections.observableArrayList();
@ -58,6 +59,14 @@ final class FileListModel {
refreshShown();
}
public void setAll(Stream<FileSystem.FileEntry> newFiles) {
try (var s = newFiles) {
var l = s.limit(5000).toList();
all.setValue(l);
refreshShown();
}
}
public void setComparator(Comparator<FileSystem.FileEntry> comparator) {
comparatorProperty.setValue(comparator);
refreshShown();

View file

@ -18,11 +18,9 @@ import lombok.SneakyThrows;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
@Getter
final class OpenFileSystemModel {
@ -32,7 +30,7 @@ final class OpenFileSystemModel {
private final Property<String> filter = new SimpleStringProperty();
private final FileListModel fileList;
private final ReadOnlyObjectWrapper<String> currentPath = new ReadOnlyObjectWrapper<>();
private final FileBrowserNavigationHistory history = new FileBrowserNavigationHistory();
private final FileBrowserHistory history = new FileBrowserHistory();
private final BooleanProperty busy = new SimpleBooleanProperty();
private final FileBrowserModel browserModel;
private final BooleanProperty noDirectory = new SimpleBooleanProperty();
@ -76,28 +74,28 @@ final class OpenFileSystemModel {
}
public Optional<String> cd(String path) {
if (Objects.equals(path, currentPath.get())) {
return Optional.empty();
}
String newPath = null;
try {
newPath = FileSystemHelper.resolveDirectoryPath(this, path);
} catch (Exception ex) {
ErrorEvent.fromThrowable(ex).handle();
return Optional.of(currentPath.get());
}
if (!Objects.equals(path, newPath)) {
return Optional.of(newPath);
}
ThreadHelper.runFailableAsync(() -> {
try (var ignored = new BusyProperty(busy)) {
cdSync(path);
if (Objects.equals(path, currentPath.get())) {
return Optional.empty();
}
});
return Optional.empty();
String newPath = null;
try {
newPath = FileSystemHelper.resolveDirectoryPath(this, path);
} catch (Exception ex) {
ErrorEvent.fromThrowable(ex).handle();
return Optional.of(currentPath.get());
}
if (!Objects.equals(path, newPath)) {
return Optional.of(newPath);
}
ThreadHelper.runFailableAsync(() -> {
try (var ignored = new BusyProperty(busy)) {
cdSync(path);
}
});
return Optional.empty();
}
private void cdSync(String path) throws Exception {
@ -107,28 +105,27 @@ final class OpenFileSystemModel {
this.fileSystem = fs;
}
// Assumed that the path is normalized to improve performance!
// Assume that the path is normalized to improve performance!
// path = FileSystemHelper.normalizeDirectoryPath(this, path);
navigateToSync(path);
filter.setValue(null);
currentPath.set(path);
history.cd(path);
history.updateCurrent(path);
loadFilesSync(path);
}
private boolean navigateToSync(String dir) {
private boolean loadFilesSync(String dir) {
try {
List<FileSystem.FileEntry> newList;
if (dir != null) {
newList = getFileSystem().listFiles(dir).collect(Collectors.toCollection(ArrayList::new));
var stream = getFileSystem().listFiles(dir);
noDirectory.set(false);
fileList.setAll(stream);
} else {
newList = getFileSystem().listRoots().stream()
.map(s -> new FileSystem.FileEntry(getFileSystem(), s, Instant.now(), true, false, false, 0, null))
.collect(Collectors.toCollection(ArrayList::new));
var stream = getFileSystem().listRoots().stream()
.map(s -> new FileSystem.FileEntry(getFileSystem(), s, Instant.now(), true, false, false, 0, null));
noDirectory.set(true);
fileList.setAll(stream);
}
fileList.setAll(newList);
return true;
} catch (Exception e) {
fileList.setAll(List.of());
@ -287,15 +284,19 @@ final class OpenFileSystemModel {
});
}
public FileBrowserNavigationHistory getHistory() {
public FileBrowserHistory getHistory() {
return history;
}
public void back() {
history.back().ifPresent(s -> cd(s));
try (var ignored = new BusyProperty(busy)) {
history.back().ifPresent(s -> cd(s));
}
}
public void forth() {
history.forth().ifPresent(s -> cd(s));
try (var ignored = new BusyProperty(busy)) {
history.forth().ifPresent(s -> cd(s));
}
}
}

View file

@ -1,6 +1,6 @@
.download-background {
-fx-border-color: -color-neutral-muted;
-fx-border-width: 2px 0 0 0;
-fx-border-width: 3px 0 0 0;
-fx-padding: 1em;
}
@ -25,7 +25,11 @@
-fx-border-width: 0;
-fx-border-radius: 0;
-fx-background-radius: 0;
-fx-background-insets: 1px 2px 1px 0;
-fx-background-insets: 0;
}
.bookmark-list .button:hover {
-fx-background-color: -color-accent-muted;
}
*:drag-over .download-background {
@ -82,7 +86,7 @@
-fx-opacity: 0.8;
}
.browser .table-row-cell:hover {
.browser .table-row-cell:file:hover,.table-row-cell:folder:hover {
-fx-background-color: -color-accent-subtle;
}