panelui-native 0.113.1 → 0.115.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/module/components/camera/camera-model.js +142 -0
- package/lib/module/components/camera/camera-model.js.map +1 -0
- package/lib/module/components/camera/index.js +1546 -0
- package/lib/module/components/camera/index.js.map +1 -0
- package/lib/module/components/image-viewer/image-viewer-geometry.js +15 -0
- package/lib/module/components/image-viewer/image-viewer-geometry.js.map +1 -1
- package/lib/module/components/image-viewer/index.js +101 -37
- package/lib/module/components/image-viewer/index.js.map +1 -1
- package/lib/module/components/tabs/index.js +19 -7
- package/lib/module/components/tabs/index.js.map +1 -1
- package/lib/module/icons/index.js +20 -0
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/components/camera/camera-model.d.ts +79 -0
- package/lib/typescript/src/components/camera/camera-model.d.ts.map +1 -0
- package/lib/typescript/src/components/camera/index.d.ts +481 -0
- package/lib/typescript/src/components/camera/index.d.ts.map +1 -0
- package/lib/typescript/src/components/image-viewer/image-viewer-geometry.d.ts +9 -0
- package/lib/typescript/src/components/image-viewer/image-viewer-geometry.d.ts.map +1 -1
- package/lib/typescript/src/components/image-viewer/index.d.ts +10 -3
- package/lib/typescript/src/components/image-viewer/index.d.ts.map +1 -1
- package/lib/typescript/src/components/tabs/index.d.ts +24 -2
- package/lib/typescript/src/components/tabs/index.d.ts.map +1 -1
- package/lib/typescript/src/icons/index.d.ts +4 -0
- package/lib/typescript/src/icons/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +5 -1
- package/src/components/camera/camera-model.ts +140 -0
- package/src/components/camera/index.tsx +1880 -0
- package/src/components/image-viewer/image-viewer-geometry.ts +15 -0
- package/src/components/image-viewer/index.tsx +122 -43
- package/src/components/tabs/index.tsx +48 -8
- package/src/icons/index.tsx +8 -0
- package/src/index.ts +36 -0
|
@@ -0,0 +1,1546 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Camera — a full-screen camera assembled from parts: the viewfinder, the
|
|
5
|
+
* shutter, zoom, flash, a self-timer, the recording clock, front and back, and
|
|
6
|
+
* the button into the library.
|
|
7
|
+
*
|
|
8
|
+
* ```tsx
|
|
9
|
+
* <Camera onCapture={keep}>
|
|
10
|
+
* <Camera.Preview />
|
|
11
|
+
* <Camera.Header>
|
|
12
|
+
* <Camera.Flash />
|
|
13
|
+
* <Camera.Duration />
|
|
14
|
+
* <Camera.Timer />
|
|
15
|
+
* </Camera.Header>
|
|
16
|
+
* <Camera.Footer>
|
|
17
|
+
* <Camera.Zoom />
|
|
18
|
+
* <Camera.Modes />
|
|
19
|
+
* <Camera.Controls>
|
|
20
|
+
* <Camera.Library source={last} onPress={openLibrary} />
|
|
21
|
+
* <Camera.Shutter />
|
|
22
|
+
* <Camera.Flip />
|
|
23
|
+
* </Camera.Controls>
|
|
24
|
+
* </Camera.Footer>
|
|
25
|
+
* </Camera>
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* ## The result is the app's
|
|
29
|
+
*
|
|
30
|
+
* A photo or a clip is handed to `onCapture` as the file the camera wrote, and
|
|
31
|
+
* nothing else happens to it here. Saving it to the photo library, holding it
|
|
32
|
+
* in memory for a preview, uploading it: each app wants a different one, and a
|
|
33
|
+
* component that picked would be wrong for most of them. The library button is
|
|
34
|
+
* the same — it draws the thumbnail it is given and calls `onPress`.
|
|
35
|
+
*
|
|
36
|
+
* ## Every part reads one state
|
|
37
|
+
*
|
|
38
|
+
* The root owns facing, mode, flash, zoom and the timer, each controlled or
|
|
39
|
+
* not, and every part reads them through context. `useCamera()` hands the same
|
|
40
|
+
* state and actions to anything else, so a part that does not suit can be
|
|
41
|
+
* swapped for a control of the app's own without losing the behaviour behind
|
|
42
|
+
* it. Leave a part out and its feature is simply not offered.
|
|
43
|
+
*
|
|
44
|
+
* ## Built on expo-camera
|
|
45
|
+
*
|
|
46
|
+
* The preview, the capture and the recording are expo-camera's, which runs in
|
|
47
|
+
* Expo Go. It is an optional dependency: without it the preview says so, and
|
|
48
|
+
* `hasCamera` is false.
|
|
49
|
+
*
|
|
50
|
+
* Its zoom is a fraction of the device's maximum, and that maximum is not
|
|
51
|
+
* exposed — on iOS the fraction is an exponent of it, on Android a multiple —
|
|
52
|
+
* so the same zoom is a different magnification on every phone. That is why
|
|
53
|
+
* nothing here labels a zoom "2×" on its own: the stops are the app's to name,
|
|
54
|
+
* and between them a bar shows where in the range the lens is.
|
|
55
|
+
*
|
|
56
|
+
* There is no way to focus on a point either, so a tap on the preview does
|
|
57
|
+
* nothing rather than draw a square over a focus that never moved.
|
|
58
|
+
*
|
|
59
|
+
* ## Over a black viewfinder
|
|
60
|
+
*
|
|
61
|
+
* The viewfinder is black in every theme and the picture on it is whatever
|
|
62
|
+
* the lens sees, so the chrome over it is white on a dark glaze in every theme.
|
|
63
|
+
* An active control is drawn inverted, white with a black glyph, which reads on
|
|
64
|
+
* any picture; recording is the destructive colour.
|
|
65
|
+
*
|
|
66
|
+
* Switching cameras hides the swap under a brief dark cover rather than
|
|
67
|
+
* turning the preview: on Android the preview is a surface that ignores view
|
|
68
|
+
* transforms, and the frame where one session hands over to the other is not
|
|
69
|
+
* something to show.
|
|
70
|
+
*/
|
|
71
|
+
import { createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
72
|
+
import { AccessibilityInfo, Image, Linking, Pressable, StyleSheet, View } from 'react-native';
|
|
73
|
+
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
|
|
74
|
+
import Animated, { Easing, FadeIn, cancelAnimation, runOnJS, useAnimatedProps, useAnimatedStyle, useReducedMotion, useSharedValue, withDelay, withSequence, withSpring, withTiming } from 'react-native-reanimated';
|
|
75
|
+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|
76
|
+
import Svg, { Circle } from 'react-native-svg';
|
|
77
|
+
import { tv } from 'tailwind-variants';
|
|
78
|
+
import { useCSSVariable } from 'uniwind';
|
|
79
|
+
import { CameraIcon, CameraSwitchIcon, FlashIcon, FlashOffIcon, IconColorProvider, ImageIcon, TimerIcon } from "../../icons/index.js";
|
|
80
|
+
import { AnimatedPressable } from "../../primitives/animated-pressable.js";
|
|
81
|
+
import { useControllableState } from "../../primitives/controllable-state.js";
|
|
82
|
+
import { Text } from "../../primitives/text.js";
|
|
83
|
+
import { cn } from "../../utils/cn.js";
|
|
84
|
+
import { impactKnock, selectionTick } from "../../utils/haptics.js";
|
|
85
|
+
import { activeStop, clampZoom, flashIn, formatDuration, nextFlash, nextTimer, stepZoom, stillFlash, torchFor, zoomFromPinch, zoomFromTrack } from "./camera-model.js";
|
|
86
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
87
|
+
/** Glyphs over the viewfinder, which is black in every theme. */
|
|
88
|
+
const ON_GLASS = '#ffffff';
|
|
89
|
+
/** Glyphs on an active control, which is drawn white. */
|
|
90
|
+
const ON_ACTIVE = '#000000';
|
|
91
|
+
|
|
92
|
+
/** The shutter's outer diameter, its ring, and the disc inside it. */
|
|
93
|
+
const SHUTTER = 78;
|
|
94
|
+
const SHUTTER_RING = 4;
|
|
95
|
+
const SHUTTER_DISC = 62;
|
|
96
|
+
/** The disc while recording: a rounded square, so stopping reads as a stop. */
|
|
97
|
+
const SHUTTER_STOP = 28;
|
|
98
|
+
const RING_RADIUS = SHUTTER / 2 - SHUTTER_RING / 2;
|
|
99
|
+
const RING_LENGTH = 2 * Math.PI * RING_RADIUS;
|
|
100
|
+
|
|
101
|
+
/** How long the shutter has to be held, in video, before holding records. */
|
|
102
|
+
const HOLD_TO_RECORD_MS = 300;
|
|
103
|
+
|
|
104
|
+
/** The flip cover: out, a hold while the native session swaps, and back. */
|
|
105
|
+
const FLIP_OUT_MS = 140;
|
|
106
|
+
const FLIP_HOLD_MS = 220;
|
|
107
|
+
const FLIP_IN_MS = 200;
|
|
108
|
+
|
|
109
|
+
/** A stop that lands as a recording starts can be dropped natively; it is sent again after this. */
|
|
110
|
+
const STOP_RETRY_MS = 500;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* How long a press made before the camera has reported it started waits for that
|
|
114
|
+
* report before trying anyway. The report can reach JavaScript late, so a press is
|
|
115
|
+
* held rather than dropped.
|
|
116
|
+
*/
|
|
117
|
+
const READY_GRACE_MS = 1500;
|
|
118
|
+
const ZOOM_BAR_WIDTH = 200;
|
|
119
|
+
const ZOOM_TICKS = 21;
|
|
120
|
+
const DEFAULT_ZOOM_STOPS = [{
|
|
121
|
+
value: 0,
|
|
122
|
+
label: '1×'
|
|
123
|
+
}];
|
|
124
|
+
const FLASH_LABELS = {
|
|
125
|
+
off: 'Off',
|
|
126
|
+
auto: 'Auto',
|
|
127
|
+
on: 'On'
|
|
128
|
+
};
|
|
129
|
+
const MODE_LABELS = {
|
|
130
|
+
photo: 'Photo',
|
|
131
|
+
video: 'Video'
|
|
132
|
+
};
|
|
133
|
+
const MODE_ORDER = ['video', 'photo'];
|
|
134
|
+
const AnimatedCircle = Animated.createAnimatedComponent(Circle);
|
|
135
|
+
const cameraVariants = tv({
|
|
136
|
+
slots: {
|
|
137
|
+
root: 'flex-1 overflow-hidden bg-black',
|
|
138
|
+
header: 'flex-row items-center justify-between px-4',
|
|
139
|
+
footer: 'items-center gap-4 px-6',
|
|
140
|
+
controls: 'w-full flex-row items-center justify-between'
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
const chromeButton = tv({
|
|
144
|
+
base: 'h-10 min-w-10 flex-row items-center justify-center gap-1 rounded-full px-2.5',
|
|
145
|
+
variants: {
|
|
146
|
+
active: {
|
|
147
|
+
true: 'bg-white',
|
|
148
|
+
false: 'bg-black/40'
|
|
149
|
+
},
|
|
150
|
+
// Both values are written, so the enabled state never depends on a class being removed.
|
|
151
|
+
disabled: {
|
|
152
|
+
true: 'opacity-40',
|
|
153
|
+
false: 'opacity-100'
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
defaultVariants: {
|
|
157
|
+
active: false,
|
|
158
|
+
disabled: false
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
const chromeText = tv({
|
|
162
|
+
base: 'font-semibold',
|
|
163
|
+
variants: {
|
|
164
|
+
active: {
|
|
165
|
+
true: 'text-black',
|
|
166
|
+
false: 'text-white'
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
defaultVariants: {
|
|
170
|
+
active: false
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
const durationPill = tv({
|
|
174
|
+
base: 'h-8 flex-row items-center gap-1.5 rounded-full px-3',
|
|
175
|
+
variants: {
|
|
176
|
+
recording: {
|
|
177
|
+
true: 'bg-destructive',
|
|
178
|
+
false: 'bg-black/40'
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
defaultVariants: {
|
|
182
|
+
recording: false
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
/* -------------------------------------------------------------------------- */
|
|
187
|
+
/* expo-camera, loaded if it is there */
|
|
188
|
+
/* -------------------------------------------------------------------------- */
|
|
189
|
+
|
|
190
|
+
/** The picture expo-camera returns. */
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The methods of expo-camera's `CameraView` this component calls, and the ones
|
|
194
|
+
* an app is likeliest to reach for through `CameraHandle.view`.
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* `expo-camera`, or null when it is not installed or its native module is
|
|
199
|
+
* missing from the build. The `require` sits directly in its `try` block, which
|
|
200
|
+
* is what lets Metro bundle an app that does not have the package.
|
|
201
|
+
*/
|
|
202
|
+
function loadExpoCamera() {
|
|
203
|
+
try {
|
|
204
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
205
|
+
return require('expo-camera');
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const expoCamera = loadExpoCamera();
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* True when the camera can be shown, because `expo-camera` is installed and its
|
|
214
|
+
* native module is in the build. Expo Go has it.
|
|
215
|
+
*/
|
|
216
|
+
export const hasCamera = expoCamera !== null;
|
|
217
|
+
const NO_PERMISSION = [null, async () => ({
|
|
218
|
+
granted: false,
|
|
219
|
+
canAskAgain: false
|
|
220
|
+
})];
|
|
221
|
+
function useMissingPermission() {
|
|
222
|
+
return NO_PERMISSION;
|
|
223
|
+
}
|
|
224
|
+
/** Chosen once: whether the package is there cannot change while the app runs. */
|
|
225
|
+
const useCameraPermission = expoCamera?.useCameraPermissions ?? useMissingPermission;
|
|
226
|
+
|
|
227
|
+
/* -------------------------------------------------------------------------- */
|
|
228
|
+
/* Types */
|
|
229
|
+
/* -------------------------------------------------------------------------- */
|
|
230
|
+
|
|
231
|
+
/** A photo, as the camera wrote it. */
|
|
232
|
+
|
|
233
|
+
/** A clip, as the camera wrote it. */
|
|
234
|
+
|
|
235
|
+
/** Options for each photo, passed to expo-camera's `takePictureAsync`. */
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Props passed straight to expo-camera's `CameraView`, for what the parts do not
|
|
239
|
+
* cover. The parts' own state wins over the same prop here.
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
/** One zoom stop: a value in expo-camera's range, and the label the app gives it. */
|
|
243
|
+
|
|
244
|
+
/** What `useCamera()` returns: the camera's state, and what can be done with it. */
|
|
245
|
+
|
|
246
|
+
/** A ref on `Camera`. */
|
|
247
|
+
|
|
248
|
+
const CameraContext = /*#__PURE__*/createContext(null);
|
|
249
|
+
function useCameraContext(part) {
|
|
250
|
+
const context = useContext(CameraContext);
|
|
251
|
+
if (!context) throw new Error(`${part} must be rendered inside <Camera>.`);
|
|
252
|
+
return context;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The camera's state and actions, for a control of the app's own inside
|
|
257
|
+
* `<Camera>` — a shutter drawn differently, a timer in a menu.
|
|
258
|
+
*/
|
|
259
|
+
export function useCamera() {
|
|
260
|
+
return useCameraContext('useCamera()');
|
|
261
|
+
}
|
|
262
|
+
function toError(error) {
|
|
263
|
+
if (error instanceof Error) return error;
|
|
264
|
+
return new Error(typeof error === 'string' ? error : 'The camera failed.');
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/* -------------------------------------------------------------------------- */
|
|
268
|
+
/* Root */
|
|
269
|
+
/* -------------------------------------------------------------------------- */
|
|
270
|
+
|
|
271
|
+
function CameraRoot({
|
|
272
|
+
facing: facingProp,
|
|
273
|
+
defaultFacing = 'back',
|
|
274
|
+
onFacingChange,
|
|
275
|
+
mode: modeProp,
|
|
276
|
+
defaultMode = 'photo',
|
|
277
|
+
onModeChange,
|
|
278
|
+
flash: flashProp,
|
|
279
|
+
defaultFlash = 'off',
|
|
280
|
+
onFlashChange,
|
|
281
|
+
zoom: zoomProp,
|
|
282
|
+
defaultZoom = 0,
|
|
283
|
+
onZoomChange,
|
|
284
|
+
timer: timerProp,
|
|
285
|
+
defaultTimer = 0,
|
|
286
|
+
onTimerChange,
|
|
287
|
+
onCapture,
|
|
288
|
+
onError,
|
|
289
|
+
maxDuration,
|
|
290
|
+
mute = false,
|
|
291
|
+
mirror = false,
|
|
292
|
+
pictureOptions,
|
|
293
|
+
cameraProps,
|
|
294
|
+
haptics = true,
|
|
295
|
+
safeArea = true,
|
|
296
|
+
className,
|
|
297
|
+
children,
|
|
298
|
+
...props
|
|
299
|
+
}, ref) {
|
|
300
|
+
const reduced = useReducedMotion();
|
|
301
|
+
const {
|
|
302
|
+
value: facing,
|
|
303
|
+
setValue: setFacing
|
|
304
|
+
} = useControllableState({
|
|
305
|
+
value: facingProp,
|
|
306
|
+
defaultValue: defaultFacing,
|
|
307
|
+
onChange: onFacingChange
|
|
308
|
+
});
|
|
309
|
+
const {
|
|
310
|
+
value: mode,
|
|
311
|
+
setValue: setModeState
|
|
312
|
+
} = useControllableState({
|
|
313
|
+
value: modeProp,
|
|
314
|
+
defaultValue: defaultMode,
|
|
315
|
+
onChange: onModeChange
|
|
316
|
+
});
|
|
317
|
+
const {
|
|
318
|
+
value: flash,
|
|
319
|
+
setValue: setFlashState
|
|
320
|
+
} = useControllableState({
|
|
321
|
+
value: flashProp,
|
|
322
|
+
defaultValue: defaultFlash,
|
|
323
|
+
onChange: onFlashChange
|
|
324
|
+
});
|
|
325
|
+
const {
|
|
326
|
+
value: zoom,
|
|
327
|
+
setValue: setZoomState
|
|
328
|
+
} = useControllableState({
|
|
329
|
+
value: zoomProp,
|
|
330
|
+
defaultValue: defaultZoom,
|
|
331
|
+
onChange: onZoomChange,
|
|
332
|
+
normalize: clampZoom
|
|
333
|
+
});
|
|
334
|
+
const {
|
|
335
|
+
value: timer,
|
|
336
|
+
setValue: setTimerState
|
|
337
|
+
} = useControllableState({
|
|
338
|
+
value: timerProp,
|
|
339
|
+
defaultValue: defaultTimer,
|
|
340
|
+
onChange: onTimerChange
|
|
341
|
+
});
|
|
342
|
+
const [permission, requestPermissionAsync] = useCameraPermission();
|
|
343
|
+
const [ready, setReady] = useState(false);
|
|
344
|
+
const [capturing, setCapturing] = useState(false);
|
|
345
|
+
const [recordingSince, setRecordingSince] = useState(null);
|
|
346
|
+
const [countdown, setCountdown] = useState(null);
|
|
347
|
+
const viewRef = useRef(null);
|
|
348
|
+
const cover = useSharedValue(0);
|
|
349
|
+
const turn = useSharedValue(0);
|
|
350
|
+
|
|
351
|
+
// The latest of everything an async capture reads after it has started.
|
|
352
|
+
const latest = useRef({
|
|
353
|
+
onCapture,
|
|
354
|
+
onError,
|
|
355
|
+
pictureOptions,
|
|
356
|
+
maxDuration,
|
|
357
|
+
mute,
|
|
358
|
+
haptics
|
|
359
|
+
});
|
|
360
|
+
latest.current = {
|
|
361
|
+
onCapture,
|
|
362
|
+
onError,
|
|
363
|
+
pictureOptions,
|
|
364
|
+
maxDuration,
|
|
365
|
+
mute,
|
|
366
|
+
haptics
|
|
367
|
+
};
|
|
368
|
+
const facingRef = useRef(facing);
|
|
369
|
+
facingRef.current = facing;
|
|
370
|
+
const capturingRef = useRef(false);
|
|
371
|
+
const recordingRef = useRef(false);
|
|
372
|
+
const generationRef = useRef(0);
|
|
373
|
+
const cancelStartRef = useRef(false);
|
|
374
|
+
const flippingRef = useRef(false);
|
|
375
|
+
const readyRef = useRef(false);
|
|
376
|
+
const pendingRef = useRef(null);
|
|
377
|
+
const countdownRef = useRef(null);
|
|
378
|
+
const report = useCallback(error => {
|
|
379
|
+
const failure = toError(error);
|
|
380
|
+
if (latest.current.onError) latest.current.onError(failure);else if (__DEV__) console.warn(`[Camera] ${failure.message}`);
|
|
381
|
+
}, []);
|
|
382
|
+
const requestPermission = useCallback(() => {
|
|
383
|
+
requestPermissionAsync().catch(report);
|
|
384
|
+
}, [report, requestPermissionAsync]);
|
|
385
|
+
const setZoom = useCallback(next => setZoomState(clampZoom(next)), [setZoomState]);
|
|
386
|
+
const setFlash = useCallback(next => setFlashState(next), [setFlashState]);
|
|
387
|
+
const setTimer = useCallback(next => setTimerState(next), [setTimerState]);
|
|
388
|
+
const clearCountdown = useCallback(() => {
|
|
389
|
+
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
390
|
+
countdownRef.current = null;
|
|
391
|
+
setCountdown(null);
|
|
392
|
+
}, []);
|
|
393
|
+
const capturePhoto = useCallback(async () => {
|
|
394
|
+
const view = viewRef.current;
|
|
395
|
+
if (!view || capturingRef.current || recordingRef.current) return null;
|
|
396
|
+
capturingRef.current = true;
|
|
397
|
+
setCapturing(true);
|
|
398
|
+
if (latest.current.haptics) impactKnock();
|
|
399
|
+
try {
|
|
400
|
+
const picture = await view.takePictureAsync({
|
|
401
|
+
quality: 1,
|
|
402
|
+
...latest.current.pictureOptions
|
|
403
|
+
});
|
|
404
|
+
if (!picture) return null;
|
|
405
|
+
const photo = {
|
|
406
|
+
type: 'photo',
|
|
407
|
+
uri: picture.uri,
|
|
408
|
+
width: picture.width,
|
|
409
|
+
height: picture.height,
|
|
410
|
+
...(picture.base64 ? {
|
|
411
|
+
base64: picture.base64
|
|
412
|
+
} : null),
|
|
413
|
+
...(picture.exif ? {
|
|
414
|
+
exif: picture.exif
|
|
415
|
+
} : null)
|
|
416
|
+
};
|
|
417
|
+
latest.current.onCapture?.(photo);
|
|
418
|
+
return photo;
|
|
419
|
+
} catch (error) {
|
|
420
|
+
report(error);
|
|
421
|
+
return null;
|
|
422
|
+
} finally {
|
|
423
|
+
capturingRef.current = false;
|
|
424
|
+
setCapturing(false);
|
|
425
|
+
}
|
|
426
|
+
}, [report]);
|
|
427
|
+
const record = useCallback(async () => {
|
|
428
|
+
const view = viewRef.current;
|
|
429
|
+
if (!view || recordingRef.current || capturingRef.current) return;
|
|
430
|
+
recordingRef.current = true;
|
|
431
|
+
cancelStartRef.current = false;
|
|
432
|
+
const generation = ++generationRef.current;
|
|
433
|
+
let since = null;
|
|
434
|
+
try {
|
|
435
|
+
if (!latest.current.mute && expoCamera) {
|
|
436
|
+
let microphone = await expoCamera.Camera.getMicrophonePermissionsAsync();
|
|
437
|
+
if (!microphone.granted && microphone.canAskAgain) {
|
|
438
|
+
microphone = await expoCamera.Camera.requestMicrophonePermissionsAsync();
|
|
439
|
+
}
|
|
440
|
+
if (!microphone.granted) {
|
|
441
|
+
throw new Error('Recording with sound needs microphone access. Allow it in Settings, or pass `mute` to record without sound.');
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
// Let go of the shutter while the microphone was being asked for: no recording.
|
|
445
|
+
if (cancelStartRef.current) return;
|
|
446
|
+
since = Date.now();
|
|
447
|
+
setRecordingSince(since);
|
|
448
|
+
if (latest.current.haptics) impactKnock();
|
|
449
|
+
AccessibilityInfo.announceForAccessibility('Recording');
|
|
450
|
+
const cap = latest.current.maxDuration;
|
|
451
|
+
const result = await view.recordAsync(cap ? {
|
|
452
|
+
maxDuration: cap
|
|
453
|
+
} : {});
|
|
454
|
+
if (result?.uri) {
|
|
455
|
+
latest.current.onCapture?.({
|
|
456
|
+
type: 'video',
|
|
457
|
+
uri: result.uri,
|
|
458
|
+
duration: Date.now() - since
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
} catch (error) {
|
|
462
|
+
report(error);
|
|
463
|
+
} finally {
|
|
464
|
+
if (generationRef.current === generation) {
|
|
465
|
+
recordingRef.current = false;
|
|
466
|
+
setRecordingSince(null);
|
|
467
|
+
if (since !== null) AccessibilityInfo.announceForAccessibility('Recording stopped');
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}, [report]);
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Runs a capture now if the camera has started, or once it has. Only one waits
|
|
474
|
+
* at a time: a newer press replaces it, and the replaced one resolves to the
|
|
475
|
+
* fallback.
|
|
476
|
+
*/
|
|
477
|
+
const whenReady = useCallback((action, fallback) => {
|
|
478
|
+
if (readyRef.current) return action();
|
|
479
|
+
return new Promise(resolve => {
|
|
480
|
+
pendingRef.current?.drop();
|
|
481
|
+
const pending = {
|
|
482
|
+
run: () => {
|
|
483
|
+
pendingRef.current = null;
|
|
484
|
+
action().then(resolve, () => resolve(fallback));
|
|
485
|
+
},
|
|
486
|
+
drop: () => {
|
|
487
|
+
pendingRef.current = null;
|
|
488
|
+
resolve(fallback);
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
pendingRef.current = pending;
|
|
492
|
+
setTimeout(() => {
|
|
493
|
+
if (pendingRef.current === pending) pending.run();
|
|
494
|
+
}, READY_GRACE_MS);
|
|
495
|
+
});
|
|
496
|
+
}, []);
|
|
497
|
+
const takePhoto = useCallback(() => whenReady(capturePhoto, null), [capturePhoto, whenReady]);
|
|
498
|
+
const startRecording = useCallback(() => whenReady(record, undefined), [record, whenReady]);
|
|
499
|
+
const stopRecording = useCallback(() => {
|
|
500
|
+
// A recording still waiting for the camera to start is simply not started.
|
|
501
|
+
if (pendingRef.current) {
|
|
502
|
+
pendingRef.current.drop();
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (!recordingRef.current) return;
|
|
506
|
+
cancelStartRef.current = true;
|
|
507
|
+
const generation = generationRef.current;
|
|
508
|
+
viewRef.current?.stopRecording();
|
|
509
|
+
setTimeout(() => {
|
|
510
|
+
if (recordingRef.current && generationRef.current === generation) {
|
|
511
|
+
viewRef.current?.stopRecording();
|
|
512
|
+
}
|
|
513
|
+
}, STOP_RETRY_MS);
|
|
514
|
+
}, []);
|
|
515
|
+
const startCountdown = useCallback(seconds => {
|
|
516
|
+
let left = seconds;
|
|
517
|
+
setCountdown(left);
|
|
518
|
+
if (latest.current.haptics) selectionTick();
|
|
519
|
+
countdownRef.current = setInterval(() => {
|
|
520
|
+
left -= 1;
|
|
521
|
+
if (left > 0) {
|
|
522
|
+
setCountdown(left);
|
|
523
|
+
if (latest.current.haptics) selectionTick();
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
clearCountdown();
|
|
527
|
+
void capturePhoto();
|
|
528
|
+
}, 1000);
|
|
529
|
+
}, [capturePhoto, clearCountdown]);
|
|
530
|
+
const setMode = useCallback(next => {
|
|
531
|
+
if (recordingRef.current) return;
|
|
532
|
+
clearCountdown();
|
|
533
|
+
if (latest.current.haptics) selectionTick();
|
|
534
|
+
setModeState(next);
|
|
535
|
+
}, [clearCountdown, setModeState]);
|
|
536
|
+
const commitFlip = useCallback(() => {
|
|
537
|
+
setFacing(facingRef.current === 'back' ? 'front' : 'back');
|
|
538
|
+
// Zoom belongs to one camera; carried across it would be a different view on the other.
|
|
539
|
+
setZoomState(0);
|
|
540
|
+
}, [setFacing, setZoomState]);
|
|
541
|
+
const endFlip = useCallback(() => {
|
|
542
|
+
flippingRef.current = false;
|
|
543
|
+
}, []);
|
|
544
|
+
const flip = useCallback(() => {
|
|
545
|
+
if (flippingRef.current || recordingRef.current) return;
|
|
546
|
+
if (latest.current.haptics) selectionTick();
|
|
547
|
+
turn.value = withTiming(Math.round(turn.value) + 1, {
|
|
548
|
+
duration: reduced ? 0 : 320,
|
|
549
|
+
easing: Easing.out(Easing.cubic)
|
|
550
|
+
});
|
|
551
|
+
if (reduced || !permission?.granted) {
|
|
552
|
+
commitFlip();
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
flippingRef.current = true;
|
|
556
|
+
cover.value = withSequence(withTiming(1, {
|
|
557
|
+
duration: FLIP_OUT_MS,
|
|
558
|
+
easing: Easing.in(Easing.quad)
|
|
559
|
+
}, finished => {
|
|
560
|
+
'worklet';
|
|
561
|
+
|
|
562
|
+
if (finished) runOnJS(commitFlip)();
|
|
563
|
+
}),
|
|
564
|
+
// A delay rather than a second timing to 1: the hold is what gives the
|
|
565
|
+
// native session time to swap before the cover lifts.
|
|
566
|
+
withDelay(FLIP_HOLD_MS, withTiming(0, {
|
|
567
|
+
duration: FLIP_IN_MS,
|
|
568
|
+
easing: Easing.out(Easing.quad)
|
|
569
|
+
}, () => {
|
|
570
|
+
'worklet';
|
|
571
|
+
|
|
572
|
+
runOnJS(endFlip)();
|
|
573
|
+
})));
|
|
574
|
+
}, [commitFlip, cover, endFlip, permission?.granted, reduced, turn]);
|
|
575
|
+
const press = useCallback(() => {
|
|
576
|
+
if (mode === 'video') {
|
|
577
|
+
if (recordingRef.current || pendingRef.current) stopRecording();else void startRecording();
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (countdownRef.current) {
|
|
581
|
+
clearCountdown();
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (timer > 0) startCountdown(timer);else void takePhoto();
|
|
585
|
+
}, [clearCountdown, mode, startCountdown, startRecording, stopRecording, takePhoto, timer]);
|
|
586
|
+
const onReady = useCallback(() => {
|
|
587
|
+
readyRef.current = true;
|
|
588
|
+
setReady(true);
|
|
589
|
+
pendingRef.current?.run();
|
|
590
|
+
}, []);
|
|
591
|
+
const onMountError = useCallback(event => report(new Error(event?.message ?? 'The camera could not start.')), [report]);
|
|
592
|
+
|
|
593
|
+
// Leaving the screen ends whatever was in flight.
|
|
594
|
+
useEffect(() => () => {
|
|
595
|
+
if (countdownRef.current) clearInterval(countdownRef.current);
|
|
596
|
+
pendingRef.current?.drop();
|
|
597
|
+
if (recordingRef.current) viewRef.current?.stopRecording();
|
|
598
|
+
}, []);
|
|
599
|
+
useImperativeHandle(ref, () => ({
|
|
600
|
+
takePhoto,
|
|
601
|
+
startRecording,
|
|
602
|
+
stopRecording,
|
|
603
|
+
flip,
|
|
604
|
+
get view() {
|
|
605
|
+
return viewRef.current;
|
|
606
|
+
}
|
|
607
|
+
}), [flip, startRecording, stopRecording, takePhoto]);
|
|
608
|
+
const recording = recordingSince !== null;
|
|
609
|
+
const available = expoCamera !== null;
|
|
610
|
+
// Not gated on `ready`: the shutter is usable as soon as there is a camera, and a
|
|
611
|
+
// press that comes before it has started waits for it (see `whenReady`).
|
|
612
|
+
const canCapture = available && !!permission?.granted && !capturing;
|
|
613
|
+
const context = useMemo(() => ({
|
|
614
|
+
facing,
|
|
615
|
+
mode,
|
|
616
|
+
flash,
|
|
617
|
+
zoom,
|
|
618
|
+
timer,
|
|
619
|
+
ready,
|
|
620
|
+
permission,
|
|
621
|
+
recording,
|
|
622
|
+
recordingSince,
|
|
623
|
+
capturing,
|
|
624
|
+
countdown,
|
|
625
|
+
maxDuration,
|
|
626
|
+
setMode,
|
|
627
|
+
setFlash,
|
|
628
|
+
setZoom,
|
|
629
|
+
setTimer,
|
|
630
|
+
flip,
|
|
631
|
+
press,
|
|
632
|
+
takePhoto,
|
|
633
|
+
startRecording,
|
|
634
|
+
stopRecording,
|
|
635
|
+
cancelCountdown: clearCountdown,
|
|
636
|
+
requestPermission,
|
|
637
|
+
available,
|
|
638
|
+
canCapture,
|
|
639
|
+
viewRef,
|
|
640
|
+
onReady,
|
|
641
|
+
onMountError,
|
|
642
|
+
cover,
|
|
643
|
+
turn,
|
|
644
|
+
haptics,
|
|
645
|
+
safeArea,
|
|
646
|
+
mute,
|
|
647
|
+
mirror,
|
|
648
|
+
cameraProps
|
|
649
|
+
}), [available, cameraProps, canCapture, capturing, clearCountdown, countdown, cover, facing, flash, flip, haptics, maxDuration, mirror, mode, mute, onMountError, onReady, permission, press, ready, recording, recordingSince, requestPermission, safeArea, setFlash, setMode, setTimer, setZoom, startRecording, stopRecording, takePhoto, timer, turn, zoom]);
|
|
650
|
+
const {
|
|
651
|
+
root
|
|
652
|
+
} = cameraVariants();
|
|
653
|
+
return /*#__PURE__*/_jsx(CameraContext.Provider, {
|
|
654
|
+
value: context,
|
|
655
|
+
children: /*#__PURE__*/_jsx(View, {
|
|
656
|
+
...props,
|
|
657
|
+
className: root({
|
|
658
|
+
className
|
|
659
|
+
}),
|
|
660
|
+
children: children
|
|
661
|
+
})
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
const CameraForwarded = /*#__PURE__*/forwardRef(CameraRoot);
|
|
665
|
+
CameraForwarded.displayName = 'Camera';
|
|
666
|
+
|
|
667
|
+
/* -------------------------------------------------------------------------- */
|
|
668
|
+
/* Preview */
|
|
669
|
+
/* -------------------------------------------------------------------------- */
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* The live picture, filling the camera. Until camera access is given it asks
|
|
673
|
+
* for it in place, and once access is refused for good its button opens
|
|
674
|
+
* Settings, since asking again would do nothing.
|
|
675
|
+
*/
|
|
676
|
+
function CameraPreview({
|
|
677
|
+
className,
|
|
678
|
+
children,
|
|
679
|
+
pinchToZoom = true,
|
|
680
|
+
doubleTapToFlip = true,
|
|
681
|
+
permissionTitle = 'Camera access',
|
|
682
|
+
permissionDescription = 'Allow access to the camera to take photos and record video.',
|
|
683
|
+
permissionAction = 'Allow camera',
|
|
684
|
+
settingsAction = 'Open Settings',
|
|
685
|
+
accessibilityLabel = 'Camera viewfinder',
|
|
686
|
+
style,
|
|
687
|
+
...props
|
|
688
|
+
}) {
|
|
689
|
+
const camera = useCameraContext('Camera.Preview');
|
|
690
|
+
const {
|
|
691
|
+
setZoom,
|
|
692
|
+
flip,
|
|
693
|
+
cover
|
|
694
|
+
} = camera;
|
|
695
|
+
const zoomShared = useSharedValue(camera.zoom);
|
|
696
|
+
const pinchStart = useSharedValue(0);
|
|
697
|
+
useEffect(() => {
|
|
698
|
+
zoomShared.value = camera.zoom;
|
|
699
|
+
}, [camera.zoom, zoomShared]);
|
|
700
|
+
const gesture = useMemo(() => {
|
|
701
|
+
const pinch = Gesture.Pinch().onStart(() => {
|
|
702
|
+
'worklet';
|
|
703
|
+
|
|
704
|
+
pinchStart.value = zoomShared.value;
|
|
705
|
+
}).onUpdate(event => {
|
|
706
|
+
'worklet';
|
|
707
|
+
|
|
708
|
+
const next = zoomFromPinch(pinchStart.value, event.scale);
|
|
709
|
+
if (Math.abs(next - zoomShared.value) < 0.002) return;
|
|
710
|
+
zoomShared.value = next;
|
|
711
|
+
runOnJS(setZoom)(next);
|
|
712
|
+
}).enabled(pinchToZoom);
|
|
713
|
+
const doubleTap = Gesture.Tap().numberOfTaps(2).maxDelay(250).onEnd((_event, success) => {
|
|
714
|
+
'worklet';
|
|
715
|
+
|
|
716
|
+
if (success) runOnJS(flip)();
|
|
717
|
+
}).enabled(doubleTapToFlip);
|
|
718
|
+
return Gesture.Simultaneous(pinch, doubleTap);
|
|
719
|
+
}, [doubleTapToFlip, flip, pinchStart, pinchToZoom, setZoom, zoomShared]);
|
|
720
|
+
const coverStyle = useAnimatedStyle(() => ({
|
|
721
|
+
opacity: cover.value
|
|
722
|
+
}));
|
|
723
|
+
const onAccessibilityAction = useCallback(event => {
|
|
724
|
+
if (event.nativeEvent.actionName === 'increment') setZoom(stepZoom(camera.zoom, 1));else if (event.nativeEvent.actionName === 'decrement') setZoom(stepZoom(camera.zoom, -1));
|
|
725
|
+
}, [camera.zoom, setZoom]);
|
|
726
|
+
let content = null;
|
|
727
|
+
if (!expoCamera) {
|
|
728
|
+
content = /*#__PURE__*/_jsx(Notice, {
|
|
729
|
+
title: "Camera unavailable",
|
|
730
|
+
description: "Install expo-camera to show the camera: npx expo install expo-camera"
|
|
731
|
+
});
|
|
732
|
+
} else if (camera.permission && !camera.permission.granted) {
|
|
733
|
+
const settings = !camera.permission.canAskAgain;
|
|
734
|
+
content = /*#__PURE__*/_jsx(Notice, {
|
|
735
|
+
title: permissionTitle,
|
|
736
|
+
description: permissionDescription,
|
|
737
|
+
action: settings ? settingsAction : permissionAction,
|
|
738
|
+
onAction: settings ? () => void Linking.openSettings() : camera.requestPermission
|
|
739
|
+
});
|
|
740
|
+
} else if (camera.permission?.granted) {
|
|
741
|
+
const {
|
|
742
|
+
CameraView
|
|
743
|
+
} = expoCamera;
|
|
744
|
+
content = /*#__PURE__*/_jsxs(_Fragment, {
|
|
745
|
+
children: [/*#__PURE__*/_jsx(GestureDetector, {
|
|
746
|
+
gesture: gesture,
|
|
747
|
+
children: /*#__PURE__*/_jsx(View, {
|
|
748
|
+
style: StyleSheet.absoluteFill,
|
|
749
|
+
accessible: true,
|
|
750
|
+
accessibilityRole: "adjustable",
|
|
751
|
+
accessibilityLabel: accessibilityLabel,
|
|
752
|
+
accessibilityValue: {
|
|
753
|
+
min: 0,
|
|
754
|
+
max: 100,
|
|
755
|
+
now: Math.round(camera.zoom * 100)
|
|
756
|
+
},
|
|
757
|
+
accessibilityActions: [{
|
|
758
|
+
name: 'increment'
|
|
759
|
+
}, {
|
|
760
|
+
name: 'decrement'
|
|
761
|
+
}],
|
|
762
|
+
onAccessibilityAction: onAccessibilityAction,
|
|
763
|
+
children: /*#__PURE__*/_jsx(CameraView, {
|
|
764
|
+
...camera.cameraProps,
|
|
765
|
+
ref: camera.viewRef,
|
|
766
|
+
style: StyleSheet.absoluteFill,
|
|
767
|
+
facing: camera.facing,
|
|
768
|
+
mode: camera.mode === 'video' ? 'video' : 'picture',
|
|
769
|
+
flash: stillFlash(camera.mode, camera.flash),
|
|
770
|
+
enableTorch: torchFor(camera.mode, flashIn(camera.mode, camera.flash), camera.recording),
|
|
771
|
+
zoom: camera.zoom,
|
|
772
|
+
mute: camera.mute,
|
|
773
|
+
mirror: camera.mirror,
|
|
774
|
+
onCameraReady: camera.onReady,
|
|
775
|
+
onMountError: camera.onMountError
|
|
776
|
+
})
|
|
777
|
+
})
|
|
778
|
+
}), children ? /*#__PURE__*/_jsx(View, {
|
|
779
|
+
pointerEvents: "box-none",
|
|
780
|
+
style: StyleSheet.absoluteFill,
|
|
781
|
+
children: children
|
|
782
|
+
}) : null, camera.countdown !== null ? /*#__PURE__*/_jsx(Countdown, {
|
|
783
|
+
value: camera.countdown
|
|
784
|
+
}) : null, /*#__PURE__*/_jsx(Animated.View, {
|
|
785
|
+
pointerEvents: "none",
|
|
786
|
+
style: [StyleSheet.absoluteFill, coverStyle],
|
|
787
|
+
className: "bg-black"
|
|
788
|
+
})]
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
return /*#__PURE__*/_jsx(View, {
|
|
792
|
+
...props,
|
|
793
|
+
style: [StyleSheet.absoluteFill, style],
|
|
794
|
+
className: cn('bg-black', className),
|
|
795
|
+
children: content
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
CameraPreview.displayName = 'Camera.Preview';
|
|
799
|
+
function Countdown({
|
|
800
|
+
value
|
|
801
|
+
}) {
|
|
802
|
+
return /*#__PURE__*/_jsx(Animated.View, {
|
|
803
|
+
entering: FadeIn.duration(120),
|
|
804
|
+
pointerEvents: "none",
|
|
805
|
+
style: StyleSheet.absoluteFill,
|
|
806
|
+
className: "items-center justify-center",
|
|
807
|
+
accessibilityLiveRegion: "polite",
|
|
808
|
+
children: /*#__PURE__*/_jsx(Text, {
|
|
809
|
+
weight: "semibold",
|
|
810
|
+
className: "text-white",
|
|
811
|
+
style: {
|
|
812
|
+
fontSize: 120,
|
|
813
|
+
lineHeight: 132,
|
|
814
|
+
fontVariant: ['tabular-nums']
|
|
815
|
+
},
|
|
816
|
+
children: value
|
|
817
|
+
})
|
|
818
|
+
}, value);
|
|
819
|
+
}
|
|
820
|
+
function Notice({
|
|
821
|
+
title,
|
|
822
|
+
description,
|
|
823
|
+
action,
|
|
824
|
+
onAction
|
|
825
|
+
}) {
|
|
826
|
+
return /*#__PURE__*/_jsxs(View, {
|
|
827
|
+
style: StyleSheet.absoluteFill,
|
|
828
|
+
className: "items-center justify-center gap-3 px-10",
|
|
829
|
+
children: [/*#__PURE__*/_jsx(View, {
|
|
830
|
+
className: "opacity-60",
|
|
831
|
+
children: /*#__PURE__*/_jsx(CameraIcon, {
|
|
832
|
+
size: 32,
|
|
833
|
+
color: ON_GLASS
|
|
834
|
+
})
|
|
835
|
+
}), /*#__PURE__*/_jsx(Text, {
|
|
836
|
+
size: "lg",
|
|
837
|
+
weight: "semibold",
|
|
838
|
+
className: "text-center text-white",
|
|
839
|
+
children: title
|
|
840
|
+
}), /*#__PURE__*/_jsx(Text, {
|
|
841
|
+
size: "sm",
|
|
842
|
+
className: "text-center text-white/70",
|
|
843
|
+
children: description
|
|
844
|
+
}), action && onAction ? /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
845
|
+
onPress: onAction,
|
|
846
|
+
accessibilityRole: "button",
|
|
847
|
+
className: "mt-2 h-11 items-center justify-center rounded-full bg-white px-5",
|
|
848
|
+
children: /*#__PURE__*/_jsx(Text, {
|
|
849
|
+
size: "sm",
|
|
850
|
+
weight: "semibold",
|
|
851
|
+
className: "text-black",
|
|
852
|
+
children: action
|
|
853
|
+
})
|
|
854
|
+
}) : null]
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/* -------------------------------------------------------------------------- */
|
|
859
|
+
/* Header, footer, controls */
|
|
860
|
+
/* -------------------------------------------------------------------------- */
|
|
861
|
+
|
|
862
|
+
/** The band along the top, over the picture: flash, the clock, the timer. */
|
|
863
|
+
function CameraHeader({
|
|
864
|
+
className,
|
|
865
|
+
children,
|
|
866
|
+
style,
|
|
867
|
+
...props
|
|
868
|
+
}) {
|
|
869
|
+
const camera = useCameraContext('Camera.Header');
|
|
870
|
+
const insets = useSafeAreaInsets();
|
|
871
|
+
const {
|
|
872
|
+
header
|
|
873
|
+
} = cameraVariants();
|
|
874
|
+
return /*#__PURE__*/_jsx(View, {
|
|
875
|
+
...props,
|
|
876
|
+
pointerEvents: "box-none",
|
|
877
|
+
style: [{
|
|
878
|
+
position: 'absolute',
|
|
879
|
+
left: 0,
|
|
880
|
+
right: 0,
|
|
881
|
+
top: (camera.safeArea ? insets.top : 0) + 8
|
|
882
|
+
}, style],
|
|
883
|
+
className: header({
|
|
884
|
+
className
|
|
885
|
+
}),
|
|
886
|
+
children: children
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
CameraHeader.displayName = 'Camera.Header';
|
|
890
|
+
/** The band along the bottom, over the picture: zoom, the modes, the shutter row. */
|
|
891
|
+
function CameraFooter({
|
|
892
|
+
className,
|
|
893
|
+
children,
|
|
894
|
+
style,
|
|
895
|
+
...props
|
|
896
|
+
}) {
|
|
897
|
+
const camera = useCameraContext('Camera.Footer');
|
|
898
|
+
const insets = useSafeAreaInsets();
|
|
899
|
+
const {
|
|
900
|
+
footer
|
|
901
|
+
} = cameraVariants();
|
|
902
|
+
return /*#__PURE__*/_jsx(View, {
|
|
903
|
+
...props,
|
|
904
|
+
pointerEvents: "box-none",
|
|
905
|
+
style: [{
|
|
906
|
+
position: 'absolute',
|
|
907
|
+
left: 0,
|
|
908
|
+
right: 0,
|
|
909
|
+
bottom: (camera.safeArea ? insets.bottom : 0) + 16
|
|
910
|
+
}, style],
|
|
911
|
+
className: footer({
|
|
912
|
+
className
|
|
913
|
+
}),
|
|
914
|
+
children: children
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
CameraFooter.displayName = 'Camera.Footer';
|
|
918
|
+
/**
|
|
919
|
+
* The row the shutter sits in, with what goes either side of it. Library and
|
|
920
|
+
* Flip are the same size, so the shutter lands in the middle.
|
|
921
|
+
*/
|
|
922
|
+
function CameraControls({
|
|
923
|
+
className,
|
|
924
|
+
children,
|
|
925
|
+
...props
|
|
926
|
+
}) {
|
|
927
|
+
useCameraContext('Camera.Controls');
|
|
928
|
+
const {
|
|
929
|
+
controls
|
|
930
|
+
} = cameraVariants();
|
|
931
|
+
return /*#__PURE__*/_jsx(View, {
|
|
932
|
+
...props,
|
|
933
|
+
pointerEvents: "box-none",
|
|
934
|
+
className: controls({
|
|
935
|
+
className
|
|
936
|
+
}),
|
|
937
|
+
children: children
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
CameraControls.displayName = 'Camera.Controls';
|
|
941
|
+
|
|
942
|
+
/* -------------------------------------------------------------------------- */
|
|
943
|
+
/* Settings over the picture */
|
|
944
|
+
/* -------------------------------------------------------------------------- */
|
|
945
|
+
|
|
946
|
+
function ChromeButton({
|
|
947
|
+
active = false,
|
|
948
|
+
disabled,
|
|
949
|
+
className,
|
|
950
|
+
children,
|
|
951
|
+
...props
|
|
952
|
+
}) {
|
|
953
|
+
return /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
954
|
+
...props,
|
|
955
|
+
disabled: disabled,
|
|
956
|
+
hitSlop: 6,
|
|
957
|
+
pressScale: 0.92,
|
|
958
|
+
accessibilityRole: "button",
|
|
959
|
+
accessibilityState: {
|
|
960
|
+
disabled: !!disabled,
|
|
961
|
+
selected: active
|
|
962
|
+
},
|
|
963
|
+
className: chromeButton({
|
|
964
|
+
active,
|
|
965
|
+
disabled: !!disabled,
|
|
966
|
+
className
|
|
967
|
+
}),
|
|
968
|
+
children: children
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* A round control over the picture for an action of your own: closing the
|
|
973
|
+
* camera, a grid, a setting. It is drawn like Flash and Timer, so a row of
|
|
974
|
+
* them reads as one set. Give it an `accessibilityLabel`; an icon has nothing
|
|
975
|
+
* to read out.
|
|
976
|
+
*/
|
|
977
|
+
function CameraButton({
|
|
978
|
+
className,
|
|
979
|
+
active = false,
|
|
980
|
+
children,
|
|
981
|
+
...props
|
|
982
|
+
}) {
|
|
983
|
+
useCameraContext('Camera.Button');
|
|
984
|
+
return /*#__PURE__*/_jsx(ChromeButton, {
|
|
985
|
+
...props,
|
|
986
|
+
active: active,
|
|
987
|
+
className: className,
|
|
988
|
+
children: /*#__PURE__*/_jsx(IconColorProvider, {
|
|
989
|
+
color: active ? ON_ACTIVE : ON_GLASS,
|
|
990
|
+
children: children
|
|
991
|
+
})
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
CameraButton.displayName = 'Camera.Button';
|
|
995
|
+
/**
|
|
996
|
+
* The flash. For a photo it steps off, auto, on; for a video, off and on, and
|
|
997
|
+
* on lights the torch while recording.
|
|
998
|
+
*/
|
|
999
|
+
function CameraFlashControl({
|
|
1000
|
+
className,
|
|
1001
|
+
labels,
|
|
1002
|
+
accessibilityLabel = 'Flash',
|
|
1003
|
+
...props
|
|
1004
|
+
}) {
|
|
1005
|
+
const camera = useCameraContext('Camera.Flash');
|
|
1006
|
+
const shown = flashIn(camera.mode, camera.flash);
|
|
1007
|
+
const label = {
|
|
1008
|
+
...FLASH_LABELS,
|
|
1009
|
+
...labels
|
|
1010
|
+
}[shown];
|
|
1011
|
+
const active = shown === 'on';
|
|
1012
|
+
const color = active ? ON_ACTIVE : ON_GLASS;
|
|
1013
|
+
return /*#__PURE__*/_jsxs(ChromeButton, {
|
|
1014
|
+
...props,
|
|
1015
|
+
active: active,
|
|
1016
|
+
onPress: () => {
|
|
1017
|
+
if (camera.haptics) selectionTick();
|
|
1018
|
+
camera.setFlash(nextFlash(camera.mode, camera.flash));
|
|
1019
|
+
},
|
|
1020
|
+
accessibilityLabel: accessibilityLabel,
|
|
1021
|
+
accessibilityValue: {
|
|
1022
|
+
text: label
|
|
1023
|
+
},
|
|
1024
|
+
className: className,
|
|
1025
|
+
children: [shown === 'off' ? /*#__PURE__*/_jsx(FlashOffIcon, {
|
|
1026
|
+
size: 20,
|
|
1027
|
+
color: color
|
|
1028
|
+
}) : /*#__PURE__*/_jsx(FlashIcon, {
|
|
1029
|
+
size: 20,
|
|
1030
|
+
color: color
|
|
1031
|
+
}), shown === 'auto' ? /*#__PURE__*/_jsx(Text, {
|
|
1032
|
+
size: "xs",
|
|
1033
|
+
className: chromeText({
|
|
1034
|
+
active
|
|
1035
|
+
}),
|
|
1036
|
+
children: label
|
|
1037
|
+
}) : null]
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
CameraFlashControl.displayName = 'Camera.Flash';
|
|
1041
|
+
/**
|
|
1042
|
+
* The self-timer: off, 3 s, 10 s. While it counts, the seconds left fill the
|
|
1043
|
+
* preview, and a press on the shutter cancels. Photos only, so it is dimmed in
|
|
1044
|
+
* video.
|
|
1045
|
+
*/
|
|
1046
|
+
function CameraTimerControl({
|
|
1047
|
+
className,
|
|
1048
|
+
offLabel = 'Off',
|
|
1049
|
+
accessibilityLabel = 'Timer',
|
|
1050
|
+
...props
|
|
1051
|
+
}) {
|
|
1052
|
+
const camera = useCameraContext('Camera.Timer');
|
|
1053
|
+
const active = camera.timer > 0;
|
|
1054
|
+
const disabled = camera.mode === 'video' || camera.countdown !== null;
|
|
1055
|
+
const color = active ? ON_ACTIVE : ON_GLASS;
|
|
1056
|
+
return /*#__PURE__*/_jsxs(ChromeButton, {
|
|
1057
|
+
...props,
|
|
1058
|
+
active: active,
|
|
1059
|
+
disabled: disabled,
|
|
1060
|
+
onPress: () => {
|
|
1061
|
+
if (camera.haptics) selectionTick();
|
|
1062
|
+
camera.setTimer(nextTimer(camera.timer));
|
|
1063
|
+
},
|
|
1064
|
+
accessibilityLabel: accessibilityLabel,
|
|
1065
|
+
accessibilityValue: {
|
|
1066
|
+
text: active ? `${camera.timer} seconds` : offLabel
|
|
1067
|
+
},
|
|
1068
|
+
className: className,
|
|
1069
|
+
children: [/*#__PURE__*/_jsx(TimerIcon, {
|
|
1070
|
+
size: 20,
|
|
1071
|
+
color: color
|
|
1072
|
+
}), active ? /*#__PURE__*/_jsx(Text, {
|
|
1073
|
+
size: "xs",
|
|
1074
|
+
className: chromeText({
|
|
1075
|
+
active
|
|
1076
|
+
}),
|
|
1077
|
+
children: `${camera.timer}s`
|
|
1078
|
+
}) : null]
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
CameraTimerControl.displayName = 'Camera.Timer';
|
|
1082
|
+
/**
|
|
1083
|
+
* The recording clock, `mm:ss`. It reads `00:00` in video before recording,
|
|
1084
|
+
* and turns red with a dot while it runs. Hidden in photo mode.
|
|
1085
|
+
*/
|
|
1086
|
+
function CameraDuration({
|
|
1087
|
+
className,
|
|
1088
|
+
always = false,
|
|
1089
|
+
...props
|
|
1090
|
+
}) {
|
|
1091
|
+
const camera = useCameraContext('Camera.Duration');
|
|
1092
|
+
const since = camera.recordingSince;
|
|
1093
|
+
const [now, setNow] = useState(() => Date.now());
|
|
1094
|
+
useEffect(() => {
|
|
1095
|
+
if (since === null) return;
|
|
1096
|
+
setNow(Date.now());
|
|
1097
|
+
const id = setInterval(() => setNow(Date.now()), 250);
|
|
1098
|
+
return () => clearInterval(id);
|
|
1099
|
+
}, [since]);
|
|
1100
|
+
if (!always && camera.mode !== 'video' && since === null) return null;
|
|
1101
|
+
const recording = since !== null;
|
|
1102
|
+
const clock = formatDuration(recording ? now - since : 0);
|
|
1103
|
+
return /*#__PURE__*/_jsxs(View, {
|
|
1104
|
+
...props,
|
|
1105
|
+
accessible: true,
|
|
1106
|
+
accessibilityRole: "text",
|
|
1107
|
+
accessibilityLabel: recording ? `Recording, ${clock}` : clock,
|
|
1108
|
+
className: durationPill({
|
|
1109
|
+
recording,
|
|
1110
|
+
className
|
|
1111
|
+
}),
|
|
1112
|
+
children: [recording ? /*#__PURE__*/_jsx(View, {
|
|
1113
|
+
className: "h-2 w-2 rounded-full bg-white"
|
|
1114
|
+
}) : null, /*#__PURE__*/_jsx(Text, {
|
|
1115
|
+
size: "sm",
|
|
1116
|
+
weight: "semibold",
|
|
1117
|
+
className: "text-white",
|
|
1118
|
+
style: {
|
|
1119
|
+
fontVariant: ['tabular-nums']
|
|
1120
|
+
},
|
|
1121
|
+
children: clock
|
|
1122
|
+
})]
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
CameraDuration.displayName = 'Camera.Duration';
|
|
1126
|
+
/**
|
|
1127
|
+
* Zoom stops above the shutter, and a bar that shows how far in the zoom is
|
|
1128
|
+
* once it is past the first.
|
|
1129
|
+
*/
|
|
1130
|
+
function CameraZoom({
|
|
1131
|
+
className,
|
|
1132
|
+
stops,
|
|
1133
|
+
bar = true,
|
|
1134
|
+
...props
|
|
1135
|
+
}) {
|
|
1136
|
+
const camera = useCameraContext('Camera.Zoom');
|
|
1137
|
+
const chips = stops ?? DEFAULT_ZOOM_STOPS;
|
|
1138
|
+
const active = activeStop(camera.zoom, chips);
|
|
1139
|
+
const [dragging, setDragging] = useState(false);
|
|
1140
|
+
const showBar = bar && (camera.zoom > 0.005 || dragging);
|
|
1141
|
+
return /*#__PURE__*/_jsxs(View, {
|
|
1142
|
+
...props,
|
|
1143
|
+
pointerEvents: "box-none",
|
|
1144
|
+
className: cn('items-center gap-2', className),
|
|
1145
|
+
children: [showBar ? /*#__PURE__*/_jsx(ZoomBar, {
|
|
1146
|
+
zoom: camera.zoom,
|
|
1147
|
+
onChange: camera.setZoom,
|
|
1148
|
+
onDragging: setDragging
|
|
1149
|
+
}) : null, /*#__PURE__*/_jsx(View, {
|
|
1150
|
+
className: "flex-row items-center gap-1 rounded-full bg-black/40 p-1",
|
|
1151
|
+
children: chips.map((stop, index) => {
|
|
1152
|
+
const selected = index === active;
|
|
1153
|
+
return /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
1154
|
+
onPress: () => {
|
|
1155
|
+
if (camera.haptics) selectionTick();
|
|
1156
|
+
camera.setZoom(stop.value);
|
|
1157
|
+
},
|
|
1158
|
+
hitSlop: 4,
|
|
1159
|
+
pressScale: 0.92,
|
|
1160
|
+
accessibilityRole: "button",
|
|
1161
|
+
accessibilityLabel: stop.accessibilityLabel ?? `Zoom ${stop.label}`,
|
|
1162
|
+
accessibilityState: {
|
|
1163
|
+
selected
|
|
1164
|
+
},
|
|
1165
|
+
className: cn('h-8 min-w-8 items-center justify-center rounded-full px-2', selected && 'bg-white'),
|
|
1166
|
+
children: /*#__PURE__*/_jsx(Text, {
|
|
1167
|
+
size: "xs",
|
|
1168
|
+
className: chromeText({
|
|
1169
|
+
active: selected
|
|
1170
|
+
}),
|
|
1171
|
+
children: stop.label
|
|
1172
|
+
})
|
|
1173
|
+
}, `${stop.label}-${index}`);
|
|
1174
|
+
})
|
|
1175
|
+
})]
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1178
|
+
CameraZoom.displayName = 'Camera.Zoom';
|
|
1179
|
+
function ZoomBar({
|
|
1180
|
+
zoom,
|
|
1181
|
+
onChange,
|
|
1182
|
+
onDragging
|
|
1183
|
+
}) {
|
|
1184
|
+
const gesture = useMemo(() => Gesture.Pan().minDistance(0).onBegin(event => {
|
|
1185
|
+
'worklet';
|
|
1186
|
+
|
|
1187
|
+
runOnJS(onDragging)(true);
|
|
1188
|
+
runOnJS(onChange)(zoomFromTrack(event.x, ZOOM_BAR_WIDTH));
|
|
1189
|
+
}).onUpdate(event => {
|
|
1190
|
+
'worklet';
|
|
1191
|
+
|
|
1192
|
+
runOnJS(onChange)(zoomFromTrack(event.x, ZOOM_BAR_WIDTH));
|
|
1193
|
+
}).onFinalize(() => {
|
|
1194
|
+
'worklet';
|
|
1195
|
+
|
|
1196
|
+
runOnJS(onDragging)(false);
|
|
1197
|
+
}), [onChange, onDragging]);
|
|
1198
|
+
const onAccessibilityAction = event => {
|
|
1199
|
+
if (event.nativeEvent.actionName === 'increment') onChange(stepZoom(zoom, 1));else if (event.nativeEvent.actionName === 'decrement') onChange(stepZoom(zoom, -1));
|
|
1200
|
+
};
|
|
1201
|
+
return /*#__PURE__*/_jsx(GestureDetector, {
|
|
1202
|
+
gesture: gesture,
|
|
1203
|
+
children: /*#__PURE__*/_jsxs(View, {
|
|
1204
|
+
accessible: true,
|
|
1205
|
+
accessibilityRole: "adjustable",
|
|
1206
|
+
accessibilityLabel: "Zoom",
|
|
1207
|
+
accessibilityValue: {
|
|
1208
|
+
min: 0,
|
|
1209
|
+
max: 100,
|
|
1210
|
+
now: Math.round(zoom * 100)
|
|
1211
|
+
},
|
|
1212
|
+
accessibilityActions: [{
|
|
1213
|
+
name: 'increment'
|
|
1214
|
+
}, {
|
|
1215
|
+
name: 'decrement'
|
|
1216
|
+
}],
|
|
1217
|
+
onAccessibilityAction: onAccessibilityAction,
|
|
1218
|
+
style: {
|
|
1219
|
+
width: ZOOM_BAR_WIDTH,
|
|
1220
|
+
height: 28
|
|
1221
|
+
},
|
|
1222
|
+
className: "flex-row items-center justify-between",
|
|
1223
|
+
children: [Array.from({
|
|
1224
|
+
length: ZOOM_TICKS
|
|
1225
|
+
}, (_, index) => /*#__PURE__*/_jsx(View, {
|
|
1226
|
+
className: index % 5 === 0 ? 'h-3 w-px bg-white/80' : 'h-2 w-px bg-white/40'
|
|
1227
|
+
}, index)), /*#__PURE__*/_jsx(View, {
|
|
1228
|
+
pointerEvents: "none",
|
|
1229
|
+
style: {
|
|
1230
|
+
position: 'absolute',
|
|
1231
|
+
left: zoom * ZOOM_BAR_WIDTH - 1,
|
|
1232
|
+
top: 2,
|
|
1233
|
+
bottom: 2
|
|
1234
|
+
},
|
|
1235
|
+
className: "w-0.5 rounded-full bg-white"
|
|
1236
|
+
})]
|
|
1237
|
+
})
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
/** Video or photo, as a pill with a thumb that slides between them. Locked while recording. */
|
|
1241
|
+
function CameraModes({
|
|
1242
|
+
className,
|
|
1243
|
+
labels,
|
|
1244
|
+
...props
|
|
1245
|
+
}) {
|
|
1246
|
+
const camera = useCameraContext('Camera.Modes');
|
|
1247
|
+
const reduced = useReducedMotion();
|
|
1248
|
+
const [layouts, setLayouts] = useState({});
|
|
1249
|
+
const thumbX = useSharedValue(0);
|
|
1250
|
+
const thumbWidth = useSharedValue(0);
|
|
1251
|
+
const placed = useRef(false);
|
|
1252
|
+
useEffect(() => {
|
|
1253
|
+
const target = layouts[camera.mode];
|
|
1254
|
+
if (!target) return;
|
|
1255
|
+
if (!placed.current || reduced) {
|
|
1256
|
+
placed.current = true;
|
|
1257
|
+
thumbX.value = target.x;
|
|
1258
|
+
thumbWidth.value = target.width;
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
const spring = {
|
|
1262
|
+
damping: 22,
|
|
1263
|
+
stiffness: 260,
|
|
1264
|
+
mass: 0.8
|
|
1265
|
+
};
|
|
1266
|
+
thumbX.value = withSpring(target.x, spring);
|
|
1267
|
+
thumbWidth.value = withSpring(target.width, spring);
|
|
1268
|
+
}, [camera.mode, layouts, reduced, thumbWidth, thumbX]);
|
|
1269
|
+
const thumbStyle = useAnimatedStyle(() => ({
|
|
1270
|
+
width: thumbWidth.value,
|
|
1271
|
+
transform: [{
|
|
1272
|
+
translateX: thumbX.value
|
|
1273
|
+
}]
|
|
1274
|
+
}));
|
|
1275
|
+
const names = {
|
|
1276
|
+
...MODE_LABELS,
|
|
1277
|
+
...labels
|
|
1278
|
+
};
|
|
1279
|
+
return /*#__PURE__*/_jsxs(View, {
|
|
1280
|
+
...props,
|
|
1281
|
+
accessibilityRole: "tablist",
|
|
1282
|
+
className: cn('flex-row items-center rounded-full bg-black/40 p-1', className),
|
|
1283
|
+
children: [/*#__PURE__*/_jsx(Animated.View, {
|
|
1284
|
+
pointerEvents: "none",
|
|
1285
|
+
style: [{
|
|
1286
|
+
position: 'absolute',
|
|
1287
|
+
left: 0,
|
|
1288
|
+
top: 4,
|
|
1289
|
+
bottom: 4
|
|
1290
|
+
}, thumbStyle],
|
|
1291
|
+
className: "rounded-full bg-white"
|
|
1292
|
+
}), MODE_ORDER.map(item => {
|
|
1293
|
+
const selected = camera.mode === item;
|
|
1294
|
+
return /*#__PURE__*/_jsx(Pressable, {
|
|
1295
|
+
onLayout: event => {
|
|
1296
|
+
const {
|
|
1297
|
+
x,
|
|
1298
|
+
width
|
|
1299
|
+
} = event.nativeEvent.layout;
|
|
1300
|
+
setLayouts(current => current[item]?.x === x && current[item]?.width === width ? current : {
|
|
1301
|
+
...current,
|
|
1302
|
+
[item]: {
|
|
1303
|
+
x,
|
|
1304
|
+
width
|
|
1305
|
+
}
|
|
1306
|
+
});
|
|
1307
|
+
},
|
|
1308
|
+
onPress: () => {
|
|
1309
|
+
if (!selected) camera.setMode(item);
|
|
1310
|
+
},
|
|
1311
|
+
disabled: camera.recording,
|
|
1312
|
+
accessibilityRole: "tab",
|
|
1313
|
+
accessibilityLabel: names[item],
|
|
1314
|
+
accessibilityState: {
|
|
1315
|
+
selected,
|
|
1316
|
+
disabled: camera.recording
|
|
1317
|
+
},
|
|
1318
|
+
className: "h-8 items-center justify-center rounded-full px-4",
|
|
1319
|
+
children: /*#__PURE__*/_jsx(Text, {
|
|
1320
|
+
size: "xs",
|
|
1321
|
+
className: cn('uppercase tracking-widest', chromeText({
|
|
1322
|
+
active: selected
|
|
1323
|
+
})),
|
|
1324
|
+
children: names[item]
|
|
1325
|
+
})
|
|
1326
|
+
}, item);
|
|
1327
|
+
})]
|
|
1328
|
+
});
|
|
1329
|
+
}
|
|
1330
|
+
CameraModes.displayName = 'Camera.Modes';
|
|
1331
|
+
|
|
1332
|
+
/* -------------------------------------------------------------------------- */
|
|
1333
|
+
/* The shutter row */
|
|
1334
|
+
/* -------------------------------------------------------------------------- */
|
|
1335
|
+
|
|
1336
|
+
/** The way into the photos. What it opens is `onPress`'s to decide. */
|
|
1337
|
+
function CameraLibrary({
|
|
1338
|
+
className,
|
|
1339
|
+
source,
|
|
1340
|
+
accessibilityLabel = 'Photos',
|
|
1341
|
+
...props
|
|
1342
|
+
}) {
|
|
1343
|
+
useCameraContext('Camera.Library');
|
|
1344
|
+
return /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
1345
|
+
...props,
|
|
1346
|
+
pressScale: 0.94,
|
|
1347
|
+
accessibilityRole: "button",
|
|
1348
|
+
accessibilityLabel: accessibilityLabel,
|
|
1349
|
+
className: cn('h-12 w-12 items-center justify-center overflow-hidden rounded-full border-2 border-white/80 bg-white/15', className),
|
|
1350
|
+
children: source ? /*#__PURE__*/_jsx(Image, {
|
|
1351
|
+
source: source,
|
|
1352
|
+
style: StyleSheet.absoluteFill,
|
|
1353
|
+
resizeMode: "cover",
|
|
1354
|
+
accessibilityIgnoresInvertColors: true
|
|
1355
|
+
}) : /*#__PURE__*/_jsx(ImageIcon, {
|
|
1356
|
+
size: 22,
|
|
1357
|
+
color: ON_GLASS
|
|
1358
|
+
})
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
CameraLibrary.displayName = 'Camera.Library';
|
|
1362
|
+
/** Front and back. Not while recording: the clip would cut between two cameras. */
|
|
1363
|
+
function CameraFlip({
|
|
1364
|
+
className,
|
|
1365
|
+
accessibilityLabel = 'Switch camera',
|
|
1366
|
+
...props
|
|
1367
|
+
}) {
|
|
1368
|
+
const camera = useCameraContext('Camera.Flip');
|
|
1369
|
+
const {
|
|
1370
|
+
turn
|
|
1371
|
+
} = camera;
|
|
1372
|
+
const iconStyle = useAnimatedStyle(() => ({
|
|
1373
|
+
transform: [{
|
|
1374
|
+
rotate: `${turn.value * 360}deg`
|
|
1375
|
+
}]
|
|
1376
|
+
}));
|
|
1377
|
+
const disabled = camera.recording;
|
|
1378
|
+
return /*#__PURE__*/_jsx(AnimatedPressable, {
|
|
1379
|
+
...props,
|
|
1380
|
+
onPress: camera.flip,
|
|
1381
|
+
disabled: disabled,
|
|
1382
|
+
pressScale: 0.92,
|
|
1383
|
+
accessibilityRole: "button",
|
|
1384
|
+
accessibilityLabel: accessibilityLabel,
|
|
1385
|
+
accessibilityValue: {
|
|
1386
|
+
text: camera.facing === 'back' ? 'Back camera' : 'Front camera'
|
|
1387
|
+
},
|
|
1388
|
+
accessibilityState: {
|
|
1389
|
+
disabled
|
|
1390
|
+
},
|
|
1391
|
+
className: cn('h-12 w-12 items-center justify-center rounded-full bg-black/40', disabled ? 'opacity-40' : 'opacity-100', className),
|
|
1392
|
+
children: /*#__PURE__*/_jsx(Animated.View, {
|
|
1393
|
+
style: iconStyle,
|
|
1394
|
+
children: /*#__PURE__*/_jsx(CameraSwitchIcon, {
|
|
1395
|
+
size: 24,
|
|
1396
|
+
color: ON_GLASS
|
|
1397
|
+
})
|
|
1398
|
+
})
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
CameraFlip.displayName = 'Camera.Flip';
|
|
1402
|
+
/**
|
|
1403
|
+
* The shutter. A tap takes a photo, after the timer if one is set; in video a
|
|
1404
|
+
* tap starts and stops, and a hold records for as long as it is held. The disc
|
|
1405
|
+
* is white for a photo and red for a video, and squares off while recording. A
|
|
1406
|
+
* ring fills towards `maxDuration` when there is one.
|
|
1407
|
+
*/
|
|
1408
|
+
function CameraShutter({
|
|
1409
|
+
className,
|
|
1410
|
+
holdToRecord = true,
|
|
1411
|
+
onPress,
|
|
1412
|
+
accessibilityLabel,
|
|
1413
|
+
...props
|
|
1414
|
+
}) {
|
|
1415
|
+
const camera = useCameraContext('Camera.Shutter');
|
|
1416
|
+
const reduced = useReducedMotion();
|
|
1417
|
+
const destructive = useCSSVariable('--color-destructive');
|
|
1418
|
+
const video = camera.mode === 'video';
|
|
1419
|
+
const recording = camera.recording;
|
|
1420
|
+
const pressed = useSharedValue(0);
|
|
1421
|
+
const stop = useSharedValue(recording ? 1 : 0);
|
|
1422
|
+
const progress = useSharedValue(0);
|
|
1423
|
+
const holding = useRef(false);
|
|
1424
|
+
useEffect(() => {
|
|
1425
|
+
const target = recording ? 1 : 0;
|
|
1426
|
+
stop.value = reduced ? target : withTiming(target, {
|
|
1427
|
+
duration: 220
|
|
1428
|
+
});
|
|
1429
|
+
}, [recording, reduced, stop]);
|
|
1430
|
+
useEffect(() => {
|
|
1431
|
+
cancelAnimation(progress);
|
|
1432
|
+
progress.value = 0;
|
|
1433
|
+
if (recording && camera.maxDuration) {
|
|
1434
|
+
progress.value = withTiming(1, {
|
|
1435
|
+
duration: camera.maxDuration * 1000,
|
|
1436
|
+
easing: Easing.linear
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
}, [camera.maxDuration, progress, recording]);
|
|
1440
|
+
const discStyle = useAnimatedStyle(() => {
|
|
1441
|
+
const size = SHUTTER_DISC + (SHUTTER_STOP - SHUTTER_DISC) * stop.value;
|
|
1442
|
+
const radius = size / 2 + (8 - size / 2) * stop.value;
|
|
1443
|
+
return {
|
|
1444
|
+
width: size,
|
|
1445
|
+
height: size,
|
|
1446
|
+
borderRadius: radius,
|
|
1447
|
+
transform: [{
|
|
1448
|
+
scale: 1 - pressed.value * 0.08
|
|
1449
|
+
}]
|
|
1450
|
+
};
|
|
1451
|
+
});
|
|
1452
|
+
const ringProps = useAnimatedProps(() => ({
|
|
1453
|
+
strokeDashoffset: RING_LENGTH * (1 - progress.value)
|
|
1454
|
+
}));
|
|
1455
|
+
|
|
1456
|
+
// Stopping is always allowed; starting waits for the camera.
|
|
1457
|
+
const disabled = !recording && !camera.canCapture;
|
|
1458
|
+
const label = accessibilityLabel ?? (video ? recording ? 'Stop recording' : 'Record video' : camera.countdown !== null ? 'Cancel timer' : 'Take photo');
|
|
1459
|
+
return /*#__PURE__*/_jsxs(Pressable, {
|
|
1460
|
+
...props,
|
|
1461
|
+
disabled: disabled,
|
|
1462
|
+
onPress: onPress ?? camera.press,
|
|
1463
|
+
onLongPress: video && holdToRecord && !onPress ? () => {
|
|
1464
|
+
if (camera.recording) return;
|
|
1465
|
+
holding.current = true;
|
|
1466
|
+
void camera.startRecording();
|
|
1467
|
+
} : undefined,
|
|
1468
|
+
delayLongPress: HOLD_TO_RECORD_MS,
|
|
1469
|
+
onPressIn: () => {
|
|
1470
|
+
pressed.value = withTiming(1, {
|
|
1471
|
+
duration: 90
|
|
1472
|
+
});
|
|
1473
|
+
},
|
|
1474
|
+
onPressOut: () => {
|
|
1475
|
+
pressed.value = withTiming(0, {
|
|
1476
|
+
duration: 160
|
|
1477
|
+
});
|
|
1478
|
+
if (holding.current) {
|
|
1479
|
+
holding.current = false;
|
|
1480
|
+
camera.stopRecording();
|
|
1481
|
+
}
|
|
1482
|
+
},
|
|
1483
|
+
accessibilityRole: "button",
|
|
1484
|
+
accessibilityLabel: label,
|
|
1485
|
+
accessibilityHint: video && holdToRecord && !recording ? 'Hold to record for as long as you hold it' : undefined,
|
|
1486
|
+
accessibilityState: {
|
|
1487
|
+
disabled,
|
|
1488
|
+
busy: camera.capturing
|
|
1489
|
+
}
|
|
1490
|
+
// Dimmed only without a camera to use or while a photo is being taken.
|
|
1491
|
+
,
|
|
1492
|
+
style: {
|
|
1493
|
+
width: SHUTTER,
|
|
1494
|
+
height: SHUTTER,
|
|
1495
|
+
opacity: disabled ? 0.6 : 1
|
|
1496
|
+
},
|
|
1497
|
+
className: cn('items-center justify-center', className),
|
|
1498
|
+
children: [/*#__PURE__*/_jsx(View, {
|
|
1499
|
+
pointerEvents: "none",
|
|
1500
|
+
style: [StyleSheet.absoluteFill, {
|
|
1501
|
+
borderRadius: SHUTTER / 2,
|
|
1502
|
+
borderWidth: SHUTTER_RING
|
|
1503
|
+
}],
|
|
1504
|
+
className: "border-white"
|
|
1505
|
+
}), camera.maxDuration ? /*#__PURE__*/_jsx(Svg, {
|
|
1506
|
+
pointerEvents: "none",
|
|
1507
|
+
width: SHUTTER,
|
|
1508
|
+
height: SHUTTER,
|
|
1509
|
+
style: StyleSheet.absoluteFill,
|
|
1510
|
+
children: /*#__PURE__*/_jsx(AnimatedCircle, {
|
|
1511
|
+
cx: SHUTTER / 2,
|
|
1512
|
+
cy: SHUTTER / 2,
|
|
1513
|
+
r: RING_RADIUS,
|
|
1514
|
+
fill: "none",
|
|
1515
|
+
stroke: typeof destructive === 'string' ? destructive : undefined,
|
|
1516
|
+
strokeWidth: SHUTTER_RING,
|
|
1517
|
+
strokeLinecap: "round",
|
|
1518
|
+
strokeDasharray: RING_LENGTH,
|
|
1519
|
+
animatedProps: ringProps,
|
|
1520
|
+
rotation: -90,
|
|
1521
|
+
origin: `${SHUTTER / 2}, ${SHUTTER / 2}`
|
|
1522
|
+
})
|
|
1523
|
+
}) : null, /*#__PURE__*/_jsx(Animated.View, {
|
|
1524
|
+
pointerEvents: "none",
|
|
1525
|
+
style: discStyle,
|
|
1526
|
+
className: video ? 'bg-destructive' : 'bg-white'
|
|
1527
|
+
})]
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
CameraShutter.displayName = 'Camera.Shutter';
|
|
1531
|
+
export const Camera = Object.assign(CameraForwarded, {
|
|
1532
|
+
Preview: CameraPreview,
|
|
1533
|
+
Header: CameraHeader,
|
|
1534
|
+
Footer: CameraFooter,
|
|
1535
|
+
Controls: CameraControls,
|
|
1536
|
+
Button: CameraButton,
|
|
1537
|
+
Flash: CameraFlashControl,
|
|
1538
|
+
Timer: CameraTimerControl,
|
|
1539
|
+
Duration: CameraDuration,
|
|
1540
|
+
Zoom: CameraZoom,
|
|
1541
|
+
Modes: CameraModes,
|
|
1542
|
+
Library: CameraLibrary,
|
|
1543
|
+
Flip: CameraFlip,
|
|
1544
|
+
Shutter: CameraShutter
|
|
1545
|
+
});
|
|
1546
|
+
//# sourceMappingURL=index.js.map
|