// Copyright 2019-2023 Tauri Programme within The Commons Conservancy // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT use tauri::{AppHandle, Runtime, State, command, plugin::PermissionState}; use crate::{Notification, NotificationData, Result}; #[command] pub(crate) async fn is_permission_granted( _app: AppHandle, notification: State<'_, Notification>, ) -> Result> { let state = notification.permission_state()?; match state { PermissionState::Granted => Ok(Some(true)), PermissionState::Denied => Ok(Some(false)), PermissionState::Prompt | PermissionState::PromptWithRationale => Ok(None), } } #[command] pub(crate) async fn request_permission( _app: AppHandle, notification: State<'_, Notification>, ) -> Result { notification.request_permission() } #[command] pub(crate) async fn notify( _app: AppHandle, notification: State<'_, Notification>, options: NotificationData, ) -> Result<()> { let mut builder = notification.builder(); builder.data = options; builder.show() } // The commands below are implemented by the Kotlin and Swift plugins on mobile. #[cfg(desktop)] #[command] pub(crate) async fn register_action_types( _app: AppHandle, notification: State<'_, Notification>, types: Vec, ) -> Result<()> { notification.register_action_types(types) } /// A notification of `remove_active`, as the JavaScript API sends it. #[cfg(desktop)] #[derive(serde::Deserialize)] pub(crate) struct ActiveId { id: i32, } #[cfg(desktop)] #[command] pub(crate) async fn remove_active( _app: AppHandle, notification: State<'_, Notification>, notifications: Option>, ) -> Result<()> { match notifications { Some(notifications) => { notification.remove_active(notifications.into_iter().map(|n| n.id).collect()) } None => notification.remove_all_active(), } } #[cfg(desktop)] #[command] pub(crate) fn register_listener( webview: tauri::Webview, notification: State<'_, Notification>, event: String, handler: tauri::ipc::Channel, ) { notification.register_listener(event, handler, &webview); } #[cfg(desktop)] #[command] pub(crate) fn remove_listener( _app: AppHandle, notification: State<'_, Notification>, event: String, channel_id: u32, ) { notification.remove_listener(&event, channel_id); }