From 25befec9c6ff37c91fa19d5229e7b5d7de02109d Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Thu, 5 Jan 2023 21:49:24 +0800 Subject: [PATCH] add: system Notification --- src/hooks.ts | 9 +++-- src/modules/progressWindow.ts | 68 ++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index ead45cc..5d8b1bb 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,6 +1,7 @@ import { BasicExampleFactory, UIExampleFactory } from "./modules/examples"; import { changeProgressWindowLine, + isProgressWindow, showProgressWindow, } from "./modules/progressWindow"; import { config } from "../package.json"; @@ -14,7 +15,9 @@ async function onStartup() { config.addonName, getString("startup.begin"), "default", - -1 + { + closeTime: -1, + } ); changeProgressWindowLine(progWin, { newProgress: 0 }); @@ -51,7 +54,9 @@ async function onStartup() { newProgress: 100, newText: `[100%] ${getString("startup.finish")}`, }); - progWin.startCloseTimer(5000); + if (isProgressWindow(progWin)) { + (progWin as _ZoteroProgressWindow).startCloseTimer(5000); + } } function onShutdown(): void { diff --git a/src/modules/progressWindow.ts b/src/modules/progressWindow.ts index 751f410..feabd1a 100644 --- a/src/modules/progressWindow.ts +++ b/src/modules/progressWindow.ts @@ -10,33 +10,61 @@ export function showProgressWindow( header: string, context: string, type: "success" | "fail" | "default" = "default", - t: number = 5000 -): _ZoteroProgressWindow { - // A simple wrapper of the Zotero ProgressWindow - let progressWindow = new Zotero.ProgressWindow({ - closeOnClick: true, - }) as _ZoteroProgressWindow; - progressWindow.changeHeadline(header); - // @ts-ignore - progressWindow.progress = new progressWindow.ItemProgress( - progressWindowIcon[type], - context - ); - progressWindow.show(); - if (t > 0) { - progressWindow.startCloseTimer(t); + options: { + closeTime?: number; + backend?: "Zotero" | "system"; + } = { + closeTime: 5000, + backend: "Zotero", + } +): _ZoteroProgressWindow | Notification { + // Currently Zotero 7 doesn't support progress window. + // Use system backend on Zotero 7. + if (options.backend === "system" || ztoolkit.Compat.isZotero7()) { + Zotero.Prefs.set("alerts.useSystemBackend", true, true); + const notification = new (ztoolkit.Compat.getGlobal( + "Notification" + ) as typeof Notification)(header, { + body: context, + icon: progressWindowIcon[type], + tag: config.addonName, + }); + if (options.closeTime) { + (ztoolkit.Compat.getGlobal("setTimeout") as typeof setTimeout)(() => { + notification.close(); + }, options.closeTime); + } + return notification; + } else { + // A simple wrapper of the Zotero ProgressWindow + const progressWindow = new Zotero.ProgressWindow({ + closeOnClick: true, + }) as _ZoteroProgressWindow; + progressWindow.changeHeadline(header); + // @ts-ignore + progressWindow.progress = new progressWindow.ItemProgress( + progressWindowIcon[type], + context + ); + progressWindow.show(); + if (options.closeTime) { + progressWindow.startCloseTimer(options.closeTime); + } + return progressWindow; } - return progressWindow; } export function changeProgressWindowLine( - progressWindow: _ZoteroProgressWindow, + progressWindow: _ZoteroProgressWindow | Notification, options: { newText?: string; newIcon?: string; newProgress?: number; } ) { + if (!isProgressWindow(progressWindow)) { + return; + } // @ts-ignore const progress = progressWindow.progress as _ZoteroItemProgress; if (!progress) { @@ -46,3 +74,9 @@ export function changeProgressWindowLine( options.newIcon && progress.setIcon(options.newIcon); options.newProgress && progress.setProgress(options.newProgress); } + +export function isProgressWindow( + progressWindow: _ZoteroProgressWindow | Notification +) { + return !(progressWindow as Notification).title; +}