示例工程 22-theming-and-native-appearance
22-theming-and-native-appearance/demo-02-drag-hit/hit.ts
type Region = {
kind: "drag" | "control";
x: number;
y: number;
width: number;
height: number;
};
type Point = { x: number; y: number };
function contains(region: Region, point: Point): boolean {
return (
point.x >= region.x &&
point.x < region.x + region.width &&
point.y >= region.y &&
point.y < region.y + region.height
);
}
function hitTest(regions: Region[], point: Point): Region["kind"] | "none" {
for (const region of regions) {
if (region.kind === "control" && contains(region, point)) {
return "control";
}
}
for (const region of regions) {
if (region.kind === "drag" && contains(region, point)) return "drag";
}
return "none";
}
const regions: Region[] = [
{ kind: "drag", x: 0, y: 0, width: 400, height: 56 },
{ kind: "control", x: 14, y: 14, width: 60, height: 28 },
{ kind: "control", x: 300, y: 14, width: 90, height: 28 },
];
const points: Array<[Point, string]> = [
[{ x: 200, y: 20 }, "drag"],
[{ x: 40, y: 20 }, "control"],
[{ x: 330, y: 20 }, "control"],
[{ x: 90, y: 20 }, "drag"],
[{ x: 200, y: 80 }, "none"],
[{ x: 400, y: 20 }, "none"],
];
const results = points.map(([point, expected]) => ({
point,
expected,
got: hitTest(regions, point),
}));
console.log(
JSON.stringify({
evt: "smoke",
ok: results.every((result) => result.got === result.expected),
results,
}),
);