forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

floating-button.ts75 lines, 2.8 KiBHistoryRawDownload
1/**
2 * Where the phone shell's floating action button rests. Kept per device in
3 * localStorage: a spot a thumb picked on one phone means nothing on another.
4 *
5 * The position is stored as a share (0–1) of the travel the button has inside
6 * its layer, not in pixels, so it survives a rotation or a resized window.
7 */
8export interface FloatingButtonState {
9 x: number;
10 y: number;
11 /** Put away onto the screen edge by dropping it on the hide target. */
12 parked: boolean;
13}
14
15const STORAGE_KEY = "hezarfen.floatingButton";
16
17/** Bottom-right, above the tab bar. */
18export const DEFAULT_FLOATING_BUTTON: FloatingButtonState = { x: 1, y: 1, parked: false };
19
20function canUseStorage(): boolean {
21 return typeof window !== "undefined" && typeof localStorage !== "undefined";
22}
23
24export const clamp01 = (value: number) => Math.min(1, Math.max(0, value));
25
26const share = (value: unknown, fallback: number) =>
27 typeof value === "number" && Number.isFinite(value) ? clamp01(value) : fallback;
28
29export function readFloatingButton(): FloatingButtonState {
30 if (!canUseStorage()) return { ...DEFAULT_FLOATING_BUTTON };
31 try {
32 const raw = localStorage.getItem(STORAGE_KEY);
33 if (!raw) return { ...DEFAULT_FLOATING_BUTTON };
34 const value = JSON.parse(raw) as Partial<FloatingButtonState> | null;
35 return {
36 x: share(value?.x, DEFAULT_FLOATING_BUTTON.x),
37 y: share(value?.y, DEFAULT_FLOATING_BUTTON.y),
38 parked: value?.parked === true,
39 };
40 } catch {
41 return { ...DEFAULT_FLOATING_BUTTON };
42 }
43}
44
45export function writeFloatingButton(state: FloatingButtonState): void {
46 if (!canUseStorage()) return;
47 try {
48 localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
49 } catch {
50 // ignore — a private window or a full quota just forgets the spot
51 }
52}
53
54/**
55 * Offsets of the radial menu items from the button's centre.
56 *
57 * The fan opens towards the middle of the screen, so wherever the button was
58 * dragged its items land on screen: a corner opens a quarter circle along its
59 * diagonal, an edge a wider fan away from that edge, and the middle a fan
60 * upwards. `cx`/`cy` are the button centre as a share of the layer; measuring
61 * in that unit square keeps a corner's fan on the diagonal whatever the
62 * phone's aspect ratio.
63 */
64export function radialOffsets(count: number, cx: number, cy: number, radius: number): { dx: number; dy: number }[] {
65 if (count <= 0) return [];
66 const vx = 0.5 - cx;
67 const vy = 0.5 - cy;
68 const heading = Math.hypot(vx, vy) < 0.15 ? -Math.PI / 2 : Math.atan2(vy, vx);
69 const corner = Math.abs(vx) > 0.25 && Math.abs(vy) > 0.25;
70 const spread = count === 1 ? 0 : corner ? Math.PI / 2 : (Math.PI * 5) / 6;
71 return Array.from({ length: count }, (_, i) => {
72 const angle = heading + (count === 1 ? 0 : (i / (count - 1) - 0.5) * spread);
73 return { dx: Math.cos(angle) * radius, dy: Math.sin(angle) * radius };
74 });
75}