tauri_plugin_geolocation/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use tauri::{
    plugin::{Builder, TauriPlugin},
    Manager, Runtime,
};

//use tauri_specta::*;

pub use models::*;

#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;

mod commands;
mod error;
mod models;

pub use error::{Error, Result};

#[cfg(desktop)]
use desktop::Geolocation;
#[cfg(mobile)]
use mobile::Geolocation;

/* macro_rules! specta_builder {
    () => {
        ts::builder()
            .commands(collect_commands![
                commands::get_current_position,
                commands::watch_position,
                commands::clear_watch,
                commands::check_permissions,
                commands::request_permissions
            ])
            .header("// @ts-nocheck")
            .config(
                specta::ts::ExportConfig::default()
                    .bigint(specta::ts::BigIntExportBehavior::Number),
            )
    };
} */

/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the geolocation APIs.
pub trait GeolocationExt<R: Runtime> {
    fn geolocation(&self) -> &Geolocation<R>;
}

impl<R: Runtime, T: Manager<R>> crate::GeolocationExt<R> for T {
    fn geolocation(&self) -> &Geolocation<R> {
        self.state::<Geolocation<R>>().inner()
    }
}

/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
    /*     let (invoke_handler, register_events) =
    specta_builder!().build_plugin_utils("geolocation").unwrap(); */

    Builder::new("geolocation")
        .invoke_handler(tauri::generate_handler![
            commands::get_current_position,
            commands::watch_position,
            commands::clear_watch,
            commands::check_permissions,
            commands::request_permissions
        ])
        .setup(|app, api| {
            #[cfg(mobile)]
            let geolocation = mobile::init(app, api)?;
            #[cfg(desktop)]
            let geolocation = desktop::init(app, api)?;
            app.manage(geolocation);
            Ok(())
        })
        .build()
}

/* #[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn export_types() {
        specta_builder!()
            .path("./guest-js/bindings.ts")
            .config(
                specta::ts::ExportConfig::default()
                    .formatter(specta::ts::formatter::prettier)
                    .bigint(specta::ts::BigIntExportBehavior::Number),
            )
            .export_for_plugin("geolocation")
            .expect("failed to export specta types");
    }
}
 */