Files
Hermit/app/src/ui/ThemeIcons.cpp
T
WHC c2320f946b v0.9.0: the Qt front end as an Android APK, images in the transcript
Two strands land together: the window draws pictures now, and the whole front
end builds, installs and runs on the phone with the engine linked in-process.

Images (the "make a picture actually show up" part)
- Engine, additively: cst_agent_hooks_t.on_tool_images(call, list, n, ud) fires
  when a call left attachments behind, and cst_agent_pending_rel() /
  cst_agent_pending_is_image() read that list without the app touching the
  store. tests/test_attach.c::test_agent_pending pins the three cases (an
  image, a stored name that passes cst_attach_rel_ok, out-of-range NULL).
- App: image://cstattach/<stored name> (app/src/ui/SessionImages.cpp) serves
  only the current session's attachment directory, checked with
  cst_attach_rel_ok, decodes down to the requested sourceSize and never
  upscales, and holds a mutex because requestImage() runs on the render
  thread while the directory can change underneath it. AgentCore gained
  attachmentDir(), pendingImages(), the toolImages()/promptImages() signals
  and hookToolImages(); history() gives replayed rows their images too.
- QML: ImageStrip.qml (a thumb strip, <=150 px tall, shaped by the picture,
  tooltip with name/size/origin, the file's name when it went missing),
  wired into MessageCard and ToolCard. Qt 6's ListModel cannot hold a
  list-valued role (append() reads back undefined, setProperty drops it), so
  a row carries one image per line as <stored>|<name>|<origin>|<w>x<h> and
  ImageStrip decodes that.

Android (the engine is already in the APK; this is the front end)
- app/src/main.cpp: on Android the workspace, config, tmp and HOME go under
  the app's own data directory before the engine opens anything, because
  QDir::currentPath() is "/" there and /tmp is not writable.
- app/android/: the manifest (QtApplication/QtActivity, INTERNET and
  POST_NOTIFICATIONS, adjustResize, no storage permission -- the picker will
  be the system one), strings, a vector icon; minSdk 24 and targetSdk 35 come
  from app/CMakeLists.txt through QT_ANDROID_MIN/TARGET_SDK_VERSION.
- app/src/platform/android/AndroidTools.cpp: the Android file behind
  PlatformInit(), owner "app:android" (notification, clipboard, share sheet,
  file picker are stubs for now, so the tool list does not lie about them).
- app/tools/selftest.c decides its scratch workspace with os_temp_dir(), so
  the C smoke test runs on the device too.
- scripts/build_apk_android.sh: sources to installed APK in one command
  (environment checks, qt-cmake configure, --target apk, adb install), and
  the host-side plumbing the NixOS + qt.io combination needs -- the NDK
  sysroot include dirs for Qt's FindGLESv2/FindEGL (they only look under
  <sysroot>/include), -L$PREFIX/lib on the shared/module link lines as well
  as the executables', the Android debug keystore created on first use with
  QT_ANDROID_SIGN_APK + QT_ANDROID_KEYSTORE_* (androiddeployqt copies the
  unsigned gradle output and signs it itself), and the #!/bin/bash shebang of
  the build-tools wrappers, which NixOS does not have.
- scripts/gradle-android-init.gradle (installed into ~/.gradle/init.d/): the
  release lint task is switched off -- lintVitalAnalyzeRelease resolves its
  own tool jars straight from dl.google.com, bypassing every repository the
  build configures, and fails the build after all the real work is done.
- Icons: app/icons/*.svg (Breeze's 24@3x files, the 19 names the window asks
  for) are compiled in as :/cst-icons and ThemeIcons.cpp falls back to them
  when the platform has no icon theme. On the phone the Qt theme lookup found
  nothing and the QML glyph fallback was an empty box in the phone's fonts.
- docs/android.md: "The APK (Qt front end)" -- the SDK assembled without
  sdkmanager, the host Qt's missing libraries on NixOS, the Gradle mirrors,
  lint, signing -- and a Status that says what runs where. app/README.md
  gains the images and APK lines, and stops claiming Android platform tools
  that are still stubs.

Verified: engine `make check` green; desktop `cst-app-selftest: OK` and
--uitest all checks pass; phone (NX789J, Android 16, arm64-v8a) -- the APK
installs, starts on the workspace under /data/user/0/xyz.whcself.cstagent/
files/, a turn reaches a configured provider over HTTPS from inside the ART
process and renders the reply, rotation keeps the session and docks the
sidebar, and the toolbar/card icons draw.

Version 0.9.0.

mode = coding
2026-10-11 15:22:10 +08:00

137 lines
4.3 KiB
C++

#include "ui/ThemeIcons.h"
#include <QDir>
#include <QGuiApplication>
#include <QIcon>
#include <QPainter>
#include <QQmlApplicationEngine>
#include <QScreen>
#include <QStandardPaths>
#include <QStringList>
#include <QUrl>
namespace app {
namespace {
/* What a usable theme has to have; every window in the app needs it. */
const char kProbe[] = "mail-attachment";
/* The desktop's own theme first (whatever the platform theme set), then
* Breeze: this window is dark, so the dark variant, and Breeze proper as
* Qt's fallback for the odd name that only lives there. Before any of that
* the search paths have to be filled in -- Qt only builds them from XDG once
* a theme name was set, and a shell that was started outside the desktop
* session (nix-shell) has no XDG_DATA_DIRS pointing at the icon packages,
* hence the system profile. */
void adoptIconTheme() {
const QStringList known = QStandardPaths::standardLocations(
QStandardPaths::GenericDataLocation);
QStringList paths;
for (const QString &d : known)
paths << d + QStringLiteral("/icons");
paths << QDir::homePath() + QStringLiteral("/.icons");
const QString system = QStringLiteral("/run/current-system/sw/share/icons");
if (!paths.contains(system) && QDir(system).exists())
paths << system;
paths << QStringLiteral(":/icons");
QIcon::setThemeSearchPaths(paths);
QStringList candidates;
if (!QIcon::themeName().isEmpty())
candidates << QIcon::themeName();
candidates << QStringLiteral("breeze-dark") << QStringLiteral("breeze")
<< QStringLiteral("hicolor");
for (const QString &name : candidates) {
QIcon::setThemeName(name);
if (QIcon::hasThemeIcon(QLatin1String(kProbe)))
return;
}
}
/* The bundled set: app/icons/*.svg, compiled in as :/cst-icons/<name>.svg by
* app/CMakeLists.txt. A machine without an icon theme -- Android, where the
* Qt theme lookup finds nothing and QML was left showing its glyph fallback,
* which the phone fonts cannot draw -- gets the same icons from here. */
QIcon bundledIcon(const QString &name) {
return QIcon(QStringLiteral(":/cst-icons/") + name + QStringLiteral(".svg"));
}
QIcon iconForName(const QString &name) {
const QIcon themed = QIcon::fromTheme(name);
if (!themed.isNull())
return themed;
return bundledIcon(name);
}
/* "?color=%23eff0f1": the tint, and nothing else so far. */
QColor colorOption(const QString &id, QString *name) {
QColor color;
*name = id;
const int q = id.indexOf(QLatin1Char('?'));
if (q < 0)
return color;
*name = id.left(q);
const QStringList options = id.mid(q + 1).split(QLatin1Char('&'));
for (const QString &opt : options) {
if (!opt.startsWith(QLatin1String("color=")))
continue;
const QColor c(QUrl::fromPercentEncoding(opt.mid(6).toUtf8()));
if (c.isValid())
color = c;
}
return color;
}
qreal targetRatio() {
if (const QScreen *s = QGuiApplication::primaryScreen())
return qMax<qreal>(1.0, s->devicePixelRatio());
return 1.0;
}
} // namespace
ThemeIconProvider::ThemeIconProvider()
: QQuickImageProvider(QQuickImageProvider::Image) {}
QImage ThemeIconProvider::requestImage(const QString &id, QSize *size,
const QSize &requestedSize) {
QString name;
const QColor color = colorOption(id, &name);
if (name.isEmpty())
return QImage();
const QIcon icon = iconForName(name);
if (icon.isNull())
return QImage(); /* QML shows its fallback glyph */
const int px = requestedSize.isValid() && requestedSize.width() > 0
? requestedSize.width()
: 22;
const qreal ratio = targetRatio();
QPixmap pm = icon.pixmap(QSize(px, px), ratio, QIcon::Normal, QIcon::Off);
if (pm.isNull())
return QImage();
if (color.isValid()) {
QPainter p(&pm);
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
p.fillRect(pm.rect(), color);
}
QImage img = pm.toImage();
img.setDevicePixelRatio(ratio);
if (size)
*size = QSize(px, px);
return img;
}
void installThemeIcons(QQmlApplicationEngine *engine) {
adoptIconTheme();
if (engine)
engine->addImageProvider(QStringLiteral("csttheme"), new ThemeIconProvider);
}
QString iconThemeName() { return QIcon::themeName(); }
bool themeIconAvailable(const QString &name) { return !iconForName(name).isNull(); }
} // namespace app