Add icon row arrangement and consistent sizing
This commit is contained in:
1 parent
f79e30897b
commit
8d604a5807
9 files changed
+213
-67
No files matched your search
+1
-1
@@ -99,7 +99,7 @@ $env:LAUNCHER_DEVICE_TOKEN = '<设备令牌>'
|
||||
| --- | --- |
|
||||
| 服务器 | `175.178.213.60`,Debian 12,k3s 单节点,SSH 密钥登录 |
|
||||
| 命名空间 / 工作负载 | `vibe-launcher` / `deploy/vibe-launcher-backend`,单副本 `Recreate` |
|
||||
| 镜像 | `docker.io/library/vibe-launcher-backend:1.1.0-deploy11`,`imagePullPolicy: IfNotPresent` |
|
||||
| 镜像 | `docker.io/library/vibe-launcher-backend:1.1.0-deploy14`,`imagePullPolicy: IfNotPresent` |
|
||||
| 端口 | 容器 8080,Service NodePort **30830**(30800 是 youlai-boot,30810 是 tts-server,30820 是 compose-remote-server) |
|
||||
| 公网入口 | `https://api.ttstd.com/launcher/`(灰云直连源站,不走 Cloudflare);nginx 剥掉 `/launcher/` 前缀后反代 `127.0.0.1:30830` |
|
||||
| 数据 | 宿主机 `/opt/vibe-launcher/data/layouts.sqlite` 挂到容器 `/data`,Pod 重建不丢版本历史 |
|
||||
|
||||
@@ -34,10 +34,10 @@
|
||||
{
|
||||
"id": "i0",
|
||||
"type": "app_icon",
|
||||
"x": 150,
|
||||
"y": 604,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 142,
|
||||
"y": 596,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.tencent.mm",
|
||||
@@ -47,10 +47,10 @@
|
||||
{
|
||||
"id": "i1",
|
||||
"type": "app_icon",
|
||||
"x": 366,
|
||||
"y": 604,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 358,
|
||||
"y": 596,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.camera2",
|
||||
@@ -60,10 +60,10 @@
|
||||
{
|
||||
"id": "i2",
|
||||
"type": "app_icon",
|
||||
"x": 582,
|
||||
"y": 604,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 574,
|
||||
"y": 596,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.gallery3d",
|
||||
@@ -73,10 +73,10 @@
|
||||
{
|
||||
"id": "i3",
|
||||
"type": "app_icon",
|
||||
"x": 798,
|
||||
"y": 604,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 790,
|
||||
"y": 596,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.settings",
|
||||
@@ -86,10 +86,10 @@
|
||||
{
|
||||
"id": "i4",
|
||||
"type": "app_icon",
|
||||
"x": 150,
|
||||
"y": 828,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 142,
|
||||
"y": 820,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.chrome",
|
||||
@@ -99,10 +99,10 @@
|
||||
{
|
||||
"id": "i5",
|
||||
"type": "app_icon",
|
||||
"x": 366,
|
||||
"y": 828,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 358,
|
||||
"y": 820,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.tencent.qqmusic",
|
||||
@@ -112,10 +112,10 @@
|
||||
{
|
||||
"id": "i6",
|
||||
"type": "app_icon",
|
||||
"x": 582,
|
||||
"y": 828,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 574,
|
||||
"y": 820,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.dialer",
|
||||
@@ -125,10 +125,10 @@
|
||||
{
|
||||
"id": "i7",
|
||||
"type": "app_icon",
|
||||
"x": 798,
|
||||
"y": 828,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 790,
|
||||
"y": 820,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.calendar",
|
||||
@@ -164,10 +164,10 @@
|
||||
{
|
||||
"id": "s0",
|
||||
"type": "app_icon",
|
||||
"x": 150,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 142,
|
||||
"y": 1642,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.contacts",
|
||||
@@ -177,10 +177,10 @@
|
||||
{
|
||||
"id": "s1",
|
||||
"type": "app_icon",
|
||||
"x": 366,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 358,
|
||||
"y": 1642,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.messaging",
|
||||
@@ -190,10 +190,10 @@
|
||||
{
|
||||
"id": "s2",
|
||||
"type": "app_icon",
|
||||
"x": 582,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 574,
|
||||
"y": 1642,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.android.documentsui",
|
||||
@@ -203,10 +203,10 @@
|
||||
{
|
||||
"id": "s3",
|
||||
"type": "app_icon",
|
||||
"x": 798,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"x": 790,
|
||||
"y": 1642,
|
||||
"w": 148,
|
||||
"h": 184,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.tencent.mobileqq",
|
||||
|
||||
@@ -26,7 +26,7 @@ spec:
|
||||
spec:
|
||||
containers:
|
||||
- name: server
|
||||
image: docker.io/library/vibe-launcher-backend:1.1.0-deploy11
|
||||
image: docker.io/library/vibe-launcher-backend:1.1.0-deploy14
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- name: http
|
||||
|
||||
@@ -389,7 +389,7 @@ function newState() {
|
||||
}
|
||||
|
||||
function demoItems() {
|
||||
const pad = 108, colW = 216, icon = 132, ih = 168;
|
||||
const pad = 108, colW = 216, icon = 148, ih = 184;
|
||||
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,
|
||||
@@ -397,7 +397,7 @@ function demoItems() {
|
||||
|
||||
[['微信','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,
|
||||
out.push({ id:'i'+i, type:'app_icon', x:colX(c), y:596 + r*224, w:icon, h:ih, z:2,
|
||||
payload:{ package:pkg, label } });
|
||||
});
|
||||
|
||||
@@ -407,7 +407,7 @@ function demoItems() {
|
||||
payload:{ provider:'com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall', label:'音乐' } });
|
||||
|
||||
[['联系人','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,
|
||||
out.push({ id:'s'+i, type:'app_icon', x:colX(i), y:1642, w:icon, h:ih, z:2,
|
||||
payload:{ package:pkg, label } });
|
||||
});
|
||||
return out;
|
||||
|
||||
+111
-12
@@ -11,7 +11,7 @@ import ReleaseDialog from "./ReleaseDialog.vue";
|
||||
import LayoutSurface from "./LayoutSurface.vue";
|
||||
import DevicePreview from "./DevicePreview.vue";
|
||||
import {
|
||||
alignmentGuide, builtInLayout, clamp, copyLayout, normalizeLayout, templates, validate,
|
||||
alignmentGuide, arrangeIconRow, builtInLayout, clamp, copyLayout, isIcon, normalizeLayout, resizedIconBounds, templates, validate,
|
||||
type Layout, type LayoutItem, type Payload, type Template,
|
||||
} from "./layout";
|
||||
|
||||
@@ -52,11 +52,32 @@ function markPublished(revision: number) {
|
||||
}
|
||||
const pageIndex = ref(0);
|
||||
const selectedId = ref<string>();
|
||||
const rowSelection = ref<string[]>([]);
|
||||
const pickingRow = ref(false);
|
||||
const page = computed(() => layout.value.pages[pageIndex.value]!);
|
||||
const selected = computed(() => page.value.items.find(item => item.id === selectedId.value));
|
||||
const tab = ref("preview");
|
||||
const snap = ref(true);
|
||||
const guides = ref(true);
|
||||
const iconSizeKey = "vibe-launcher-icon-size";
|
||||
const iconSize = ref({ enabled: false, width: 148, height: 184 });
|
||||
try {
|
||||
const stored = JSON.parse(localStorage.getItem(iconSizeKey) ?? "null");
|
||||
if (stored && typeof stored.enabled === "boolean" && Number.isInteger(stored.width) &&
|
||||
Number.isInteger(stored.height) && stored.width > 0 && stored.height > 0) {
|
||||
iconSize.value = stored;
|
||||
}
|
||||
} catch {
|
||||
// Use the default settings when browser storage is unavailable.
|
||||
}
|
||||
watch(iconSize, value => {
|
||||
try {
|
||||
localStorage.setItem(iconSizeKey, JSON.stringify(value));
|
||||
} catch {
|
||||
ElMessage.warning("无法保存图标大小设置");
|
||||
}
|
||||
}, { deep: true });
|
||||
|
||||
const release = ref(false);
|
||||
|
||||
const widgetCatalog = ref(false);
|
||||
@@ -198,7 +219,8 @@ function restore(from: Layout[], to: Layout[]) {
|
||||
}
|
||||
function selectPage(index: number) {
|
||||
pageIndex.value = index;
|
||||
selectedId.value = undefined;
|
||||
clearSelection();
|
||||
pickingRow.value = false;
|
||||
}
|
||||
function changePages(add: boolean) {
|
||||
remember();
|
||||
@@ -222,30 +244,49 @@ function updateValue<T extends object, K extends keyof T>(record: T, key: K, val
|
||||
remember();
|
||||
record[key] = value;
|
||||
}
|
||||
const effectiveIconSize = computed(() => ({
|
||||
w: clamp(iconSize.value.width, 1, canvas.value.designWidth),
|
||||
h: clamp(iconSize.value.height, 1, canvas.value.designHeight),
|
||||
}));
|
||||
const allIcons = computed(() => layout.value.pages.flatMap(entry => entry.items.filter(item => isIcon(item.type))));
|
||||
|
||||
function applyIconSize() {
|
||||
const { w, h } = effectiveIconSize.value;
|
||||
const updates = allIcons.value.filter(item => item.w !== w || item.h !== h);
|
||||
if (!updates.length) {
|
||||
ElMessage.info("所有图标已是指定大小");
|
||||
return;
|
||||
}
|
||||
remember();
|
||||
for (const item of updates) Object.assign(item, resizedIconBounds(item, w, h, canvas.value));
|
||||
ElMessage.success("已统一 " + updates.length + " 个图标的大小");
|
||||
}
|
||||
|
||||
const grid = (n: number) => snap.value ? Math.round(n / 12) * 12 : Math.round(n);
|
||||
|
||||
function addItem(template: Template, x?: number, y?: number) {
|
||||
const count = page.value.items.length;
|
||||
if (count >= 80) { ElMessage.warning("本页最多 80 个元素"); return; }
|
||||
remember();
|
||||
const w = Math.min(template.w, canvas.value.designWidth);
|
||||
const h = Math.min(template.h, canvas.value.designHeight);
|
||||
const { w, h } = iconSize.value.enabled && isIcon(template.type)
|
||||
? effectiveIconSize.value
|
||||
: { w: Math.min(template.w, canvas.value.designWidth), h: Math.min(template.h, canvas.value.designHeight) };
|
||||
let suffix = count + 1;
|
||||
while (page.value.items.some(item => item.id === "e" + suffix)) suffix++;
|
||||
const item = {
|
||||
id: "e" + suffix, type: template.type, w, h, z: 2, payload: { ...template.payload },
|
||||
x: clamp(grid(x ?? 108 + count % 4 * 216), 0, canvas.value.designWidth - w),
|
||||
y: clamp(grid(y ?? 300 + count % 3 * 60), 0, canvas.value.designHeight - h),
|
||||
x: clamp(grid(x === undefined ? 108 + count % 4 * 216 : x - w / 2), 0, canvas.value.designWidth - w),
|
||||
y: clamp(grid(y === undefined ? 300 + count % 3 * 60 : y - h / 2), 0, canvas.value.designHeight - h),
|
||||
};
|
||||
page.value.items.push(item);
|
||||
selectedId.value = item.id;
|
||||
tab.value = "properties";
|
||||
}
|
||||
function clickBlank(event: MouseEvent) {
|
||||
if ((event.target as HTMLElement).closest(".layout-item")) return;
|
||||
if (pickingRow.value || (event.target as HTMLElement).closest(".layout-item")) return;
|
||||
const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
|
||||
addItem(templates[0]!, (event.clientX - rect.left) / designScale.value - 66,
|
||||
(event.clientY - rect.top) / designScale.value - 84);
|
||||
addItem(templates[0]!, (event.clientX - rect.left) / designScale.value,
|
||||
(event.clientY - rect.top) / designScale.value);
|
||||
}
|
||||
function removeItem() {
|
||||
if (!selected.value) return;
|
||||
@@ -260,6 +301,43 @@ function changeLayer(item: LayoutItem, top: boolean) {
|
||||
: Math.max(-2147483648, Math.min(...layers) - 1);
|
||||
updateValue(item, "z", layer);
|
||||
}
|
||||
function clearSelection() {
|
||||
selectedId.value = undefined;
|
||||
rowSelection.value = [];
|
||||
}
|
||||
|
||||
function toggleRowSelection() {
|
||||
const item = selected.value;
|
||||
pickingRow.value = !pickingRow.value;
|
||||
clearSelection();
|
||||
if (pickingRow.value && item?.type === "app_icon") rowSelection.value = [item.id];
|
||||
}
|
||||
|
||||
function arrangeRow() {
|
||||
const icons = page.value.items.filter(item => item.type === "app_icon" && rowSelection.value.includes(item.id));
|
||||
const arranged = arrangeIconRow(icons, canvas.value);
|
||||
if (!arranged) {
|
||||
ElMessage.warning("画布宽度不足,请减少选中的图标");
|
||||
return;
|
||||
}
|
||||
if (arranged.some(({ item, x, y }) => page.value.items.some(other =>
|
||||
other.type !== "dock_bg" && !rowSelection.value.includes(other.id) &&
|
||||
x < other.x + other.w - 0.5 && x + item.w > other.x + 0.5 &&
|
||||
y < other.y + other.h - 0.5 && y + item.h > other.y + 0.5))) {
|
||||
ElMessage.warning("排列后会与其他元素重叠,请选择同一排图标");
|
||||
return;
|
||||
}
|
||||
if (arranged.some(({ item, x, y }) => item.x !== x || item.y !== y)) {
|
||||
remember();
|
||||
for (const { item, x, y } of arranged) {
|
||||
item.x = x;
|
||||
item.y = y;
|
||||
}
|
||||
}
|
||||
pickingRow.value = false;
|
||||
clearSelection();
|
||||
ElMessage.success("已按行排列");
|
||||
}
|
||||
function centerHorizontally() {
|
||||
const item = selected.value;
|
||||
if (!item || item.w > canvas.value.designWidth) return;
|
||||
@@ -283,6 +361,14 @@ let drag: { item: LayoutItem; original: LayoutItem; pointer: number; clientX: nu
|
||||
function startDrag(event: PointerEvent, item: LayoutItem, resize: boolean) {
|
||||
if (!event.isPrimary || event.button !== 0 || drag) return;
|
||||
event.stopPropagation();
|
||||
if (pickingRow.value) {
|
||||
if (!resize && item.type === "app_icon") {
|
||||
rowSelection.value = rowSelection.value.includes(item.id)
|
||||
? rowSelection.value.filter(id => id !== item.id)
|
||||
: [...rowSelection.value, item.id];
|
||||
}
|
||||
return;
|
||||
}
|
||||
selectedId.value = item.id;
|
||||
const neighbors = resize ? [] : page.value.items.filter(entry => entry.id !== item.id);
|
||||
drag = { item, original: { ...item }, pointer: event.pointerId, clientX: event.clientX,
|
||||
@@ -405,6 +491,9 @@ onUnmounted(() => {
|
||||
<ElButton :disabled="!redoStack.length" @click="restore(redoStack, undoStack)">重做</ElButton>
|
||||
<ElButton :disabled="!page.items.length" @click="align">对齐栅格</ElButton>
|
||||
<ElButton :disabled="!selected || selected.w > canvas.designWidth" @click="centerHorizontally">水平居中</ElButton>
|
||||
<ElButton :type="pickingRow ? 'primary' : 'default'" :aria-pressed="pickingRow"
|
||||
@click="toggleRowSelection">{{ pickingRow ? '退出多选' : '多选图标' }}</ElButton>
|
||||
<ElButton v-if="pickingRow" :disabled="rowSelection.length < 2" @click="arrangeRow">按行排列</ElButton>
|
||||
<ElButton :disabled="!selected" @click="removeItem">删除元素</ElButton>
|
||||
<ElButton @click="showIssues">校验</ElButton>
|
||||
<ElButton @click="fileInput?.click()">导入</ElButton>
|
||||
@@ -427,6 +516,15 @@ onUnmounted(() => {
|
||||
</ElButton>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>图标大小</h2>
|
||||
<ElCheckbox v-model="iconSize.enabled">统一图标大小</ElCheckbox>
|
||||
<label>宽度 <ElInputNumber :model-value="effectiveIconSize.w" :min="1" :max="canvas.designWidth"
|
||||
:precision="0" aria-label="统一图标宽度" @change="value => { if (value !== undefined) iconSize.width = value; }" /></label>
|
||||
<label>高度 <ElInputNumber :model-value="effectiveIconSize.h" :min="1" :max="canvas.designHeight"
|
||||
:precision="0" aria-label="统一图标高度" @change="value => { if (value !== undefined) iconSize.height = value; }" /></label>
|
||||
<ElButton :disabled="!allIcons.length" @click="applyIconSize">应用全部</ElButton>
|
||||
</section>
|
||||
<section>
|
||||
<h2>画布</h2>
|
||||
<label>基准尺寸
|
||||
@@ -474,13 +572,14 @@ onUnmounted(() => {
|
||||
<div class="canvas-head">
|
||||
<strong>设计稿</strong>
|
||||
<span>{{ canvas.designWidth }} × {{ canvas.designHeight }}</span>
|
||||
<span class="hint">双击空白添加图标 · 拖动右下角调整尺寸</span>
|
||||
<span class="hint" v-if="pickingRow">点选同一排的应用图标 · 已选 {{ rowSelection.length }} 个</span>
|
||||
<span class="hint" v-else>双击空白添加图标 · 拖动右下角调整尺寸</span>
|
||||
</div>
|
||||
<div ref="stage" class="stage" @pointermove="moveDrag" @pointerup="finishDrag"
|
||||
@pointercancel="finishDrag" @lostpointercapture="finishDrag">
|
||||
<LayoutSurface :layout="layout" :page="page" :selected-id="selectedId" :guides="guides" :drag-guides="dragGuides" editable
|
||||
<LayoutSurface :layout="layout" :page="page" :selected-id="selectedId" :selected-row="rowSelection" :guides="guides" :drag-guides="dragGuides" editable
|
||||
:style="{ width: (canvas.designWidth * designScale) + 'px' }"
|
||||
@start-drag="startDrag" @clear="selectedId = undefined" @dblclick="clickBlank" />
|
||||
@start-drag="startDrag" @clear="clearSelection" @dblclick="clickBlank" />
|
||||
</div>
|
||||
<div class="page-bar" aria-label="布局页面">
|
||||
<ElButton v-for="(_, index) in layout.pages" :key="index"
|
||||
|
||||
@@ -10,6 +10,7 @@ const props = defineProps<{
|
||||
page: Page;
|
||||
device?: typeof devices[number];
|
||||
selectedId?: string;
|
||||
selectedRow?: string[];
|
||||
editable?: boolean;
|
||||
guides?: boolean;
|
||||
dragGuides?: { x?: number; y?: number };
|
||||
@@ -65,7 +66,7 @@ function title(item: LayoutItem) {
|
||||
<div class="safe-zone bottom" :style="{ height: percent(clamp(canvas.safeBottom, 0, height), height) }"></div>
|
||||
</template>
|
||||
<div v-for="item in visible" :key="item.id" class="layout-item"
|
||||
:class="[item.type, { selected: item.id === selectedId }]" :style="itemStyle(item)"
|
||||
:class="[item.type, { selected: item.id === selectedId, 'row-selected': selectedRow?.includes(item.id) }]" :style="itemStyle(item)"
|
||||
:title="title(item)" :tabindex="editable ? 0 : undefined"
|
||||
@pointerdown="editable && emit('startDrag', $event, item, false)">
|
||||
<template v-if="item.type === 'app_icon' || item.type === 'shortcut'">
|
||||
|
||||
@@ -212,6 +212,35 @@ export function deviceMapper(canvas: Layout["canvas"], fit: Layout["fit"], items
|
||||
};
|
||||
}
|
||||
|
||||
export const isIcon = (type: string) => type === "app_icon" || type === "shortcut";
|
||||
|
||||
export function resizedIconBounds(item: LayoutItem, w: number, h: number, canvas: Layout["canvas"]) {
|
||||
return {
|
||||
w, h,
|
||||
x: clamp(item.x + (item.w - w) / 2, 0, canvas.designWidth - w),
|
||||
y: clamp(item.y + (item.h - h) / 2, 0, canvas.designHeight - h),
|
||||
};
|
||||
}
|
||||
|
||||
export function arrangeIconRow(items: LayoutItem[], canvas: Layout["canvas"]) {
|
||||
if (items.length < 2) return undefined;
|
||||
const sorted = [...items].sort((a, b) => a.x + a.w / 2 - b.x - b.w / 2);
|
||||
const { designWidth: width, designHeight: height } = canvas;
|
||||
const maxWidth = Math.max(...sorted.map(item => item.w));
|
||||
const maxHeight = Math.max(...sorted.map(item => item.h));
|
||||
if (maxHeight > height || maxWidth * sorted.length > width) return undefined;
|
||||
const margin = Math.max(maxWidth / 2, Math.min(width / (sorted.length + 1),
|
||||
(width - maxWidth * (sorted.length - 1)) / 2));
|
||||
const step = (width - margin * 2) / (sorted.length - 1);
|
||||
const centerY = clamp(
|
||||
sorted.reduce((sum, item) => sum + item.y + item.h / 2, 0) / sorted.length,
|
||||
maxHeight / 2, height - maxHeight / 2,
|
||||
);
|
||||
return sorted.map((item, index) => ({
|
||||
item, x: margin + step * index - item.w / 2, y: centerY - item.h / 2,
|
||||
}));
|
||||
}
|
||||
|
||||
export function alignmentGuide(start: number, size: number, targetPoints: number[], tolerance: number) {
|
||||
let nearest: number | undefined;
|
||||
let bestDistance = Infinity;
|
||||
|
||||
@@ -71,6 +71,7 @@ small { color: #9caec3; }
|
||||
}
|
||||
.editable .layout-item { cursor: grab; }
|
||||
.layout-item.selected, .editable .layout-item:focus-visible { outline: 1px solid #7bc2ff; outline-offset: -1px; }
|
||||
.layout-item.row-selected { outline: 2px solid #7bc2ff; outline-offset: -2px; background: #4b9cf22a; }
|
||||
.layout-item.widget { background: #6f91b442; border: 1px solid #9ab6d267; border-radius: 1.6cqw; }
|
||||
.layout-item.dock_bg { background: #b0bfdd30; border: 1px solid #a3b8d44a; }
|
||||
.icon-art {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { runInNewContext } from "node:vm";
|
||||
import { alignmentGuide, builtInLayout, deviceMapper, normalizeLayout, validate } from "./src/layout.ts";
|
||||
import { alignmentGuide, arrangeIconRow, builtInLayout, deviceMapper, normalizeLayout, resizedIconBounds, validate } from "./src/layout.ts";
|
||||
|
||||
const fixtures = JSON.parse(readFileSync(new URL("../docs/layout-cases.json", import.meta.url), "utf8"));
|
||||
const offline = readFileSync(new URL("./canvas.html", import.meta.url), "utf8");
|
||||
@@ -67,3 +67,19 @@ assert.equal(alignmentGuide(82, 36, [120, 118], 4), 118);
|
||||
assert.equal(alignmentGuide(82, 36, [122], 3), undefined);
|
||||
assert.equal(alignmentGuide(0, 36, [0], 0), 0);
|
||||
console.log("Drag alignment guides passed");
|
||||
|
||||
const icons = items.filter(item => item.type === "app_icon").slice(0, 4);
|
||||
const misplaced = icons.map((item, index) => ({ ...item, x: item.x + index * 7, y: item.y + index * 8 }));
|
||||
const arranged = arrangeIconRow(misplaced.reverse(), canvas);
|
||||
assert.deepEqual(arranged.map(({ item, x }) => x + item.w / 2), [216, 432, 648, 864]);
|
||||
assert.equal(new Set(arranged.map(({ item, y }) => y + item.h / 2)).size, 1);
|
||||
assert.deepEqual(arranged.map(({ item }) => item.id), icons.map(item => item.id));
|
||||
assert.equal(arrangeIconRow(icons, { ...canvas, designWidth: 320 }), undefined);
|
||||
console.log("Four-column row arrangement passed");
|
||||
|
||||
const icon = icons[0];
|
||||
assert.deepEqual(resizedIconBounds(icon, icon.w + 16, icon.h + 20, canvas),
|
||||
{ x: icon.x - 8, y: icon.y - 10, w: icon.w + 16, h: icon.h + 20 });
|
||||
assert.deepEqual(resizedIconBounds({ ...icon, x: 0, y: 0 }, 200, 220, canvas),
|
||||
{ x: 0, y: 0, w: 200, h: 220 });
|
||||
console.log("Icon resize centering and canvas bounds passed");
|
||||
Reference in new issue
Block a user