Add real-time layout sync and refresh default layout

This commit is contained in:
tongtong committed 2026-11-27 03:16:42 +08:00
1 parent 274b8538b7
commit f79e30897b
20 files changed
+289 -77

No files matched your search

+16 -16
View File
@@ -222,7 +222,7 @@ pre.json{font:11px/1.55 ui-monospace,Menlo,monospace;color:#9fb4d8;
<option value="1080x2400">1080 × 2400</option>
</select>
</div>
<div class="row">safeTop <input type="number" id="safeTop" value="132" step="4"></div>
<div class="row">safeTop <input type="number" id="safeTop" value="72" step="4"></div>
<div class="row">safeBottom <input type="number" id="safeBottom" value="72" step="4"></div>
<div class="row">吸附 12px <input type="checkbox" id="snap" checked></div>
<div class="row">参考线 <input type="checkbox" id="showGuides" checked></div>
@@ -351,10 +351,10 @@ oy = (H − Dh·s) · anchor</div>
* ===========================================================================*/
const LIB = [
{ type:'app_icon', label:'图标', w:132, h:168, payload:{ package:'com.tencent.mm', label:'微信' } },
{ type:'app_icon', label:'相机', w:132, h:168, payload:{ package:'com.android.camera', label:'相机' } },
{ type:'widget', label:'时钟组件', w:864, h:320, payload:{ provider:'com.android.deskclock/.ClockWidget', label:'时钟' } },
{ type:'widget', label:'天气组件', w:420, h:300, payload:{ provider:'com.android.weather/.WeatherWidget', label:'天气' } },
{ type:'widget', label:'音乐组件', w:420, h:300, payload:{ provider:'com.spotify/.MusicWidget', label:'音乐' } },
{ type:'app_icon', label:'相机', w:132, h:168, payload:{ package:'com.android.camera2', label:'相机' } },
{ type:'widget', label:'时钟组件', w:864, h:320, payload:{ provider:'com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider', label:'时钟' } },
{ type:'widget', label:'搜索组件', w:420, h:300, payload:{ provider:'com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider', label:'搜索' } },
{ type:'widget', label:'音乐组件', w:420, h:300, payload:{ provider:'com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall', label:'音乐' } },
{ type:'shortcut', label:'快捷方式', w:132, h:168, payload:{ label:'扫一扫', intent:'weixin://scanqrcode' } },
{ type:'dock_bg', label:'Dock 底板', w:936, h:200, payload:{ radius:56 } },
];
@@ -382,7 +382,7 @@ function newState() {
return {
schemaVersion: 3,
revision: 1,
canvas: { designWidth:1080, designHeight:1920, safeTop:132, safeBottom:72 },
canvas: { designWidth:1080, designHeight:1920, safeTop:72, safeBottom:72 },
fit: { mode:'contain', anchorY:0.5 },
pages: [{ index:0, background:{ type:'wallpaper' }, items: demoItems() }],
};
@@ -393,22 +393,22 @@ function demoItems() {
const colX = c => pad + c * colW + (colW - icon) / 2;
const out = [];
out.push({ id:'w_clock', type:'widget', x:pad, y:196, w:864, h:320, z:1,
payload:{ provider:'com.android.deskclock/.ClockWidget', label:'时钟' } });
payload:{ provider:'com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider', label:'时钟' } });
['微信','相机','相册','设置','浏览器','音乐','地图','日历'].forEach((t, i) => {
[['微信','com.tencent.mm'],['相机','com.android.camera2'],['相册','com.android.gallery3d'],['设置','com.android.settings'],['浏览器','com.android.chrome'],['QQ音乐','com.tencent.qqmusic'],['电话','com.android.dialer'],['日历','com.android.calendar']].forEach(([label, pkg], i) => {
const c = i % 4, r = Math.floor(i / 4);
out.push({ id:'i'+i, type:'app_icon', x:colX(c), y:604 + r*224, w:icon, h:ih, z:2,
payload:{ package:'com.demo.app'+i, label:t } });
payload:{ package:pkg, label } });
});
out.push({ id:'w_weather', type:'widget', x:pad, y:1076, w:420, h:300, z:1,
payload:{ provider:'com.android.weather/.WeatherWidget', label:'天气' } });
payload:{ provider:'com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider', label:'搜索' } });
out.push({ id:'w_music', type:'widget', x:552, y:1076, w:420, h:300, z:1,
payload:{ provider:'com.spotify/.MusicWidget', label:'音乐' } });
payload:{ provider:'com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall', label:'音乐' } });
['扫一扫','支付','钱包','更多'].forEach((t, i) => {
out.push({ id:'s'+i, type:'shortcut', x:colX(i), y:1650, w:icon, h:ih, z:2,
payload:{ label:t, intent:'demo://'+i } });
[['联系人','com.android.contacts'],['短信','com.android.messaging'],['文件','com.android.documentsui'],['QQ','com.tencent.mobileqq']].forEach(([label, pkg], i) => {
out.push({ id:'s'+i, type:'app_icon', x:colX(i), y:1650, w:icon, h:ih, z:2,
payload:{ package:pkg, label } });
});
return out;
}
@@ -830,7 +830,7 @@ function validate() {
if (c.safeTop < 0 || c.safeBottom < 0) push('err', '-', '安全区不能为负数');
if (!['contain', 'elastic', 'stretch'].includes(state.fit.mode.toLowerCase())) push('warn', '-', '未知映射模式');
if (c.safeTop < 118) push('warn', '-', `safeTop=${c.safeTop} 偏小,1440 屏状态栏约 176px`);
if (c.safeTop < 118) push('warn', '-', `safeTop=${c.safeTop} 偏小`);
if (c.safeBottom < 40) push('warn', '-', `safeBottom=${c.safeBottom} 偏小,可能被手势条压住`);
const errors = issues.filter(i => i.lv === 'err').length;
@@ -1083,7 +1083,7 @@ $('download').onclick = $('exportBtn').onclick = () => {
function importLayout(d) {
if (!d || (d.schemaVersion ?? 3) !== 3) throw new Error('仅支持布局协议 v3');
const canvas = { designWidth:1080, designHeight:1920, safeTop:132, safeBottom:72, ...d.canvas };
const canvas = { designWidth:1080, designHeight:1920, safeTop:72, safeBottom:72, ...d.canvas };
const fit = { mode:'contain', anchorY:0.5, ...d.fit };
if (![canvas.designWidth, canvas.designHeight, canvas.safeTop, canvas.safeBottom].every(Number.isInteger) ||
canvas.designWidth < 320 || canvas.designHeight < 480 ||
+41 -13
View File
@@ -1,8 +1,8 @@
<script setup lang="ts">
import { ref, watch } from "vue";
import { ElButton, ElDialog, ElInput, ElMessageBox } from "element-plus";
import { ElButton, ElDialog, ElMessageBox } from "element-plus";
import { loggedIn } from "./auth";
import { normalizeLayout, type Issue, type Layout } from "./layout";
import { builtInLayout, normalizeLayout, type Issue, type Layout } from "./layout";
interface HistoryEntry { revision: number; publishedAt: string; restoredFrom?: number }
interface History { revisions: HistoryEntry[]; nextBefore: number | null }
@@ -118,21 +118,48 @@ async function confirmPublish(version?: number) {
} catch {
return;
}
return run(() => publish(
rollback ? "rollback" : "layout",
rollback ? "POST" : "PUT",
rollback ? { revision: version } : props.layout,
tag.value,
rollback,
"已发布",
));
}
function restoreDefault() {
return run(async () => {
if (!tag.value) throw new Error("发布前请先读取线上版本");
const { data, etag } = await request<Layout>(rollback ? "rollback" : "layout", {
method: rollback ? "POST" : "PUT",
headers: { "If-Match": tag.value, "Content-Type": "application/json" },
body: JSON.stringify(rollback ? { revision: version } : props.layout),
});
tag.value = etag;
revision.value = data.revision;
if (rollback) emit("load", normalizeLayout(data), true);
else emit("published", data.revision);
return refreshHistory("已发布 v" + data.revision + ",设备会在下次同步时更新。");
if (!(await props.confirmReplace())) return "";
const { data: latest, etag } = await request<Layout>("layout");
try {
await ElMessageBox.confirm(
"当前线上为 v" + latest.revision + "。将内置默认布局发布为新版本,并应用到所有已连接设备?",
"恢复默认布局",
{ type: "warning", confirmButtonText: "恢复并发布", cancelButtonText: "取消" },
);
} catch {
return "";
}
return publish("layout", "PUT", builtInLayout(), etag, true, "已恢复默认布局,发布");
});
}
async function publish(path: string, method: "POST" | "PUT", layout: Layout | { revision: number | undefined },
expectedTag: string | undefined, replaceCanvas: boolean, message: string) {
if (!expectedTag) throw new Error("未取得线上版本,请重试");
const { data, etag } = await request<Layout>(path, {
method,
headers: { "If-Match": expectedTag, "Content-Type": "application/json" },
body: JSON.stringify(layout),
});
tag.value = etag;
revision.value = data.revision;
if (replaceCanvas) emit("load", normalizeLayout(data), true);
else emit("published", data.revision);
return refreshHistory(message + " v" + data.revision + ",设备会在下次同步时更新。");
}
function moreHistory() {
return run(async () => {
await loadHistory(true);
@@ -148,6 +175,7 @@ function moreHistory() {
<div class="release-actions">
<ElButton :disabled="busy" @click="loadOnline">读取线上布局</ElButton>
<ElButton type="primary" :disabled="busy || !tag || hasErrors" @click="confirmPublish()">发布当前画布</ElButton>
<ElButton type="warning" plain :disabled="busy || !loggedIn" @click="restoreDefault">恢复默认</ElButton>
<ElButton :disabled="busy" @click="open = false">返回编辑</ElButton>
</div>
<p v-if="hasErrors" class="release-error">请先修正画布校验错误。</p>
+5 -5
View File
@@ -36,10 +36,10 @@ export type Template = Pick<LayoutItem, "type" | "w" | "h" | "payload"> & { labe
export const templates: Template[] = [
{ type: "app_icon", label: "应用图标", w: 132, h: 168, payload: { package: "com.tencent.mm", label: "微信" } },
{ type: "app_icon", label: "相机", w: 132, h: 168, payload: { package: "com.android.camera", label: "相机" } },
{ type: "widget", label: "时钟组件", w: 864, h: 320, payload: { provider: "com.android.deskclock/.ClockWidget", label: "时钟" } },
{ type: "widget", label: "天气组件", w: 420, h: 300, payload: { provider: "com.android.weather/.WeatherWidget", label: "天气" } },
{ type: "widget", label: "音乐组件", w: 420, h: 300, payload: { provider: "com.spotify/.MusicWidget", label: "音乐" } },
{ type: "app_icon", label: "相机", w: 132, h: 168, payload: { package: "com.android.camera2", label: "相机" } },
{ type: "widget", label: "时钟组件", w: 864, h: 320, payload: { provider: "com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider", label: "时钟" } },
{ type: "widget", label: "搜索组件", w: 420, h: 300, payload: { provider: "com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider", label: "搜索" } },
{ type: "widget", label: "音乐组件", w: 420, h: 300, payload: { provider: "com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall", label: "音乐" } },
{ type: "shortcut", label: "快捷方式", w: 132, h: 168, payload: { intent: "weixin://scanqrcode", label: "扫一扫" } },
{ type: "dock_bg", label: "Dock 底板", w: 936, h: 200, payload: { radius: 56 } },
];
@@ -53,7 +53,7 @@ export const devices = [
{ name: "平板 4:3", w: 1536, h: 2048 },
];
const canvasDefaults = { designWidth: 1080, designHeight: 1920, safeTop: 132, safeBottom: 72 };
const canvasDefaults = { designWidth: 1080, designHeight: 1920, safeTop: 72, safeBottom: 72 };
const fitDefaults = { mode: "contain", anchorY: 0.5 };
const object = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);