From eea7497dfe3e99bef5c832aa1271660f7b322586 Mon Sep 17 00:00:00 2001 From: kevinpardo Date: Wed, 11 Nov 2026 01:58:40 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Move=20the=20preview=20to=20the=20item=20ab?= =?UTF-8?q?ove=20or=20below=20with=20=E2=86=91=20and=20=E2=86=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the preview, ↑ and ↓ only scrolled, so on a picture, a video or a folder they did nothing, and in the list and column views, where files sit one above the other, there was no way to step to the next one with the keys that move through them in the folder. Now ↑ and ↓ still scroll what's taller than the window (a long text, a zoomed picture, a PDF), and anything that fits moves to the item above or below as the folder view lays them out: the row above or below in the list and columns, the tile above or below in the grid. The preview asks Folio with a "step -1" or "step 1" line, since only the window knows its layout; Folio moves its selection and sends the new item back as it already does. A preview running on its own falls back to the previous and next file. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Ufqf7cLLeC113cXjB33N3R --- src/FolderColumns.qml | 2 ++ src/FolderGrid.qml | 2 ++ src/FolderList.qml | 2 ++ src/FolderPane.qml | 1 + src/PreviewWindow.qml | 18 +++++++++++++++--- src/backend.cpp | 4 ++++ src/backend.h | 3 +++ src/previewer.cpp | 9 +++++++++ src/previewer.h | 7 ++++++- tests/folio_tests.cpp | 22 ++++++++++++++++++++++ 10 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/FolderColumns.qml b/src/FolderColumns.qml index 7e802fff..6a833840 100644 --- a/src/FolderColumns.qml +++ b/src/FolderColumns.qml @@ -18,6 +18,8 @@ Item { Selecting { id: selecting; view: here } function focusView() { here.forceActiveFocus() } + // The preview's ↑ and ↓: the row above or below in the current column. + function stepRow(direction) { direction < 0 ? here.decrementCurrentIndex() : here.incrementCurrentIndex() } function menuAnchor() { return here.currentItem } // Every column has a menu: on an item there, or on the column's folder. diff --git a/src/FolderGrid.qml b/src/FolderGrid.qml index 37385b8a..7e134b8e 100644 --- a/src/FolderGrid.qml +++ b/src/FolderGrid.qml @@ -11,6 +11,8 @@ GridView { signal cursorLetGo() function letGo() { selecting.letGo(); cursorLetGo() } function focusView() { forceActiveFocus() } + // The preview's ↑ and ↓: the tile above or below, as the arrow keys move here. + function stepRow(direction) { direction < 0 ? moveCurrentIndexUp() : moveCurrentIndexDown() } // Where the keyboard's menu (Alt+Return) opens: beside the cursor's item. function menuAnchor() { return currentItem } diff --git a/src/FolderList.qml b/src/FolderList.qml index 8e93a633..6b09167e 100644 --- a/src/FolderList.qml +++ b/src/FolderList.qml @@ -12,6 +12,8 @@ ListView { signal cursorLetGo() function letGo() { selecting.letGo(); cursorLetGo() } function focusView() { forceActiveFocus() } + // The preview's ↑ and ↓: the row above or below. + function stepRow(direction) { direction < 0 ? decrementCurrentIndex() : incrementCurrentIndex() } // Where the keyboard's menu (Alt+Return) opens: beside the cursor's item. function menuAnchor() { return currentItem } diff --git a/src/FolderPane.qml b/src/FolderPane.qml index 1c3d5315..93a18c6b 100644 --- a/src/FolderPane.qml +++ b/src/FolderPane.qml @@ -216,6 +216,7 @@ FocusScope { Connections { target: backend function onSelectRequested(name) { if (view.item) view.item.select(backend.folder.indexOf(name), false) } + function onStepRequested(direction) { if (view.item) view.item.stepRow(direction) } function onActivateRequested() { if (pane.host) { pane.host.raise(); pane.host.requestActivate() } } } diff --git a/src/PreviewWindow.qml b/src/PreviewWindow.qml index 1ffc1361..ffd87406 100644 --- a/src/PreviewWindow.qml +++ b/src/PreviewWindow.qml @@ -51,8 +51,20 @@ ApplicationWindow { Shortcut { sequence: "Left"; context: Qt.ApplicationShortcut; onActivated: win.rtl ? previewer.next() : previewer.previous() } Shortcut { sequence: "Right"; context: Qt.ApplicationShortcut; onActivated: win.rtl ? previewer.previous() : previewer.next() } Shortcut { sequences: ["Return", "Enter"]; context: Qt.ApplicationShortcut; onActivated: previewer.open() } - Shortcut { sequences: ["Up"]; context: Qt.ApplicationShortcut; onActivated: win.call("scroll", -1) } - Shortcut { sequences: ["Down"]; context: Qt.ApplicationShortcut; onActivated: win.call("scroll", 1) } + // ↑ and ↓ scroll what's taller than the window (a long text, a zoomed + // picture, a PDF); anything that fits moves to the item above or below, + // as the arrows do in the folder. + function upDown(direction) { + const v = win.view + const scrolls = v && typeof v.scroll === "function" + && (v.contentHeight === undefined || v.contentHeight > v.height + 1) + if (scrolls) + v.scroll(direction) + else + previewer.stepRow(direction) + } + Shortcut { sequences: ["Up"]; context: Qt.ApplicationShortcut; onActivated: win.upDown(-1) } + Shortcut { sequences: ["Down"]; context: Qt.ApplicationShortcut; onActivated: win.upDown(1) } Shortcut { sequences: [StandardKey.MoveToPreviousPage]; context: Qt.ApplicationShortcut; onActivated: win.call("page", -1) } Shortcut { sequences: [StandardKey.MoveToNextPage]; context: Qt.ApplicationShortcut; onActivated: win.call("page", 1) } Shortcut { sequences: [StandardKey.ZoomIn, "Ctrl+="]; context: Qt.ApplicationShortcut; onActivated: win.call("zoomBy", 1.25) } @@ -180,7 +192,7 @@ ApplicationWindow { visible: win.helpVisible onDismissed: win.helpVisible = false shortcuts: [ - [win.rtl ? "→ / ←" : "← / →", qsTr("Previous / next file")], + [win.rtl ? "→ ← ↑ ↓" : "← → ↑ ↓", qsTr("Previous / next file")], ["⏎", qsTr("Open in its app")], ["↑ ↓ PgUp PgDn", qsTr("Scroll")], ["Ctrl+= / Ctrl+-", qsTr("Zoom in / out")], diff --git a/src/backend.cpp b/src/backend.cpp index 6224ee0c..fca68f7f 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -41,6 +41,10 @@ Backend::Backend(const QString &home, QObject *parent) connect(&m_preview, &QProcess::readyReadStandardOutput, this, [this] { while (m_preview.canReadLine()) { const QString shown = QString::fromUtf8(m_preview.readLine()).trimmed(); + if (shown.startsWith("step ")) { + emit stepRequested(shown.mid(5).toInt() < 0 ? -1 : 1); + continue; + } m_previewed = shown; const QFileInfo info(shown); if (info.absolutePath() == path()) diff --git a/src/backend.h b/src/backend.h index 7d0f582d..09876d80 100644 --- a/src/backend.h +++ b/src/backend.h @@ -121,6 +121,9 @@ class Backend : public QObject { void detailsChanged(); void newWindowRequested(const QString &path); void selectRequested(const QString &name); + // The preview's ↑ or ↓ on a file it can't scroll: move one row in the + // folder view, whose layout only this window knows. + void stepRequested(int direction); void activateRequested(); private: diff --git a/src/previewer.cpp b/src/previewer.cpp index 7ac8bf0e..c3951108 100644 --- a/src/previewer.cpp +++ b/src/previewer.cpp @@ -195,6 +195,15 @@ void Previewer::previous() { setIndex(m_index - 1, true); } +void Previewer::stepRow(int direction) { + if (!m_reporting) { + direction < 0 ? previous() : next(); + return; + } + std::fprintf(stdout, "step %d\n", direction < 0 ? -1 : 1); + std::fflush(stdout); +} + void Previewer::open() { if (m_index >= 0) // The same lookup as opening from the folder: the type's default diff --git a/src/previewer.h b/src/previewer.h index 9aee60e9..6102b4f1 100644 --- a/src/previewer.h +++ b/src/previewer.h @@ -14,7 +14,9 @@ // // Talks to the Folio window that started it over stdin and stdout, one path // per line: Folio writes the item selected in its grid, the preview writes -// the item it steps to, and each side follows the other. +// the item it steps to, and each side follows the other. ↑ and ↓ are written +// as "step -1" and "step 1": rows belong to the folder view, so Folio moves +// its selection and sends the new item back. class Previewer : public QObject { Q_OBJECT // Listing the folder only changes where the item sits among its @@ -52,6 +54,9 @@ class Previewer : public QObject { Q_INVOKABLE void next(); Q_INVOKABLE void previous(); + // ↑ or ↓: the item above or below in the Folio window's view, or the + // previous or next file when the preview runs on its own. + Q_INVOKABLE void stepRow(int direction); Q_INVOKABLE void open(); // Which view shows a type: image, animated, thumbnail, video, audio, diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index e5278791..5de284ee 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5731,6 +5731,28 @@ private slots: } } + void previewerStepsByRowOnItsOwn() + { + QTemporaryDir dir; + write(dir.filePath("a.png"), "a"); + write(dir.filePath("b.png"), "b"); + write(dir.filePath("c.png"), "c"); + + // Without a Folio window to ask for the row above or below, ↑ and ↓ + // fall back to the previous and next file. + Previewer previewer; + previewer.start(dir.filePath("b.png"), false, "name"); + QTRY_COMPARE(previewer.count(), 3); + QCOMPARE(previewer.index(), 1); + previewer.stepRow(1); + QCOMPARE(previewer.index(), 2); + previewer.stepRow(1); + QCOMPARE(previewer.index(), 2); + previewer.stepRow(-1); + previewer.stepRow(-1); + QCOMPARE(previewer.index(), 0); + } + void themeFallsBack() { QTemporaryDir dir; From a25f46177ce58d1df5b70805134fb2ddfa465c9c Mon Sep 17 00:00:00 2001 From: kevinpardo Date: Thu, 12 Nov 2026 01:40:53 +0100 Subject: [PATCH 2/2] Hold the preview's arrows until Folio has stepped a row, and while text loads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After ↑ or ↓ the preview asked Folio for the row above or below, but an arrow pressed before the answer was taken from the old item: ↓ then → in a grid undid the ↓. Folio now sends the new item at once and then "stepped", and the preview keeps the arrows that came meanwhile until it does (a second at most, if no answer comes). A text still being read looked short, so ↑ and ↓ stepped past a long file on a slow mount. They do nothing until the text is in. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Ufqf7btMhVDAcTs2d8sLqB --- src/FolderPane.qml | 4 +- src/PreviewWindow.qml | 4 ++ src/backend.cpp | 5 +++ src/previewer.cpp | 85 +++++++++++++++++++++++++++++++++---------- src/previewer.h | 28 ++++++++++++-- tests/folio_tests.cpp | 54 +++++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 24 deletions(-) diff --git a/src/FolderPane.qml b/src/FolderPane.qml index 93a18c6b..ff4b685f 100644 --- a/src/FolderPane.qml +++ b/src/FolderPane.qml @@ -216,7 +216,9 @@ FocusScope { Connections { target: backend function onSelectRequested(name) { if (view.item) view.item.select(backend.folder.indexOf(name), false) } - function onStepRequested(direction) { if (view.item) view.item.stepRow(direction) } + // Followed now, not later as a re-read is: the preview waits for the + // new item before its next arrow. + function onStepRequested(direction) { if (view.item) { view.item.stepRow(direction); pane.follow() } } function onActivateRequested() { if (pane.host) { pane.host.raise(); pane.host.requestActivate() } } } diff --git a/src/PreviewWindow.qml b/src/PreviewWindow.qml index ffd87406..eacc36ab 100644 --- a/src/PreviewWindow.qml +++ b/src/PreviewWindow.qml @@ -55,6 +55,10 @@ ApplicationWindow { // picture, a PDF); anything that fits moves to the item above or below, // as the arrows do in the folder. function upDown(direction) { + // A text still being read looks short; stepping past it then would + // skip a long one. The next press, once it's in, knows which. + if (previewer.readingText) + return const v = win.view const scrolls = v && typeof v.scroll === "function" && (v.contentHeight === undefined || v.contentHeight > v.height + 1) diff --git a/src/backend.cpp b/src/backend.cpp index fca68f7f..6490ee7a 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -42,7 +42,12 @@ Backend::Backend(const QString &home, QObject *parent) while (m_preview.canReadLine()) { const QString shown = QString::fromUtf8(m_preview.readLine()).trimmed(); if (shown.startsWith("step ")) { + // The view moves and sends its new item right away (or + // nothing, at the first or last row); "stepped" after it + // lets the preview take the arrows that came meanwhile. emit stepRequested(shown.mid(5).toInt() < 0 ? -1 : 1); + if (previewing()) + m_preview.write("stepped\n"); continue; } m_previewed = shown; diff --git a/src/previewer.cpp b/src/previewer.cpp index c3951108..070c7e95 100644 --- a/src/previewer.cpp +++ b/src/previewer.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -185,23 +186,64 @@ QString Previewer::type() const { return QMimeDatabase().mimeTypeForName(m_entries.at(m_index).mime).comment(); } -void Previewer::next() { - if (m_index + 1 < m_entries.size()) - setIndex(m_index + 1, true); +void Previewer::next() { move(Move::Next); } +void Previewer::previous() { move(Move::Previous); } +void Previewer::stepRow(int direction) { move(direction < 0 ? Move::Up : Move::Down); } + +void Previewer::move(Move move) { + if (m_awaitingStep) { + m_queued.append(move); + return; + } + // On its own, the preview has no rows: ↑ and ↓ go to the previous and + // next file. + if (!m_reporting && (move == Move::Up || move == Move::Down)) + move = move == Move::Up ? Move::Previous : Move::Next; + switch (move) { + case Move::Previous: + if (m_index > 0) + setIndex(m_index - 1, true); + break; + case Move::Next: + if (m_index + 1 < m_entries.size()) + setIndex(m_index + 1, true); + break; + case Move::Up: + case Move::Down: { + m_awaitingStep = true; + // A reply that never comes (a Folio too old to send it, or stuck) + // holds the arrows for a second, not for good. + const quint64 step = ++m_step; + QTimer::singleShot(1000, this, [this, step] { + if (step == m_step) + stepped(); + }); + report(move == Move::Up ? "step -1" : "step 1"); + break; + } + } } -void Previewer::previous() { - if (m_index > 0) - setIndex(m_index - 1, true); +void Previewer::stepped() { + m_awaitingStep = false; + ++m_step; + while (!m_awaitingStep && !m_queued.isEmpty()) + move(m_queued.takeFirst()); } -void Previewer::stepRow(int direction) { - if (!m_reporting) { - direction < 0 ? previous() : next(); - return; +void Previewer::receive(const QString &line) { + if (line == "stepped") + stepped(); + else if (!line.isEmpty()) + show(line); +} + +void Previewer::report(const QString &line) { + emit reported(line); + if (m_overStdio) { + std::fprintf(stdout, "%s\n", line.toUtf8().constData()); + std::fflush(stdout); } - std::fprintf(stdout, "step %d\n", direction < 0 ? -1 : 1); - std::fflush(stdout); } void Previewer::open() { @@ -217,6 +259,7 @@ void Previewer::setIndex(int index, bool report) { const FolderModel::Entry e = m_entries.at(index); m_viewer = viewerFor(e.mime, e.isDir); m_text.clear(); + m_readingText = false; // A folder's count and first pictures, and a text's opening part, are // read off the UI thread: a big folder, or any file on a slow mount, @@ -225,6 +268,7 @@ void Previewer::setIndex(int index, bool report) { const bool count = e.isDir && e.itemCount < 0; const bool text = m_viewer == "text" || m_viewer == "markdown"; if (count || text) { + m_readingText = text; auto *watcher = new QFutureWatcher>(this); connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, text] { watcher->deleteLater(); @@ -233,6 +277,7 @@ void Previewer::setIndex(int index, bool report) { const auto [entry, read] = watcher->result(); m_entries[m_index] = entry; m_text = read; + m_readingText = false; // A ".txt" that turns out to be binary is shown as a plain file. if (text && m_text.isEmpty() && entry.size > 0) m_viewer = "other"; @@ -253,14 +298,15 @@ void Previewer::setIndex(int index, bool report) { emit changed(); emit listed(); - if (report && m_reporting) { - std::fprintf(stdout, "%s\n", e.path.toUtf8().constData()); - std::fflush(stdout); - } + if (report && m_reporting) + this->report(e.path); } -void Previewer::connectToFolio() { +void Previewer::connectToFolio(bool overStdio) { m_reporting = true; + m_overStdio = overStdio; + if (!overStdio) + return; m_stdin = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read, this); connect(m_stdin, &QSocketNotifier::activated, this, [this] { char buffer[4096]; @@ -273,10 +319,9 @@ void Previewer::connectToFolio() { m_pending.append(buffer, n); int newline; while ((newline = m_pending.indexOf('\n')) >= 0) { - const QString path = QString::fromUtf8(m_pending.left(newline)); + const QString line = QString::fromUtf8(m_pending.left(newline)); m_pending.remove(0, newline + 1); - if (!path.isEmpty()) - show(path); + receive(line); } }); } diff --git a/src/previewer.h b/src/previewer.h index 6102b4f1..f3b4742a 100644 --- a/src/previewer.h +++ b/src/previewer.h @@ -16,7 +16,8 @@ // per line: Folio writes the item selected in its grid, the preview writes // the item it steps to, and each side follows the other. ↑ and ↓ are written // as "step -1" and "step 1": rows belong to the folder view, so Folio moves -// its selection and sends the new item back. +// its selection, sends the new item back, then "stepped". Arrows pressed +// before that wait for it: taken from the old item, they'd undo the move. class Previewer : public QObject { Q_OBJECT // Listing the folder only changes where the item sits among its @@ -28,6 +29,9 @@ class Previewer : public QObject { Q_PROPERTY(QString viewer READ viewer NOTIFY changed) Q_PROPERTY(QString url READ url NOTIFY changed) Q_PROPERTY(QString text READ text NOTIFY changed) + // A text's opening part is still being read: until it's in, its length + // (whether ↑ and ↓ scroll it or step past it) isn't known. + Q_PROPERTY(bool readingText READ readingText NOTIFY changed) Q_PROPERTY(QString type READ type NOTIFY changed) public: @@ -36,8 +40,12 @@ class Previewer : public QObject { // Shows the file right away, then lists its folder in the background so // the arrow keys can step to its neighbours. void start(const QString &path, bool showHidden, const QString &sort); - // Reads paths from stdin and reports steps on stdout. - void connectToFolio(); + // Reads paths from stdin and reports steps on stdout; the tests leave + // stdin and stdout alone and talk through receive() and reported(). + void connectToFolio(bool overStdio = true); + // One line from Folio: a path to show, or "stepped" once it has moved a + // row for ↑ or ↓. + void receive(const QString &line); // Shows another file; one in the same folder keeps the list. void show(const QString &path); // Lists the folder again, keeping the file shown: after a language @@ -50,6 +58,7 @@ class Previewer : public QObject { QString viewer() const { return m_viewer; } QString url() const; QString text() const { return m_text; } + bool readingText() const { return m_readingText; } QString type() const; Q_INVOKABLE void next(); @@ -74,14 +83,21 @@ class Previewer : public QObject { signals: void changed(); void listed(); + // A line for Folio: the path stepped to, or "step -1" / "step 1". + void reported(const QString &line); private: + enum class Move { Previous, Next, Up, Down }; + void move(Move move); + void stepped(); void setIndex(int index, bool report); + void report(const QString &line); QVector m_entries; int m_index = -1; QString m_viewer; QString m_text; + bool m_readingText = false; bool m_showHidden = false; QString m_sort = "name"; quint64 m_generation = 0; @@ -89,6 +105,12 @@ class Previewer : public QObject { // or skipped when they haven't started. std::shared_ptr> m_reading = std::make_shared>(0); bool m_reporting = false; + bool m_overStdio = false; + // Set from a "step" until Folio's "stepped"; the arrows pressed meanwhile + // wait in m_queued, in order. + bool m_awaitingStep = false; + quint64 m_step = 0; + QVector m_queued; QSocketNotifier *m_stdin = nullptr; QByteArray m_pending; }; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 5de284ee..d61d6388 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5753,6 +5753,60 @@ private slots: QCOMPARE(previewer.index(), 0); } + void previewerHoldsArrowsUntilFolioHasStepped() + { + QTemporaryDir dir; + for (const char *name : {"a.png", "b.png", "c.png", "d.png"}) + write(dir.filePath(name), "x"); + + Previewer previewer; + previewer.connectToFolio(false); + QSignalSpy reported(&previewer, &Previewer::reported); + previewer.start(dir.filePath("a.png"), false, "name"); + QTRY_COMPARE(previewer.count(), 4); + + // ↓ then → before Folio answers: → waits, and is taken from the item + // Folio moved to, not from a.png. + previewer.stepRow(1); + previewer.next(); + QCOMPARE(reported.count(), 1); + QCOMPARE(reported.at(0).at(0).toString(), QString("step 1")); + previewer.receive(dir.filePath("c.png")); + QCOMPARE(previewer.index(), 2); + QCOMPARE(reported.count(), 1); + previewer.receive("stepped"); + QCOMPARE(previewer.index(), 3); + QCOMPARE(reported.count(), 2); + QCOMPARE(reported.at(1).at(0).toString(), QFileInfo(dir.filePath("d.png")).absoluteFilePath()); + + // At the last row Folio doesn't move, and only says it has stepped. + previewer.stepRow(1); + previewer.previous(); + previewer.receive("stepped"); + QCOMPARE(previewer.index(), 2); + + // A reply that never comes holds the arrows for a moment only. + previewer.stepRow(-1); + previewer.previous(); + QCOMPARE(previewer.index(), 2); + QTRY_COMPARE(previewer.index(), 1); + } + + void previewerSaysWhileItReadsText() + { + QTemporaryDir dir; + write(dir.filePath("long.txt"), QByteArray(4096, 'x')); + write(dir.filePath("b.png"), "b"); + + Previewer previewer; + previewer.start(dir.filePath("long.txt"), false, "name"); + QVERIFY(previewer.readingText()); + QTRY_VERIFY(!previewer.readingText()); + QCOMPARE(previewer.text().size(), 4096); + previewer.show(dir.filePath("b.png")); + QVERIFY(!previewer.readingText()); + } + void themeFallsBack() { QTemporaryDir dir;