From 08bf3669ba078d951d447b533417a26fc48623d7 Mon Sep 17 00:00:00 2001 From: crschnick Date: Sun, 10 Dec 2023 16:46:05 +0000 Subject: [PATCH] Fix history context menu --- .../io/xpipe/app/browser/BrowserNavBar.java | 49 ++++++++++++++----- .../app/browser/OpenFileSystemHistory.java | 12 ++++- .../app/browser/OpenFileSystemModel.java | 8 +-- .../io/xpipe/ext/base/browser/BackAction.java | 2 +- .../xpipe/ext/base/browser/ForwardAction.java | 2 +- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/browser/BrowserNavBar.java b/app/src/main/java/io/xpipe/app/browser/BrowserNavBar.java index 5ef31ef7d..00312e2b6 100644 --- a/app/src/main/java/io/xpipe/app/browser/BrowserNavBar.java +++ b/app/src/main/java/io/xpipe/app/browser/BrowserNavBar.java @@ -28,9 +28,7 @@ import javafx.scene.input.MouseButton; import javafx.scene.layout.Region; import org.kordamp.ikonli.javafx.FontIcon; -import java.util.LinkedList; import java.util.List; -import java.util.Objects; public class BrowserNavBar extends SimpleComp { @@ -168,31 +166,56 @@ public class BrowserNavBar extends SimpleComp { var cm = new ContextMenu(); - var f = model.getHistory().getForwardHistory(8).stream().filter(Objects::nonNull).toList(); - new LinkedList<>(f) - .descendingIterator() - .forEachRemaining(s -> cm.getItems().add(new MenuItem(s))); + var f = model.getHistory().getForwardHistory(8).stream().toList(); + for (int i = f.size() - 1; i >= 0; i--) { + if (f.get(i) == null) { + continue; + } + + var mi = new MenuItem(f.get(i)); + int target = i + 1; + mi.setOnAction(event -> { + ThreadHelper.runFailableAsync(() -> { + BooleanScope.executeExclusive(model.getBusy(), () -> { + model.forthSync(target); + }); + }); + event.consume(); + }); + cm.getItems().add(mi); + } if (!f.isEmpty()) { cm.getItems().add(new SeparatorMenuItem()); } if (model.getHistory().getCurrent() != null) { - var current = new MenuItem("> " + model.getHistory().getCurrent()); + var current = new MenuItem(model.getHistory().getCurrent()); current.setDisable(true); cm.getItems().add(current); } - var b = model.getHistory().getBackwardHistory(Integer.MAX_VALUE).stream().filter(Objects::nonNull).toList(); + var b = model.getHistory().getBackwardHistory(Integer.MAX_VALUE).stream().toList(); if (!b.isEmpty()) { cm.getItems().add(new SeparatorMenuItem()); } - b.forEach(s -> { - cm.getItems().add(new MenuItem(s)); - }); + for (int i = 0; i < b.size(); i++) { + if (b.get(i) == null) { + continue; + } - for (int i = 15; i > 0; i--) { - cm.getItems().add(new MenuItem("abc")); + var mi = new MenuItem(b.get(i)); + int target = i + 1; + mi.setOnAction(event -> { + ThreadHelper.runFailableAsync(() -> { + BooleanScope.executeExclusive(model.getBusy(), () -> { + model.backSync(target); + }); + }); + event.consume(); + }); + cm.getItems().add(mi); } + cm.addEventHandler(Menu.ON_SHOWING, e -> { Node content = cm.getSkin().getNode(); if (content instanceof Region r) { diff --git a/app/src/main/java/io/xpipe/app/browser/OpenFileSystemHistory.java b/app/src/main/java/io/xpipe/app/browser/OpenFileSystemHistory.java index 3ed0b3707..0b841fe6a 100644 --- a/app/src/main/java/io/xpipe/app/browser/OpenFileSystemHistory.java +++ b/app/src/main/java/io/xpipe/app/browser/OpenFileSystemHistory.java @@ -53,18 +53,26 @@ public final class OpenFileSystemHistory { } public String back() { + return back(1); + } + + public String back(int i) { if (!canGoBack.get()) { return null; } - cursor.set(cursor.get() - 1); + cursor.set(Math.max(0, cursor.get() - i)); return history.get(cursor.get()); } public String forth() { + return forth(1); + } + + public String forth(int i) { if (!canGoForth.get()) { return null; } - cursor.set(cursor.get() + 1); + cursor.set(Math.min(history.size() - 1, cursor.get() + i)); return history.get(cursor.get()); } diff --git a/app/src/main/java/io/xpipe/app/browser/OpenFileSystemModel.java b/app/src/main/java/io/xpipe/app/browser/OpenFileSystemModel.java index 6a235763f..ee224b1db 100644 --- a/app/src/main/java/io/xpipe/app/browser/OpenFileSystemModel.java +++ b/app/src/main/java/io/xpipe/app/browser/OpenFileSystemModel.java @@ -419,11 +419,11 @@ public final class OpenFileSystemModel { }); } - public void backSync() throws Exception { - cdSyncWithoutCheck(history.back()); + public void backSync(int i) throws Exception { + cdSyncWithoutCheck(history.back(i)); } - public void forthSync() throws Exception { - cdSyncWithoutCheck(history.forth()); + public void forthSync(int i) throws Exception { + cdSyncWithoutCheck(history.forth(i)); } } diff --git a/ext/base/src/main/java/io/xpipe/ext/base/browser/BackAction.java b/ext/base/src/main/java/io/xpipe/ext/base/browser/BackAction.java index cc6a1457a..10c0e17d0 100644 --- a/ext/base/src/main/java/io/xpipe/ext/base/browser/BackAction.java +++ b/ext/base/src/main/java/io/xpipe/ext/base/browser/BackAction.java @@ -19,7 +19,7 @@ public class BackAction implements LeafAction { @Override public void execute(OpenFileSystemModel model, List entries) throws Exception { - model.backSync(); + model.backSync(1); } @Override diff --git a/ext/base/src/main/java/io/xpipe/ext/base/browser/ForwardAction.java b/ext/base/src/main/java/io/xpipe/ext/base/browser/ForwardAction.java index a3946f418..e49c0ec26 100644 --- a/ext/base/src/main/java/io/xpipe/ext/base/browser/ForwardAction.java +++ b/ext/base/src/main/java/io/xpipe/ext/base/browser/ForwardAction.java @@ -19,7 +19,7 @@ public class ForwardAction implements LeafAction { @Override public void execute(OpenFileSystemModel model, List entries) throws Exception { - model.forthSync(); + model.forthSync(1); } @Override