Files
launcher/qt/qml/Layout.js
T
Claude 70fb8ee633 Qt: bring over the WebView build's home screen
The WebView build saved its home in its page's localStorage, which stays in
the app's data now that the Qt build is dev.ultra.launcher. On first start
HomeActivity reads the ultra.home.* keys through a throwaway WebView on the
same origin and hands them to QML: apps by component, widgets with their px
heights turned into rows, pins kept when the Qt home is still empty. The
widget host id goes back to the WebView build's, so its widgets stay bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiQsCq11CwCPnKPp9D
2026-11-02 05:49:32 +00:00

162 lines
5.8 KiB
JavaScript

.pragma library
// The home grid, as plain functions over the home list so they can be tested on their
// own (tests/layout.test.mjs). A slot is
// {kind: "app", str: COMPONENT, x, y}
// {kind: "widget", str: LABEL, wid: HOST_ID, w: COLUMNS (0: whole row), rows, x, y}
// where x, y is the cell it was dropped into (column, row from 0), or -1, -1: placed in
// order into the first free cells that fit it ("dense" packing, so apps fill the cells
// beside a widget and the grid has no holes).
var CELL = 64; // row height, px
var RGAP = 8; // gap between rows
var CGAP = 8; // gap between columns
// Rows of app tiles nearest to a widget height in px, at least one.
function widgetRows(px) {
return Math.max(1, Math.floor((px + RGAP + (CELL + RGAP) / 2) / (CELL + RGAP)));
}
function slotW(s, cols) {
if (s.kind !== "widget") return 1;
return (s.w <= 0 || s.w > cols) ? cols : s.w;
}
function slotH(s) {
return s.kind === "widget" ? Math.max(1, s.rows | 0) : 1;
}
function pinned(s, cols) {
return s.x >= 0 && s.y >= 0 && s.x + slotW(s, cols) <= cols;
}
// Where each slot goes: [{col, row, w, h}] (null for a hidden slot), and the rows used,
// at least nrows (the rows that fill the screen).
function pack(home, cols, nrows, hidden) {
var occ = [];
function taken(c, r) { return occ[r] !== undefined && occ[r][c] === true; }
function fits(c, r, w, h) {
if (c < 0 || c + w > cols) return false;
for (var y = r; y < r + h; y++)
for (var x = c; x < c + w; x++)
if (taken(x, y)) return false;
return true;
}
function mark(c, r, w, h) {
for (var y = r; y < r + h; y++) {
if (occ[y] === undefined) occ[y] = [];
for (var x = c; x < c + w; x++) occ[y][x] = true;
}
}
var cells = new Array(home.length);
var rows = Math.max(0, nrows | 0);
var i, s, w, h;
// Pinned slots first, where they were put (they may overlap each other).
for (i = 0; i < home.length; i++) {
s = home[i];
cells[i] = null;
if ((hidden && hidden[i]) || !pinned(s, cols)) continue;
w = slotW(s, cols); h = slotH(s);
cells[i] = {col: s.x, row: s.y, w: w, h: h};
mark(s.x, s.y, w, h);
rows = Math.max(rows, s.y + h);
}
// The rest in order, each into the first free cells that fit it.
for (i = 0; i < home.length; i++) {
s = home[i];
if ((hidden && hidden[i]) || cells[i] !== null) continue;
w = slotW(s, cols); h = slotH(s);
for (var r = 0; cells[i] === null; r++) {
for (var c = 0; c + w <= cols; c++) {
if (fits(c, r, w, h)) {
cells[i] = {col: c, row: r, w: w, h: h};
mark(c, r, w, h);
rows = Math.max(rows, r + h);
break;
}
}
}
}
return {cells: cells, rows: rows};
}
// A cell's box in px, given the column width.
function box(cell, colW) {
return {x: cell.col * (colW + CGAP), y: cell.row * (CELL + RGAP),
w: cell.w * colW + (cell.w - 1) * CGAP, h: cell.h * CELL + (cell.h - 1) * RGAP};
}
// The cell nearest to the top left of a box at x, y (px in the grid).
function cellAt(x, y, colW) {
return [Math.max(0, Math.round(x / (colW + CGAP))), Math.max(0, Math.round(y / (CELL + RGAP)))];
}
// Slot i pinned with its top left at col, row (kept on the screen's grid).
function pin(home, i, col, row, cols, nrows) {
var s = home[i];
var x = col, y = row;
if (x + slotW(s, cols) > cols) x = cols - slotW(s, cols);
if (nrows > 0 && y + slotH(s) > nrows) y = nrows - slotH(s);
s.x = Math.max(0, x);
s.y = Math.max(0, y);
}
// Unpin the slots under pinned slot i, so they flow into free cells.
function clearCells(home, i, cols) {
var a = home[i];
for (var j = 0; j < home.length; j++) {
var b = home[j];
if (j === i || b.x < 0) continue;
if (b.x < a.x + slotW(a, cols) && a.x < b.x + slotW(b, cols)
&& b.y < a.y + slotH(a) && a.y < b.y + slotH(b)) {
b.x = -1;
b.y = -1;
}
}
}
// Take slot a out and put it back at b, shifting the slots between.
function move(home, a, b) {
var s = home.splice(a, 1)[0];
home.splice(b, 0, s);
}
// Resize steps: the number of cells (of size px, gap apart) nearest to want px, kept
// within [min, max] px where it can be (max 0: no limit), and within [lo, hi] cells.
function span(k, size, gap) { return k * size + (k - 1) * gap; }
function fit(want, lo, hi, size, gap, min, max) {
var k = Math.round((want + gap) / (size + gap));
while (k < hi && span(k, size, gap) < min - 8) k++;
while (k > lo && max > 0 && span(k, size, gap) > max + 8) k--;
return Math.max(lo, Math.min(hi, k));
}
// Columns a new widget of natural width px starts at (0: the whole row).
function widgetCols(px, colW, cols) {
if (!px) return 0;
var k = Math.max(1, Math.ceil((px + CGAP - 8) / (colW + CGAP)));
return k >= cols ? 0 : k;
}
// The WebView build's saved home (its ultra.home.* localStorage keys) as Qt slots.
// Widget heights were px; they become whole rows.
function legacy(kv) {
var out = [];
var n = parseInt(kv["ultra.home.n"], 10) || 0;
var num = function (k, i, d) {
var v = parseInt(kv["ultra.home." + k + "." + i], 10);
return isNaN(v) ? d : v;
};
for (var i = 0; i < n; i++) {
var str = kv["ultra.home.str." + i];
if (typeof str !== "string" || !str) continue;
var x = num("cx", i, -1), y = num("cy", i, -1);
if (num("kind", i, 0) === 1)
out.push({kind: "widget", str: str, wid: num("wid", i, 0), w: Math.max(0, num("w", i, 0)),
rows: widgetRows(num("h", i, 120)), x: x, y: y});
else
out.push({kind: "app", str: str, x: x, y: y});
}
return out;
}