Skip to content
Closed
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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Quick QuickControls2 DBus Conc
find_package(PkgConfig REQUIRED)
pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0 gio-unix-2.0)
find_package(ZLIB REQUIRED)
add_subdirectory(tools/stl-thumbnailer)

# Everything but main.cpp, shared by the app and the tests, so a new source
# file in src/ needs no line here.
Expand All @@ -26,13 +27,20 @@ target_link_libraries(folio_core PUBLIC

add_executable(folio src/main.cpp src/resources.qrc)
target_link_libraries(folio PRIVATE folio_core)
add_dependencies(folio folio-stl-thumbnailer)

# The tests compile the same sources, run offscreen, and read the QML they
# check straight from src/.
add_executable(folio_tests tests/folio_tests.cpp)
target_link_libraries(folio_tests PRIVATE folio_core Qt6::Test)
add_dependencies(folio_tests folio-stl-thumbnailer)
# The bundled plugins, which the tests run against stubs.
target_compile_definitions(folio_tests PRIVATE FOLIO_PLUGINS="${CMAKE_SOURCE_DIR}/plugins")
enable_testing()
add_test(NAME folio_tests COMMAND folio_tests)
set_tests_properties(folio_tests PROPERTIES ENVIRONMENT QT_QPA_PLATFORM=offscreen)

add_executable(stl_thumbnail_tests tests/stl_thumbnail_tests.cpp)
target_link_libraries(stl_thumbnail_tests PRIVATE folio_stl Qt6::Test)
add_dependencies(stl_thumbnail_tests folio-stl-thumbnailer)
add_test(NAME stl_thumbnail_tests COMMAND stl_thumbnail_tests)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

Files (`folio`) is Omarchy's file manager. It's one quiet window that follows your Omarchy theme and language, works from the keyboard, and never crawls your home folder in the background: folders are read when you open them, and thumbnails are made when a tile needs one, shared with every other app through `~/.cache/thumbnails`.

STL models get transparent thumbnails from a small bundled CPU renderer, without a graphics driver or a separate 3D application. An explicit user thumbnailer takes precedence; an installed system thumbnailer handles files the bundled renderer declines.

- **Three views**: a grid with real previews, a list, and Finder-style columns. Ctrl+1/2/3 switches.
- **Space previews** pictures, video, audio, PDFs, text and Markdown at full size, in their own window.
- **The sidebar** holds everything in your home folder, pinned folders (shared with GTK file dialogs), Recents, Trash with restore, your drives, and the machines on your tailnet, which open over SFTP.
Expand Down
4 changes: 2 additions & 2 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ set -eu

ROOT="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
cmake -S "$ROOT" -B "$ROOT/build" -DCMAKE_BUILD_TYPE=Release >/dev/null
cmake --build "$ROOT/build" --target folio_tests -j"$(nproc)"
cmake --build "$ROOT/build" --target folio_tests stl_thumbnail_tests -j"$(nproc)"
cd "$ROOT/build"
QT_QPA_PLATFORM=offscreen ./folio_tests
QT_QPA_PLATFORM=offscreen ctest --output-on-failure --no-tests=error "$@"
71 changes: 57 additions & 14 deletions src/thumbnails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QBuffer>
#include <QDeadlineTimer>
#include <QCryptographicHash>
#include <QCoreApplication>
#include <QtEndian>
#include <zlib.h>
#include <QDir>
Expand Down Expand Up @@ -152,12 +153,11 @@ void stop(QProcess &process, qint64 session) {
process.waitForFinished(stoppedPatience);
}

Ran run(QProcess &process, const QString &program, const QStringList &args, int patience, const std::atomic_bool &quitting) {
Ran run(QProcess &process, const QString &program, const QStringList &args, QDeadlineTimer deadline, const std::atomic_bool &quitting) {
// A thumbnail asked for as Folio quits is never started.
if (quitting)
if (quitting || deadline.hasExpired())
return Ran::Unjudged;
// One deadline for the start and the finish together.
const QDeadlineTimer deadline(patience);
// The caller's absolute deadline also covers an STL's system fallback.
process.setChildProcessModifier([folio = ::getpid()] { keepInASession(folio); });
process.start(program, args);
// In slices, so quitting doesn't wait on a program slow to start (one on a stalled network mount, say).
Expand Down Expand Up @@ -250,11 +250,12 @@ const QHash<QString, QString> &packageFor() {

} // namespace

Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories, int patience)
Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories, int patience, const QString &stlHelper)
: m_cache(cacheDirectory.isEmpty()
? QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/thumbnails"
: cacheDirectory),
m_patience(patience) {
m_patience(patience),
m_stlHelper(stlHelper.isNull() ? QCoreApplication::applicationDirPath() + "/folio-stl-thumbnailer" : stlHelper) {
QStringList directories = thumbnailerDirectories;
if (directories.isEmpty()) {
// Later directories win, so the user's own thumbnailers override the
Expand All @@ -265,6 +266,7 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna
directories << base + "/thumbnailers";
}
QMimeDatabase mimes;
const QString userDirectory = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).filePath("thumbnailers");
for (const QString &directory : directories) {
// A .thumbnailer file may name a type by an alias, as glycin names DDS pictures image/x-dds.
QHash<QString, QString> named, aliased;
Expand Down Expand Up @@ -301,6 +303,10 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna
// Within one directory the file that names a type itself wins over one that names an alias of it.
m_thumbnailers.insert(aliased);
m_thumbnailers.insert(named);
if (QDir(directory).absolutePath() == QDir(userDirectory).absolutePath()) {
for (auto it = aliased.cbegin(); it != aliased.cend(); ++it) m_userThumbnailers.insert(it.key());
for (auto it = named.cbegin(); it != named.cend(); ++it) m_userThumbnailers.insert(it.key());
}
}
// An older Folio's failure markers (fail/folio, or a fail/folio-<n> with a lower number) are never read again, so
// they are cleared away, on a worker since there may be thousands. A newer Folio's are left for it, so an installed
Expand All @@ -323,12 +329,28 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna
QString Thumbnails::missingPackage(const QString &mime) const {
// Qt draws its own pictures in-process, an installed thumbnailer needs nothing more, and pdftoppm draws a PDF without one.
if ((mime.startsWith("image/") && QImageReader::supportedMimeTypes().contains(mime.toLatin1()))
|| m_thumbnailers.contains(mime)
|| m_thumbnailers.contains(mime) || nativeStl(mime)
|| (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()))
return {};
return packageFor().value(mime);
}

bool Thumbnails::nativeStl(const QString &mime) const {
if (mime != "model/stl" && mime != "model/x.stl-ascii" && mime != "model/x.stl-binary") return false;
if (m_userThumbnailers.contains(mime) || m_userThumbnailers.contains("model/stl")) return false;
const QFileInfo helper(m_stlHelper);
return !m_stlHelper.isEmpty() && helper.isFile() && helper.isExecutable();
}

QString Thumbnails::providerKey(const QString &mime, int pixels) const {
const QString fallback = m_thumbnailers.value(mime);
if (!nativeStl(mime)) return fallback;
const QFileInfo helper(m_stlHelper);
// A new helper, fallback or smaller size gets its turn after an earlier failure.
return QStringLiteral("stl-cpu-1:%1:%2:%3:%4\n%5").arg(m_stlHelper)
.arg(helper.size()).arg(helper.lastModified().toMSecsSinceEpoch()).arg(pixels).arg(fallback);
}

QString Thumbnails::cacheKey(const QString &path) {
return QCryptographicHash::hash(fileUri(path).toUtf8(), QCryptographicHash::Md5).toHex() + ".png";
}
Expand Down Expand Up @@ -380,7 +402,7 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons
const QString type = mime.endsWith("/*")
? QMimeDatabase().mimeTypeForFile(path, QMimeDatabase::MatchExtension).name()
: mime;
const QString thumbnailer = m_thumbnailers.value(type);
const QString thumbnailer = providerKey(type, pixels);
if (failedBefore(path, modified, thumbnailer))
return {};
// A file that cannot be read is not a broken one: nothing is tried, so nothing is remembered.
Expand All @@ -403,6 +425,17 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons
}

QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted) const {
const QDeadlineTimer deadline(m_patience);
if (nativeStl(mime)) {
QImage image;
attempted = stlPicture(path, pixels, image, deadline);
if (!image.isNull()) return image;
if (m_quitting || deadline.hasExpired()) {
// A fallback not given a turn has not judged the file.
if (m_quitting || !m_thumbnailers.value(mime).isEmpty()) attempted = false;
return {};
}
}
// A camera RAW with no installed thumbnailer, the desktop's own answer, shows the JPEG the camera embedded.
if (RawEmbed::handles(mime) && !m_thumbnailers.contains(mime)) {
attempted = true;
Expand All @@ -428,15 +461,15 @@ QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels
if (!command.isEmpty()) {
// A thumbnailer that could not start has not judged the file, whatever a reader made of it first; a PDF is then left to pdftoppm.
QImage image;
attempted = runThumbnailer(command, path, pixels, image);
attempted = runThumbnailer(command, path, pixels, image, deadline);
if (attempted)
return image;
}
// Without a PDF thumbnailer installed, Poppler's pdftoppm (there with
// CUPS) draws the first page.
if (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()) {
QImage image;
attempted = pdfPage(path, pixels, image);
attempted = pdfPage(path, pixels, image, deadline);
return image;
}
return {};
Expand Down Expand Up @@ -489,19 +522,29 @@ QImage Thumbnails::textPage(const QString &path, int pixels, const QColor &paper
return page;
}

bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image) const {
bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image, QDeadlineTimer deadline) const {
QTemporaryDir scratch;
if (!scratch.isValid())
return false;
QProcess process;
const Ran ran = run(process, "pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels),
path, scratch.filePath("page")}, m_patience, m_quitting);
path, scratch.filePath("page")}, deadline, m_quitting);
if (ran == Ran::Finished)
image = QImage(scratch.filePath("page.png"));
return ran != Ran::Unjudged;
}

bool Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const {
bool Thumbnails::stlPicture(const QString &path, int pixels, QImage &image, QDeadlineTimer deadline) const {
QTemporaryDir scratch;
if (!scratch.isValid()) return false;
const QString output = scratch.filePath("thumbnail.png");
QProcess process;
const Ran ran = run(process, m_stlHelper, {path, output, QString::number(pixels)}, deadline, m_quitting);
if (ran == Ran::Finished) image = QImage(output);
return ran != Ran::Unjudged;
}

bool Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image, QDeadlineTimer deadline) const {
QTemporaryDir scratch;
if (!scratch.isValid())
return false;
Expand All @@ -517,7 +560,7 @@ bool Thumbnails::runThumbnailer(const QString &command, const QString &path, int

QProcess process;
process.setProcessChannelMode(QProcess::ForwardedErrorChannel);
const Ran ran = run(process, program, args, m_patience, m_quitting);
const Ran ran = run(process, program, args, deadline, m_quitting);
if (ran == Ran::Finished)
image = QImage(output);
return ran != Ran::Unjudged;
Expand Down
20 changes: 14 additions & 6 deletions src/thumbnails.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <QHash>
#include <QDateTime>
#include <QDeadlineTimer>
#include <QImage>
#include <QObject>
#include <QQuickAsyncImageProvider>
#include <QStringList>
#include <QSet>
#include <QThreadPool>
#include <atomic>

Expand All @@ -21,9 +23,10 @@ class Thumbnails : public QObject {
Q_OBJECT

public:
// `patience` is how long a thumbnailer may take, in milliseconds; tests shorten it.
// `patience` covers a thumbnail and any fallback, in milliseconds. A null
// stlHelper finds the bundled companion; an explicit empty string disables it.
explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {},
int patience = 15000);
int patience = 15000, const QString &stlHelper = {});

// Returns a thumbnail no larger than `size` on its longest side, from the
// cache when it is fresh, or a null image when none can be made.
Expand All @@ -34,8 +37,8 @@ class Thumbnails : public QObject {

static QString cacheKey(const QString &path);
// The spec's fail/<program>-<version>: the number is raised when Folio learns to draw more, so old failures are tried
// again. 3: names with a thumbnailer's codes in them, and PDFs whose thumbnailer couldn't start, are drawn now.
static inline const QString failures = QStringLiteral("fail/folio-3");
// again. 4: a bundled CPU helper can draw STL files a system thumbnailer could not.
static inline const QString failures = QStringLiteral("fail/folio-4");
static QString bucketFor(int size);
// A text file's first lines set on a page in the given colors. Made
// fresh each time rather than cached, since the colors follow the theme.
Expand All @@ -47,11 +50,14 @@ class Thumbnails : public QObject {
private:
QString cachePath(const QString &path, int size) const;
QString failPath(const QString &path) const;
bool nativeStl(const QString &mime) const;
QString providerKey(const QString &mime, int pixels) const;
// Sets `attempted` when Qt's reader or a thumbnailer could take the file.
QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted) const;
// Both answer whether the program judged the file: one that could not start, or was stopped as Folio quit, has not.
bool runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const;
bool pdfPage(const QString &path, int pixels, QImage &image) const;
bool runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image, QDeadlineTimer deadline) const;
bool pdfPage(const QString &path, int pixels, QImage &image, QDeadlineTimer deadline) const;
bool stlPicture(const QString &path, int pixels, QImage &image, QDeadlineTimer deadline) const;
void remember(QImage image, const QString &path, int size, const QDateTime &modified) const;
// A failure goes with the type's thumbnailer as it was then, none included: one installed since gets its turn.
void rememberFailure(const QString &path, const QDateTime &modified, const QString &thumbnailer) const;
Expand All @@ -63,6 +69,8 @@ class Thumbnails : public QObject {
int m_patience;
std::atomic_bool m_quitting{false};
QHash<QString, QString> m_thumbnailers;
QSet<QString> m_userThumbnailers;
QString m_stlHelper;
};

// Serves image://thumb/<size>/<modified>/<mime>/<path> to QML on a thread pool.
Expand Down
Loading