keyPolygon function
The polygon for geometry at cell aspect (width / height),
normalized into the unit square (the long side spans 0–1, the short
side is centred).
Implementation
RoundedPolygon keyPolygon(KeyGeometry geometry, double aspect) {
final a = aspect.isFinite && aspect > 0 ? aspect : 1.0;
final shortSide = math.min(a, 1);
RoundedPolygon rect(double radiusFraction, {double smoothing = 0}) =>
RoundedPolygon.rectangle(
width: a,
height: 1,
rounding: CornerRounding(
radius: radiusFraction.clamp(0.0, 0.5) * shortSide,
smoothing: smoothing,
),
centerX: a / 2,
centerY: 0.5,
);
RoundedPolygon stretched(RoundedPolygon unit) =>
_normalize(unit).transformed((x, y) => (x * a, y));
final f = geometry.cornerFraction;
final polygon = switch (geometry.kind) {
KeyShapeKind.circle || KeyShapeKind.pill => rect(0.5),
KeyShapeKind.roundedRect ||
KeyShapeKind.keycap ||
KeyShapeKind.stepped => rect(f),
KeyShapeKind.squircle => rect(f, smoothing: 0.6),
KeyShapeKind.hexagon => stretched(
RoundedPolygon.fromVerticesNum(
6,
radius: 0.5,
centerX: 0.5,
centerY: 0.5,
rounding: CornerRounding(radius: f * 0.5),
),
),
KeyShapeKind.cut => _cutRect(a, f * shortSide),
KeyShapeKind.m3eMorph => switch (geometry.m3eShape) {
null || M3eShape.circle || M3eShape.pill => rect(0.5),
M3eShape.square => rect(0.3),
final shape => stretched(materialShape(shape)),
},
};
return _normalize(polygon);
}