Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/FolderColumns.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Item {

Selecting { id: selecting; view: here; pageStep: Math.max(1, Math.floor(here.height / 32) - 1) }
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.
Expand Down
2 changes: 2 additions & 0 deletions src/FolderGrid.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
2 changes: 2 additions & 0 deletions src/FolderList.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ FileListView {
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 }

Expand Down
8 changes: 8 additions & 0 deletions src/FolderPane.qml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ FocusScope {
Connections {
target: backend
function onSelectRequested(name) { if (view.item) view.item.select(backend.folder.indexOf(name), false) }
function onSelectPathRequested(path) {
const row = backend.folder.indexOfPath(path)
if (view.item && row >= 0)
view.item.select(row, false)
}
// 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() } }
Comment thread
dhh marked this conversation as resolved.
Resolved
function onActivateRequested() { if (pane.host) { pane.host.raise(); pane.host.requestActivate() } }
}

Expand Down
3 changes: 3 additions & 0 deletions src/PdfPreview.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import QtQuick.Pdf
Item {
id: pdf

// ↑ and ↓ move between the pages, or with only one, to the next item.
readonly property bool scrolls: doc.pageCount > 1

function fit() { view.scaleToWidth(Math.min(width - 48, 960), height) }
function zoomBy(factor) { view.renderScale = Math.max(0.1, Math.min(10, view.renderScale * factor)) }
function scroll(direction) { page(direction) }
Expand Down
22 changes: 19 additions & 3 deletions src/PreviewWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,24 @@ 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) {
// 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.scrolls !== undefined ? v.scrolls : 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) }
Expand Down Expand Up @@ -188,7 +204,7 @@ ApplicationWindow {
visible: win.helpVisible
onDismissed: win.helpVisible = false
groups: [{ title: qsTr("Preview"), shortcuts: [
[qsTr("Previous / next file"), win.rtl ? [["→"], ["←"]] : [["←"], ["→"]]],
[qsTr("Previous / next file"), (win.rtl ? [["→"], ["←"]] : [["←"], ["→"]]).concat([["↑"], ["↓"]])],
[qsTr("Open in its app"), [["⏎"]]],
[qsTr("Scroll"), [["↑", "↓"], ["Page Up"], ["Page Down"]]],
[qsTr("Zoom in / out"), [["Ctrl", "="], ["Ctrl", "-"]]],
Expand Down
28 changes: 21 additions & 7 deletions src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,8 @@ Backend::Backend(const QString &home, QObject *parent)
});
// The preview reports each file it steps to, and the grid follows.
connect(&m_preview, &QProcess::readyReadStandardOutput, this, [this] {
while (m_preview.canReadLine()) {
const QString shown = QString::fromUtf8(m_preview.readLine()).trimmed();
m_previewed = shown;
const QFileInfo info(shown);
if (info.absolutePath() == path())
emit selectRequested(info.fileName());
}
while (m_preview.canReadLine())
previewSaid(QString::fromUtf8(m_preview.readLine()).trimmed());
});
connect(&m_preview, &QProcess::stateChanged, this, &Backend::previewingChanged);
connect(&m_preview, &QProcess::finished, this, [this](int, QProcess::ExitStatus status) {
Expand Down Expand Up @@ -720,3 +715,22 @@ void Backend::setBusy(const QString &busy) {
m_busy = busy;
emit busyChanged();
}

void Backend::previewSaid(const QString &shown) {
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");
return;
}
m_previewed = shown;
// Any file in the folder shown or below it: one in a folder opened in
// place (the list's tree) is a row too, and the cursor goes to it, so
// ↑ and ↓ step from there.
const QString here = path().endsWith('/') ? path() : path() + '/';
if (shown.startsWith(here))
emit selectPathRequested(shown);
}
9 changes: 9 additions & 0 deletions src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class Backend : public QObject {
// Space: opens the large preview of a file in its own process, or closes
// it when it's already open.
Q_INVOKABLE void togglePreview(const QString &path);
// A line from the preview: the file it moved to, or "step -1"/"step 1"
// asking for the row above or below.
void previewSaid(const QString &line);
// Ctrl+N: another window, on the folder this one shows.
Q_INVOKABLE void newWindow();
// In a file chooser, opening a file picks it (chosen) rather than opening
Expand Down Expand Up @@ -150,6 +153,12 @@ class Backend : public QObject {
void newWindowRequested(const QString &path);
void chosen(const QString &path);
void selectRequested(const QString &name);
// The preview moved to `path`, in the folder shown or a folder opened in
// place in it: the cursor goes to its row.
void selectPathRequested(const QString &path);
// 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:
Expand Down
80 changes: 66 additions & 14 deletions src/previewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QFutureWatcher>
#include <QImageReader>
#include <QMimeDatabase>
#include <QTimer>
#include <QUrl>
#include <QtConcurrent>
#include <cstdio>
Expand Down Expand Up @@ -191,14 +192,61 @@ QString Previewer::type() const {
return QMimeDatabase().mimeTypeForName(m_readType.isEmpty() ? m_entries.at(m_index).mime : m_readType).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) {
// Folio answers within milliseconds, sooner than a key repeats.
if (m_awaitingStep)
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)
// stills 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::stepped() {
m_awaitingStep = false;
++m_step;
}

void Previewer::previous() {
if (m_index > 0)
setIndex(m_index - 1, true);
void Previewer::receive(const QString &line) {
if (line == "stepped")
stepped();
else if (!line.isEmpty())
show(line);
Comment thread
dhh marked this conversation as resolved.
Resolved
}

void Previewer::report(const QString &line) {
emit reported(line);
if (m_overStdio) {
std::fprintf(stdout, "%s\n", line.toUtf8().constData());
std::fflush(stdout);
}
}

void Previewer::open() {
Expand All @@ -218,6 +266,7 @@ void Previewer::setIndex(int index, bool report) {
m_viewer = viewerFor(e.mime, e.isDir);
m_text.clear();
m_readType.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,
Expand All @@ -228,6 +277,8 @@ void Previewer::setIndex(int index, bool report) {
// A file its extension could not name is named by what it holds, read with the rest and kept apart from the listing.
const bool unnamed = e.mime == "application/octet-stream";
if (count || text || unnamed) {
// A file its name gave no type may turn out to be a long text.
m_readingText = text || unnamed;
auto *watcher = new QFutureWatcher<QPair<FolderModel::Entry, QString>>(this);
connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, unnamed] {
watcher->deleteLater();
Expand All @@ -240,6 +291,7 @@ void Previewer::setIndex(int index, bool report) {
m_entries[m_index] = entry;
m_text = read;
m_viewer = viewerFor(entry.mime, entry.isDir);
m_readingText = false;
// A ".txt" that turns out to be binary is shown as a plain file.
if ((m_viewer == "text" || m_viewer == "markdown") && m_text.isEmpty() && entry.size > 0)
m_viewer = "other";
Expand All @@ -261,14 +313,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];
Expand All @@ -281,10 +334,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);
}
});
}
34 changes: 31 additions & 3 deletions src/previewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
//
// 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, sends the new item back, then "stepped". Arrows pressed
// before that are let go: taken from the old item, they'd undo the move,
// and replayed later, from a file not yet looked at, they'd skip it.
class Previewer : public QObject {
Q_OBJECT
// Listing the folder only changes where the item sits among its
Expand All @@ -28,6 +32,10 @@ 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, or the type of a file its name gave none, is
// still being read: until it's in, whether it's a long text (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:
Expand All @@ -39,8 +47,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
Expand All @@ -53,10 +65,14 @@ 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();
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,
Expand All @@ -76,9 +92,15 @@ class Previewer : public QObject {
void listed();
// The item was handed to its app, and the preview has done its part.
void opened();
// 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);

Opener m_opener;
QVector<FolderModel::Entry> m_entries;
Expand All @@ -87,13 +109,19 @@ class Previewer : public QObject {
QString m_text;
// The type read from what the shown file holds when its name gave none, for this showing only: a file written since is read again.
QString m_readType;
bool m_readingText = false;
bool m_showHidden = false;
QString m_sort = "name";
quint64 m_generation = 0;
// The newest item read in the background; older readings are dropped,
// or skipped when they haven't started.
std::shared_ptr<std::atomic<quint64>> m_reading = std::make_shared<std::atomic<quint64>>(0);
bool m_reporting = false;
bool m_overStdio = false;
// Set from a "step" until Folio's "stepped"; the arrows pressed meanwhile
// are let go.
bool m_awaitingStep = false;
quint64 m_step = 0;
QSocketNotifier *m_stdin = nullptr;
QByteArray m_pending;
};
Loading
Loading