feat(notification): add plugin (#326)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-02 09:09:50 -07:00
committed by GitHub
parent 864b9d790f
commit e9bbe94181
34 changed files with 1536 additions and 8 deletions
+54
View File
@@ -0,0 +1,54 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::Deserialize;
use tauri::{command, AppHandle, Runtime, State};
use crate::{Notification, PermissionState, Result};
/// The options for the notification API.
#[derive(Debug, Clone, Deserialize)]
pub struct NotificationOptions {
/// The notification title.
pub title: String,
/// The notification body.
pub body: Option<String>,
/// The notification icon.
pub icon: Option<String>,
}
#[command]
pub(crate) async fn is_permission_granted<R: Runtime>(
_app: AppHandle<R>,
notification: State<'_, Notification<R>>,
) -> Result<bool> {
notification
.permission_state()
.map(|s| s == PermissionState::Granted)
}
#[command]
pub(crate) async fn request_permission<R: Runtime>(
_app: AppHandle<R>,
notification: State<'_, Notification<R>>,
) -> Result<PermissionState> {
notification.request_permission()
}
#[command]
pub(crate) async fn notify<R: Runtime>(
_app: AppHandle<R>,
notification: State<'_, Notification<R>>,
options: NotificationOptions,
) -> Result<()> {
let mut builder = notification.builder().title(options.title);
if let Some(body) = options.body {
builder = builder.body(body);
}
if let Some(icon) = options.icon {
builder = builder.icon(icon);
}
builder.show()
}