Skip to main content

fractal/components/camera/
mod.rs

1//! Camera API.
2
3#[cfg(target_os = "linux")]
4mod linux;
5mod qrcode_scanner;
6mod viewfinder;
7
8pub(crate) use self::qrcode_scanner::QrCodeScanner;
9use self::{
10    qrcode_scanner::QrVerificationDataBoxed,
11    viewfinder::{
12        CameraViewfinder, CameraViewfinderExt, CameraViewfinderImpl, CameraViewfinderState,
13    },
14};
15
16cfg_if::cfg_if! {
17    if #[cfg(target_os = "linux")] {
18        /// The camera API.
19        pub(crate) type Camera = linux::LinuxCamera;
20    } else {
21        /// The camera API.
22        pub(crate) type Camera = unimplemented::UnimplementedCamera;
23    }
24}
25
26/// Trait implemented by camera backends.
27pub trait CameraExt {
28    /// Whether any cameras are available.
29    async fn has_cameras() -> bool;
30
31    /// Get a viewfinder displaying the output of the camera.
32    ///
33    /// This method should try to get the permission to access cameras, and
34    /// return `None` when it fails.
35    async fn viewfinder() -> Option<CameraViewfinder>;
36}
37
38/// The fallback `Camera` API, to use on platforms where it is unimplemented.
39#[cfg(not(target_os = "linux"))]
40mod unimplemented {
41    use super::*;
42
43    #[derive(Debug)]
44    pub(crate) struct UnimplementedCamera;
45
46    impl CameraExt for UnimplementedCamera {
47        async fn has_cameras() -> bool {
48            false
49        }
50
51        async fn viewfinder() -> Option<CameraViewfinder> {
52            tracing::error!("The camera API is not supported on this platform");
53            None
54        }
55    }
56}