mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-10 16:27:05 +02:00
feat(notification): add plugin (#326)
This commit is contained in:
committed by
GitHub
parent
864b9d790f
commit
e9bbe94181
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user