Support website message notification display

This commit is contained in:
Tw93
2024-12-24 20:33:26 +08:00
parent 7a02d8fb71
commit c06182151e
6 changed files with 240 additions and 20 deletions

View File

@@ -18,6 +18,13 @@ pub struct BinaryDownloadParams {
binary: Vec<u8>,
}
#[derive(serde::Deserialize)]
pub struct NotificationParams {
title: String,
body: String,
icon: String,
}
#[command]
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
@@ -71,3 +78,16 @@ pub async fn download_file_by_binary(
}
}
}
#[command]
pub fn send_notification(app: AppHandle, params: NotificationParams) -> Result<(), String> {
use tauri_plugin_notification::NotificationExt;
app.notification()
.builder()
.title(&params.title)
.body(&params.body)
.icon(&params.icon)
.show()
.unwrap();
Ok(())
}

View File

@@ -187,7 +187,6 @@ document.addEventListener('DOMContentLoaded', () => {
};
const detectAnchorElementClick = e => {
const anchorElement = e.target.closest('a');
if (anchorElement && anchorElement.href) {
@@ -256,6 +255,38 @@ document.addEventListener('DOMContentLoaded', () => {
);
});
document.addEventListener('DOMContentLoaded', function () {
let permVal = 'granted';
window.Notification = function (title, options) {
const { invoke } = window.__TAURI__.core;
const body = options?.body || '';
let icon = options?.icon || '';
// If the icon is a relative path, convert to full path using URI
if (icon.startsWith('/')) {
icon = window.location.origin + icon;
}
invoke('send_notification', {
params: {
title,
body,
icon,
},
});
};
window.Notification.requestPermission = async () => 'granted';
Object.defineProperty(window.Notification, 'permission', {
enumerable: true,
get: () => permVal,
set: v => {
permVal = v;
},
});
});
function setDefaultZoom() {
const htmlZoom = window.localStorage.getItem('htmlZoom');
if (htmlZoom) {

View File

@@ -3,7 +3,7 @@ mod app;
mod util;
use app::{invoke, menu::set_system_tray, window};
use invoke::{download_file, download_file_by_binary};
use invoke::{download_file, download_file_by_binary, send_notification};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
@@ -38,10 +38,12 @@ pub fn run_app() {
.plugin(tauri_plugin_oauth::init())
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_single_instance::init(|_, _, _| ()))
.invoke_handler(tauri::generate_handler![
download_file,
download_file_by_binary
download_file_by_binary,
send_notification,
])
.setup(move |app| {
let data_dir = get_data_dir(app.app_handle(), tauri_config.clone());