Start the Qt Quick rewrite of the launcher in qt/

A QML UI with feature parity as the goal: home grid that fills the screen with
dense packing and pinned cells, drag to reorder or into an empty cell, widget
resize handles snapping to the grid, the app drawer with search and long-press
menu, sheets, and the widget picker. HomeActivity (a QtActivity) keeps the
Android parts and hosts native widget views over the QML slots, as the WebView
shell did. A desktop backend serves the demo data, so qt/tests/e2e.cpp drives
the UI end to end offscreen; CI runs it and builds an APK that installs next to
the current launcher as ultra Home (Qt).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiQsCq11CwCPnKPp9D
This commit is contained in:
Claude committed 2026-11-02 04:40:25 +00:00
1 parent a880573afb
commit 74ddcfeab6
33 files changed
+3680

No files matched your search

+244
View File
@@ -0,0 +1,244 @@
#include "backend.h"
#include <QFile>
#include <QGuiApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QScreen>
#include <QSettings>
#include <QTimer>
#include <cmath>
#ifdef Q_OS_ANDROID
#include <QJniEnvironment>
#include <QJniObject>
#endif
namespace {
Backend *g_backend = nullptr;
#ifdef Q_OS_ANDROID
constexpr char kActivity[] = "dev/ultra/launcher/HomeActivity";
QString callString(const char *method) {
return QJniObject::callStaticObjectMethod(kActivity, method, "()Ljava/lang/String;").toString();
}
void callWithString(const char *method, const QString &s) {
QJniObject::callStaticMethod<void>(kActivity, method, "(Ljava/lang/String;)V",
QJniObject::fromString(s).object<jstring>());
}
void callVoid(const char *method) {
QJniObject::callStaticMethod<void>(kActivity, method, "()V");
}
// Called by HomeActivity on Android's main thread: hop over to Qt's.
template <typename F>
void post(F f) {
if (g_backend) QMetaObject::invokeMethod(g_backend, f, Qt::QueuedConnection);
}
void jAppsChanged(JNIEnv *, jclass) { post([] { emit g_backend->appsChanged(); }); }
void jHomePressed(JNIEnv *, jclass) { post([] { emit g_backend->homePressed(); }); }
void jBackPressed(JNIEnv *, jclass) { post([] { emit g_backend->backPressed(); }); }
void jInsets(JNIEnv *, jclass, jint top, jint bottom) { post([=] { g_backend->setInsets(top, bottom); }); }
void jWidgetAdded(JNIEnv *, jclass, jint id) { post([=] { emit g_backend->widgetAdded(id); }); }
void jWidgetMenu(JNIEnv *, jclass, jint id) { post([=] { emit g_backend->widgetMenu(id); }); }
void jWidgetGrab(JNIEnv *, jclass, jint id) { post([=] { emit g_backend->widgetGrab(id); }); }
void jWidgetDrag(JNIEnv *, jclass, jint x, jint y) {
post([=] {
const qreal r = QGuiApplication::primaryScreen()->devicePixelRatio();
emit g_backend->widgetDrag(x / r, y / r);
});
}
void jWidgetDrop(JNIEnv *, jclass, jboolean moved) { post([=] { emit g_backend->widgetDrop(moved); }); }
void registerNatives() {
static bool done = false;
if (done) return;
done = true;
const JNINativeMethod methods[] = {
{"appsChanged", "()V", reinterpret_cast<void *>(jAppsChanged)},
{"homePressed", "()V", reinterpret_cast<void *>(jHomePressed)},
{"backPressed", "()V", reinterpret_cast<void *>(jBackPressed)},
{"insets", "(II)V", reinterpret_cast<void *>(jInsets)},
{"widgetAdded", "(I)V", reinterpret_cast<void *>(jWidgetAdded)},
{"widgetMenu", "(I)V", reinterpret_cast<void *>(jWidgetMenu)},
{"widgetGrab", "(I)V", reinterpret_cast<void *>(jWidgetGrab)},
{"widgetDrag", "(II)V", reinterpret_cast<void *>(jWidgetDrag)},
{"widgetDrop", "(Z)V", reinterpret_cast<void *>(jWidgetDrop)},
};
QJniEnvironment env;
env.registerNativeMethods("dev/ultra/launcher/Native", methods, int(std::size(methods)));
}
#endif
QJsonDocument demo(const QString &name) {
QFile f(QStringLiteral(":/qt/qml/Launcher/demo/") + name);
if (!f.open(QIODevice::ReadOnly)) return {};
return QJsonDocument::fromJson(f.readAll());
}
} // namespace
Backend::Backend(QObject *parent) : QObject(parent) { g_backend = this; }
Backend *Backend::instance() { return g_backend; }
bool Backend::isAndroid() const {
#ifdef Q_OS_ANDROID
return true;
#else
return false;
#endif
}
qreal Backend::dpr() const {
QScreen *s = QGuiApplication::primaryScreen();
return s ? s->devicePixelRatio() : 1.0;
}
void Backend::setInsets(int top, int bottom) {
m_insetTop = top / dpr();
m_insetBottom = bottom / dpr();
emit insetsChanged();
}
void Backend::ready() {
#ifdef Q_OS_ANDROID
registerNatives();
callVoid("ready");
#endif
}
QVariantList Backend::apps() const {
#ifdef Q_OS_ANDROID
const QJsonDocument doc = QJsonDocument::fromJson(callString("apps").toUtf8());
#else
const QJsonDocument doc = demo(QStringLiteral("apps.json"));
#endif
QVariantList out;
for (const QJsonValue &v : doc.object().value(QStringLiteral("apps")).toArray()) {
const QJsonObject o = v.toObject();
out.append(QVariantMap{{"label", o.value("label").toString()},
{"component", o.value("component").toString()},
{"system", o.value("system").toBool()}});
}
return out;
}
QVariantList Backend::widgetProviders() const {
#ifdef Q_OS_ANDROID
const QJsonDocument doc = QJsonDocument::fromJson(callString("widgetProviders").toUtf8());
#else
const QJsonDocument doc = demo(QStringLiteral("widgets.json"));
#endif
QVariantList out;
for (const QJsonValue &v : doc.object().value(QStringLiteral("widgets")).toArray()) {
const QJsonObject o = v.toObject();
out.append(QVariantMap{{"label", o.value("label").toString()},
{"app", o.value("app").toString()},
{"provider", o.value("provider").toString()},
{"h", o.value("h").toInt(120)}});
}
return out;
}
#ifdef Q_OS_ANDROID
void Backend::launch(const QString &c) { callWithString("launch", c); }
void Backend::appInfo(const QString &c) { callWithString("appInfo", c); }
void Backend::uninstall(const QString &c) { callWithString("uninstall", c); }
void Backend::openSettings() { callVoid("openSettings"); }
void Backend::pickWallpaper() { callVoid("pickWallpaper"); }
void Backend::openRecents() { callVoid("openRecents"); }
void Backend::checkUpdates() { callVoid("checkUpdates"); }
void Backend::haptic() { callVoid("haptic"); }
void Backend::addWidget(const QString &p) { callWithString("addWidget", p); }
void Backend::removeWidget(int id) { QJniObject::callStaticMethod<void>(kActivity, "removeWidget", "(I)V", jint(id)); }
#else
void Backend::launch(const QString &c) { emit toast("Open " + c); }
void Backend::appInfo(const QString &c) { emit toast("App info for " + c); }
void Backend::uninstall(const QString &c) { emit toast("Uninstall " + c); }
void Backend::openSettings() { emit toast("Open system settings"); }
void Backend::pickWallpaper() { emit toast("Pick a wallpaper"); }
void Backend::openRecents() { emit toast("Show open windows"); }
void Backend::checkUpdates() { emit toast("Check for updates"); }
void Backend::haptic() {}
void Backend::addWidget(const QString &) {
const int id = m_demoWidget++;
QTimer::singleShot(0, this, [this, id] { emit widgetAdded(id); });
}
void Backend::removeWidget(int) {}
#endif
void Backend::placeWidgets(const QVariantList &list) {
#ifdef Q_OS_ANDROID
const qreal r = dpr();
QJsonArray out;
for (const QVariant &v : list) {
const QVariantMap m = v.toMap();
const qreal x = m.value("x").toReal() * r, y = m.value("y").toReal() * r;
const qreal w = m.value("w").toReal() * r, h = m.value("h").toReal() * r;
out.append(QJsonArray{m.value("id").toInt(), qRound(x), qRound(y), qRound(x + w) - qRound(x),
qRound(y + h) - qRound(y), m.value("editing").toBool() ? 1 : 0});
}
callWithString("placeWidgets", QString::fromUtf8(QJsonDocument(out).toJson(QJsonDocument::Compact)));
#else
Q_UNUSED(list);
#endif
}
void Backend::coverWidgets(qreal y) {
#ifdef Q_OS_ANDROID
const int py = y <= 0 ? int(y) : int(std::ceil(y * dpr()));
QJniObject::callStaticMethod<void>(kActivity, "coverWidgets", "(I)V", jint(py));
#else
Q_UNUSED(y);
#endif
}
QVariantMap Backend::widgetLimits(int id) const {
#ifdef Q_OS_ANDROID
const QJsonObject o = QJsonDocument::fromJson(
QJniObject::callStaticObjectMethod(kActivity, "widgetLimits", "(I)Ljava/lang/String;", jint(id))
.toString().toUtf8()).object();
const qreal r = dpr();
QVariantMap m;
for (const char *k : {"w", "minW", "minH", "maxW", "maxH"}) {
if (o.contains(k)) m.insert(k, o.value(k).toDouble() / r);
}
for (const char *k : {"h", "v"}) {
if (o.contains(k)) m.insert(k, o.value(k).toBool());
}
return m;
#else
Q_UNUSED(id);
return {{"minW", 120}, {"minH", 80}, {"maxW", 0}, {"maxH", 0}, {"h", true}, {"v", true}};
#endif
}
void Backend::excludeGestures(const QVariantList &rects) {
#ifdef Q_OS_ANDROID
const qreal r = dpr();
QJsonArray out;
for (const QVariant &v : rects) {
const QVariantMap m = v.toMap();
out.append(QJsonArray{qRound(m.value("x").toReal() * r), qRound(m.value("y").toReal() * r),
qRound(m.value("w").toReal() * r), qRound(m.value("h").toReal() * r)});
}
callWithString("excludeGestures", QString::fromUtf8(QJsonDocument(out).toJson(QJsonDocument::Compact)));
#else
Q_UNUSED(rects);
#endif
}
QString Backend::loadHome() const {
QSettings s;
return s.value(QStringLiteral("home")).toString();
}
void Backend::saveHome(const QString &json) {
QSettings s;
s.setValue(QStringLiteral("home"), json);
}
+80
View File
@@ -0,0 +1,80 @@
#pragma once
#include <QObject>
#include <QString>
#include <QVariantList>
#include <QVariantMap>
#include <QtQml/qqmlregistration.h>
// The QML app's window onto Android (HomeActivity.java). On a phone each call goes
// through JNI; on a desktop the backend serves demo data from qt/demo and turns the
// actions into toasts, so the UI can be tried and tested without a device.
//
// Geometry in and out of QML is in logical (QML) px; the backend scales it to the
// physical screen px HomeActivity works in.
class Backend : public QObject {
Q_OBJECT
Q_PROPERTY(bool android READ isAndroid CONSTANT)
Q_PROPERTY(qreal insetTop READ insetTop NOTIFY insetsChanged)
Q_PROPERTY(qreal insetBottom READ insetBottom NOTIFY insetsChanged)
public:
explicit Backend(QObject *parent = nullptr);
static Backend *instance();
bool isAndroid() const;
qreal insetTop() const { return m_insetTop; }
qreal insetBottom() const { return m_insetBottom; }
// Lists: [{label, component, system}] and [{label, app, provider, h}].
Q_INVOKABLE QVariantList apps() const;
Q_INVOKABLE QVariantList widgetProviders() const;
Q_INVOKABLE void launch(const QString &component);
Q_INVOKABLE void appInfo(const QString &component);
Q_INVOKABLE void uninstall(const QString &component);
Q_INVOKABLE void openSettings();
Q_INVOKABLE void pickWallpaper();
Q_INVOKABLE void openRecents();
Q_INVOKABLE void checkUpdates();
Q_INVOKABLE void haptic();
// Widgets: addWidget answers with widgetAdded(id) once bound (and configured).
Q_INVOKABLE void addWidget(const QString &provider);
Q_INVOKABLE void removeWidget(int id);
// [{id, x, y, w, h, editing}] in global logical px.
Q_INVOKABLE void placeWidgets(const QVariantList &list);
// The drawer covers the screen down to global logical y; -1 all of it, 0 none.
Q_INVOKABLE void coverWidgets(qreal y);
// {w, minW, minH, maxW, maxH (0: no limit), h, v} in logical px.
Q_INVOKABLE QVariantMap widgetLimits(int id) const;
// [{x, y, w, h}] in global logical px: no system back swipe there.
Q_INVOKABLE void excludeGestures(const QVariantList &rects);
// The home layout, as JSON, kept in the app's settings.
Q_INVOKABLE QString loadHome() const;
Q_INVOKABLE void saveHome(const QString &json);
// Tell HomeActivity QML is up (it then adds the widget layer and starts calling back).
Q_INVOKABLE void ready();
// From HomeActivity (on Android's thread; queued to Qt's).
void setInsets(int top, int bottom);
signals:
void insetsChanged();
void appsChanged();
void homePressed();
void backPressed();
void widgetAdded(int id);
void widgetMenu(int id);
void widgetGrab(int id);
void widgetDrag(qreal x, qreal y); // global logical px
void widgetDrop(bool moved);
void toast(const QString &text); // desktop stand-in for an action
private:
qreal dpr() const;
qreal m_insetTop = 0, m_insetBottom = 0;
mutable int m_demoWidget = 100;
};
+53
View File
@@ -0,0 +1,53 @@
#include "iconprovider.h"
#include <QColor>
#include <QPainter>
#include <QUrl>
#ifdef Q_OS_ANDROID
#include <QJniEnvironment>
#include <QJniObject>
#endif
QImage IconProvider::requestImage(const QString &id, QSize *size, const QSize &requested) {
QString key = id;
const int q = key.lastIndexOf(QLatin1Char('?'));
if (q >= 0) key.truncate(q);
key = QUrl::fromPercentEncoding(key.toUtf8());
const int px = requested.width() > 0 ? requested.width() : 144;
QImage img;
#ifdef Q_OS_ANDROID
QJniObject png = QJniObject::callStaticObjectMethod(
"dev/ultra/launcher/HomeActivity", "icon", "(Ljava/lang/String;I)[B",
QJniObject::fromString(key).object<jstring>(), jint(px));
if (png.isValid()) {
QJniEnvironment env;
auto arr = png.object<jbyteArray>();
const jsize n = env->GetArrayLength(arr);
QByteArray bytes(n, Qt::Uninitialized);
env->GetByteArrayRegion(arr, 0, n, reinterpret_cast<jbyte *>(bytes.data()));
img.loadFromData(bytes, "PNG");
}
#else
// A disc in a colour picked from the key, with the name's first letter.
img = QImage(px, px, QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
QPainter p(&img);
p.setRenderHint(QPainter::Antialiasing);
const QColor c = QColor::fromHsv(int(qHash(key) % 360), 160, 220);
const bool widget = key.startsWith(QLatin1String("w:"));
p.setBrush(c);
p.setPen(Qt::NoPen);
if (widget) p.drawRoundedRect(QRectF(0, 0, px, px), px / 4.0, px / 4.0);
else p.drawEllipse(QRectF(0, 0, px, px));
QString name = key.mid(2).section(QLatin1Char('/'), 0, 0).section(QLatin1Char('.'), -1);
p.setPen(Qt::white);
QFont f;
f.setPixelSize(px * 5 / 12);
f.setBold(true);
p.setFont(f);
p.drawText(QRectF(0, 0, px, px), Qt::AlignCenter, name.left(1).toUpper());
#endif
if (size) *size = img.size();
return img;
}
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include <QQuickImageProvider>
// image://icon/a:COMPONENT is an app's icon, image://icon/w:PROVIDER a widget's.
// On a phone HomeActivity draws them; on a desktop they are lettered discs.
// QML asks with the key percent-encoded and an optional "?n" suffix that only
// busts the pixmap cache after the app list changes.
class IconProvider : public QQuickImageProvider {
public:
IconProvider() : QQuickImageProvider(QQuickImageProvider::Image) {}
QImage requestImage(const QString &id, QSize *size, const QSize &requested) override;
};
+32
View File
@@ -0,0 +1,32 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml/qqmlextensionplugin.h>
#include <QQuickWindow>
#include <QSurfaceFormat>
#include "backend.h"
#include "iconprovider.h"
Q_IMPORT_QML_PLUGIN(LauncherPlugin)
int main(int argc, char *argv[]) {
// The window is see-through: the wallpaper shows behind the home screen.
QQuickWindow::setDefaultAlphaBuffer(true);
QSurfaceFormat fmt = QSurfaceFormat::defaultFormat();
fmt.setAlphaBufferSize(8);
QSurfaceFormat::setDefaultFormat(fmt);
QGuiApplication app(argc, argv);
QGuiApplication::setOrganizationName(QStringLiteral("ultra"));
QGuiApplication::setApplicationName(QStringLiteral("home"));
Backend backend;
QQmlApplicationEngine engine;
engine.addImageProvider(QStringLiteral("icon"), new IconProvider);
qmlRegisterSingletonInstance("Launcher.Host", 1, 0, "Host", &backend);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app,
[] { QCoreApplication::exit(1); }, Qt::QueuedConnection);
engine.loadFromModule("Launcher", "Main");
if (engine.rootObjects().isEmpty()) return 1;
return app.exec();
}