Files
ComposeLauncher/frontend/src/LayoutSurface.vue
T

93 lines
4.3 KiB
Vue

<script setup lang="ts">
import { computed } from "vue";
import {
backgroundStyle, clamp, deviceMapper, devices, iconColor, sortedItems,
type Layout, type LayoutItem, type Page,
} from "./layout";
const props = defineProps<{
layout: Layout;
page: Page;
device?: typeof devices[number];
selectedId?: string;
selectedRow?: string[];
editable?: boolean;
guides?: boolean;
dragGuides?: { x?: number; y?: number };
showWhitespace?: boolean;
}>();
const emit = defineEmits<{
startDrag: [event: PointerEvent, item: LayoutItem, resize: boolean];
clear: [];
}>();
const canvas = computed(() => props.layout.canvas);
const width = computed(() => props.device?.w ?? canvas.value.designWidth);
const height = computed(() => props.device?.h ?? canvas.value.designHeight);
const deviceMap = computed(() => props.device
? deviceMapper(canvas.value, props.layout.fit, props.page.items, props.device)
: undefined);
const mapper = computed(() => deviceMap.value ?? { sx: 1, sy: 1, x: (n: number) => n, y: (n: number) => n });
const whitespace = computed(() => {
const insets = deviceMap.value?.insets;
if (!props.showWhitespace || !insets || !Object.values(insets).some(value => value > 0.5)) return undefined;
return {
top: percent(insets.top, height.value),
bottom: percent(insets.bottom, height.value),
left: percent(insets.left, width.value),
right: percent(insets.right, width.value),
};
});
const items = computed(() => sortedItems(props.page));
const known = new Set(["app_icon", "widget", "shortcut", "dock_bg"]);
const visible = computed(() => props.editable ? items.value : items.value.filter(item => known.has(item.type)));
const percent = (n: number, total: number) => (n / total * 100) + "%";
function itemStyle(item: LayoutItem) {
const m = mapper.value;
return {
left: percent(m.x(item.x), width.value),
top: percent(m.y(item.y), height.value),
width: percent(item.w * m.sx, width.value),
height: percent(item.h * m.sy, height.value),
borderRadius: item.type === "dock_bg" ? Math.max(0, item.payload.radius ?? 56) * m.sx / width.value * 100 + "cqw" : undefined,
"--icon-fill": iconColor(item),
};
}
function title(item: LayoutItem) {
return item.payload.label || item.payload.package || item.payload.provider || item.payload.intent || item.type;
}
</script>
<template>
<div class="layout-surface" :class="{ editable, 'with-guides': guides }"
:style="{ aspectRatio: width + '/' + height, background: backgroundStyle(page.background) }"
@pointerdown="editable && emit('clear')">
<template v-if="editable">
<div class="safe-zone top" :style="{ height: percent(clamp(canvas.safeTop, 0, height), height) }"></div>
<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, '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'">
<span class="icon-art">{{ (item.payload.label || title(item)).slice(0, 1).toUpperCase() }}</span>
<span class="icon-label">{{ title(item) }}</span>
</template>
<span v-else-if="item.type !== 'dock_bg'" class="widget-label">{{ title(item) }}</span>
<span v-if="editable" class="resize-handle" aria-hidden="true"
@pointerdown="emit('startDrag', $event, item, true)"></span>
</div>
<template v-if="editable && guides && dragGuides">
<div v-if="dragGuides.x !== undefined" class="drag-guide vertical"
:style="{ left: percent(dragGuides.x, width) }" aria-hidden="true" />
<div v-if="dragGuides.y !== undefined" class="drag-guide horizontal"
:style="{ top: percent(dragGuides.y, height) }" aria-hidden="true" />
</template>
<template v-if="whitespace">
<div v-for="(size, side) in whitespace" :key="side" class="preview-gap" :class="side"
:style="{ '--gap-size': size }" aria-hidden="true" />
<div class="preview-content-boundary" :style="whitespace" aria-hidden="true" />
</template>
</div>
</template>