Add AppWidgetHost support, translucent wallpaper compositing, and home layout parity
This commit is contained in:
1 parent
72b0d7d896
commit
f5c3b8ac60
7 files changed
+476
-43
No files matched your search
+157
-14
@@ -46,8 +46,11 @@ LauncherWindow::LauncherWindow() {
|
||||
resize(412, 892);
|
||||
|
||||
refreshInsets();
|
||||
loadHome();
|
||||
refreshApps();
|
||||
loadHome();
|
||||
if (m_home.isEmpty() && !QFile::exists(QStringLiteral("/data/home.json"))) {
|
||||
seedDefaultHome();
|
||||
}
|
||||
m_pollTimer.start(100, this);
|
||||
}
|
||||
|
||||
@@ -87,6 +90,25 @@ void LauncherWindow::refreshApps() {
|
||||
update();
|
||||
}
|
||||
|
||||
void LauncherWindow::refreshWidgetProviders() {
|
||||
gui_string_t raw{};
|
||||
ultra_launcher_android_widget_providers(&raw);
|
||||
QByteArray json = fromWitBytes(raw);
|
||||
gui_string_free(&raw);
|
||||
|
||||
m_providers.clear();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json);
|
||||
for (const QJsonValue &v : doc.array()) {
|
||||
QJsonObject o = v.toObject();
|
||||
WidgetProviderInfo info;
|
||||
info.provider = o.value(QStringLiteral("provider")).toString();
|
||||
info.label = o.value(QStringLiteral("label")).toString();
|
||||
info.w = std::max(1, o.value(QStringLiteral("w")).toInt(2));
|
||||
info.h = std::max(1, o.value(QStringLiteral("h")).toInt(2));
|
||||
if (!info.provider.isEmpty()) m_providers.append(info);
|
||||
}
|
||||
}
|
||||
|
||||
void LauncherWindow::loadHome() {
|
||||
QFile f(QStringLiteral("/data/home.json"));
|
||||
if (!f.open(QIODevice::ReadOnly)) return;
|
||||
@@ -95,26 +117,97 @@ void LauncherWindow::loadHome() {
|
||||
m_home.clear();
|
||||
for (const QJsonValue &v : doc.array()) {
|
||||
QJsonObject o = v.toObject();
|
||||
if (o.value(QStringLiteral("kind")).toString(QStringLiteral("app")) != QLatin1String("app"))
|
||||
continue;
|
||||
QString comp = o.value(QStringLiteral("str")).toString();
|
||||
if (comp.isEmpty()) continue;
|
||||
const QString kind = o.value(QStringLiteral("kind")).toString(QStringLiteral("app"));
|
||||
Layout::HomeSlot slot;
|
||||
slot.component = comp;
|
||||
slot.x = o.value(QStringLiteral("x")).toInt(-1);
|
||||
slot.y = o.value(QStringLiteral("y")).toInt(-1);
|
||||
if (kind == QLatin1String("widget")) {
|
||||
slot.widgetId = o.value(QStringLiteral("id")).toInt(-1);
|
||||
slot.provider = o.value(QStringLiteral("str")).toString();
|
||||
slot.w = std::max(1, o.value(QStringLiteral("w")).toInt(2));
|
||||
slot.h = std::max(1, o.value(QStringLiteral("h")).toInt(2));
|
||||
if (slot.widgetId < 0) continue;
|
||||
} else {
|
||||
slot.component = o.value(QStringLiteral("str")).toString();
|
||||
if (slot.component.isEmpty()) continue;
|
||||
}
|
||||
m_home.append(slot);
|
||||
if (m_home.size() >= 200) break;
|
||||
}
|
||||
}
|
||||
|
||||
void LauncherWindow::seedDefaultHome() {
|
||||
struct SeedApp { const char *label; int x; int y; int nth; };
|
||||
static const SeedApp kSeeds[] = {
|
||||
{"Maps", 0, 0, 0},
|
||||
{"Muse", 1, 0, 0},
|
||||
{"Claude", 2, 0, 0},
|
||||
{"Claude", 3, 0, 0},
|
||||
{"Transit", 4, 0, 0},
|
||||
{"Chrome", 0, 1, 0},
|
||||
{"Coinbase", 1, 1, 0},
|
||||
{"Cash App", 2, 1, 0},
|
||||
{"Webby", 3, 1, 0},
|
||||
{"Amazon Shopping", 4, 1, 0},
|
||||
{"Bluesky", 0, 2, 0},
|
||||
{"HEY Calendar", 1, 2, 0},
|
||||
{"fanchat", 2, 2, 0},
|
||||
{"Files", 0, 3, 0},
|
||||
};
|
||||
for (const SeedApp &seed : kSeeds) {
|
||||
const QString want = QString::fromUtf8(seed.label);
|
||||
QString comp;
|
||||
for (const AppInfo &a : m_apps) {
|
||||
if (a.label.compare(want, Qt::CaseInsensitive) == 0) {
|
||||
comp = a.component;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (comp.isEmpty()) {
|
||||
for (const AppInfo &a : m_apps) {
|
||||
if (a.label.startsWith(want, Qt::CaseInsensitive)) {
|
||||
comp = a.component;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!comp.isEmpty()) {
|
||||
Layout::HomeSlot s;
|
||||
s.component = comp;
|
||||
s.x = seed.x;
|
||||
s.y = seed.y;
|
||||
m_home.append(s);
|
||||
}
|
||||
}
|
||||
saveHome();
|
||||
|
||||
// Also request binding the two widgets from the original home screen.
|
||||
const char *kDefaultWidgets[] = {
|
||||
"com.google.android.apps.weather/com.google.android.apps.weather.widget.CurrentForecastWidgetReceiver",
|
||||
"com.probadosoft.moonphasecalendar/com.probadosoft.moonphasecalendar.AppWidget",
|
||||
};
|
||||
for (const char *p : kDefaultWidgets) {
|
||||
gui_string_t witP = toWitStr(QString::fromUtf8(p));
|
||||
ultra_launcher_android_add_widget(&witP);
|
||||
gui_string_free(&witP);
|
||||
}
|
||||
}
|
||||
|
||||
void LauncherWindow::saveHome() const {
|
||||
QDir().mkpath(QStringLiteral("/data"));
|
||||
QJsonArray arr;
|
||||
for (const auto &s : m_home) {
|
||||
QJsonObject o;
|
||||
o.insert(QStringLiteral("kind"), QStringLiteral("app"));
|
||||
o.insert(QStringLiteral("str"), s.component);
|
||||
if (s.isWidget()) {
|
||||
o.insert(QStringLiteral("kind"), QStringLiteral("widget"));
|
||||
o.insert(QStringLiteral("id"), s.widgetId);
|
||||
o.insert(QStringLiteral("str"), s.provider);
|
||||
o.insert(QStringLiteral("w"), s.w);
|
||||
o.insert(QStringLiteral("h"), s.h);
|
||||
} else {
|
||||
o.insert(QStringLiteral("kind"), QStringLiteral("app"));
|
||||
o.insert(QStringLiteral("str"), s.component);
|
||||
}
|
||||
o.insert(QStringLiteral("x"), s.x);
|
||||
o.insert(QStringLiteral("y"), s.y);
|
||||
arr.append(o);
|
||||
@@ -196,6 +289,28 @@ void LauncherWindow::pollHostEvents() {
|
||||
}
|
||||
}
|
||||
changedApps = true;
|
||||
} else if (ev.startsWith(QLatin1String("widget-added:"))) {
|
||||
const QString rest = ev.mid(13);
|
||||
const int sep = rest.indexOf(QLatin1Char(':'));
|
||||
const int id = (sep >= 0 ? rest.left(sep) : rest).toInt();
|
||||
const QString prov = sep >= 0 ? rest.mid(sep + 1) : QString();
|
||||
if (id >= 0) {
|
||||
Layout::HomeSlot ws;
|
||||
ws.widgetId = id;
|
||||
ws.provider = prov;
|
||||
ws.w = 2;
|
||||
ws.h = 2;
|
||||
if (prov.contains(QLatin1String("CurrentForecastWidgetReceiver"))) {
|
||||
ws.x = 3;
|
||||
ws.y = 2;
|
||||
} else if (prov.contains(QLatin1String("moonphasecalendar"))) {
|
||||
ws.x = 0;
|
||||
ws.y = 5;
|
||||
}
|
||||
m_home.append(ws);
|
||||
saveHome();
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
gui_list_string_free(&evs);
|
||||
@@ -252,7 +367,7 @@ int LauncherWindow::hitHomeSlot(const QPointF &pos) const {
|
||||
const auto packed = Layout::packHome(m_home, homeCols(), hidden);
|
||||
const qreal cw = homeColW();
|
||||
for (int i = 0; i < packed.cells.size(); ++i) {
|
||||
if (packed.cells[i].col < 0) continue;
|
||||
if (packed.cells[i].col < 0 || m_home[i].isWidget()) continue;
|
||||
QRectF r = Layout::cellBox(packed.cells[i], cw);
|
||||
r.translate(gridX(), gridTop() - m_homeScroll);
|
||||
if (r.contains(pos)) return i;
|
||||
@@ -369,7 +484,9 @@ void LauncherWindow::triggerSheetAction(int actionIdx) {
|
||||
if (existing >= 0) {
|
||||
m_home.removeAt(existing);
|
||||
} else if (m_home.size() < 200) {
|
||||
m_home.append({app.component, -1, -1});
|
||||
Layout::HomeSlot slot;
|
||||
slot.component = app.component;
|
||||
m_home.append(slot);
|
||||
}
|
||||
saveHome();
|
||||
update();
|
||||
@@ -400,6 +517,12 @@ void LauncherWindow::paintEvent(QPaintEvent *) {
|
||||
p.fillRect(QRect(QPoint(0, 0), size()), Qt::transparent);
|
||||
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
||||
|
||||
const bool showWidgets = !m_drawerOpen && m_sheet == SheetKind::None;
|
||||
if (showWidgets != m_lastWidgetsVisible) {
|
||||
m_lastWidgetsVisible = showWidgets;
|
||||
ultra_launcher_android_set_widgets_visible(showWidgets);
|
||||
}
|
||||
|
||||
if (m_drawerOpen) drawDrawer(p);
|
||||
else drawHome(p);
|
||||
|
||||
@@ -462,14 +585,34 @@ void LauncherWindow::drawHome(QPainter &p) {
|
||||
QSet<QString> hidden;
|
||||
const auto packed = Layout::packHome(m_home, homeCols(), hidden);
|
||||
const qreal cw = homeColW();
|
||||
const qreal dpr = devicePixelRatio();
|
||||
QJsonArray placedWidgets;
|
||||
for (int i = 0; i < m_home.size(); ++i) {
|
||||
if (i == m_liftedHome || packed.cells[i].col < 0) continue;
|
||||
QRectF r = Layout::cellBox(packed.cells[i], cw);
|
||||
const auto &slot = m_home[i];
|
||||
QRectF r = Layout::cellBox(packed.cells[i], cw, slot.w, slot.h);
|
||||
r.translate(gridX(), gridTop() - m_homeScroll);
|
||||
if (slot.isWidget()) {
|
||||
QJsonArray entry;
|
||||
entry.append(slot.widgetId);
|
||||
entry.append(int(std::lround(r.x() * dpr)));
|
||||
entry.append(int(std::lround(r.y() * dpr)));
|
||||
entry.append(int(std::lround(r.width() * dpr)));
|
||||
entry.append(int(std::lround(r.height() * dpr)));
|
||||
placedWidgets.append(entry);
|
||||
continue;
|
||||
}
|
||||
if (r.bottom() < gridTop() || r.top() > height()) continue;
|
||||
const int appIdx = m_appIndex.value(m_home[i].component, -1);
|
||||
const QString label = appIdx >= 0 ? m_apps[appIdx].label : m_home[i].component.section(QLatin1Char('/'), 0, 0);
|
||||
drawIconAndLabel(p, r, m_home[i].component, label, 52, false);
|
||||
const int appIdx = m_appIndex.value(slot.component, -1);
|
||||
const QString label = appIdx >= 0 ? m_apps[appIdx].label : slot.component.section(QLatin1Char('/'), 0, 0);
|
||||
drawIconAndLabel(p, r, slot.component, label, 52, false);
|
||||
}
|
||||
const QString placedJson = QString::fromUtf8(QJsonDocument(placedWidgets).toJson(QJsonDocument::Compact));
|
||||
if (placedJson != m_lastPlacedWidgets) {
|
||||
m_lastPlacedWidgets = placedJson;
|
||||
gui_string_t witJson = toWitStr(placedJson);
|
||||
ultra_launcher_android_place_widgets(&witJson);
|
||||
gui_string_free(&witJson);
|
||||
}
|
||||
|
||||
if (m_liftedHome >= 0 && m_liftedHome < m_home.size()) {
|
||||
|
||||
@@ -18,6 +18,13 @@ struct AppInfo {
|
||||
bool system = false;
|
||||
};
|
||||
|
||||
struct WidgetProviderInfo {
|
||||
QString provider;
|
||||
QString label;
|
||||
int w = 2;
|
||||
int h = 2;
|
||||
};
|
||||
|
||||
class LauncherWindow : public QRasterWindow {
|
||||
Q_OBJECT
|
||||
|
||||
@@ -35,12 +42,14 @@ protected:
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
|
||||
private:
|
||||
enum class SheetKind { None, Menu, HomeItem, DrawerApp };
|
||||
enum class SheetKind { None, Menu, HomeItem, DrawerApp, Widgets };
|
||||
|
||||
void pollHostEvents();
|
||||
void refreshInsets();
|
||||
void refreshApps();
|
||||
void refreshWidgetProviders();
|
||||
void loadHome();
|
||||
void seedDefaultHome();
|
||||
void saveHome() const;
|
||||
QImage iconFor(const QString &component, int px);
|
||||
|
||||
@@ -82,8 +91,11 @@ private:
|
||||
QVector<AppInfo> m_apps;
|
||||
QHash<QString, int> m_appIndex;
|
||||
QVector<Layout::HomeSlot> m_home;
|
||||
QVector<WidgetProviderInfo> m_providers;
|
||||
QHash<QString, QImage> m_icons;
|
||||
QSet<QString> m_missingIcons;
|
||||
QString m_lastPlacedWidgets;
|
||||
bool m_lastWidgetsVisible = true;
|
||||
|
||||
// UI state
|
||||
bool m_drawerOpen = false;
|
||||
|
||||
+42
-25
@@ -15,52 +15,69 @@ PackedGrid packHome(const QVector<HomeSlot> &items, int cols, const QSet<QString
|
||||
auto ensureRow = [&](int r) {
|
||||
while (static_cast<int>(occ.size()) <= r) occ.emplace_back(cols, false);
|
||||
};
|
||||
auto isFree = [&](int c, int r) -> bool {
|
||||
if (c < 0 || c >= cols || r < 0) return false;
|
||||
if (r >= static_cast<int>(occ.size())) return true;
|
||||
return !occ[r][c];
|
||||
auto isFree = [&](int c, int r, int w, int h) -> bool {
|
||||
if (c < 0 || c + w > cols || r < 0) return false;
|
||||
for (int dr = 0; dr < h; ++dr) {
|
||||
const int rr = r + dr;
|
||||
if (rr >= static_cast<int>(occ.size())) continue;
|
||||
for (int dc = 0; dc < w; ++dc) {
|
||||
if (occ[rr][c + dc]) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
auto occupy = [&](int c, int r) {
|
||||
ensureRow(r);
|
||||
occ[r][c] = true;
|
||||
out.rows = std::max(out.rows, r + 1);
|
||||
auto occupy = [&](int c, int r, int w, int h) {
|
||||
for (int dr = 0; dr < h; ++dr) {
|
||||
const int rr = r + dr;
|
||||
ensureRow(rr);
|
||||
for (int dc = 0; dc < w; ++dc) {
|
||||
occ[rr][c + dc] = true;
|
||||
}
|
||||
out.rows = std::max(out.rows, rr + 1);
|
||||
}
|
||||
};
|
||||
|
||||
// Pass 1: place pinned slots first if their cell is free.
|
||||
// Pass 1: place pinned slots first if their area is free.
|
||||
QVector<bool> placed(items.size(), false);
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
const auto &s = items[i];
|
||||
if (hidden.contains(s.component)) continue;
|
||||
if (s.x >= 0 && s.x < cols && s.y >= 0 && isFree(s.x, s.y)) {
|
||||
occupy(s.x, s.y);
|
||||
if (!s.isWidget() && hidden.contains(s.component)) continue;
|
||||
const int w = std::clamp(s.w, 1, cols);
|
||||
const int h = std::max(1, s.h);
|
||||
if (s.x >= 0 && s.y >= 0 && isFree(s.x, s.y, w, h)) {
|
||||
occupy(s.x, s.y, w, h);
|
||||
out.cells[i] = {s.x, s.y};
|
||||
placed[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 2: pack remaining visible slots into the earliest free cell.
|
||||
int cursorR = 0;
|
||||
int cursorC = 0;
|
||||
// Pass 2: pack remaining visible slots into the earliest free area.
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
if (placed[i] || hidden.contains(items[i].component)) continue;
|
||||
while (!isFree(cursorC, cursorR)) {
|
||||
++cursorC;
|
||||
if (cursorC >= cols) {
|
||||
cursorC = 0;
|
||||
++cursorR;
|
||||
const auto &s = items[i];
|
||||
if (placed[i] || (!s.isWidget() && hidden.contains(s.component))) continue;
|
||||
const int w = std::clamp(s.w, 1, cols);
|
||||
const int h = std::max(1, s.h);
|
||||
int r = 0, c = 0;
|
||||
while (!isFree(c, r, w, h)) {
|
||||
++c;
|
||||
if (c + w > cols) {
|
||||
c = 0;
|
||||
++r;
|
||||
}
|
||||
}
|
||||
occupy(cursorC, cursorR);
|
||||
out.cells[i] = {cursorC, cursorR};
|
||||
occupy(c, r, w, h);
|
||||
out.cells[i] = {c, r};
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
QRectF cellBox(const CellPos &cell, qreal colW) {
|
||||
QRectF cellBox(const CellPos &cell, qreal colW, int spanW, int spanH) {
|
||||
const qreal x = cell.col * (colW + kColGap);
|
||||
const qreal y = cell.row * (kCellH + kRowGap);
|
||||
return QRectF(x, y, colW, kCellH);
|
||||
const qreal w = spanW * colW + std::max(0, spanW - 1) * kColGap;
|
||||
const qreal h = spanH * kCellH + std::max(0, spanH - 1) * kRowGap;
|
||||
return QRectF(x, y, w, h);
|
||||
}
|
||||
|
||||
} // namespace Layout
|
||||
+8
-2
@@ -12,9 +12,15 @@ inline constexpr qreal kColGap = 6.0;
|
||||
inline constexpr qreal kRowGap = 12.0;
|
||||
|
||||
struct HomeSlot {
|
||||
QString component;
|
||||
QString component; // non-empty for app slots
|
||||
int widgetId = -1; // >= 0 for Android AppWidget slots
|
||||
QString provider; // widget provider component
|
||||
int x = -1;
|
||||
int y = -1;
|
||||
int w = 1;
|
||||
int h = 1;
|
||||
|
||||
bool isWidget() const { return widgetId >= 0 || !provider.isEmpty(); }
|
||||
};
|
||||
|
||||
struct CellPos {
|
||||
@@ -28,6 +34,6 @@ struct PackedGrid {
|
||||
};
|
||||
|
||||
PackedGrid packHome(const QVector<HomeSlot> &items, int cols, const QSet<QString> &hidden);
|
||||
QRectF cellBox(const CellPos &cell, qreal colW);
|
||||
QRectF cellBox(const CellPos &cell, qreal colW, int spanW = 1, int spanH = 1);
|
||||
|
||||
} // namespace Layout
|
||||
Reference in new issue
Block a user