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
@@ -1,6 +1,10 @@
|
||||
package dev.ultra.launcher.wasm;
|
||||
|
||||
import android.app.NativeActivity;
|
||||
import android.appwidget.AppWidgetHost;
|
||||
import android.appwidget.AppWidgetHostView;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProviderInfo;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
@@ -24,12 +28,17 @@ import android.os.Process;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.util.SizeF;
|
||||
import android.view.Gravity;
|
||||
import android.view.HapticFeedbackConstants;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
import android.view.WindowInsets;
|
||||
import android.view.WindowManager;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONArray;
|
||||
@@ -66,6 +75,18 @@ public class LauncherActivity extends NativeActivity {
|
||||
private final Map<String, LauncherActivityInfo> activities = new ConcurrentHashMap<>();
|
||||
private final Object iconLock = new Object();
|
||||
|
||||
private static final int WIDGET_HOST_ID = 369;
|
||||
private static final int REQ_BIND_WIDGET = 101;
|
||||
private static final int REQ_CONFIGURE_WIDGET = 102;
|
||||
private AppWidgetManager widgets;
|
||||
private AppWidgetHost widgetHost;
|
||||
private FrameLayout widgetLayer;
|
||||
private final Map<Integer, AppWidgetHostView> widgetViews = new HashMap<>();
|
||||
private final Map<Integer, Long> widgetSizes = new HashMap<>();
|
||||
private final int[] layerAt = new int[2];
|
||||
private int pendingWidget = -1;
|
||||
private String pendingProvider = "";
|
||||
|
||||
private final LauncherApps.Callback packages = new LauncherApps.Callback() {
|
||||
@Override public void onPackageRemoved(String pkg, UserHandle u) { appsChanged(pkg); }
|
||||
@Override public void onPackageAdded(String pkg, UserHandle u) { appsChanged(pkg); }
|
||||
@@ -84,6 +105,9 @@ public class LauncherActivity extends NativeActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
launcher = getSystemService(LauncherApps.class);
|
||||
launcher.registerCallback(packages, main);
|
||||
widgets = AppWidgetManager.getInstance(this);
|
||||
widgetHost = new AppWidgetHost(this, WIDGET_HOST_ID);
|
||||
widgetLayer = new FrameLayout(this);
|
||||
seeThrough();
|
||||
getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
|
||||
onInsets(insets);
|
||||
@@ -92,6 +116,20 @@ public class LauncherActivity extends NativeActivity {
|
||||
getWindow().getDecorView().requestApplyInsets();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
if (widgetHost != null) widgetHost.startListening();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
if (widgetHost != null) {
|
||||
try { widgetHost.stopListening(); } catch (RuntimeException ignored) { }
|
||||
}
|
||||
}
|
||||
|
||||
private void seeThrough() {
|
||||
Window w = getWindow();
|
||||
w.setFormat(PixelFormat.TRANSLUCENT);
|
||||
@@ -329,4 +367,182 @@ public class LauncherActivity extends NativeActivity {
|
||||
else imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
public String widgetProviders() {
|
||||
JSONArray out = new JSONArray();
|
||||
PackageManager pm = getPackageManager();
|
||||
for (AppWidgetProviderInfo info : widgets.getInstalledProvidersForProfile(user)) {
|
||||
if (info.provider == null) continue;
|
||||
JSONObject o = new JSONObject();
|
||||
try {
|
||||
o.put("provider", info.provider.flattenToString());
|
||||
o.put("label", info.loadLabel(pm));
|
||||
o.put("w", Math.max(1, Math.min(5, (info.minWidth + 69) / 70)));
|
||||
o.put("h", Math.max(1, Math.min(5, (info.minHeight + 69) / 70)));
|
||||
out.put(o);
|
||||
} catch (JSONException ignored) {
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public void addWidget(String provider) {
|
||||
main.post(() -> {
|
||||
ComponentName cn = ComponentName.unflattenFromString(provider);
|
||||
if (cn == null) return;
|
||||
AppWidgetProviderInfo target = null;
|
||||
for (AppWidgetProviderInfo info : widgets.getInstalledProvidersForProfile(user)) {
|
||||
if (cn.equals(info.provider)) { target = info; break; }
|
||||
}
|
||||
if (target == null) return;
|
||||
int id = widgetHost.allocateAppWidgetId();
|
||||
pendingWidget = id;
|
||||
pendingProvider = provider;
|
||||
if (widgets.bindAppWidgetIdIfAllowed(id, user, target.provider, null)) {
|
||||
configureWidget(id);
|
||||
} else {
|
||||
Intent i = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
|
||||
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id)
|
||||
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, target.provider)
|
||||
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, user);
|
||||
try {
|
||||
startActivityForResult(i, REQ_BIND_WIDGET);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
dropPendingWidget();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void configureWidget(int id) {
|
||||
AppWidgetProviderInfo w = widgets.getAppWidgetInfo(id);
|
||||
if (w != null && w.configure != null) {
|
||||
try {
|
||||
widgetHost.startAppWidgetConfigureActivityForResult(this, id, 0, REQ_CONFIGURE_WIDGET, null);
|
||||
return;
|
||||
} catch (ActivityNotFoundException | SecurityException ignored) {
|
||||
}
|
||||
}
|
||||
finishPendingWidget(id);
|
||||
}
|
||||
|
||||
private void finishPendingWidget(int id) {
|
||||
String prov = pendingProvider;
|
||||
pendingWidget = -1;
|
||||
pendingProvider = "";
|
||||
nativeOnEvent("widget-added:" + id + ":" + prov);
|
||||
}
|
||||
|
||||
private void dropPendingWidget() {
|
||||
if (pendingWidget != -1) widgetHost.deleteAppWidgetId(pendingWidget);
|
||||
pendingWidget = -1;
|
||||
pendingProvider = "";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int req, int result, Intent data) {
|
||||
super.onActivityResult(req, result, data);
|
||||
if (pendingWidget == -1 || (req != REQ_BIND_WIDGET && req != REQ_CONFIGURE_WIDGET)) return;
|
||||
if (result != RESULT_OK) {
|
||||
dropPendingWidget();
|
||||
} else if (req == REQ_BIND_WIDGET) {
|
||||
configureWidget(pendingWidget);
|
||||
} else {
|
||||
finishPendingWidget(pendingWidget);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean widgetsVisible = true;
|
||||
private String lastPlacedWidgetsJson = "[]";
|
||||
|
||||
public void removeWidget(int id) {
|
||||
main.post(() -> {
|
||||
AppWidgetHostView v = widgetViews.remove(id);
|
||||
if (v != null && v.isAttachedToWindow()) {
|
||||
try { getWindowManager().removeViewImmediate(v); } catch (RuntimeException ignored) { }
|
||||
}
|
||||
widgetSizes.remove(id);
|
||||
widgetHost.deleteAppWidgetId(id);
|
||||
});
|
||||
}
|
||||
|
||||
public void setWidgetsVisible(boolean visible) {
|
||||
main.post(() -> {
|
||||
widgetsVisible = visible;
|
||||
for (AppWidgetHostView v : widgetViews.values()) {
|
||||
v.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void placeWidgets(String json) {
|
||||
main.post(() -> {
|
||||
lastPlacedWidgetsJson = json;
|
||||
if (getWindow().getDecorView().getWindowToken() == null) {
|
||||
getWindow().getDecorView().post(() -> placeWidgets(lastPlacedWidgetsJson));
|
||||
return;
|
||||
}
|
||||
float d = getResources().getDisplayMetrics().density;
|
||||
Map<Integer, int[]> want = new HashMap<>();
|
||||
try {
|
||||
JSONArray arr = new JSONArray(json);
|
||||
for (int i = 0; i < arr.length(); i++) {
|
||||
JSONArray r = arr.getJSONArray(i);
|
||||
want.put(r.getInt(0), new int[] {
|
||||
r.getInt(1),
|
||||
r.getInt(2),
|
||||
r.getInt(3),
|
||||
r.getInt(4)
|
||||
});
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
return;
|
||||
}
|
||||
WindowManager wm = getWindowManager();
|
||||
for (Map.Entry<Integer, AppWidgetHostView> e : widgetViews.entrySet()) {
|
||||
if (!want.containsKey(e.getKey())) e.getValue().setVisibility(View.GONE);
|
||||
}
|
||||
for (Map.Entry<Integer, int[]> e : want.entrySet()) {
|
||||
int id = e.getKey();
|
||||
int[] r = e.getValue();
|
||||
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
|
||||
r[2],
|
||||
r[3],
|
||||
WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
|
||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
||||
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
|
||||
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
|
||||
PixelFormat.TRANSLUCENT);
|
||||
lp.gravity = Gravity.TOP | Gravity.LEFT;
|
||||
lp.x = r[0];
|
||||
lp.y = r[1];
|
||||
lp.token = getWindow().getDecorView().getWindowToken();
|
||||
|
||||
AppWidgetHostView v = widgetViews.get(id);
|
||||
if (v == null) {
|
||||
AppWidgetProviderInfo info = widgets.getAppWidgetInfo(id);
|
||||
if (info == null) continue;
|
||||
v = widgetHost.createView(this, id, info);
|
||||
widgetViews.put(id, v);
|
||||
v.setVisibility(widgetsVisible ? View.VISIBLE : View.GONE);
|
||||
wm.addView(v, lp);
|
||||
} else {
|
||||
v.setVisibility(widgetsVisible ? View.VISIBLE : View.GONE);
|
||||
wm.updateViewLayout(v, lp);
|
||||
}
|
||||
int wdp = Math.round(r[2] / d), hdp = Math.round(r[3] / d);
|
||||
long size = ((long) wdp << 32) | hdp;
|
||||
Long was = widgetSizes.put(id, size);
|
||||
if (was == null || was != size) {
|
||||
if (Build.VERSION.SDK_INT >= 31) {
|
||||
List<SizeF> sizes = new ArrayList<>();
|
||||
sizes.add(new SizeF(wdp, hdp));
|
||||
v.updateAppWidgetSize(new Bundle(), sizes);
|
||||
} else {
|
||||
v.updateAppWidgetSize(new Bundle(), wdp, hdp, wdp, hdp);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -223,6 +223,37 @@ impl ultra::launcher::android::Host for HostState {
|
||||
}
|
||||
out
|
||||
}
|
||||
fn widget_providers(&mut self) -> String {
|
||||
let Ok(mut env) = self.vm.attach_current_thread() else {
|
||||
return "[]".to_owned();
|
||||
};
|
||||
let Ok(res) = env.call_method(&self.activity, "widgetProviders", "()Ljava/lang/String;", &[]) else {
|
||||
return "[]".to_owned();
|
||||
};
|
||||
let Ok(obj) = res.l() else {
|
||||
return "[]".to_owned();
|
||||
};
|
||||
let js: JString = obj.into();
|
||||
env.get_string(&js)
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|_| "[]".to_owned())
|
||||
}
|
||||
fn add_widget(&mut self, provider: String) {
|
||||
self.call_void_str("addWidget", &provider);
|
||||
}
|
||||
fn remove_widget(&mut self, id: i32) {
|
||||
self.call_void("removeWidget", "(I)V", &[JValue::Int(id as jint)]);
|
||||
}
|
||||
fn place_widgets(&mut self, json: String) {
|
||||
self.call_void_str("placeWidgets", &json);
|
||||
}
|
||||
fn set_widgets_visible(&mut self, visible: bool) {
|
||||
self.call_void(
|
||||
"setWidgetsVisible",
|
||||
"(Z)V",
|
||||
&[JValue::Bool(if visible { 1 } else { 0 })],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
struct FrameBuf {
|
||||
|
||||
+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
|
||||
@@ -14,8 +14,16 @@ interface android {
|
||||
haptic: func();
|
||||
/// System bar insets in physical screen pixels: (top, bottom).
|
||||
insets: func() -> tuple<u32, u32>;
|
||||
/// Drain queued host events ("home", "back", "insets", "apps-changed:<pkgs>").
|
||||
/// Drain queued host events ("home", "back", "insets", "apps-changed:<pkgs>", "widget-added:<id>:<provider>").
|
||||
poll-events: func() -> list<string>;
|
||||
/// JSON array of {"provider": string, "label": string, "w": u32, "h": u32}.
|
||||
widget-providers: func() -> string;
|
||||
/// Bind a widget for provider; emits "widget-added:<id>:<provider>".
|
||||
add-widget: func(provider: string);
|
||||
remove-widget: func(id: s32);
|
||||
/// Place live widget views: JSON [[id, x, y, w, h], ...] in physical screen px.
|
||||
place-widgets: func(json: string);
|
||||
set-widgets-visible: func(visible: bool);
|
||||
}
|
||||
|
||||
world gui {
|
||||
|
||||
Reference in new issue
Block a user