| 1 | // What the dev app's bubbles show when they take a press (WidgetHost `bubbleGestures`, docs/sdk-v1.md §5.5): a short |
| 2 | // label over the bubble, as a client would open its menu or set the reaction. Shared by the single-widget view and the feed. |
| 3 | |
| 4 | import type { BubbleGesture, GestureClaim } from '@telegram/widget-sandbox/host/widget-host'; |
| 5 | |
| 6 | /** `?gestures=`: `phone` (default) arbitrates the mouse as a finger, `desktop` as a mouse, `off` gives the widget every press. */ |
| 7 | export type GestureMode = 'phone' | 'desktop' | 'off'; |
| 8 | export const GESTURE_MODES: { mode: GestureMode; title: string }[] = [ |
| 9 | { mode: 'phone', title: 'the bubble of a phone client: a vertical drag is always the list scroll; a hold is the message menu and a horizontal drag swipe-to-reply unless touch-action claims them (the mouse arbitrates as a finger); taps and double taps are the widget\'s' }, |
| 10 | { mode: 'desktop', title: 'the bubble of a desktop client: a right click is the message menu unless touch-action: none claims it; clicks, double clicks and drags are the widget\'s' }, |
| 11 | { mode: 'off', title: 'no bubble: the widget gets every press (what no client does; for comparison only)' }, |
| 12 | ]; |
| 13 | export const gestureMode = (v: string | null): GestureMode => (v === 'desktop' || v === |
| 14 | export const bubbleGesturesOption = (mode: GestureMode) => (mode === 'off' ? false : { touch: mode === 'phone' }); |
| 15 | |
| 16 | const LABELS: Record<BubbleGesture['action'], string> = { |
| 17 | menu: '⋯ message menu', |
| 18 | swipe: '↩ swipe: reply', |
| 19 | scroll: '↕ list scroll', |
| 20 | back: '← back', |
| 21 | }; |
| 22 | |
| 23 | export const bubbleGestureLabel = (g: BubbleGesture) => LABELS[g.action] + (g.edge ? ' (edge strip)' : ''); |
| 24 | |
| 25 | /** `auto`, `none`, `pan-y`: the claim as the touch-action that makes it (no claim has vertical drags since v0.1.3). */ |
| 26 | export function claimLabel(c: GestureClaim): string { |
| 27 | if (c.hold) return 'none (x + hold)'; |
| 28 | if (c.x) return 'pan-y (x)'; |
| 29 | return 'auto'; |
| 30 | } |
| 31 | |
| 32 | /** A label over `bubble` for a moment, the newest on top. */ |
| 33 | export function showBubbleGesture(bubble: HTMLElement, g: BubbleGesture) { |
| 34 | const tag = document.createElement('div'); |
| 35 | tag.className = 'bubble-gesture'; |
| 36 | tag.textContent = bubbleGestureLabel(g); |
| 37 | bubble.querySelector('.bubble-gesture')?.remove(); |
| 38 | bubble.appendChild(tag); |
| 39 | setTimeout(() => tag.remove(), 1200); |
| 40 | } |
| 41 | |