@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 92 lines 3.55 KB
Raw
1 export interface ReservedRegion {
2 readonly kind: "division" | "occlusion";
3 readonly x: number;
4 readonly y: number;
5 readonly width: number;
6 readonly height: number;
7 }
8
9 export interface NativeLayoutMetrics {
10 readonly width: number;
11 readonly height: number;
12 readonly hasHinge?: boolean;
13 readonly horizontalSizeClass: "compact" | "regular";
14 readonly verticalBarEdge: "none" | "left" | "right";
15 readonly safeArea: {
16 readonly top: number;
17 readonly bottom: number;
18 readonly left: number;
19 readonly right: number;
20 };
21 readonly reservedRegions: ReadonlyArray<ReservedRegion>;
22 /** How far above the bottom edge UIKit floats a 48pt bottom bar, level with its tab bar. iOS 27.1+. */
23 readonly bottomBarInset?: number;
24 }
25
26 function isFullHeightDivision(region: ReservedRegion, metrics: NativeLayoutMetrics) {
27 return region.kind === "division" && region.height >= metrics.height / 2;
28 }
29
30 function sideOfDivision(left: number, right: number, x: number, endX: number) {
31 if (x <= left) return { left: Math.min(right, endX), right };
32 if (endX >= right) return { left, right: Math.max(left, x) };
33 return right - endX >= x - left ? { left: endX, right } : { left, right: x };
34 }
35
36 export function deriveContentSideInsets(metrics: NativeLayoutMetrics | null) {
37 if (!metrics) return { left: 0, right: 0 };
38 let left = metrics.safeArea.left;
39 let right = metrics.width - metrics.safeArea.right;
40 for (const region of metrics.reservedRegions) {
41 if (!isFullHeightDivision(region, metrics)) continue;
42 const x = Math.max(left, region.x);
43 const endX = Math.min(right, region.x + region.width);
44 if (endX <= x) continue;
45 ({ left, right } = sideOfDivision(left, right, x, endX));
46 }
47 return { left, right: metrics.width - right };
48 }
49
50 export function deriveBottomControlInsets(metrics: NativeLayoutMetrics | null) {
51 if (!metrics) return { left: 0, right: 0, bottom: 0 };
52 let left = metrics.safeArea.left;
53 let right = metrics.width - metrics.safeArea.right;
54 let bottom = metrics.height;
55 for (const region of metrics.reservedRegions) {
56 const x = Math.max(left, region.x);
57 const endX = Math.min(right, region.x + region.width);
58 const y = Math.max(0, region.y);
59 const endY = Math.min(bottom, region.y + region.height);
60 if (endX <= x || endY <= y) continue;
61 if (isFullHeightDivision(region, metrics)) {
62 ({ left, right } = sideOfDivision(left, right, x, endX));
63 } else if (endY >= bottom - metrics.safeArea.bottom) {
64 if (region.width >= right - left) bottom = y;
65 else if (right - endX >= x - left) left = endX;
66 else right = x;
67 }
68 }
69 return { left, right: metrics.width - right, bottom: metrics.height - bottom };
70 }
71
72 /**
73 * Where a bar can share the top safe area with the system status, as Safari does on iPhone Duo:
74 * the status block covers one end of that band, so the rest of it is free. Null otherwise.
75 */
76 export function deriveSharedTopBand(metrics: NativeLayoutMetrics | null) {
77 if (!metrics || metrics.safeArea.top <= 0) return null;
78 const blocks = metrics.reservedRegions.filter(
79 (region) =>
80 region.kind === "occlusion" && region.y <= 0 && region.height >= metrics.safeArea.top - 1,
81 );
82 const [block] = blocks;
83 if (blocks.length !== 1 || !block) return null;
84 const height = metrics.safeArea.top;
85 if (block.x > metrics.width / 2 && block.x + block.width >= metrics.width - 1) {
86 return { height, left: 0, right: metrics.width - block.x };
87 }
88 if (block.x <= 1 && block.x + block.width < metrics.width / 2) {
89 return { height, left: block.x + block.width, right: 0 };
90 }
91 return null;
92 }